PageRenderTime 51ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Pristine/V/UTILS/MAKEPULL.C

http://github.com/AnimatorPro/Animator-Pro
C++ | 456 lines | 406 code | 49 blank | 1 comment | 65 complexity | 1faed5cc86e163d6213b477fcf9d9717 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, "#include \"jimk.h\"\n");
  113. fprintf(out, "#include \"flicmenu.h\"\n");
  114. fprintf(out, "\n\n");
  115. }
  116. char *
  117. skip_space(s)
  118. char *s;
  119. {
  120. while (isspace(*s))
  121. s++;
  122. return(s);
  123. }
  124. char *
  125. remove_trailing(s)
  126. char *s;
  127. {
  128. int i;
  129. i = strlen(s);
  130. while (--i >= 0)
  131. {
  132. if (isspace(s[i]))
  133. s[i] = 0;
  134. else
  135. break;
  136. }
  137. return(s);
  138. }
  139. Pull *
  140. read_m()
  141. {
  142. Pull *p;
  143. if (!next_line())
  144. return(0);
  145. if ((p = calloc(1, sizeof(*p))) == NULL)
  146. {
  147. puts("Out of memory");
  148. return(0);
  149. }
  150. p->level = isspace(inbuf[0]); /* 1'st or second level */
  151. if ((p->data = clone_string(remove_trailing(skip_space(inbuf))))==NULL)
  152. {
  153. puts("Too many strings");
  154. return(0);
  155. }
  156. return(p);
  157. }
  158. copy_bytes(s,d,count)
  159. register char *s,*d;
  160. int count;
  161. {
  162. while (--count >= 0)
  163. *d++ = *s++;
  164. }
  165. char *
  166. single_name()
  167. {
  168. char buf[256];
  169. char sb[8];
  170. copy_bytes(last_hi->data, sb, 3);
  171. sb[3] = 0;
  172. sprintf(buf, "%s_pull", sb);
  173. return(clone_string(buf));
  174. }
  175. char *
  176. double_name()
  177. {
  178. char buf[256];
  179. char sb[8],db[8];
  180. copy_bytes(last_hi->data, sb, 3);
  181. sb[3] = 0;
  182. copy_bytes(last_lo->data, db, 3);
  183. db[3] = 0;
  184. sprintf(buf, "%s_%s_pull", sb, db);
  185. return(clone_string(buf));
  186. }
  187. read_menus()
  188. {
  189. Pull *p;
  190. for (;;)
  191. {
  192. if ((p = read_m()) == NULL)
  193. break;
  194. if (root == NULL)
  195. {
  196. root = last_hi = p;
  197. }
  198. else
  199. {
  200. if (p->level == 0) /* root level */
  201. {
  202. last_hi->next = p;
  203. last_lo = NULL;
  204. last_hi = p;
  205. }
  206. else
  207. {
  208. if (last_lo == NULL)
  209. {
  210. last_hi->children = last_lo = p;
  211. }
  212. else
  213. {
  214. last_lo->next = p;
  215. last_lo = p;
  216. }
  217. }
  218. }
  219. if (p->level == 0)
  220. p->name = single_name();
  221. else
  222. p->name = double_name();
  223. if (p->name == NULL)
  224. {
  225. puts("Out of memory");
  226. break;
  227. }
  228. lcase(p->name);
  229. }
  230. }
  231. print_idents(ident)
  232. int ident;
  233. {
  234. while (--ident >= 0)
  235. fprintf(OUT, "\t");
  236. }
  237. print_by_cw(w)
  238. int w;
  239. {
  240. fprintf(OUT,"%d+%d*CH_WIDTH, ",
  241. w%CH_WIDTH, w/CH_WIDTH);
  242. }
  243. print_by_ch(h)
  244. int h;
  245. {
  246. fprintf(OUT,"%d+%d*CH_HEIGHT, ",
  247. h%CH_HEIGHT, h/CH_HEIGHT);
  248. }
  249. write_m(p, ident)
  250. Pull *p;
  251. int ident;
  252. {
  253. print_idents(ident);
  254. fprintf(OUT, "struct pull %s = {\n", p->name);
  255. ident += 1;
  256. print_idents(ident);
  257. if (p->next == NULL)
  258. fprintf(OUT, "NONEXT,\n");
  259. else
  260. fprintf(OUT, "&%s,\n", p->next->name);
  261. print_idents(ident);
  262. print_by_cw(p->xoff);
  263. print_by_ch(p->yoff);
  264. print_by_cw(p->width);
  265. print_by_ch(p->height);
  266. fprintf(OUT,"\n");
  267. print_idents(ident);
  268. if (p->children == NULL)
  269. fprintf(OUT, "NOCHILD,\n");
  270. else
  271. fprintf(OUT, "&%s,\n", p->children->name);
  272. print_idents(ident);
  273. if (p->data == NULL)
  274. fprintf(OUT, "NODATA,\n");
  275. else
  276. fprintf(OUT, "\"%s\",\n", p->data);
  277. print_idents(ident);
  278. if (p->data == NULL)
  279. fprintf(OUT, "pull_oblock,\n");
  280. else
  281. fprintf(OUT, "pull_text,\n");
  282. print_idents(ident);
  283. fprintf(OUT, "0,\n");
  284. print_idents(ident);
  285. fprintf(OUT, "};\n");
  286. }
  287. write_tree(p, ident)
  288. Pull *p;
  289. int ident;
  290. {
  291. if (p == NULL)
  292. return;
  293. write_tree(p->next, ident);
  294. write_tree(p->children, ident+1);
  295. write_m(p, ident);
  296. }
  297. list_els(p)
  298. Pull *p;
  299. {
  300. int count = 0;
  301. while (p != NULL)
  302. {
  303. count++;
  304. p = p->next;
  305. }
  306. return(count);
  307. }
  308. longest_string(p)
  309. Pull *p;
  310. {
  311. int longest = 0;
  312. int i;
  313. while (p != NULL)
  314. {
  315. i = strlen(p->data);
  316. if (i > longest)
  317. longest = i;
  318. p = p->next;
  319. }
  320. return(longest);
  321. }
  322. calc_p(p)
  323. Pull *p;
  324. {
  325. Pull *rp;
  326. Pull *cp;
  327. int w, h;
  328. int y;
  329. char buf[256];
  330. if ((rp = calloc(1, sizeof(*rp))) == NULL)
  331. {
  332. puts("Out of memory");
  333. return(0);
  334. }
  335. w = longest_string(p->children);
  336. rp->width = w*CH_WIDTH+5;
  337. h = list_els(p->children);
  338. rp->height = h*CH_HEIGHT+3;
  339. rp->xoff = -1;
  340. rp->yoff = CH_HEIGHT;
  341. sprintf(buf, "r%s", p->name);
  342. if ((rp->name = clone_string(buf)) == NULL)
  343. {
  344. puts("Out of memory");
  345. return(0);
  346. }
  347. cp = rp->children = p->children;
  348. p->children = rp;
  349. y = 1;
  350. while (cp != NULL)
  351. {
  352. cp->xoff = 1;
  353. cp->yoff = y;
  354. cp->width = 3+CH_WIDTH*w;
  355. cp->height = 1+CH_HEIGHT;
  356. y += CH_HEIGHT;
  357. cp = cp->next;
  358. }
  359. return(1);
  360. }
  361. calc_sub_positions()
  362. {
  363. Pull *p;
  364. p = root;
  365. while (p!=NULL)
  366. {
  367. if (!calc_p(p))
  368. break;
  369. p = p->next;
  370. }
  371. }
  372. calc_root_positions()
  373. {
  374. Pull *p;
  375. Pull *rp;
  376. int x, w;
  377. p = root;
  378. x = CH_WIDTH;
  379. while (p != NULL)
  380. {
  381. w = (MSPC+strlen(p->data))*CH_WIDTH;
  382. p->xoff = x;
  383. p->yoff = -1;
  384. p->width = w;
  385. p->height = CH_HEIGHT;
  386. x += w;
  387. p = p->next;
  388. }
  389. if ((rp = calloc(1, sizeof(*rp))) == NULL)
  390. {
  391. puts("Out of memory");
  392. return(0);
  393. }
  394. rp->name = "root_pull";
  395. rp->xoff = rp->yoff = 0;
  396. rp->width = XMAX;
  397. rp->height = 8;
  398. rp->children = root;
  399. root = rp;
  400. return(1);
  401. }
  402. write_menus()
  403. {
  404. calc_sub_positions();
  405. calc_root_positions();
  406. write_tree(root,0);
  407. }