path_db.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. void NodePath::simplify() {
  202. if (!data)
  203. return;
  204. for(int i=0;i<data->path.size();i++) {
  205. if (data->path.size()==1)
  206. break;
  207. if (data->path[i].operator String()==".") {
  208. data->path.remove(i);
  209. i--;
  210. } else if (data->path[i].operator String()==".." && i>0 && data->path[i-1].operator String()!="." && data->path[i-1].operator String()!="..") {
  211. //remove both
  212. data->path.remove(i-1);
  213. data->path.remove(i-1);
  214. i-=2;
  215. if (data->path.size()==0) {
  216. data->path.push_back(".");
  217. break;
  218. }
  219. }
  220. }
  221. }
  222. NodePath NodePath::simplified() const {
  223. NodePath np=*this;
  224. np.simplify();
  225. return np;
  226. }
  227. NodePath::NodePath(const String& p_path) {
  228. data=NULL;
  229. if (p_path.length()==0)
  230. return;
  231. String path=p_path;
  232. StringName property;
  233. Vector<StringName> subpath;
  234. int absolute=(path[0]=='/')?1:0;;
  235. bool valid=false;
  236. bool last_is_slash=true;
  237. int slices=0;
  238. int subpath_pos=path.find(":");
  239. if (subpath_pos!=-1) {
  240. int from=subpath_pos+1;
  241. for (int i=from;i<=path.length();i++) {
  242. if (path[i]==':' || path[i]==0) {
  243. String str = path.substr(from,i-from);
  244. if (path[i]==':') {
  245. if (str=="") {
  246. ERR_EXPLAIN("Invalid NodePath: "+p_path);
  247. ERR_FAIL();
  248. }
  249. subpath.push_back(str);
  250. } else {
  251. //property can be empty
  252. property=str;
  253. }
  254. from=i+1;
  255. }
  256. }
  257. path=path.substr(0,subpath_pos);
  258. }
  259. for (int i=absolute;i<path.length();i++) {
  260. if (path[i]=='/') {
  261. last_is_slash=true;
  262. } else {
  263. if (last_is_slash)
  264. slices++;
  265. valid=true;
  266. last_is_slash=false;
  267. }
  268. }
  269. if (slices==0 && !absolute && !property)
  270. return;
  271. data = memnew( Data );
  272. data->refcount.init();
  273. data->absolute=absolute?true:false;
  274. data->property=property;
  275. data->subpath=subpath;
  276. if (slices==0)
  277. return;
  278. data->path.resize(slices);
  279. last_is_slash=true;
  280. int from=absolute;
  281. int slice=0;
  282. for (int i=absolute;i<path.length()+1;i++) {
  283. if (path[i]=='/' || path[i]==0) {
  284. if (!last_is_slash) {
  285. String name=path.substr(from,i-from);
  286. ERR_FAIL_INDEX( slice,data->path.size() );
  287. data->path[slice++]=name;
  288. }
  289. from=i+1;
  290. last_is_slash=true;
  291. } else {
  292. last_is_slash=false;
  293. }
  294. }
  295. }
  296. bool NodePath::is_empty() const {
  297. return !data;
  298. }
  299. NodePath::NodePath() {
  300. data=NULL;
  301. }
  302. NodePath::~NodePath() {
  303. unref();
  304. }