tpr.cxx 25 KB

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