configure.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /* $Id$
  2. * MegaZeux
  3. *
  4. * Copyright (C) 2004 Gilead Kutnick
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  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 GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. // Config file options, which can be given either through config.txt
  21. // or at the command line.
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include "configure.h"
  27. void config_set_audio_buffer(config_info *conf, char *name, char *value)
  28. {
  29. conf->buffer_size = strtol(value, NULL, 10);
  30. }
  31. // Default colors for color coding:
  32. // 0 current line - 11
  33. // 1 immediates - 10
  34. // 2 characters - 14
  35. // 3 colors - color box or 2
  36. // 4 directions - 3
  37. // 5 things - 11
  38. // 6 params - 2
  39. // 7 strings - 14
  40. // 8 equalities - 0
  41. // 9 conditions - 15
  42. // 10 items - 11
  43. // 11 extras - 7
  44. // 12 commands and command fragments - 15
  45. void config_ccode_colors(config_info *conf, char *name, char *value)
  46. {
  47. conf->color_codes[4] = strtol(value, NULL, 10);
  48. }
  49. void config_ccode_commands(config_info *conf, char *name, char *value)
  50. {
  51. conf->color_codes[13] = strtol(value, NULL, 10);
  52. }
  53. void config_ccode_conditions(config_info *conf, char *name, char *value)
  54. {
  55. conf->color_codes[10] = strtol(value, NULL, 10);
  56. }
  57. void config_ccode_current_line(config_info *conf, char *name, char *value)
  58. {
  59. conf->color_codes[0] = strtol(value, NULL, 10);
  60. }
  61. void config_ccode_directions(config_info *conf, char *name, char *value)
  62. {
  63. conf->color_codes[5] = strtol(value, NULL, 10);
  64. }
  65. void config_ccode_equalities(config_info *conf, char *name, char *value)
  66. {
  67. conf->color_codes[9] = strtol(value, NULL, 10);
  68. }
  69. void config_ccode_extras(config_info *conf, char *name, char *value)
  70. {
  71. conf->color_codes[8] = strtol(value, NULL, 10);
  72. }
  73. void config_ccode_on(config_info *conf, char *name, char *value)
  74. {
  75. conf->color_coding_on = strtol(value, NULL, 10);
  76. }
  77. void config_ccode_immediates(config_info *conf, char *name, char *value)
  78. {
  79. int new_color = strtol(value, NULL, 10);
  80. conf->color_codes[1] = new_color;
  81. conf->color_codes[2] = new_color;
  82. }
  83. void config_ccode_items(config_info *conf, char *name, char *value)
  84. {
  85. conf->color_codes[11] = strtol(value, NULL, 10);
  86. }
  87. void config_ccode_params(config_info *conf, char *name, char *value)
  88. {
  89. conf->color_codes[7] = strtol(value, NULL, 10);
  90. }
  91. void config_ccode_strings(config_info *conf, char *name, char *value)
  92. {
  93. conf->color_codes[8] = strtol(value, NULL, 10);
  94. }
  95. void config_ccode_things(config_info *conf, char *name, char *value)
  96. {
  97. conf->color_codes[6] = strtol(value, NULL, 10);
  98. }
  99. void config_default_invald(config_info *conf, char *name, char *value)
  100. {
  101. if(!strcasecmp(value, "ignore"))
  102. {
  103. conf->default_invalid_status = 1;
  104. }
  105. else
  106. if(!strcasecmp(value, "delete"))
  107. {
  108. conf->default_invalid_status = 2;
  109. }
  110. else
  111. if(!strcasecmp(value, "comment"))
  112. {
  113. conf->default_invalid_status = 3;
  114. }
  115. }
  116. void config_disassemble_extras(config_info *conf, char *name, char *value)
  117. {
  118. conf->disassemble_extras = strtol(value, NULL, 10);
  119. }
  120. void config_disassemble_base(config_info *conf, char *name, char *value)
  121. {
  122. int new_base = strtol(value, NULL, 10);
  123. if((new_base == 10) || (new_base == 16))
  124. conf->disassemble_base = new_base;
  125. }
  126. void config_set_resolution(config_info *conf, char *name, char *value)
  127. {
  128. char *next;
  129. conf->resolution_x = strtol(value, &next, 10);
  130. conf->resolution_y = strtol(next + 1, NULL, 10);
  131. }
  132. void config_set_multiplier(config_info *conf, char *name, char *value)
  133. {
  134. conf->height_multiplier = strtol(value, NULL, 10);
  135. }
  136. void config_set_fullscreen(config_info *conf, char *name, char *value)
  137. {
  138. conf->fullscreen = strtol(value, NULL, 10);
  139. }
  140. void config_macro_five(config_info *conf, char *name, char *value)
  141. {
  142. value[63] = 0;
  143. strcpy(conf->default_macros[4], value);
  144. }
  145. void config_macro_four(config_info *conf, char *name, char *value)
  146. {
  147. value[63] = 0;
  148. strcpy(conf->default_macros[3], value);
  149. }
  150. void config_macro_one(config_info *conf, char *name, char *value)
  151. {
  152. value[63] = 0;
  153. strcpy(conf->default_macros[0], value);
  154. }
  155. void config_macro_three(config_info *conf, char *name, char *value)
  156. {
  157. value[63] = 0;
  158. strcpy(conf->default_macros[2], value);
  159. }
  160. void config_macro_two(config_info *conf, char *name, char *value)
  161. {
  162. value[63] = 0;
  163. strcpy(conf->default_macros[1], value);
  164. }
  165. void config_set_music(config_info *conf, char *name, char *value)
  166. {
  167. conf->music_on = strtol(value, NULL, 10);
  168. }
  169. void config_set_mod_volume(config_info *conf, char *name, char *value)
  170. {
  171. int new_volume = strtol(value, NULL, 10);
  172. if(new_volume < 1)
  173. new_volume = 1;
  174. if(new_volume > 8)
  175. new_volume = 8;
  176. conf->music_volume = new_volume;
  177. }
  178. void config_set_mzx_speed(config_info *conf, char *name, char *value)
  179. {
  180. int new_speed = strtol(value, NULL, 10);
  181. if(new_speed < 1)
  182. new_speed = 1;
  183. if(new_speed > 9)
  184. new_speed = 9;
  185. conf->mzx_speed = new_speed;
  186. }
  187. void config_set_pc_speaker(config_info *conf, char *name, char *value)
  188. {
  189. conf->pc_speaker_on = strtol(value, NULL, 10);
  190. }
  191. void config_set_sam_volume(config_info *conf, char *name, char *value)
  192. {
  193. int new_volume = strtol(value, NULL, 10);
  194. if(new_volume < 1)
  195. new_volume = 1;
  196. if(new_volume > 8)
  197. new_volume = 8;
  198. conf->sam_volume = new_volume;
  199. }
  200. void config_save_file(config_info *conf, char *name, char *value)
  201. {
  202. strcpy(conf->default_save_name, value);
  203. }
  204. void config_startup_file(config_info *conf, char *name, char *value)
  205. {
  206. strcpy(conf->startup_file, value);
  207. }
  208. void config_enable_oversampling(config_info *conf, char *name, char *value)
  209. {
  210. conf->oversampling_on = strtol(value, NULL, 10);
  211. }
  212. void config_resampling_mode(config_info *conf, char *name, char *value)
  213. {
  214. if(!strcmp(value, "none"))
  215. {
  216. conf->resampling_mode = 0;
  217. }
  218. else
  219. if(!strcmp(value, "linear"))
  220. {
  221. conf->resampling_mode = 1;
  222. }
  223. else
  224. if(!strcmp(value, "cubic"))
  225. {
  226. conf->resampling_mode = 2;
  227. }
  228. else
  229. if(!strcmp(value, "fir"))
  230. {
  231. conf->resampling_mode = 3;
  232. }
  233. }
  234. config_entry config_options[] =
  235. {
  236. { "audio_buffer", config_set_audio_buffer },
  237. { "ccode_colors", config_ccode_colors },
  238. { "ccode_commands", config_ccode_commands },
  239. { "ccode_conditions", config_ccode_conditions },
  240. { "ccode_current_line", config_ccode_current_line },
  241. { "ccode_directions", config_ccode_directions },
  242. { "ccode_equalities", config_ccode_equalities },
  243. { "ccode_extras", config_ccode_extras },
  244. { "ccode_immediates", config_ccode_immediates },
  245. { "ccode_items", config_ccode_items },
  246. { "ccode_params", config_ccode_params },
  247. { "ccode_strings", config_ccode_strings },
  248. { "ccode_things", config_ccode_things },
  249. { "color_coding_on", config_ccode_on },
  250. { "default_invalid_status", config_default_invald },
  251. { "disassemble_base", config_disassemble_base },
  252. { "disassemble_extras", config_disassemble_extras },
  253. { "enable_oversampling", config_enable_oversampling },
  254. { "force_height_multiplier", config_set_multiplier },
  255. { "force_resolution", config_set_resolution },
  256. { "fullscreen", config_set_fullscreen },
  257. { "macro_five", config_macro_five },
  258. { "macro_four", config_macro_four },
  259. { "macro_one", config_macro_one },
  260. { "macro_three", config_macro_three },
  261. { "macro_two", config_macro_two },
  262. { "music_on", config_set_music },
  263. { "music_volume", config_set_mod_volume },
  264. { "mzx_speed", config_set_mzx_speed },
  265. { "pc_speaker_on", config_set_pc_speaker },
  266. { "resampling_mode", config_resampling_mode },
  267. { "sample_volume", config_set_sam_volume },
  268. { "save_file", config_save_file },
  269. { "startup_file", config_startup_file }
  270. };
  271. const int num_config_options =
  272. sizeof(config_options) / sizeof(config_entry);
  273. config_entry *find_option(char *name)
  274. {
  275. int bottom = 0, top = num_config_options - 1, middle;
  276. int cmpval;
  277. config_entry *base = config_options;
  278. while(bottom <= top)
  279. {
  280. middle = (top + bottom) / 2;
  281. cmpval = strcasecmp(name, (base[middle]).option_name);
  282. if(cmpval > 0)
  283. bottom = middle + 1;
  284. else
  285. if(cmpval < 0)
  286. top = middle - 1;
  287. else
  288. return base + middle;
  289. }
  290. return NULL;
  291. }
  292. config_info default_options =
  293. {
  294. // Video options
  295. 0,
  296. 640,
  297. 350,
  298. 1,
  299. // Audio options
  300. 2048,
  301. 0,
  302. 1,
  303. 7,
  304. 7,
  305. 1,
  306. 1,
  307. // Game options
  308. "caverns.mzx",
  309. "saved.sav",
  310. 4,
  311. // Robot editor options
  312. { 11, 10, 10, 14, 255, 3, 11, 2, 14, 0, 15, 11, 7, 15, 1, 2, 3 },
  313. 1,
  314. 1,
  315. 10,
  316. 1,
  317. { "char ", "color ", "goto ", "send ", ": playershot^" }
  318. };
  319. void default_config(config_info *conf)
  320. {
  321. memcpy(conf, &default_options, sizeof(config_info));
  322. }
  323. void set_config_from_file(config_info *conf, char *conf_file_name)
  324. {
  325. FILE *conf_file = fopen(conf_file_name, "rb");
  326. if(conf_file)
  327. {
  328. char line_buffer[256];
  329. char line_buffer_alternate[256];
  330. char current_char, *input_position, *output_position;
  331. char *equals_position;
  332. char *value;
  333. config_entry *current_option;
  334. while(fgets(line_buffer_alternate, 255, conf_file))
  335. {
  336. if(line_buffer_alternate[0] != '#')
  337. {
  338. input_position = line_buffer_alternate;
  339. output_position = line_buffer;
  340. equals_position = NULL;
  341. do
  342. {
  343. current_char = *input_position;
  344. if(!isspace(current_char))
  345. {
  346. if((current_char == '\\') &&
  347. (input_position[1] == 's'))
  348. {
  349. input_position++;
  350. current_char = ' ';
  351. }
  352. if(current_char == '=')
  353. equals_position = output_position;
  354. *output_position = current_char;
  355. output_position++;
  356. }
  357. input_position++;
  358. } while(current_char);
  359. if(equals_position)
  360. {
  361. *equals_position = 0;
  362. value = equals_position + 1;
  363. }
  364. else
  365. {
  366. value = "1";
  367. }
  368. if(line_buffer[0])
  369. {
  370. current_option = find_option(line_buffer);
  371. if(current_option)
  372. current_option->change_option(conf, line_buffer, value);
  373. }
  374. }
  375. }
  376. }
  377. }
  378. void set_config_from_command_line(config_info *conf, int argc,
  379. char *argv[])
  380. {
  381. char line_buffer[256];
  382. char current_char, *input_position, *output_position;
  383. char *equals_position;
  384. char *value;
  385. config_entry *current_option;
  386. int i;
  387. for(i = 1; i < argc; i++)
  388. {
  389. input_position = argv[i];
  390. output_position = line_buffer;
  391. equals_position = NULL;
  392. do
  393. {
  394. current_char = *input_position;
  395. if((current_char == '\\') &&
  396. (input_position[1] == 's'))
  397. {
  398. input_position++;
  399. current_char = ' ';
  400. }
  401. if(current_char == '=')
  402. equals_position = output_position;
  403. *output_position = current_char;
  404. output_position++;
  405. input_position++;
  406. } while(current_char);
  407. if(equals_position)
  408. {
  409. *equals_position = 0;
  410. value = equals_position + 1;
  411. }
  412. else
  413. {
  414. value = "1";
  415. }
  416. if(line_buffer[0])
  417. {
  418. current_option = find_option(line_buffer);
  419. if(current_option)
  420. current_option->change_option(conf, line_buffer, value);
  421. }
  422. }
  423. }