PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Pristine-Pro/MAKEMENU/MAKEPULL.C

http://github.com/AnimatorPro/Animator-Pro
C | 454 lines | 404 code | 49 blank | 1 comment | 65 complexity | dd90483077e7e2b4f9d38cad483fddf1 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. /* characters between top level menus */
  7. #define MSPC 3
  8. extern void *calloc(int, int);
  9. typedef int (*Vector)();
  10. #define OUT out
  11. struct pull
  12. {
  13. struct pull *next;
  14. short xoff, yoff, width, height;
  15. struct pull *children;
  16. char *data; /* actually just some old pointer */
  17. Vector see;
  18. char disabled;
  19. char *name; /* what do we call it??? */
  20. short level;
  21. };
  22. typedef struct pull Pull;
  23. char *inname, *outname;
  24. FILE *in, *out;
  25. int line;
  26. Pull *root;
  27. Pull *last_hi, *last_lo;
  28. char inbuf[256];
  29. char strings[256*32];
  30. char *freestring = strings;
  31. char *
  32. clone_string(s)
  33. char *s;
  34. {
  35. int i;
  36. char *ns;
  37. i = strlen(s) + 1;
  38. ns = freestring;
  39. freestring += i;
  40. if (freestring >= strings + sizeof(strings))
  41. {
  42. freestring -= i;
  43. return(0);
  44. }
  45. strcpy(ns,s);
  46. return(ns);
  47. }
  48. lcase(s)
  49. char *s;
  50. {
  51. while (*s != 0)
  52. {
  53. if (isupper(*s))
  54. *s += 'a' - 'A';
  55. s++;
  56. }
  57. }
  58. n_line()
  59. {
  60. if ((fgets(inbuf, sizeof(inbuf), in)) == NULL)
  61. return(0);
  62. else
  63. return(1);
  64. }
  65. next_line()
  66. {
  67. for (;;)
  68. {
  69. if (!n_line())
  70. return(0);
  71. if (inbuf[0] != '#')
  72. return(1);
  73. }
  74. }
  75. main(argc, argv)
  76. int argc;
  77. char *argv[];
  78. {
  79. if (argc != 3)
  80. {
  81. puts("Usage: makepull infile.txt outfile.c");
  82. puts(" creates a c file with menu data structures suggested by");
  83. puts(" what's in infile.txt");
  84. exit(0);
  85. }
  86. inname = argv[1];
  87. outname = argv[2];
  88. if ((in = fopen(inname, "r")) == NULL)
  89. {
  90. printf("Couldn't open %s to read, sorry.", inname);
  91. exit(0);
  92. }
  93. if ((out = fopen(outname, "w")) == NULL)
  94. {
  95. printf("Couldn't open %s to write, sorry.", outname);
  96. exit(0);
  97. }
  98. write_headers();
  99. read_menus();
  100. write_menus();
  101. cleanup();
  102. }
  103. cleanup()
  104. {
  105. fclose(in);
  106. fclose(out);
  107. exit(0);
  108. }
  109. write_headers()
  110. {
  111. fprintf(out, "/* generated with makepull */\n");
  112. fprintf(out, "\n\n");
  113. }
  114. char *
  115. skip_space(s)
  116. char *s;
  117. {
  118. while (isspace(*s))
  119. s++;
  120. return(s);
  121. }
  122. char *
  123. remove_trailing(s)
  124. char *s;
  125. {
  126. int i;
  127. i = strlen(s);
  128. while (--i >= 0)
  129. {
  130. if (isspace(s[i]))
  131. s[i] = 0;
  132. else
  133. break;
  134. }
  135. return(s);
  136. }
  137. Pull *
  138. read_m()
  139. {
  140. Pull *p;
  141. if (!next_line())
  142. return(0);
  143. if ((p = calloc(1, sizeof(*p))) == NULL)
  144. {
  145. puts("Out of memory");
  146. return(0);
  147. }
  148. p->level = isspace(inbuf[0]); /* 1'st or second level */
  149. if ((p->data = clone_string(remove_trailing(skip_space(inbuf))))==NULL)
  150. {
  151. puts("Too many strings");
  152. return(0);
  153. }
  154. return(p);
  155. }
  156. pj_copy_bytes(s,d,count)
  157. register char *s,*d;
  158. int count;
  159. {
  160. while (--count >= 0)
  161. *d++ = *s++;
  162. }
  163. char *
  164. single_name()
  165. {
  166. char buf[256];
  167. char sb[8];
  168. pj_copy_bytes(last_hi->data, sb, 3);
  169. sb[3] = 0;
  170. sprintf(buf, "%s_pull", sb);
  171. return(clone_string(buf));
  172. }
  173. char *
  174. double_name()
  175. {
  176. char buf[256];
  177. char sb[8],db[8];
  178. pj_copy_bytes(last_hi->data, sb, 3);
  179. sb[3] = 0;
  180. pj_copy_bytes(last_lo->data, db, 3);
  181. db[3] = 0;
  182. sprintf(buf, "%s_%s_pull", sb, db);
  183. return(clone_string(buf));
  184. }
  185. read_menus()
  186. {
  187. Pull *p;
  188. for (;;)
  189. {
  190. if ((p = read_m()) == NULL)
  191. break;
  192. if (root == NULL)
  193. {
  194. root = last_hi = p;
  195. }
  196. else
  197. {
  198. if (p->level == 0) /* root level */
  199. {
  200. last_hi->next = p;
  201. last_lo = NULL;
  202. last_hi = p;
  203. }
  204. else
  205. {
  206. if (last_lo == NULL)
  207. {
  208. last_hi->children = last_lo = p;
  209. }
  210. else
  211. {
  212. last_lo->next = p;
  213. last_lo = p;
  214. }
  215. }
  216. }
  217. if (p->level == 0)
  218. p->name = single_name();
  219. else
  220. p->name = double_name();
  221. if (p->name == NULL)
  222. {
  223. puts("Out of memory");
  224. break;
  225. }
  226. lcase(p->name);
  227. }
  228. }
  229. print_idents(ident)
  230. int ident;
  231. {
  232. while (--ident >= 0)
  233. fprintf(OUT, "\t");
  234. }
  235. print_by_cw(w)
  236. int w;
  237. {
  238. fprintf(OUT,"%d+%d*CH_WIDTH, ",
  239. w%CH_WIDTH, w/CH_WIDTH);
  240. }
  241. print_by_ch(h)
  242. int h;
  243. {
  244. fprintf(OUT,"%d+%d*CH_HEIGHT, ",
  245. h%CH_HEIGHT, h/CH_HEIGHT);
  246. }
  247. write_m(p, ident)
  248. Pull *p;
  249. int ident;
  250. {
  251. print_idents(ident);
  252. fprintf(OUT, "static struct pull %s = INIT_PULL1(\n", p->name);
  253. ident += 1;
  254. print_idents(ident);
  255. if (p->next == NULL)
  256. fprintf(OUT, "NONEXT,\n");
  257. else
  258. fprintf(OUT, "&%s,\n", p->next->name);
  259. print_idents(ident);
  260. print_by_cw(p->xoff);
  261. print_by_ch(p->yoff);
  262. print_by_cw(p->width);
  263. print_by_ch(p->height);
  264. fprintf(OUT,"\n");
  265. print_idents(ident);
  266. if (p->children == NULL)
  267. fprintf(OUT, "NOCHILD,\n");
  268. else
  269. fprintf(OUT, "&%s,\n", p->children->name);
  270. print_idents(ident);
  271. if (p->data == NULL)
  272. fprintf(OUT, "NODATA,\n");
  273. else
  274. fprintf(OUT, "\"%s\",\n", p->data);
  275. print_idents(ident);
  276. if (p->data == NULL)
  277. fprintf(OUT, "pull_oblock,\n");
  278. else
  279. fprintf(OUT, "pull_leftext,\n");
  280. print_idents(ident);
  281. fprintf(OUT, "PULL_DOESKEYS,\n");
  282. print_idents(ident);
  283. fprintf(OUT, ");\n");
  284. }
  285. write_tree(p, ident)
  286. Pull *p;
  287. int ident;
  288. {
  289. if (p == NULL)
  290. return;
  291. write_tree(p->next, ident);
  292. write_tree(p->children, ident+1);
  293. write_m(p, ident);
  294. }
  295. list_els(p)
  296. Pull *p;
  297. {
  298. int count = 0;
  299. while (p != NULL)
  300. {
  301. count++;
  302. p = p->next;
  303. }
  304. return(count);
  305. }
  306. longest_string(p)
  307. Pull *p;
  308. {
  309. int longest = 0;
  310. int i;
  311. while (p != NULL)
  312. {
  313. i = strlen(p->data);
  314. if (i > longest)
  315. longest = i;
  316. p = p->next;
  317. }
  318. return(longest);
  319. }
  320. calc_p(p)
  321. Pull *p;
  322. {
  323. Pull *rp;
  324. Pull *cp;
  325. int w, h;
  326. int y;
  327. char buf[256];
  328. if ((rp = calloc(1, sizeof(*rp))) == NULL)
  329. {
  330. puts("Out of memory");
  331. return(0);
  332. }
  333. w = longest_string(p->children);
  334. rp->width = w*CH_WIDTH+5;
  335. h = list_els(p->children);
  336. rp->height = h*CH_HEIGHT+3;
  337. rp->xoff = -1;
  338. rp->yoff = CH_HEIGHT;
  339. sprintf(buf, "r%s", p->name);
  340. if ((rp->name = clone_string(buf)) == NULL)
  341. {
  342. puts("Out of memory");
  343. return(0);
  344. }
  345. cp = rp->children = p->children;
  346. p->children = rp;
  347. y = 1;
  348. while (cp != NULL)
  349. {
  350. cp->xoff = 1;
  351. cp->yoff = y;
  352. cp->width = 3+CH_WIDTH*w;
  353. cp->height = 1+CH_HEIGHT;
  354. y += CH_HEIGHT;
  355. cp = cp->next;
  356. }
  357. return(1);
  358. }
  359. calc_sub_positions()
  360. {
  361. Pull *p;
  362. p = root;
  363. while (p!=NULL)
  364. {
  365. if (!calc_p(p))
  366. break;
  367. p = p->next;
  368. }
  369. }
  370. calc_root_positions()
  371. {
  372. Pull *p;
  373. Pull *rp;
  374. int x, w;
  375. p = root;
  376. x = CH_WIDTH;
  377. while (p != NULL)
  378. {
  379. w = (MSPC+strlen(p->data))*CH_WIDTH;
  380. p->xoff = x;
  381. p->yoff = -1;
  382. p->width = w;
  383. p->height = CH_HEIGHT;
  384. x += w;
  385. p = p->next;
  386. }
  387. if ((rp = calloc(1, sizeof(*rp))) == NULL)
  388. {
  389. puts("Out of memory");
  390. return(0);
  391. }
  392. rp->name = "root_pull";
  393. rp->xoff = rp->yoff = 0;
  394. rp->width = XMAX;
  395. rp->height = 8;
  396. rp->children = root;
  397. root = rp;
  398. return(1);
  399. }
  400. write_menus()
  401. {
  402. calc_sub_positions();
  403. calc_root_positions();
  404. write_tree(root,0);
  405. }