tpr.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. ktigcc - TIGCC IDE for KDE
  3. tpr handling routines adapted from tprbuilder
  4. Copyright (C) 2002 Romain Liévin
  5. Copyright (C) 2002-2006 Kevin Kofler
  6. Copyright (C) 2006 Joey Adams
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software Foundation,
  17. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <unistd.h>
  22. #include <sys/stat.h>
  23. #include <kapplication.h>
  24. #include <kcmdlineargs.h>
  25. #include <kaboutdata.h>
  26. #include <qtextcodec.h>
  27. #include "tpr.h"
  28. #define find_param(s_,t_) find_param_ex((s_),(t_),sizeof(t_)-1)
  29. char *find_param_ex(char *s, const char *t, size_t l)
  30. {
  31. if (!strncmp(s,t,l))
  32. return s+l;
  33. else
  34. return NULL;
  35. }
  36. char *find_numbered_param(char *s, const char*t, int *i)
  37. {
  38. char *p;
  39. int endpos = 0;
  40. int ret = 0;
  41. char arglist[256];
  42. strcpy(arglist, t);
  43. strcat(arglist, "%n");
  44. ret = sscanf(s, arglist, i, &endpos);
  45. if(ret < 1 || !endpos) return NULL;
  46. p = s + endpos;
  47. return p;
  48. }
  49. // converts Windows paths to Unix paths if necessary.
  50. QString convert_path_separators(const char *file)
  51. {
  52. QString s=file;
  53. int o;
  54. #ifndef __WIN32__
  55. while ((o=s.find('\\',0,TRUE))>=0)
  56. s[o]='/';
  57. #endif
  58. return s;
  59. }
  60. char* strip(char *str)
  61. {
  62. int len = strlen(str);
  63. if(len > 0)
  64. if( (str[len-1] == '\r') || (str[len-1] == '\n') )
  65. str[len-1] = '\0';
  66. if(len > 1)
  67. if( (str[len-2] == '\r') || (str[len-2] == '\n') )
  68. str[len-2] = '\0';
  69. return str;
  70. }
  71. /*
  72. Read a line from file and do a clean-up
  73. */
  74. int read_line(FILE *f, char *buffer, int *l)
  75. {
  76. strcpy(buffer, "");
  77. if(feof(f)) return EOF;
  78. fgets(buffer, 256, f);
  79. strip(buffer);
  80. (*l)++;
  81. while( (buffer[0] == '\r') || (buffer[0] == '\n') || (buffer[0] == '\0') )
  82. {
  83. if(feof(f)) return EOF;
  84. fgets(buffer, 256, f);
  85. strip(buffer);
  86. (*l)++;
  87. }
  88. return 0;
  89. }
  90. //some items from the TSR might still be ignored or not shown here.
  91. //if you need one of them, add an entry to one of the
  92. //definition "tables".
  93. //If you add an entry here, don't forget to add something to
  94. // save_tpr!
  95. int parse_file(FILE *f,TPRDataStruct *dest)
  96. {
  97. char buffer[256];
  98. int l = 0;
  99. SectionType stype = SECTION_NONE;
  100. // Some defaults are different when opening an existing project.
  101. dest->settings.cc_switches="";
  102. dest->settings.as_switches="";
  103. dest->settings.a68k_switches="";
  104. dest->libopts.use_ti89=0;
  105. dest->libopts.use_ti92p=0;
  106. dest->libopts.use_v200=0;
  107. dest->libopts.use_minams=0;
  108. dest->libopts.save_screen=0;
  109. while(!feof(f))
  110. {
  111. char *p;
  112. // Get a line from file
  113. if(read_line(f, buffer, &l) == EOF) break;
  114. // Search for sections
  115. if( !strcmp(buffer, "[Settings]") )
  116. {
  117. stype = SECTION_SETTINGS;
  118. continue;
  119. }
  120. if( !strcmp(buffer, "[Library Options]") )
  121. {
  122. stype = SECTION_LIBOPTS;
  123. continue;
  124. }
  125. if( !strcmp(buffer, "[File Editing]") )
  126. {
  127. stype = SECTION_FILEEDIT;
  128. continue;
  129. }
  130. if( !strcmp(buffer, "[Included Files]") )
  131. {
  132. stype = SECTION_FILES;
  133. continue;
  134. }
  135. // Keywords in the [Settings] section
  136. if(stype == SECTION_SETTINGS) {
  137. #define boolean_param(token,setting) \
  138. if ( (p=find_param(buffer, token)) ) \
  139. { \
  140. if(!strcmp(p, "0")) dest->settings.setting = FALSE; \
  141. else if(!strcmp(p, "1")) dest->settings.setting = TRUE; \
  142. else return l; \
  143. continue; \
  144. } else
  145. #define string_vparam(token,var) \
  146. if ( (p=find_param(buffer, token)) ) \
  147. { \
  148. if (*p) dest->var = p; \
  149. continue; \
  150. } else
  151. #define string_param(token,setting) string_vparam(token,settings.setting)
  152. #define ignore_param(token) \
  153. if( (p=find_param(buffer, token)) ) \
  154. { \
  155. continue; \
  156. } else
  157. boolean_param("Archive=",archive)
  158. boolean_param("Pack=",pack)
  159. string_param("Packed Variable=",pack_name)
  160. string_vparam("Project Name=",prj_name)
  161. string_param("GCC Switches=",cc_switches)
  162. string_param("Assembler Switches=",a68k_switches)
  163. ignore_param("Linker Switches=") // Obsolete. Ignore.
  164. ignore_param("GNU Linker Switches=") // Obsolete. Ignore.
  165. string_param("GNU Assembler Switches=",as_switches)
  166. ignore_param("BSR Patch=") // Obsolete. Ignore.
  167. boolean_param("Debug Info=",debug_info)
  168. boolean_param("Standard Library=",std_lib)
  169. string_param("Command Line=",cmd_line)
  170. string_param("Post-Build Process=",post_build)
  171. boolean_param("Use Data Variable=",use_data_var)
  172. string_param("Data Variable=",data_var)
  173. boolean_param("Copy Data Variable=",copy_data_var)
  174. boolean_param("Copy Data Variable if Archived=",copy_data_var_arc)
  175. boolean_param("Optimize NOPs=",optimize_nops)
  176. boolean_param("Optimize Returns=",optimize_returns)
  177. boolean_param("Optimize Branches=",optimize_branches)
  178. boolean_param("Optimize Moves=",optimize_moves)
  179. boolean_param("Optimize Tests=",optimize_tests)
  180. boolean_param("Optimize Calculations=",optimize_calcs)
  181. boolean_param("Remove Unused Sections=",remove_unused)
  182. boolean_param("Binary Output=",outputbin)
  183. boolean_param("Fargo=",fargo)
  184. boolean_param("Flash OS=",flash_os)
  185. boolean_param("Cut Unused Ranges=",cut_ranges)
  186. boolean_param("Reorder Sections=",reorder_sections)
  187. boolean_param("Merge Constants=",merge_constants)
  188. boolean_param("Initialize BSS=",initialize_bss)
  189. return l;
  190. #undef boolean_param
  191. #undef string_vparam
  192. #undef string_param
  193. #undef ignore_param
  194. }
  195. // Keywords in the [Library Options] section
  196. if(stype == SECTION_LIBOPTS) {
  197. #define boolean_param(token,setting) \
  198. if ( (p=find_param(buffer, token)) ) \
  199. { \
  200. if(!strcmp(p, "0")) dest->libopts.setting = FALSE; \
  201. else if(!strcmp(p, "1")) dest->libopts.setting = TRUE; \
  202. else return l; \
  203. continue; \
  204. } else
  205. #define reloc_param(token,setting) \
  206. if ( (p=find_param(buffer, token)) ) \
  207. { \
  208. if (!strcmp(p,"None") || !strcmp(p,"AMS") \
  209. || !strcmp(p,"Direct")) \
  210. dest->libopts.setting = RT_NONE; \
  211. else if (!strcmp(p, "Precomputed")) \
  212. dest->libopts.setting = RT_PRECOMP; \
  213. else if (!strcmp(p, "Kernel")) \
  214. dest->libopts.setting = RT_KERNEL; \
  215. else if (!strcmp(p, "Compressed")) \
  216. dest->libopts.setting = RT_COMPRESSED; \
  217. else if (!strcmp(p, "F-Line")) \
  218. dest->libopts.setting = RT_FLINE; \
  219. else if (strcmp(p, "Unknown")) \
  220. return l; \
  221. continue; \
  222. } else
  223. //this new macro was added to make merging with save_tpr more consistent.
  224. #define minams_param(token,setting) \
  225. if ( (p=find_param(buffer, "Minimum AMS Version=")) ) \
  226. { \
  227. int major, minor; \
  228. if ((strlen(p)==4) \
  229. && (sscanf(p,"%1d.%2d",&major,&minor)==2)) \
  230. dest->libopts.minams = major * 100 + minor; \
  231. else \
  232. return l; \
  233. continue; \
  234. } else
  235. boolean_param("Use TI-89=",use_ti89)
  236. boolean_param("Use TI-92 Plus=",use_ti92p)
  237. boolean_param("Use V200=",use_v200)
  238. boolean_param("Optimize Calc Consts=",opt_calc_consts)
  239. boolean_param("Use Kernel=",use_kernel)
  240. boolean_param("Use PreOS=",use_preos)
  241. boolean_param("Minimum AMS Version Defined=",use_minams)
  242. minams_param("Minimum AMS Version=",minams);
  243. boolean_param("Unofficial OS Support=",unofficial_os)
  244. reloc_param("Reloc Format=",reloc_format)
  245. reloc_param("ROM Call Format=",rom_call_format)
  246. reloc_param("BSS Ref Format=",bss_ref_format)
  247. reloc_param("Data Ref Format=",data_ref_format)
  248. boolean_param("Use F-Line Jumps=",use_fline_jumps)
  249. boolean_param("Use 4-Byte F-Line Jumps=",use_4b_fline_jumps)
  250. boolean_param("Use Internal F-Line Emulator=",use_internal_fline_emu)
  251. boolean_param("Use Return Value=",use_return_value)
  252. boolean_param("Enable Error Return=",enable_error_return)
  253. boolean_param("Save Screen=",save_screen)
  254. boolean_param("Optimize ROM Calls=",opt_rom_calls)
  255. return l;
  256. #undef boolean_param
  257. #undef reloc_param
  258. #undef minams_param
  259. }
  260. // Keywords in the [File Editing] section
  261. if(stype == SECTION_FILEEDIT)
  262. {
  263. if ( (p=find_param(buffer, "Open File=")) ) \
  264. { \
  265. if (*p) dest->open_file = p; \
  266. continue; \
  267. } else return l;
  268. }
  269. // Keywords in the [Included Files] section
  270. if(stype == SECTION_FILES)
  271. {
  272. int v;
  273. if( (p=find_numbered_param(buffer, "C File %i=", &v)) )
  274. {
  275. QString s = convert_path_separators(p);
  276. dest->c_files.path << s;
  277. dest->c_files.folder << QString::null;
  278. continue;
  279. }
  280. else if( (p=find_numbered_param(buffer, "C File %i Folder=", &v)) )
  281. {
  282. dest->c_files.folder[v-1]=p;
  283. continue;
  284. }
  285. else if( (p=find_numbered_param(buffer, "GNU Assembler File %i=", &v)) )
  286. {
  287. QString s = convert_path_separators(p);
  288. dest->s_files.path << s;
  289. dest->s_files.folder << QString::null;
  290. continue;
  291. }
  292. else if( (p=find_numbered_param(buffer, "GNU Assembler File %i Folder=", &v)) )
  293. {
  294. dest->s_files.folder[v-1]=p;
  295. continue;
  296. }
  297. else if( (p=find_numbered_param(buffer, "Header File %i=", &v)) )
  298. {
  299. QString s = convert_path_separators(p);
  300. dest->h_files.path << s;
  301. dest->h_files.folder << QString::null;
  302. continue;
  303. }
  304. else if( (p=find_numbered_param(buffer, "Header File %i Folder=", &v)) )
  305. {
  306. dest->h_files.folder[v-1]=p;
  307. continue;
  308. }
  309. else if( (p=find_numbered_param(buffer, "Assembler File %i=", &v)) )
  310. {
  311. QString s = convert_path_separators(p);
  312. dest->asm_files.path << s;
  313. dest->asm_files.folder << QString::null;
  314. continue;
  315. }
  316. else if( (p=find_numbered_param(buffer, "Assembler File %i Folder=", &v)) )
  317. {
  318. dest->asm_files.folder[v-1]=p;
  319. continue;
  320. }
  321. else if( (p=find_numbered_param(buffer, "Object File %i=", &v)) )
  322. {
  323. QString s = convert_path_separators(p);
  324. dest->o_files.path << s;
  325. dest->o_files.folder << QString::null;
  326. continue;
  327. }
  328. else if( (p=find_numbered_param(buffer, "Object File %i Folder=", &v)) )
  329. {
  330. dest->o_files.folder[v-1]=p;
  331. continue;
  332. }
  333. else if( (p=find_numbered_param(buffer, "Archive File %i=", &v)) )
  334. {
  335. QString s = convert_path_separators(p);
  336. dest->a_files.path << s;
  337. dest->a_files.folder << QString::null;
  338. continue;
  339. }
  340. else if( (p=find_numbered_param(buffer, "Archive File %i Folder=", &v)) )
  341. {
  342. dest->a_files.folder[v-1]=p;
  343. continue;
  344. }
  345. else if( (p=find_numbered_param(buffer, "Text File %i=", &v)) )
  346. {
  347. QString s = convert_path_separators(p);
  348. dest->txt_files.path << s;
  349. dest->txt_files.folder << QString::null;
  350. continue;
  351. }
  352. else if( (p=find_numbered_param(buffer, "Text File %i Folder=", &v)) )
  353. {
  354. dest->txt_files.folder[v-1]=p;
  355. continue;
  356. }
  357. else if( (p=find_numbered_param(buffer, "Quill File %i=", &v)) )
  358. {
  359. QString s = convert_path_separators(p);
  360. dest->quill_files.path << s;
  361. dest->quill_files.folder << QString::null;
  362. continue;
  363. }
  364. else if( (p=find_numbered_param(buffer, "Quill File %i Folder=", &v)) )
  365. {
  366. dest->quill_files.folder[v-1]=p;
  367. continue;
  368. }
  369. else if( (p=find_numbered_param(buffer, "Other File %i=", &v)) )
  370. {
  371. QString s = convert_path_separators(p);
  372. dest->oth_files.path << s;
  373. dest->oth_files.folder << QString::null;
  374. continue;
  375. }
  376. else if( (p=find_numbered_param(buffer, "Other File %i Folder=", &v)) )
  377. {
  378. dest->oth_files.folder[v-1]=p;
  379. continue;
  380. }
  381. else return l;
  382. }
  383. }
  384. return 0;
  385. }
  386. //returns 0 on success
  387. int loadTPR(const QString &fileName,TPRDataStruct *dest)
  388. {
  389. FILE *f;
  390. int ret;
  391. f = fopen(fileName, "r");
  392. if(f == NULL) {
  393. return -1;
  394. }
  395. ret=parse_file(f,dest);
  396. fclose(f);
  397. return ret;
  398. }
  399. QString loadFileText(const char *fileName)
  400. {
  401. FILE *f;
  402. f=fopen(fileName,"rb");
  403. if (!f)
  404. return QString::null;
  405. fseek(f,0,SEEK_END);
  406. size_t flen=ftell(f);
  407. fseek(f,0,SEEK_SET);
  408. char buffer[flen+1];
  409. memset(buffer,0,flen+1);
  410. fread(buffer,1,flen,f);
  411. return QString(buffer);
  412. }
  413. QString convert_path_separators_save(QString s)
  414. {
  415. int o;
  416. #ifndef __WIN32__
  417. while ((o=s.find('/',0,TRUE))>=0)
  418. s[o]='\\';
  419. #endif
  420. return s;
  421. }
  422. //this is here because QString::ascii() returns "(null)" on a null string.
  423. const char *smartAscii(const QString &s)
  424. {
  425. if (s.isNull())
  426. return "";
  427. return s.ascii();
  428. }
  429. //To do:
  430. //use macros to merge token with "%d\r\n" and "%s\r\n" instead of adding token to the pattern, which might not be necessary.
  431. //can a macro have absolutely nothing in it? that's why I said i there, since it had to be something to do.
  432. //reloc_param's macro here just puts "None" for the RT_NONE macro, which is probably not entirely correct, but it should work in all cases anyway.
  433. //some parameters are booleans for if a parameter is defined or not. should the parameter be shown anyway, even if the associated boolean is false?
  434. int save_tpr(FILE *f,TPRDataStruct *dest)
  435. {
  436. int i=0,e;
  437. QString tmp;
  438. #define boolean_param(token,setting) fprintf(f,"%s%d\r\n",token,!!dest->settings.setting);
  439. #define string_vparam(token,var) fprintf(f,"%s%s\r\n",token,smartAscii(dest->var));
  440. #define string_param(token,setting) string_vparam(token,settings.setting)
  441. #define ignore_param(token) /**/
  442. fputs("[Settings]\r\n",f);
  443. boolean_param("Archive=",archive)
  444. boolean_param("Pack=",pack)
  445. string_param("Packed Variable=",pack_name)
  446. string_vparam("Project Name=",prj_name)
  447. string_param("GCC Switches=",cc_switches)
  448. string_param("Assembler Switches=",a68k_switches)
  449. ignore_param("Linker Switches=") // Obsolete. Ignore.
  450. ignore_param("GNU Linker Switches=") // Obsolete. Ignore.
  451. string_param("GNU Assembler Switches=",as_switches)
  452. ignore_param("BSR Patch=") // Obsolete. Ignore.
  453. boolean_param("Debug Info=",debug_info)
  454. boolean_param("Standard Library=",std_lib)
  455. string_param("Command Line=",cmd_line)
  456. string_param("Post-Build Process=",post_build)
  457. boolean_param("Use Data Variable=",use_data_var)
  458. string_param("Data Variable=",data_var)
  459. boolean_param("Copy Data Variable=",copy_data_var)
  460. boolean_param("Copy Data Variable if Archived=",copy_data_var_arc)
  461. boolean_param("Optimize NOPs=",optimize_nops)
  462. boolean_param("Optimize Returns=",optimize_returns)
  463. boolean_param("Optimize Branches=",optimize_branches)
  464. boolean_param("Optimize Moves=",optimize_moves)
  465. boolean_param("Optimize Tests=",optimize_tests)
  466. boolean_param("Optimize Calculations=",optimize_calcs)
  467. boolean_param("Remove Unused Sections=",remove_unused)
  468. boolean_param("Binary Output=",outputbin)
  469. boolean_param("Fargo=",fargo)
  470. boolean_param("Flash OS=",flash_os)
  471. boolean_param("Cut Unused Ranges=",cut_ranges)
  472. boolean_param("Reorder Sections=",reorder_sections)
  473. boolean_param("Merge Constants=",merge_constants)
  474. boolean_param("Initialize BSS=",initialize_bss)
  475. #undef boolean_param
  476. #undef string_vparam
  477. #undef string_param
  478. #undef ignore_param
  479. #define boolean_param(token,setting) fprintf(f,"%s%d\r\n",token,!!dest->libopts.setting);
  480. #define reloc_param(token,setting) \
  481. switch(dest->libopts.setting) \
  482. { \
  483. case RT_NONE: \
  484. fprintf(f,"%sNone\r\n",token); \
  485. break; \
  486. case RT_PRECOMP: \
  487. fprintf(f,"%sPrecomputed\r\n",token); \
  488. break; \
  489. case RT_KERNEL: \
  490. fprintf(f,"%sKernel\r\n",token); \
  491. break; \
  492. case RT_COMPRESSED: \
  493. fprintf(f,"%sCompressed\r\n",token); \
  494. break; \
  495. case RT_FLINE: \
  496. fprintf(f,"%sF-Line\r\n",token); \
  497. break; \
  498. }
  499. #define minams_param(token,setting) \
  500. { \
  501. int major,minor; \
  502. major=dest->libopts.setting/100; \
  503. minor=dest->libopts.setting%100; \
  504. if (minor<10) \
  505. fprintf(f,"%s%d.0%d\r\n",token,major,minor); \
  506. else \
  507. fprintf(f,"%s%d.%d\r\n",token,major,minor); \
  508. }
  509. fputs("\r\n[Library Options]\r\n",f);
  510. boolean_param("Use TI-89=",use_ti89)
  511. boolean_param("Use TI-92 Plus=",use_ti92p)
  512. boolean_param("Use V200=",use_v200)
  513. boolean_param("Optimize Calc Consts=",opt_calc_consts)
  514. boolean_param("Use Kernel=",use_kernel)
  515. boolean_param("Use PreOS=",use_preos)
  516. boolean_param("Minimum AMS Version Defined=",use_minams)
  517. minams_param("Minimum AMS Version=",minams);
  518. boolean_param("Unofficial OS Support=",unofficial_os)
  519. reloc_param("Reloc Format=",reloc_format)
  520. reloc_param("ROM Call Format=",rom_call_format)
  521. reloc_param("BSS Ref Format=",bss_ref_format)
  522. reloc_param("Data Ref Format=",data_ref_format)
  523. boolean_param("Use F-Line Jumps=",use_fline_jumps)
  524. boolean_param("Use 4-Byte F-Line Jumps=",use_4b_fline_jumps)
  525. boolean_param("Use Internal F-Line Emulator=",use_internal_fline_emu)
  526. boolean_param("Use Return Value=",use_return_value)
  527. boolean_param("Enable Error Return=",enable_error_return)
  528. boolean_param("Save Screen=",save_screen)
  529. boolean_param("Optimize ROM Calls=",opt_rom_calls)
  530. #undef boolean_param
  531. #undef reloc_param
  532. #undef minams_param
  533. fprintf(f,"\r\n[File Editing]\r\nOpen File=%s\r\n\r\n[Included Files]\r\n",smartAscii(dest->open_file));
  534. #define filepath_param(token,filetype) \
  535. e=dest->filetype.path.count(); \
  536. for(i=0;i<e;i++) \
  537. { \
  538. tmp=convert_path_separators_save(smartAscii(dest->filetype.path[i])); \
  539. fprintf(f,"%s %ld=%s\r\n",token,(long)(i+1),smartAscii(tmp)); \
  540. if (!dest->filetype.folder[i].isEmpty()) \
  541. { \
  542. fprintf(f,"%s %ld Folder=%s\r\n",token,(long)(i+1),smartAscii(dest->filetype.folder[i])); \
  543. } \
  544. }
  545. filepath_param("C File",c_files)
  546. filepath_param("GNU Assembler File",s_files)
  547. filepath_param("Header File",h_files)
  548. filepath_param("Assembler File",asm_files)
  549. filepath_param("Object File",o_files)
  550. filepath_param("Archive File",a_files)
  551. filepath_param("Text File",txt_files)
  552. filepath_param("Quill File",quill_files)
  553. filepath_param("Other File",oth_files)
  554. #undef filepath_param
  555. fputs("\r\n",f);
  556. return 0;
  557. }
  558. //returns 0 on success, -1 if the file isn't there, and -2 if there's not enough memory.
  559. int saveTPR(const QString &fileName,TPRDataStruct *src)
  560. {
  561. FILE *f;
  562. int ret;
  563. f=fopen(fileName,"wb");
  564. if (!f)
  565. {
  566. return -1;
  567. }
  568. ret=save_tpr(f,src);
  569. fclose(f);
  570. return ret;
  571. }
  572. void mkdir_multi(const char *fileName)
  573. {
  574. int l=strlen(fileName);
  575. char buffer[l+2];
  576. char *ptr;
  577. #ifdef __WIN32__
  578. ptr=strchr(fileName,'\\');
  579. #else
  580. ptr=strchr(fileName,'/');
  581. #endif
  582. while (ptr)
  583. {
  584. memcpy(buffer,fileName,ptr-fileName);
  585. buffer[ptr-fileName]=0;
  586. mkdir(buffer,S_IRWXU | S_IRWXG | S_IRWXO);
  587. #ifdef __WIN32__
  588. ptr=strchr(ptr+1,'\\');
  589. #else
  590. ptr=strchr(ptr+1,'/');
  591. #endif
  592. }
  593. }
  594. int saveFileText(const char *fileName,QString &fileText)
  595. {
  596. FILE *f;
  597. const char *s;
  598. f=fopen(fileName,"wb");
  599. if (!f)
  600. {
  601. mkdir_multi(fileName);
  602. f=fopen(fileName,"wb");
  603. if (!f)
  604. return -1;
  605. }
  606. s=smartAscii(fileText);
  607. fwrite(s,1,strlen(s),f);
  608. fclose(f);
  609. return 0;
  610. }
  611. void kurlNewFileName(KURL &dir,const QString &newFileName)
  612. {
  613. if (newFileName[0]=='/')
  614. dir.setPath(newFileName);
  615. else
  616. dir.setFileName(newFileName);
  617. }
  618. QString pullOutFileSuffix(const QString &srcFileName,QString &destFileName)
  619. {
  620. int a,b;
  621. QString ret;
  622. destFileName=srcFileName;
  623. a=destFileName.findRev('.');
  624. b=destFileName.findRev('/');
  625. if (a<0)
  626. return QString::null;
  627. if (a<b)
  628. return QString::null;
  629. ret=destFileName.mid(a+1);
  630. destFileName.truncate(a);
  631. return ret;
  632. }
  633. int checkFileName(const QString &fileName,const QStringList &fileNameList)
  634. {
  635. int i;
  636. QString fileName_name,fileName_suffix;
  637. QString name,suffix;
  638. fileName_suffix=pullOutFileSuffix(fileName,fileName_name);
  639. for (i=fileNameList.count()-1;i>=0;i--)
  640. {
  641. suffix=pullOutFileSuffix(fileNameList[i],name);
  642. if (!suffix.compare("c")||!suffix.compare("s")||!suffix.compare("asm")||!suffix.compare("o")||!suffix.compare("qll"))
  643. {
  644. if (!fileName_suffix.compare("c")||!fileName_suffix.compare("s")||!fileName_suffix.compare("asm")||!fileName_suffix.compare("o")||!fileName_suffix.compare("qll"))
  645. {
  646. if (!name.compare(fileName_name))
  647. return 0;
  648. }
  649. }
  650. else
  651. {
  652. if (!fileNameList[i].compare(fileName))
  653. return 0;
  654. }
  655. }
  656. return 1;
  657. }