PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Pristine-Pro/MAKEMENU/MAKEMENU.C

http://github.com/AnimatorPro/Animator-Pro
C | 466 lines | 415 code | 50 blank | 1 comment | 74 complexity | 584c7662d8a98122ff8833f90cd3aa2e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #define CH_WIDTH 6
  4. #define CH_HEIGHT 8
  5. #define XMAX 320
  6. extern void *calloc(int, int);
  7. typedef int (*Vector)();
  8. #define OUT out
  9. struct flicmenu
  10. {
  11. struct flicmenu *next;
  12. struct flicmenu *children;
  13. short x, y;
  14. unsigned short width, height;
  15. void *text;
  16. int (*seeme)();
  17. int (*feelme)();
  18. int *group;
  19. int identity;
  20. short key_equiv;
  21. int (*optme)();
  22. char *name;
  23. int level;
  24. };
  25. typedef struct flicmenu Flicmenu;
  26. extern Flicmenu *parent_menu();
  27. extern Flicmenu quick_menu, edit_menu, title_menu;
  28. #define NOTEXT NULL
  29. #define NOSEE 0L
  30. #define NOFEEL 0L
  31. #define NOGROUP NULL
  32. #define NOKEY 0
  33. #define NOOPT 0L
  34. struct pull
  35. {
  36. struct pull *next;
  37. short xoff, yoff, width, height;
  38. struct pull *children;
  39. char *data; /* actually just some old pointer */
  40. Vector see;
  41. char disabled;
  42. char *name; /* what do we call it??? */
  43. short level;
  44. };
  45. typedef struct pull Pull;
  46. char *inname, *outname;
  47. FILE *in, *out;
  48. int line;
  49. Flicmenu *root;
  50. char *rootname = "fml";
  51. char inbuf[256];
  52. char strings[256*32];
  53. char *freestring = strings;
  54. char *
  55. clone_string(s)
  56. char *s;
  57. {
  58. int i;
  59. char *ns;
  60. i = strlen(s) + 1;
  61. ns = freestring;
  62. freestring += i;
  63. if (freestring >= strings + sizeof(strings))
  64. {
  65. freestring -= i;
  66. return(0);
  67. }
  68. strcpy(ns,s);
  69. return(ns);
  70. }
  71. lcase(s)
  72. char *s;
  73. {
  74. while (*s != 0)
  75. {
  76. if (isupper(*s))
  77. *s += 'a' - 'A';
  78. s++;
  79. }
  80. }
  81. n_line()
  82. {
  83. if ((fgets(inbuf, sizeof(inbuf), in)) == NULL)
  84. return(0);
  85. else
  86. return(1);
  87. }
  88. next_line()
  89. {
  90. for (;;)
  91. {
  92. if (!n_line())
  93. return(0);
  94. if (inbuf[0] != '#')
  95. return(1);
  96. }
  97. }
  98. main(argc, argv)
  99. int argc;
  100. char *argv[];
  101. {
  102. if (argc != 4)
  103. {
  104. puts("Usage: ");
  105. puts(" makemenu infile.txt outfile.c rootname");
  106. puts("Example:");
  107. puts(" makemenu fontmenu.txt fontmenu.c fmu");
  108. puts("");
  109. puts("This creates a set of Button structures in outfile.c, one for ");
  110. puts("each line of infile.txt. (The first line makes a Menuhdr.)");
  111. puts("");
  112. puts("If fontmenu.txt consisted of the single line:");
  113. puts(" points 5 9 88 10");
  114. puts("then fontmenu.c would have one structure named fmu_poi_sel and");
  115. puts("the x/y/width/height of fmu_poi_sel would be 5 9 (88 10).");
  116. exit(0);
  117. }
  118. inname = argv[1];
  119. outname = argv[2];
  120. rootname = argv[3];
  121. if ((in = fopen(inname, "r")) == NULL)
  122. {
  123. printf("Couldn't open %s to read, sorry.", inname);
  124. exit(0);
  125. }
  126. if ((out = fopen(outname, "w")) == NULL)
  127. {
  128. printf("Couldn't open %s to write, sorry.", outname);
  129. exit(0);
  130. }
  131. if (read_lines())
  132. {
  133. write_headers();
  134. write_flicm();
  135. write_button_list();
  136. fprintf(OUT,
  137. "\n/********* End of makemenu generated code *********/\n\n");
  138. }
  139. cleanup();
  140. }
  141. cleanup()
  142. {
  143. fclose(in);
  144. fclose(out);
  145. exit(0);
  146. }
  147. write_headers()
  148. {
  149. fprintf(out, "/* generated with makemenu 2.0 */\n");
  150. fprintf(out, "#include \"jimk.h\"\n");
  151. fprintf(out, "#include \"menus.h\"\n");
  152. fprintf(out, "\n\n");
  153. fprintf(out, "/*** Button Data ***/\n");
  154. }
  155. char *skip_space(s)
  156. char *s;
  157. {
  158. char c;
  159. for (;;)
  160. {
  161. c = *s;
  162. if (c == 0 || !isspace(c))
  163. break;
  164. s++;
  165. }
  166. return(s);
  167. }
  168. char *
  169. skip_to_space(s)
  170. char *s;
  171. {
  172. char c;
  173. for (;;)
  174. {
  175. c = *s;
  176. if (c == 0 || isspace(c))
  177. break;
  178. s++;
  179. }
  180. return(s);
  181. }
  182. char *
  183. remove_trailing(s)
  184. char *s;
  185. {
  186. int i;
  187. i = strlen(s);
  188. while (--i >= 0)
  189. {
  190. if (isspace(s[i]))
  191. s[i] = 0;
  192. else
  193. break;
  194. }
  195. return(s);
  196. }
  197. char *single_name(char *start, int is_root)
  198. {
  199. char buf[256];
  200. if (is_root)
  201. sprintf(buf, "%s_menu", rootname);
  202. else
  203. sprintf(buf, "%s_%.8s_sel", rootname, start);
  204. lcase(buf);
  205. return(clone_string(buf));
  206. }
  207. char *
  208. next_word(line,buf)
  209. char *line, *buf;
  210. {
  211. char c;
  212. char *end;
  213. line = skip_space(line);
  214. if (line[0] == 0)
  215. return(NULL);
  216. end = skip_to_space(line);
  217. c = *end;
  218. *end = 0;
  219. strcpy(buf,line);
  220. *end = c;
  221. return(end);
  222. }
  223. Flicmenu *
  224. read_m(int is_root)
  225. {
  226. Flicmenu *p;
  227. char buf[256];
  228. char *line;
  229. AGAIN:
  230. if (!next_line())
  231. return(0);
  232. line = inbuf;
  233. if ((p = calloc(1, sizeof(*p))) == NULL)
  234. {
  235. puts("Out of memory");
  236. return(0);
  237. }
  238. if ((line = next_word(line,buf)) == NULL)
  239. goto AGAIN;
  240. if ((p->text = clone_string(buf))==NULL)
  241. {
  242. puts("Too many strings");
  243. return(0);
  244. }
  245. if ((p->name = single_name(p->text,is_root)) == NULL)
  246. {
  247. puts("Too many strings");
  248. return(0);
  249. }
  250. if ((line = next_word(line,buf)) == NULL)
  251. goto END;
  252. p->x = atoi(buf);
  253. if ((line = next_word(line,buf)) == NULL)
  254. goto END;
  255. p->y = atoi(buf);
  256. if ((line = next_word(line,buf)) == NULL)
  257. goto END;
  258. p->width = atoi(buf);
  259. if ((line = next_word(line,buf)) == NULL)
  260. goto END;
  261. p->height = atoi(buf);
  262. END:
  263. return(p);
  264. }
  265. pj_copy_bytes(s,d,count)
  266. register char *s,*d;
  267. int count;
  268. {
  269. while (--count >= 0)
  270. *d++ = *s++;
  271. }
  272. read_lines()
  273. {
  274. Flicmenu *p;
  275. int i;
  276. Flicmenu *last = NULL;
  277. if ((root = read_m(1)) == NULL)
  278. {
  279. fprintf(stderr, "No data in %s!\n", inname);
  280. return(0);
  281. }
  282. for (i = 0;;i++)
  283. {
  284. if ((p = read_m(0)) == NULL)
  285. break;
  286. if (last == NULL)
  287. {
  288. root->children = last = p;
  289. }
  290. else
  291. {
  292. last->next = p;
  293. last = p;
  294. }
  295. }
  296. if (i == 0)
  297. {
  298. fprintf(stderr, "Only one line in %s, hope that's ok...\n", inname);
  299. }
  300. return(1);
  301. }
  302. print_idents(ident)
  303. int ident;
  304. {
  305. while (--ident >= 0)
  306. fprintf(OUT, "\t");
  307. }
  308. write_button(p, ident)
  309. Flicmenu *p;
  310. int ident;
  311. {
  312. print_idents(ident);
  313. fprintf(OUT, "static Button %s = MB_INIT1(\n", p->name);
  314. ident += 1;
  315. print_idents(ident);
  316. if (p->next == NULL)
  317. fprintf(OUT, "NONEXT, /* next */\n");
  318. else
  319. fprintf(OUT, "&%s, /* next */\n", p->next->name);
  320. print_idents(ident);
  321. if (p->children == NULL)
  322. fprintf(OUT, "NOCHILD, /* children */\n");
  323. else
  324. fprintf(OUT, "&%s,\n /* children */", p->children->name);
  325. print_idents(ident);
  326. fprintf(OUT, "%d, %d, %d, %d, /* w,h,x,y */\n", p->width,p->height,p->x,p->y);
  327. print_idents(ident);
  328. if (p->text == NULL)
  329. fprintf(OUT, "NOTEXT, /* datme */\n");
  330. else
  331. fprintf(OUT, "\"%s\", /* datme */\n", p->text);
  332. print_idents(ident);
  333. fprintf(OUT, "NOSEE,\n");
  334. print_idents(ident);
  335. fprintf(OUT, "NOFEEL,\n");
  336. print_idents(ident);
  337. fprintf(OUT, "NOOPT,\n");
  338. print_idents(ident);
  339. fprintf(OUT, "NOGROUP,0,\n");
  340. print_idents(ident);
  341. fprintf(OUT, "NOKEY,\n");
  342. print_idents(ident);
  343. fprintf(OUT, "0, /* flags */\n");
  344. print_idents(ident);
  345. fprintf(OUT, ");\n");
  346. }
  347. write_tree(p, ident)
  348. Flicmenu *p;
  349. int ident;
  350. {
  351. if (p == NULL)
  352. return;
  353. write_tree(p->next, ident);
  354. write_tree(p->children, ident+1);
  355. write_button(p, ident);
  356. }
  357. list_els(p)
  358. Pull *p;
  359. {
  360. int count = 0;
  361. while (p != NULL)
  362. {
  363. count++;
  364. p = p->next;
  365. }
  366. return(count);
  367. }
  368. write_menuhdr(Flicmenu *b)
  369. {
  370. fprintf(OUT, "\nMenuhdr %s = MENU_INIT0(\n", b->name);
  371. fprintf(OUT, "\t%d,%d,%d,%d,\t/* w,h,x,y */ \n",
  372. b->width, b->height, b->x, b->y);
  373. fprintf(OUT, "\tMAKEMENU_MUID,\t\t/* id */\n");
  374. fprintf(OUT, "\tPANELMENU,\t\t/* type */\n");
  375. fprintf(OUT, "\t&%s,\t/* buttons */\n",
  376. (b->children == NULL ? "NULL" : b->children->name));
  377. fprintf(OUT, "\tSCREEN_FONT,\t/* font */\n");
  378. fprintf(OUT, "\t&menu_cursor,\t/* cursor */\n");
  379. fprintf(OUT, "\tseebg_white,\t/* seebg */\n");
  380. fprintf(OUT, "\tNULL,\t\t\t/* data */\n");
  381. fprintf(OUT, "\tNULL,\t\t\t/* domenu */\n");
  382. fprintf(OUT, "\t(MBPEN|MBRIGHT|KEYHIT),\t/* ioflags */\n");
  383. fprintf(OUT, "\t0,\t\t\t/* flags */\n");
  384. fprintf(OUT, "\tNULL,\t\t\t/* procmouse */\n");
  385. fprintf(OUT, "\tNULL,\t\t\t/* on_showhide */\n");
  386. fprintf(OUT, "\tNULL,\t\t\t/* cleanup */\n");
  387. fprintf(OUT, ");\n");
  388. }
  389. write_flicm()
  390. {
  391. write_tree(root->children,0);
  392. write_menuhdr(root);
  393. }
  394. rwrite_blist(Flicmenu *p)
  395. {
  396. if (p == NULL)
  397. return;
  398. rwrite_blist(p->next);
  399. rwrite_blist(p->children);
  400. fprintf(OUT, "&%s,\n",p->name);
  401. }
  402. write_button_list()
  403. /* write out the scaling array and function to do the scale */
  404. {
  405. if (root != NULL && root->children != NULL)
  406. {
  407. fprintf(OUT, "\nstatic Button *%s_buttons[] = {\n", rootname);
  408. rwrite_blist(root->children);
  409. fprintf(OUT, "};\n\n");
  410. fprintf(OUT, "void scale_%s_menu(Rscale *scale)\n", rootname);
  411. fprintf(OUT, "{\n");
  412. fprintf(OUT, "scale_menu(&%s,%s_buttons,Array_els(%s_buttons),scale);\n",
  413. root->name, rootname, rootname);
  414. fprintf(OUT, "}\n");
  415. }
  416. }