tpr.cxx 25 KB

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