tpr.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. //To do:
  434. //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.
  435. //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?
  436. int save_tpr(FILE *f,TPRDataStruct *dest)
  437. {
  438. unsigned i=0,e;
  439. QString tmp;
  440. #define boolean_param(token,setting) fprintf(f,token "%d\r\n",!!dest->settings.setting);
  441. #define string_vparam(token,var) fprintf(f,token "%s\r\n",smartAscii(dest->var));
  442. #define string_param(token,setting) string_vparam(token,settings.setting)
  443. #define ignore_param(token) /**/
  444. fputs("[Settings]\r\n",f);
  445. boolean_param("Archive=",archive)
  446. boolean_param("Pack=",pack)
  447. string_param("Packed Variable=",pack_name)
  448. string_vparam("Project Name=",prj_name)
  449. string_param("GCC Switches=",cc_switches)
  450. string_param("Assembler Switches=",a68k_switches)
  451. ignore_param("Linker Switches=") // Obsolete. Ignore.
  452. ignore_param("GNU Linker Switches=") // Obsolete. Ignore.
  453. string_param("GNU Assembler Switches=",as_switches)
  454. ignore_param("BSR Patch=") // Obsolete. Ignore.
  455. boolean_param("Debug Info=",debug_info)
  456. boolean_param("Standard Library=",std_lib)
  457. string_param("Command Line=",cmd_line)
  458. string_param("Post-Build Process=",post_build)
  459. boolean_param("Use Data Variable=",use_data_var)
  460. string_param("Data Variable=",data_var)
  461. boolean_param("Copy Data Variable=",copy_data_var)
  462. boolean_param("Copy Data Variable if Archived=",copy_data_var_arc)
  463. boolean_param("Optimize NOPs=",optimize_nops)
  464. boolean_param("Optimize Returns=",optimize_returns)
  465. boolean_param("Optimize Branches=",optimize_branches)
  466. boolean_param("Optimize Moves=",optimize_moves)
  467. boolean_param("Optimize Tests=",optimize_tests)
  468. boolean_param("Optimize Calculations=",optimize_calcs)
  469. boolean_param("Remove Unused Sections=",remove_unused)
  470. boolean_param("Binary Output=",outputbin)
  471. boolean_param("Fargo=",fargo)
  472. boolean_param("Flash OS=",flash_os)
  473. boolean_param("Cut Unused Ranges=",cut_ranges)
  474. boolean_param("Reorder Sections=",reorder_sections)
  475. boolean_param("Merge Constants=",merge_constants)
  476. boolean_param("Initialize BSS=",initialize_bss)
  477. #undef boolean_param
  478. #undef string_vparam
  479. #undef string_param
  480. #undef ignore_param
  481. #define boolean_param(token,setting) fprintf(f,token "%d\r\n",!!dest->libopts.setting);
  482. #define reloc_param(token,setting) \
  483. switch(dest->libopts.setting) \
  484. { \
  485. case RT_NONE: \
  486. fprintf(f,token "None\r\n"); \
  487. break; \
  488. case RT_DIRECT: \
  489. fprintf(f,"%sDirect\r\n",token); \
  490. break; \
  491. case RT_AMS: \
  492. fprintf(f,"%sAMS",token); \
  493. break; \
  494. case RT_PRECOMP: \
  495. fprintf(f,token "Precomputed\r\n"); \
  496. break; \
  497. case RT_KERNEL: \
  498. fprintf(f,token "Kernel\r\n"); \
  499. break; \
  500. case RT_COMPRESSED: \
  501. fprintf(f,token "Compressed\r\n"); \
  502. break; \
  503. case RT_FLINE: \
  504. fprintf(f,token "F-Line\r\n"); \
  505. break; \
  506. }
  507. #define minams_param(token,setting) \
  508. { \
  509. int major,minor; \
  510. major=dest->libopts.setting/100; \
  511. minor=dest->libopts.setting%100; \
  512. fprintf(f,token "%d.%02d\r\n",major,minor); \
  513. }
  514. fputs("\r\n[Library Options]\r\n",f);
  515. boolean_param("Use TI-89=",use_ti89)
  516. boolean_param("Use TI-92 Plus=",use_ti92p)
  517. boolean_param("Use V200=",use_v200)
  518. boolean_param("Optimize Calc Consts=",opt_calc_consts)
  519. boolean_param("Use Kernel=",use_kernel)
  520. boolean_param("Use PreOS=",use_preos)
  521. boolean_param("Minimum AMS Version Defined=",use_minams)
  522. minams_param("Minimum AMS Version=",minams);
  523. boolean_param("Unofficial OS Support=",unofficial_os)
  524. reloc_param("Reloc Format=",reloc_format)
  525. reloc_param("ROM Call Format=",rom_call_format)
  526. reloc_param("BSS Ref Format=",bss_ref_format)
  527. reloc_param("Data Ref Format=",data_ref_format)
  528. boolean_param("Use F-Line Jumps=",use_fline_jumps)
  529. boolean_param("Use 4-Byte F-Line Jumps=",use_4b_fline_jumps)
  530. boolean_param("Use Internal F-Line Emulator=",use_internal_fline_emu)
  531. boolean_param("Use Return Value=",use_return_value)
  532. boolean_param("Enable Error Return=",enable_error_return)
  533. boolean_param("Save Screen=",save_screen)
  534. boolean_param("Optimize ROM Calls=",opt_rom_calls)
  535. #undef boolean_param
  536. #undef reloc_param
  537. #undef minams_param
  538. fprintf(f,"\r\n[File Editing]\r\nOpen File=%s\r\n\r\n[Included Files]\r\n",smartAscii(dest->open_file));
  539. #define filepath_param(token,filetype) \
  540. e=dest->filetype.path.count(); \
  541. for(i=0;i<e;i++) \
  542. { \
  543. tmp=convert_path_separators_save(smartAscii(dest->filetype.path[i])); \
  544. fprintf(f,token " %u=%s\r\n",i+1,smartAscii(tmp)); \
  545. if (!dest->filetype.folder[i].isEmpty()) \
  546. { \
  547. fprintf(f,token " %u Folder=%s\r\n",i+1,smartAscii(dest->filetype.folder[i])); \
  548. } \
  549. }
  550. filepath_param("C File",c_files)
  551. filepath_param("GNU Assembler File",s_files)
  552. filepath_param("Header File",h_files)
  553. filepath_param("Assembler File",asm_files)
  554. filepath_param("Object File",o_files)
  555. filepath_param("Archive File",a_files)
  556. filepath_param("Text File",txt_files)
  557. filepath_param("Quill File",quill_files)
  558. filepath_param("Other File",oth_files)
  559. #undef filepath_param
  560. fputs("\r\n",f);
  561. return 0;
  562. }
  563. //returns 0 on success, -1 if the file isn't there, and -2 if there's not enough memory.
  564. int saveTPR(const QString &fileName,TPRDataStruct *src)
  565. {
  566. FILE *f;
  567. int ret;
  568. f=fopen(fileName,"wb");
  569. if (!f)
  570. {
  571. return -1;
  572. }
  573. ret=save_tpr(f,src);
  574. fclose(f);
  575. return ret;
  576. }
  577. void mkdir_multi(const char *fileName)
  578. {
  579. int l=strlen(fileName);
  580. char buffer[l+2];
  581. char *ptr;
  582. #ifdef __WIN32__
  583. ptr=strchr(fileName,'\\');
  584. #else
  585. ptr=strchr(fileName,'/');
  586. #endif
  587. while (ptr)
  588. {
  589. memcpy(buffer,fileName,ptr-fileName);
  590. buffer[ptr-fileName]=0;
  591. mkdir(buffer,S_IRWXU | S_IRWXG | S_IRWXO);
  592. #ifdef __WIN32__
  593. ptr=strchr(ptr+1,'\\');
  594. #else
  595. ptr=strchr(ptr+1,'/');
  596. #endif
  597. }
  598. }
  599. int saveFileText(const char *fileName,QString &fileText)
  600. {
  601. FILE *f;
  602. const char *s;
  603. f=fopen(fileName,"wb");
  604. if (!f)
  605. {
  606. mkdir_multi(fileName);
  607. f=fopen(fileName,"wb");
  608. if (!f)
  609. return -1;
  610. }
  611. s=smartAscii(fileText);
  612. fwrite(s,1,strlen(s),f);
  613. fclose(f);
  614. return 0;
  615. }
  616. void kurlNewFileName(KURL &dir,const QString &newFileName)
  617. {
  618. if (newFileName[0]=='/')
  619. dir.setPath(newFileName);
  620. else
  621. dir.setFileName(newFileName);
  622. }
  623. QString pullOutFileSuffix(const QString &srcFileName,QString &destFileName)
  624. {
  625. int a,b;
  626. QString ret;
  627. destFileName=srcFileName;
  628. a=destFileName.findRev('.');
  629. b=destFileName.findRev('/');
  630. if (a<0)
  631. return QString::null;
  632. if (a<b)
  633. return QString::null;
  634. ret=destFileName.mid(a+1);
  635. destFileName.truncate(a);
  636. return ret;
  637. }
  638. int checkFileName(const QString &fileName,const QStringList &fileNameList)
  639. {
  640. int i;
  641. QString fileName_name,fileName_suffix;
  642. QString name,suffix;
  643. fileName_suffix=pullOutFileSuffix(fileName,fileName_name);
  644. for (i=fileNameList.count()-1;i>=0;i--)
  645. {
  646. suffix=pullOutFileSuffix(fileNameList[i],name);
  647. if (!suffix.compare("c")||!suffix.compare("s")||!suffix.compare("asm")||!suffix.compare("o")||!suffix.compare("qll"))
  648. {
  649. if (!fileName_suffix.compare("c")||!fileName_suffix.compare("s")||!fileName_suffix.compare("asm")||!fileName_suffix.compare("o")||!fileName_suffix.compare("qll"))
  650. {
  651. if (!name.compare(fileName_name))
  652. return 0;
  653. }
  654. }
  655. else
  656. {
  657. if (!fileNameList[i].compare(fileName))
  658. return 0;
  659. }
  660. }
  661. return 1;
  662. }