tpr.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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")) \
  209. dest->libopts.setting = RT_NONE; \
  210. else if (!strcmp(p,"Direct")) \
  211. dest->libopts.setting = RT_DIRECT; \
  212. else if (!strcmp(p,"AMS")) \
  213. dest->libopts.setting = RT_AMS; \
  214. else if (!strcmp(p, "Precomputed")) \
  215. dest->libopts.setting = RT_PRECOMP; \
  216. else if (!strcmp(p, "Kernel")) \
  217. dest->libopts.setting = RT_KERNEL; \
  218. else if (!strcmp(p, "Compressed")) \
  219. dest->libopts.setting = RT_COMPRESSED; \
  220. else if (!strcmp(p, "F-Line")) \
  221. dest->libopts.setting = RT_FLINE; \
  222. else if (strcmp(p, "Unknown")) \
  223. return l; \
  224. continue; \
  225. } else
  226. //this new macro was added to make merging with save_tpr more consistent.
  227. #define minams_param(token,setting) \
  228. if ( (p=find_param(buffer, "Minimum AMS Version=")) ) \
  229. { \
  230. int major, minor; \
  231. if ((strlen(p)==4) \
  232. && (sscanf(p,"%1d.%2d",&major,&minor)==2)) \
  233. dest->libopts.minams = major * 100 + minor; \
  234. else \
  235. return l; \
  236. continue; \
  237. } else
  238. boolean_param("Use TI-89=",use_ti89)
  239. boolean_param("Use TI-92 Plus=",use_ti92p)
  240. boolean_param("Use V200=",use_v200)
  241. boolean_param("Optimize Calc Consts=",opt_calc_consts)
  242. boolean_param("Use Kernel=",use_kernel)
  243. boolean_param("Use PreOS=",use_preos)
  244. boolean_param("Minimum AMS Version Defined=",use_minams)
  245. minams_param("Minimum AMS Version=",minams);
  246. boolean_param("Unofficial OS Support=",unofficial_os)
  247. reloc_param("Reloc Format=",reloc_format)
  248. reloc_param("ROM Call Format=",rom_call_format)
  249. reloc_param("BSS Ref Format=",bss_ref_format)
  250. reloc_param("Data Ref Format=",data_ref_format)
  251. boolean_param("Use F-Line Jumps=",use_fline_jumps)
  252. boolean_param("Use 4-Byte F-Line Jumps=",use_4b_fline_jumps)
  253. boolean_param("Use Internal F-Line Emulator=",use_internal_fline_emu)
  254. boolean_param("Use Return Value=",use_return_value)
  255. boolean_param("Enable Error Return=",enable_error_return)
  256. boolean_param("Save Screen=",save_screen)
  257. boolean_param("Optimize ROM Calls=",opt_rom_calls)
  258. return l;
  259. #undef boolean_param
  260. #undef reloc_param
  261. #undef minams_param
  262. }
  263. // Keywords in the [File Editing] section
  264. if(stype == SECTION_FILEEDIT)
  265. {
  266. if ( (p=find_param(buffer, "Open File=")) ) \
  267. { \
  268. if (*p) dest->open_file = p; \
  269. continue; \
  270. } else return l;
  271. }
  272. // Keywords in the [Included Files] section
  273. if(stype == SECTION_FILES)
  274. {
  275. int v;
  276. if( (p=find_numbered_param(buffer, "C File %i=", &v)) )
  277. {
  278. QString s = convert_path_separators(p);
  279. dest->c_files.path << s;
  280. dest->c_files.folder << QString::null;
  281. continue;
  282. }
  283. else if( (p=find_numbered_param(buffer, "C File %i Folder=", &v)) )
  284. {
  285. dest->c_files.folder[v-1]=p;
  286. continue;
  287. }
  288. else if( (p=find_numbered_param(buffer, "GNU Assembler File %i=", &v)) )
  289. {
  290. QString s = convert_path_separators(p);
  291. dest->s_files.path << s;
  292. dest->s_files.folder << QString::null;
  293. continue;
  294. }
  295. else if( (p=find_numbered_param(buffer, "GNU Assembler File %i Folder=", &v)) )
  296. {
  297. dest->s_files.folder[v-1]=p;
  298. continue;
  299. }
  300. else if( (p=find_numbered_param(buffer, "Header File %i=", &v)) )
  301. {
  302. QString s = convert_path_separators(p);
  303. dest->h_files.path << s;
  304. dest->h_files.folder << QString::null;
  305. continue;
  306. }
  307. else if( (p=find_numbered_param(buffer, "Header File %i Folder=", &v)) )
  308. {
  309. dest->h_files.folder[v-1]=p;
  310. continue;
  311. }
  312. else if( (p=find_numbered_param(buffer, "Assembler File %i=", &v)) )
  313. {
  314. QString s = convert_path_separators(p);
  315. dest->asm_files.path << s;
  316. dest->asm_files.folder << QString::null;
  317. continue;
  318. }
  319. else if( (p=find_numbered_param(buffer, "Assembler File %i Folder=", &v)) )
  320. {
  321. dest->asm_files.folder[v-1]=p;
  322. continue;
  323. }
  324. else if( (p=find_numbered_param(buffer, "Object File %i=", &v)) )
  325. {
  326. QString s = convert_path_separators(p);
  327. dest->o_files.path << s;
  328. dest->o_files.folder << QString::null;
  329. continue;
  330. }
  331. else if( (p=find_numbered_param(buffer, "Object File %i Folder=", &v)) )
  332. {
  333. dest->o_files.folder[v-1]=p;
  334. continue;
  335. }
  336. else if( (p=find_numbered_param(buffer, "Archive File %i=", &v)) )
  337. {
  338. QString s = convert_path_separators(p);
  339. dest->a_files.path << s;
  340. dest->a_files.folder << QString::null;
  341. continue;
  342. }
  343. else if( (p=find_numbered_param(buffer, "Archive File %i Folder=", &v)) )
  344. {
  345. dest->a_files.folder[v-1]=p;
  346. continue;
  347. }
  348. else if( (p=find_numbered_param(buffer, "Text File %i=", &v)) )
  349. {
  350. QString s = convert_path_separators(p);
  351. dest->txt_files.path << s;
  352. dest->txt_files.folder << QString::null;
  353. continue;
  354. }
  355. else if( (p=find_numbered_param(buffer, "Text File %i Folder=", &v)) )
  356. {
  357. dest->txt_files.folder[v-1]=p;
  358. continue;
  359. }
  360. else if( (p=find_numbered_param(buffer, "Quill File %i=", &v)) )
  361. {
  362. QString s = convert_path_separators(p);
  363. dest->quill_files.path << s;
  364. dest->quill_files.folder << QString::null;
  365. continue;
  366. }
  367. else if( (p=find_numbered_param(buffer, "Quill File %i Folder=", &v)) )
  368. {
  369. dest->quill_files.folder[v-1]=p;
  370. continue;
  371. }
  372. else if( (p=find_numbered_param(buffer, "Other File %i=", &v)) )
  373. {
  374. QString s = convert_path_separators(p);
  375. dest->oth_files.path << s;
  376. dest->oth_files.folder << QString::null;
  377. continue;
  378. }
  379. else if( (p=find_numbered_param(buffer, "Other File %i Folder=", &v)) )
  380. {
  381. dest->oth_files.folder[v-1]=p;
  382. continue;
  383. }
  384. else return l;
  385. }
  386. }
  387. return 0;
  388. }
  389. // returns 0 on success, -1 if it can't open the TPR and a (positive) line
  390. // number if there is an error in the TPR
  391. int loadTPR(const QString &fileName,TPRDataStruct *dest)
  392. {
  393. FILE *f;
  394. int ret;
  395. f = fopen(fileName, "r");
  396. if(f == NULL) {
  397. return -1;
  398. }
  399. ret=parse_file(f,dest);
  400. fclose(f);
  401. return ret;
  402. }
  403. QString loadFileText(const char *fileName)
  404. {
  405. FILE *f;
  406. f=fopen(fileName,"rb");
  407. if (!f)
  408. return QString::null;
  409. fseek(f,0,SEEK_END);
  410. size_t flen=ftell(f);
  411. fseek(f,0,SEEK_SET);
  412. char buffer[flen+1];
  413. memset(buffer,0,flen+1);
  414. fread(buffer,1,flen,f);
  415. return QString(buffer);
  416. }
  417. QString convert_path_separators_save(QString s)
  418. {
  419. int o;
  420. #ifndef __WIN32__
  421. while ((o=s.find('/',0,TRUE))>=0)
  422. s[o]='\\';
  423. #endif
  424. return s;
  425. }
  426. //this is here because QString::ascii() returns "(null)" on a null string.
  427. const char *smartAscii(const QString &s)
  428. {
  429. if (s.isNull())
  430. return "";
  431. return s.ascii();
  432. }
  433. int save_tpr(FILE *f,TPRDataStruct *dest)
  434. {
  435. unsigned i=0,e;
  436. QString tmp;
  437. #define boolean_param(token,setting) fprintf(f,token "%d\r\n",!!dest->settings.setting);
  438. #define string_vparam(token,var) fprintf(f,token "%s\r\n",smartAscii(dest->var));
  439. #define string_param(token,setting) string_vparam(token,settings.setting)
  440. #define ignore_param(token) /**/
  441. fputs("[Settings]\r\n",f);
  442. boolean_param("Archive=",archive)
  443. boolean_param("Pack=",pack)
  444. string_param("Packed Variable=",pack_name)
  445. string_vparam("Project Name=",prj_name)
  446. string_param("GCC Switches=",cc_switches)
  447. string_param("Assembler Switches=",a68k_switches)
  448. ignore_param("Linker Switches=") // Obsolete. Ignore.
  449. ignore_param("GNU Linker Switches=") // Obsolete. Ignore.
  450. string_param("GNU Assembler Switches=",as_switches)
  451. ignore_param("BSR Patch=") // Obsolete. Ignore.
  452. boolean_param("Debug Info=",debug_info)
  453. boolean_param("Standard Library=",std_lib)
  454. string_param("Command Line=",cmd_line)
  455. string_param("Post-Build Process=",post_build)
  456. boolean_param("Use Data Variable=",use_data_var)
  457. string_param("Data Variable=",data_var)
  458. boolean_param("Copy Data Variable=",copy_data_var)
  459. boolean_param("Copy Data Variable if Archived=",copy_data_var_arc)
  460. boolean_param("Optimize NOPs=",optimize_nops)
  461. boolean_param("Optimize Returns=",optimize_returns)
  462. boolean_param("Optimize Branches=",optimize_branches)
  463. boolean_param("Optimize Moves=",optimize_moves)
  464. boolean_param("Optimize Tests=",optimize_tests)
  465. boolean_param("Optimize Calculations=",optimize_calcs)
  466. boolean_param("Remove Unused Sections=",remove_unused)
  467. boolean_param("Binary Output=",outputbin)
  468. boolean_param("Fargo=",fargo)
  469. boolean_param("Flash OS=",flash_os)
  470. boolean_param("Cut Unused Ranges=",cut_ranges)
  471. boolean_param("Reorder Sections=",reorder_sections)
  472. boolean_param("Merge Constants=",merge_constants)
  473. boolean_param("Initialize BSS=",initialize_bss)
  474. #undef boolean_param
  475. #undef string_vparam
  476. #undef string_param
  477. #undef ignore_param
  478. #define boolean_param(token,setting) fprintf(f,token "%d\r\n",!!dest->libopts.setting);
  479. #define reloc_param(token,setting) \
  480. switch(dest->libopts.setting) \
  481. { \
  482. case RT_NONE: \
  483. fprintf(f,token "None\r\n"); \
  484. break; \
  485. case RT_DIRECT: \
  486. fprintf(f,token "Direct\r\n"); \
  487. break; \
  488. case RT_AMS: \
  489. fprintf(f,token "AMS\r\n"); \
  490. break; \
  491. case RT_PRECOMP: \
  492. fprintf(f,token "Precomputed\r\n"); \
  493. break; \
  494. case RT_KERNEL: \
  495. fprintf(f,token "Kernel\r\n"); \
  496. break; \
  497. case RT_COMPRESSED: \
  498. fprintf(f,token "Compressed\r\n"); \
  499. break; \
  500. case RT_FLINE: \
  501. fprintf(f,token "F-Line\r\n"); \
  502. break; \
  503. }
  504. #define minams_param(token,setting) \
  505. { \
  506. int major,minor; \
  507. major=dest->libopts.setting/100; \
  508. minor=dest->libopts.setting%100; \
  509. fprintf(f,token "%d.%02d\r\n",major,minor); \
  510. }
  511. fputs("\r\n[Library Options]\r\n",f);
  512. boolean_param("Use TI-89=",use_ti89)
  513. boolean_param("Use TI-92 Plus=",use_ti92p)
  514. boolean_param("Use V200=",use_v200)
  515. boolean_param("Optimize Calc Consts=",opt_calc_consts)
  516. boolean_param("Use Kernel=",use_kernel)
  517. boolean_param("Use PreOS=",use_preos)
  518. boolean_param("Minimum AMS Version Defined=",use_minams)
  519. minams_param("Minimum AMS Version=",minams);
  520. boolean_param("Unofficial OS Support=",unofficial_os)
  521. reloc_param("Reloc Format=",reloc_format)
  522. reloc_param("ROM Call Format=",rom_call_format)
  523. reloc_param("BSS Ref Format=",bss_ref_format)
  524. reloc_param("Data Ref Format=",data_ref_format)
  525. boolean_param("Use F-Line Jumps=",use_fline_jumps)
  526. boolean_param("Use 4-Byte F-Line Jumps=",use_4b_fline_jumps)
  527. boolean_param("Use Internal F-Line Emulator=",use_internal_fline_emu)
  528. boolean_param("Use Return Value=",use_return_value)
  529. boolean_param("Enable Error Return=",enable_error_return)
  530. boolean_param("Save Screen=",save_screen)
  531. boolean_param("Optimize ROM Calls=",opt_rom_calls)
  532. #undef boolean_param
  533. #undef reloc_param
  534. #undef minams_param
  535. fprintf(f,"\r\n[File Editing]\r\nOpen File=%s\r\n\r\n[Included Files]\r\n",smartAscii(dest->open_file));
  536. #define filepath_param(token,filetype) \
  537. e=dest->filetype.path.count(); \
  538. for(i=0;i<e;i++) \
  539. { \
  540. tmp=convert_path_separators_save(smartAscii(dest->filetype.path[i])); \
  541. fprintf(f,token " %u=%s\r\n",i+1,smartAscii(tmp)); \
  542. if (!dest->filetype.folder[i].isEmpty()) \
  543. { \
  544. fprintf(f,token " %u Folder=%s\r\n",i+1,smartAscii(dest->filetype.folder[i])); \
  545. } \
  546. }
  547. filepath_param("C File",c_files)
  548. filepath_param("GNU Assembler File",s_files)
  549. filepath_param("Header File",h_files)
  550. filepath_param("Assembler File",asm_files)
  551. filepath_param("Object File",o_files)
  552. filepath_param("Archive File",a_files)
  553. filepath_param("Text File",txt_files)
  554. filepath_param("Quill File",quill_files)
  555. filepath_param("Other File",oth_files)
  556. #undef filepath_param
  557. fputs("\r\n",f);
  558. return 0;
  559. }
  560. //returns 0 on success, -1 if the file isn't there, and -2 if there's not enough memory.
  561. int saveTPR(const QString &fileName,TPRDataStruct *src)
  562. {
  563. FILE *f;
  564. int ret;
  565. f=fopen(fileName,"wb");
  566. if (!f)
  567. {
  568. return -1;
  569. }
  570. ret=save_tpr(f,src);
  571. fclose(f);
  572. return ret;
  573. }
  574. void mkdir_multi(const char *fileName)
  575. {
  576. int l=strlen(fileName);
  577. char buffer[l+2];
  578. char *ptr;
  579. #ifdef __WIN32__
  580. ptr=strchr(fileName,'\\');
  581. #else
  582. ptr=strchr(fileName,'/');
  583. #endif
  584. while (ptr)
  585. {
  586. memcpy(buffer,fileName,ptr-fileName);
  587. buffer[ptr-fileName]=0;
  588. mkdir(buffer,S_IRWXU | S_IRWXG | S_IRWXO);
  589. #ifdef __WIN32__
  590. ptr=strchr(ptr+1,'\\');
  591. #else
  592. ptr=strchr(ptr+1,'/');
  593. #endif
  594. }
  595. }
  596. int saveFileText(const char *fileName,QString &fileText)
  597. {
  598. FILE *f;
  599. const char *s;
  600. f=fopen(fileName,"wb");
  601. if (!f)
  602. {
  603. mkdir_multi(fileName);
  604. f=fopen(fileName,"wb");
  605. if (!f)
  606. return -1;
  607. }
  608. s=smartAscii(fileText);
  609. fwrite(s,1,strlen(s),f);
  610. fclose(f);
  611. return 0;
  612. }
  613. void kurlNewFileName(KURL &dir,const QString &newFileName)
  614. {
  615. if (newFileName[0]=='/')
  616. dir.setPath(newFileName);
  617. else
  618. dir.setFileName(newFileName);
  619. }
  620. QString pullOutFileSuffix(const QString &srcFileName,QString &destFileName)
  621. {
  622. int a,b;
  623. QString ret;
  624. destFileName=srcFileName;
  625. a=destFileName.findRev('.');
  626. b=destFileName.findRev('/');
  627. if (a<0)
  628. return QString::null;
  629. if (a<b)
  630. return QString::null;
  631. ret=destFileName.mid(a+1);
  632. destFileName.truncate(a);
  633. return ret;
  634. }
  635. int checkFileName(const QString &fileName,const QStringList &fileNameList)
  636. {
  637. int i;
  638. QString fileName_name,fileName_suffix;
  639. QString name,suffix;
  640. fileName_suffix=pullOutFileSuffix(fileName,fileName_name);
  641. for (i=fileNameList.count()-1;i>=0;i--)
  642. {
  643. suffix=pullOutFileSuffix(fileNameList[i],name);
  644. if (!suffix.compare("c")||!suffix.compare("s")||!suffix.compare("asm")||!suffix.compare("o")||!suffix.compare("qll"))
  645. {
  646. if (!fileName_suffix.compare("c")||!fileName_suffix.compare("s")||!fileName_suffix.compare("asm")||!fileName_suffix.compare("o")||!fileName_suffix.compare("qll"))
  647. {
  648. if (!name.compare(fileName_name))
  649. return 0;
  650. }
  651. }
  652. else
  653. {
  654. if (!fileNameList[i].compare(fileName))
  655. return 0;
  656. }
  657. }
  658. return 1;
  659. }