file.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. //{{{ File & File Manager Classes
  9. //
  10. //$Id: file.cc,v 1.48 1998/07/15 21:16:39 jc Exp $
  11. #include "file/file.hh"
  12. #include "memory/malloc.hh"
  13. #include "error/error.hh"
  14. #include "time/profile.hh"
  15. #include "error/error.hh"
  16. #include "file/file_man.hh"
  17. #include "string/string.hh"
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <stdarg.h>
  22. static i4_profile_class pf_tell("File tell");
  23. static i4_profile_class pf_open("File Open");
  24. static i4_profile_class pf_sopen("System file Open");
  25. static i4_profile_class pf_close("File Close");
  26. static i4_profile_class pf_read("File Read");
  27. static i4_profile_class pf_con_buf("::buffered file");
  28. i4_bool i4_file_class::async_write(const void *buffer, w32 size,
  29. async_callback call,
  30. void *context)
  31. {
  32. (*call)(write(buffer, size), context);
  33. return i4_T;
  34. }
  35. i4_bool i4_file_class::async_read (void *buffer, w32 size,
  36. async_callback call,
  37. void *context)
  38. {
  39. (*call)(read(buffer, size), context);
  40. return i4_T;
  41. }
  42. class file_string : public i4_str
  43. {
  44. public:
  45. file_string(w16 size) : i4_str(size) {}
  46. char *buffer() { return (char *)ptr; }
  47. void set_len(w16 _len) { len = _len; }
  48. };
  49. i4_str* i4_file_class::read_str(w32 len)
  50. {
  51. file_string *str = new file_string((w16)len);
  52. len = read(str->buffer(),len);
  53. str->set_len((w16)len);
  54. return str;
  55. }
  56. i4_str* i4_file_class::read_counted_str()
  57. {
  58. w16 len = read_16();
  59. return read_str(len);
  60. }
  61. class file_write_string : public i4_const_str
  62. {
  63. public:
  64. char *buffer() { return (char *)ptr; }
  65. };
  66. w32 i4_file_class::write_str(const i4_const_str &str)
  67. {
  68. file_write_string *s = (file_write_string *)&str;
  69. return write(s->buffer(), s->length());
  70. }
  71. w32 i4_file_class::write_counted_str(const i4_const_str &str)
  72. {
  73. w32 count;
  74. count = write_16((w16)str.length());
  75. if (str.length())
  76. return (count + write_str(str));
  77. else
  78. return count;
  79. }
  80. static inline int fmt_char(char c)
  81. {
  82. if ((c>='a' && c<='z') || (c>='A' && c<='Z'))
  83. return 1;
  84. return 0;
  85. }
  86. // same as fprintf, but with the addition %S is a i4_const_str *
  87. int i4_file_class::printf(char *fmt, ...)
  88. {
  89. w32 start=tell();
  90. va_list ap;
  91. va_start(ap, fmt);
  92. while (*fmt)
  93. {
  94. if (*fmt=='%')
  95. {
  96. char *fmt_end=fmt;
  97. while (!fmt_char(*fmt_end) && *fmt_end) fmt_end++;
  98. char f[10], out[500];
  99. memcpy(f, fmt, fmt_end-fmt+1);
  100. f[fmt_end-fmt+1]=0;
  101. out[0]=0;
  102. switch (*fmt_end)
  103. {
  104. case 's' :
  105. {
  106. char *str=va_arg(ap,char *);
  107. write(str,strlen(str));
  108. } break;
  109. case 'S' :
  110. {
  111. i4_const_str *s=va_arg(ap,i4_const_str *);
  112. write_str(*s);
  113. } break;
  114. case 'd' :
  115. case 'i' :
  116. case 'x' :
  117. case 'X' :
  118. case 'c' :
  119. case 'o' :
  120. {
  121. ::sprintf(out,f,va_arg(ap,int));
  122. write(out,strlen(out));
  123. } break;
  124. case 'f' :
  125. case 'g' :
  126. ::sprintf(out,f,va_arg(ap,double));
  127. write(out,strlen(out));
  128. break;
  129. default :
  130. ::sprintf(out,f,va_arg(ap,void *));
  131. write(out,strlen(out));
  132. break;
  133. }
  134. fmt=fmt_end;
  135. if (*fmt)
  136. fmt++;
  137. }
  138. else
  139. {
  140. write_8(*fmt);
  141. fmt++;
  142. }
  143. }
  144. va_end(ap);
  145. return tell()-start;
  146. }
  147. int i4_file_class::write_format(char *format, ...)
  148. {
  149. char *f=format;
  150. va_list ap;
  151. va_start(ap, format);
  152. int start=tell();
  153. while (*f)
  154. {
  155. switch (*f)
  156. {
  157. case '1' : write_8(*(va_arg(ap, w8 *))); break;
  158. case '2' : write_16(*(va_arg(ap, w16 *))); break;
  159. case '4' : write_32(*(va_arg(ap, w32 *))); break;
  160. case 'f' : write_float(*(va_arg(ap, float *))); break;
  161. case 'S' : write_counted_str( *(va_arg(ap, i4_const_str *))); break;
  162. }
  163. f++;
  164. }
  165. va_end(ap);
  166. return tell()-start;
  167. }
  168. // format is same as write_format, returns number of fields written
  169. int i4_file_class::read_format(char *format, ...)
  170. {
  171. char *f=format;
  172. va_list ap;
  173. va_start(ap, format);
  174. int start=tell();
  175. while (*f)
  176. {
  177. switch (*f)
  178. {
  179. case '1' : *(va_arg(ap, w8 *))=read_8(); break;
  180. case '2' : *(va_arg(ap, w16 *))=read_16(); break;
  181. case '4' : *(va_arg(ap, w32 *))=read_32(); break;
  182. case 'f' : *(va_arg(ap, float*))=read_float(); break;
  183. case 'S' : *(va_arg(ap, i4_str **))=read_counted_str(); break;
  184. }
  185. f++;
  186. }
  187. va_end(ap);
  188. return tell()-start;
  189. }
  190. // returns NULL if unable to open file
  191. i4_file_class *i4_open(const i4_const_str &name, w32 flags)
  192. {
  193. for (i4_file_manager_class *m=i4_file_manager_class::first; m; m=m->next)
  194. {
  195. i4_file_class *fp=m->open(name,flags);
  196. if (fp)
  197. return fp;
  198. }
  199. return 0;
  200. }
  201. // return i4_F on failure
  202. i4_bool i4_unlink(const i4_const_str &name)
  203. {
  204. for (i4_file_manager_class *m=i4_file_manager_class::first; m; m=m->next)
  205. if (m->unlink(name))
  206. return i4_T;
  207. return i4_F;
  208. }
  209. // returns i4_F if file does not exsist
  210. i4_bool i4_get_status(const i4_const_str &filename,
  211. i4_file_status_struct &return_stat)
  212. {
  213. for (i4_file_manager_class *m=i4_file_manager_class::first; m; m=m->next)
  214. if (m->get_status(filename, return_stat))
  215. return i4_T;
  216. return i4_F;
  217. }
  218. // return i4_F on failure
  219. i4_bool i4_mkdir(const i4_const_str &name)
  220. {
  221. for (i4_file_manager_class *m=i4_file_manager_class::first; m; m=m->next)
  222. if (m->mkdir(name))
  223. return i4_T;
  224. return i4_F;
  225. }
  226. // returns i4_F if path is bad (tfiles and tdirs will be 0 as well)
  227. // you are responsible for deleting both the array of strings and each string in the array
  228. // file_status is a pointer to an array of file_status's that will be created, you
  229. // must free these as well. file_status may be 0 (default), in which case no array is created
  230. i4_bool i4_get_directory(const i4_const_str &path,
  231. i4_directory_struct &dir_struct,
  232. i4_bool get_status,
  233. i4_status_class *status)
  234. {
  235. for (i4_file_manager_class *m=i4_file_manager_class::first; m; m=m->next)
  236. if (m->get_directory(path, dir_struct, get_status, status))
  237. return i4_T;
  238. return i4_F;
  239. }
  240. // returns i4_F if path cannot be split
  241. i4_bool i4_split_path(const i4_const_str &name, i4_filename_struct &fname_struct)
  242. {
  243. for (i4_file_manager_class *m=i4_file_manager_class::first; m; m=m->next)
  244. if (m->split_path(name, fname_struct))
  245. return i4_T;
  246. return i4_F;
  247. }
  248. // return 0 if full path cannot be determined
  249. i4_str *i4_full_path(const i4_const_str &relative_name)
  250. {
  251. for (i4_file_manager_class *m=i4_file_manager_class::first; m; m=m->next)
  252. {
  253. i4_str *s=m->full_path(relative_name);
  254. if (s)
  255. return s;
  256. }
  257. return 0;
  258. }
  259. i4_file_manager_class *i4_file_manager_class::first=0;
  260. void i4_add_file_manager(i4_file_manager_class *fman, i4_bool add_front)
  261. {
  262. if (add_front)
  263. {
  264. fman->next=i4_file_manager_class::first;
  265. i4_file_manager_class::first=fman;
  266. }
  267. else
  268. {
  269. i4_file_manager_class *last=0, *p=i4_file_manager_class::first;
  270. while (p)
  271. {
  272. last=p;
  273. p=p->next;
  274. }
  275. if (!last)
  276. i4_add_file_manager(fman, i4_T);
  277. else
  278. {
  279. last->next=fman;
  280. fman->next=0;
  281. }
  282. }
  283. }
  284. void i4_remove_file_manger(i4_file_manager_class *fman)
  285. {
  286. i4_file_manager_class *last=0, *p=i4_file_manager_class::first;
  287. while (p && p!=fman)
  288. {
  289. last=p;
  290. p=p->next;
  291. }
  292. if (p!=fman)
  293. i4_error("unable to find file manager");
  294. else if (last)
  295. last->next=fman->next;
  296. else
  297. i4_file_manager_class::first=fman->next;
  298. }
  299. i4_bool i4_file_manager_class::split_path(const i4_const_str &name, i4_filename_struct &fn)
  300. {
  301. char buf[512];
  302. i4_os_string(name, buf, 512);
  303. char *p=buf, *last_slash=0, *last_dot=0, *q;
  304. while (*p)
  305. {
  306. if (*p=='/' || *p=='\\')
  307. last_slash=p;
  308. else if (*p=='.')
  309. last_dot=p;
  310. p++;
  311. }
  312. if (last_dot)
  313. {
  314. q=fn.extension;
  315. for (p=last_dot+1; *p; )
  316. *(q++)=*(p++);
  317. *q=0;
  318. }
  319. else last_dot=p;
  320. if (last_slash)
  321. {
  322. q=fn.path;
  323. for (p=buf; p!=last_slash; )
  324. *(q++)=*(p++);
  325. *q=0;
  326. last_slash++;
  327. }
  328. else last_slash=buf;
  329. q=fn.filename;
  330. for (p=last_slash; p!=last_dot;)
  331. *(q++)=*(p++);
  332. *q=0;
  333. return i4_T;
  334. }
  335. i4_directory_struct::~i4_directory_struct()
  336. {
  337. int i;
  338. for (i=0; i<tfiles; i++)
  339. delete files[i];
  340. if (files)
  341. i4_free(files);
  342. for (i=0; i<tdirs; i++)
  343. delete dirs[i];
  344. if (dirs)
  345. i4_free(dirs);
  346. if (file_status)
  347. i4_free(file_status);
  348. }
  349. static char convert_slash(char c)
  350. {
  351. if (c=='\\') return '/';
  352. else return c;
  353. }
  354. i4_str *i4_relative_path(const i4_const_str &path)
  355. {
  356. i4_str *full_path=i4_full_path(path);
  357. i4_str *full_current=i4_full_path(i4_const_str("."));
  358. char full[512], current[512], ret[512];
  359. i4_os_string(*full_path, full, 512);
  360. i4_os_string(*full_current, current, 512);
  361. delete full_path;
  362. delete full_current;
  363. // files are on different drives
  364. if (full[1]==':' && current[1]==':' && full[0]!=current[0])
  365. return new i4_str(full);
  366. // one files is on a network drive and one is local
  367. if ((full[1]==':' && current[1]!=':') ||
  368. (full[1]!=':' && current[1]==':'))
  369. return new i4_str(full);
  370. int start=0;
  371. while (convert_slash(full[start])==convert_slash(current[start]))
  372. start++;
  373. char *c=current+start;
  374. if (*c==0)
  375. return new i4_str(full+start+1);
  376. strcpy(ret,"../");
  377. while (*c)
  378. {
  379. if (convert_slash(*c)=='/')
  380. strcpy(ret+strlen(ret),"../");
  381. c++;
  382. }
  383. strcpy(ret+strlen(ret), full+start);
  384. return new i4_str(ret);
  385. }