path_db.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*************************************************************************/
  2. /* path_db.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "path_db.h"
  30. #include "print_string.h"
  31. uint32_t NodePath::hash() const {
  32. if (!data)
  33. return 0;
  34. uint32_t h = data->absolute ? 1 : 0;
  35. int pc = data->path.size();
  36. const StringName *sn = data->path.ptr();
  37. for(int i=0;i<pc;i++) {
  38. h = h ^ sn[i].hash();
  39. }
  40. int spc = data->subpath.size();
  41. const StringName *ssn = data->subpath.ptr();
  42. for(int i=0;i<spc;i++) {
  43. h = h ^ ssn[i].hash();
  44. }
  45. h = h ^ data->property.hash();
  46. return h;
  47. }
  48. bool NodePath::is_absolute() const {
  49. if (!data)
  50. return false;
  51. return data->absolute;
  52. }
  53. int NodePath::get_name_count() const {
  54. if (!data)
  55. return 0;
  56. return data->path.size();
  57. }
  58. StringName NodePath::get_name(int p_idx) const {
  59. ERR_FAIL_COND_V(!data,StringName());
  60. ERR_FAIL_INDEX_V(p_idx,data->path.size(),StringName());
  61. return data->path[p_idx];
  62. }
  63. StringName NodePath::get_property() const {
  64. if (!data)
  65. return StringName();
  66. return data->property;
  67. }
  68. int NodePath::get_subname_count() const {
  69. if (!data)
  70. return 0;
  71. return data->subpath.size();
  72. }
  73. StringName NodePath::get_subname(int p_idx) const {
  74. ERR_FAIL_COND_V(!data,StringName());
  75. ERR_FAIL_INDEX_V(p_idx,data->subpath.size(),StringName());
  76. return data->subpath[p_idx];
  77. }
  78. void NodePath::unref() {
  79. if (data && data->refcount.unref()) {
  80. memdelete(data);
  81. }
  82. data=NULL;
  83. }
  84. bool NodePath::operator==(const NodePath& p_path) const {
  85. if (data==p_path.data)
  86. return true;
  87. if (!data || !p_path.data)
  88. return false;
  89. if (data->absolute != p_path.data->absolute)
  90. return false;
  91. if (data->path.size() != p_path.data->path.size())
  92. return false;
  93. if (data->subpath.size() != p_path.data->subpath.size())
  94. return false;
  95. if (data->property != p_path.data->property)
  96. return false;
  97. for (int i=0;i<data->path.size();i++) {
  98. if (data->path[i]!=p_path.data->path[i])
  99. return false;
  100. }
  101. for (int i=0;i<data->subpath.size();i++) {
  102. if (data->subpath[i]!=p_path.data->subpath[i])
  103. return false;
  104. }
  105. return true;
  106. }
  107. bool NodePath::operator!=(const NodePath& p_path) const {
  108. return (!(*this == p_path));
  109. }
  110. void NodePath::operator=(const NodePath& p_path) {
  111. if (this==&p_path)
  112. return;
  113. unref();
  114. if (p_path.data && p_path.data->refcount.ref()) {
  115. data=p_path.data;
  116. }
  117. }
  118. NodePath::operator String() const {
  119. if (!data)
  120. return String();
  121. String ret;
  122. if (data->absolute)
  123. ret="/";
  124. for (int i=0;i<data->path.size();i++) {
  125. if (i>0)
  126. ret+="/";
  127. ret+=data->path[i].operator String();
  128. }
  129. for (int i=0;i<data->subpath.size();i++) {
  130. ret+=":"+data->subpath[i].operator String();
  131. }
  132. if (data->property.operator String()!="")
  133. ret+=":"+String(data->property);
  134. return ret;
  135. }
  136. NodePath::NodePath(const NodePath& p_path) {
  137. data=NULL;
  138. if (p_path.data && p_path.data->refcount.ref()) {
  139. data=p_path.data;
  140. }
  141. }
  142. Vector<StringName> NodePath::get_names() const {
  143. if (data)
  144. return data->path;
  145. return Vector<StringName>();
  146. }
  147. Vector<StringName> NodePath::get_subnames() const{
  148. if (data)
  149. return data->subpath;
  150. return Vector<StringName>();
  151. }
  152. NodePath NodePath::rel_path_to(const NodePath& p_np) const {
  153. ERR_FAIL_COND_V( !is_absolute(), NodePath() );
  154. ERR_FAIL_COND_V( !p_np.is_absolute(), NodePath() );
  155. Vector<StringName> src_dirs = get_names();
  156. Vector<StringName> dst_dirs = p_np.get_names();
  157. //find common parent
  158. int common_parent=0;
  159. while(true) {
  160. if (src_dirs.size()==common_parent)
  161. break;
  162. if (dst_dirs.size()==common_parent)
  163. break;
  164. if (src_dirs[common_parent]!=dst_dirs[common_parent])
  165. break;
  166. common_parent++;
  167. }
  168. common_parent--;
  169. Vector<StringName> relpath;
  170. for(int i=src_dirs.size()-1;i>common_parent;i--) {
  171. relpath.push_back("..");
  172. }
  173. for(int i=common_parent+1;i<dst_dirs.size();i++) {
  174. relpath.push_back(dst_dirs[i]);
  175. }
  176. if (relpath.size()==0)
  177. relpath.push_back(".");
  178. return NodePath(relpath,p_np.get_subnames(),false,p_np.get_property());
  179. }
  180. NodePath::NodePath(const Vector<StringName>& p_path,bool p_absolute,const String& p_property) {
  181. data=NULL;
  182. if (p_path.size()==0)
  183. return;
  184. data = memnew( Data );
  185. data->refcount.init();
  186. data->absolute=p_absolute;
  187. data->path=p_path;
  188. data->property=p_property;
  189. }
  190. NodePath::NodePath(const Vector<StringName>& p_path,const Vector<StringName>& p_subpath,bool p_absolute,const String& p_property) {
  191. data=NULL;
  192. if (p_path.size()==0)
  193. return;
  194. data = memnew( Data );
  195. data->refcount.init();
  196. data->absolute=p_absolute;
  197. data->path=p_path;
  198. data->subpath=p_subpath;
  199. data->property=p_property;
  200. }
  201. NodePath::NodePath(const String& p_path) {
  202. data=NULL;
  203. if (p_path.length()==0)
  204. return;
  205. String path=p_path;
  206. StringName property;
  207. Vector<StringName> subpath;
  208. int absolute=(path[0]=='/')?1:0;;
  209. bool valid=false;
  210. bool last_is_slash=true;
  211. int slices=0;
  212. int subpath_pos=path.find(":");
  213. if (subpath_pos!=-1) {
  214. int from=subpath_pos+1;
  215. for (int i=from;i<=path.length();i++) {
  216. if (path[i]==':' || path[i]==0) {
  217. String str = path.substr(from,i-from);
  218. if (path[i]==':') {
  219. if (str=="") {
  220. ERR_EXPLAIN("Invalid NodePath: "+p_path);
  221. ERR_FAIL();
  222. }
  223. subpath.push_back(str);
  224. } else {
  225. //property can be empty
  226. property=str;
  227. }
  228. from=i+1;
  229. }
  230. }
  231. path=path.substr(0,subpath_pos);
  232. }
  233. for (int i=absolute;i<path.length();i++) {
  234. if (path[i]=='/') {
  235. last_is_slash=true;
  236. } else {
  237. if (last_is_slash)
  238. slices++;
  239. valid=true;
  240. last_is_slash=false;
  241. }
  242. }
  243. if (slices==0 && !absolute && !property)
  244. return;
  245. data = memnew( Data );
  246. data->refcount.init();
  247. data->absolute=absolute?true:false;
  248. data->property=property;
  249. data->subpath=subpath;
  250. if (slices==0)
  251. return;
  252. data->path.resize(slices);
  253. last_is_slash=true;
  254. int from=absolute;
  255. int slice=0;
  256. for (int i=absolute;i<path.length()+1;i++) {
  257. if (path[i]=='/' || path[i]==0) {
  258. if (!last_is_slash) {
  259. String name=path.substr(from,i-from);
  260. ERR_FAIL_INDEX( slice,data->path.size() );
  261. data->path[slice++]=name;
  262. }
  263. from=i+1;
  264. last_is_slash=true;
  265. } else {
  266. last_is_slash=false;
  267. }
  268. }
  269. }
  270. bool NodePath::is_empty() const {
  271. return !data;
  272. }
  273. NodePath::NodePath() {
  274. data=NULL;
  275. }
  276. NodePath::~NodePath() {
  277. unref();
  278. }