tpr.cxx 22 KB

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