home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / genial / func / logio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-14  |  9.1 KB  |  415 lines

  1. /*
  2.  * logio.c -- routines for reading and writing logs on top of the image
  3.  *
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <strings.h>
  8.  
  9. #include "common.h"
  10. #include "llist.h"
  11. #include "display.h"
  12. #include "ui.h"
  13. #include "reg.h"
  14. #include "log.h"
  15.  
  16. #define HEADER_LOG_DATA_NAME "genial-log"
  17. #define LOG_SIZE 1000        /* allocate log buffer in increments of this
  18.                  * size */
  19.  
  20. int       load_log = 0;
  21.  
  22. char     *parse_logrec(), *parse_op(), *parse_id(), *parse_fnum(), *parse_regrec(),
  23.          *parse_plist(), *parse_auxd(), *parse_text();
  24.  
  25. /***********************************************************************/
  26. add_log(head, loglist)
  27.     struct header *head;
  28.     struct logent *loglist;
  29. {
  30.     struct logent *tmp;
  31.     char      tstr[256];
  32.     char     *buffer;
  33.     int       log_size;
  34.  
  35.     if (loglist == NULL)
  36.     return;
  37.  
  38.     buffer = (char *)calloc(LOG_SIZE, 1);
  39.     log_size = LOG_SIZE;
  40.  
  41.     strcpy(buffer, "GENLOG B\n");
  42.     for (tmp = loglist; tmp != NULL && tmp->reg != NULL; tmp = tmp->next) {
  43.     strcat(buffer, "LOGREC B\n");
  44.     sprintf(tstr, "OPCODE %d\n", tmp->opcode);
  45.     strcat(buffer, tstr);
  46.     sprintf(tstr, "ID %d\n", tmp->id);
  47.     strcat(buffer, tstr);
  48.     sprintf(tstr, "FNUM %d\n", tmp->fnum);
  49.     strcat(buffer, tstr);
  50.     add_reg(buffer, tmp->reg);
  51.     if (tmp->auxdsize != 0) {
  52.         sprintf(tstr, "AUXDSIZE %u\n", tmp->auxdsize);
  53.         strcat(buffer, tstr);
  54.         add_auxd(buffer, tmp->auxdsize, tmp->auxdata);
  55.     }
  56.     strcat(buffer, "LOGREC E\n");
  57.  
  58.     if (strlen(buffer) > log_size - 300) {
  59.         log_size += LOG_SIZE;
  60.         buffer = (char *)realloc(buffer, log_size);
  61.     }
  62.     }
  63.     strcat(buffer, "GENLOG E\n");
  64.  
  65.     setparam(head, HEADER_LOG_DATA_NAME, PFASCII, strlen(buffer) + 1, buffer);
  66.  
  67. #ifdef DEBUG
  68.     printf("log: %s", buffer);
  69. #endif
  70. }
  71.  
  72. /***********************************************************************/
  73. /* add a region to a header */
  74. add_reg(buffer, reg)
  75.     char     *buffer;
  76.     struct region *reg;
  77. {
  78.     char      tstr[256];
  79.  
  80.     strcat(buffer, "REGREC B\n");
  81.     sprintf(tstr, "REGTYPE %d\n", reg->r_type);
  82.     strcat(buffer, tstr);
  83.     sprintf(tstr, "REGFLAGS %d\n", reg->r_flags);
  84.     strcat(buffer, tstr);
  85.     if (reg->r_type != NOREG) {
  86.     add_plen(buffer, reg->r_plen);
  87.     add_plist(buffer, reg->r_plist);
  88.     if (reg->r_type == AN_TEXT)
  89.         add_text(buffer, reg->r_sbs);
  90.     }
  91.     strcat(buffer, "REGREC E\n");
  92. }
  93.  
  94. /***********************************************************************/
  95. /* add length of plist vector to log buffer */
  96. add_plen(buffer, len)
  97.     char     *buffer;
  98.     int       len;
  99. {
  100.     char      tstr[256];
  101.  
  102.     sprintf(tstr, "PLEN %d\n", len);
  103.     strcat(buffer, tstr);
  104. }
  105.  
  106.  
  107. /***********************************************************************/
  108. /* add a point list (plist) to log buffer */
  109. add_plist(buffer, ptlist)
  110.     char     *buffer;
  111.     struct plist *ptlist;
  112. {
  113.     struct plist *trav;
  114.     char      tstr[256];
  115.  
  116.     strcat(buffer, "PLIST B\n");
  117.     for (trav = ptlist; trav != NULL; trav = trav->next) {
  118.     strcat(buffer, "PLREC B\n");
  119.     sprintf(tstr, "%4d %4d\n", trav->pt.x, trav->pt.y);
  120.     strcat(buffer, tstr);
  121.     strcat(buffer, "PLREC E\n");
  122.     }
  123.     strcat(buffer, "PLIST E\n");
  124. }
  125.  
  126. /***********************************************************************/
  127. /* add a text string to log buffer */
  128. add_text(buffer, sbs)
  129.     char     *buffer;
  130.     struct strbs *sbs;
  131. {
  132.     char      tstr[256];
  133.  
  134.     sprintf(tstr, "ATEXT %s\n", sbs->string);
  135.     strcat(buffer, tstr);
  136. }
  137.  
  138. /***********************************************************************/
  139. /* add a auxiliary data to a bufferer */
  140. add_auxd(buffer, len, data)
  141.     char     *buffer;
  142.     u_long    len;
  143.     u_char   *data;
  144. {
  145.     char      tstr[256], hexstr[8];
  146.     u_long    count = 0, index = 0;
  147.  
  148.     while (count < len) {
  149.     bzero(tstr, 256);
  150.     for (index = 0; (index <= 23) && (count < len); index++, count++) {
  151.         sprintf(hexstr, "%2x ", data[count]);
  152.         (void) strcat(tstr, hexstr);
  153.     }
  154.     (void) strcat(tstr, "\n");
  155.     strcat(buffer, tstr);
  156.     }
  157. }
  158.  
  159. /****************************************************************************/
  160. /*
  161.  * The following routines are for parsing / reading logs
  162.  *
  163.  */
  164.  
  165. read_log(head)
  166.     struct header *head;
  167. {
  168.     int       count = 1000;  /* any large value */
  169.     char     *log;
  170.  
  171.     if (findparam(head, HEADER_LOG_DATA_NAME) == NULLPAR)
  172.     return;
  173.  
  174.     getparam(head, HEADER_LOG_DATA_NAME, PFASCII, &count, &log);
  175.     load_log = 1;
  176.     if (log != NULL) {
  177.     parse_log(log);
  178.     }
  179.     load_log = 0;
  180. }
  181.  
  182. /* routine to parse a log given in a particular string */
  183. parse_log(lstr)
  184.     char     *lstr;
  185. {
  186.     char     *rhead;        /* a pointer to the current position in the
  187.                  * reading frame */
  188.  
  189.     rhead = fstring(lstr, "GENLOG B\n");
  190.     if (rhead == NULL) {
  191.     return;
  192.     }
  193.     /*
  194.      * basic algorithm is to look for matching "LOGREC B" and "LOGREC E"
  195.      * tokens. When we find one, move one level through the call stack
  196.      */
  197.     rhead = fstring(rhead, "LOGREC B\n");
  198.     while (rhead != NULL) {
  199.     rhead = parse_logrec((char *) rhead + strlen("LOGREC B\n"));
  200.     rhead = fstring(rhead, "LOGREC B\n");
  201.     }
  202.     fxn_select(0);
  203. }
  204.  
  205. /*routine to parse individual log records and advance the read head */
  206. char     *
  207. parse_logrec(rhead)
  208.     char     *rhead;
  209. {
  210.  
  211.     int       i;
  212.     static char *tokens[] =
  213.     {
  214.     "OPCODE", "ID", "FNUM", "REGREC", "AUXDSIZE", "LOGREC", NULL
  215.     };
  216.  
  217.     while (1) {
  218.     for (i = 0; tokens[i] != NULL; i++) {
  219.         if (strncmp(rhead, tokens[i], strlen(tokens[i])) == 0) {
  220.         break;
  221.         }
  222.     }
  223.     switch (i) {
  224.     case 0:
  225.         rhead = parse_op(rhead, curfunc);
  226.         break;
  227.     case 1:
  228.         rhead = parse_id(rhead, curfunc);
  229.         break;
  230.     case 2:
  231.         rhead = parse_fnum(rhead, curfunc);
  232.         break;
  233.     case 3:
  234.         rhead = parse_regrec(rhead, curfunc);
  235.         break;
  236.     case 4:
  237.         rhead = parse_auxd(rhead, curfunc);
  238.         break;
  239.     case 5:
  240.         rhead += strlen("LOGREC E\n");
  241.         if (curfunc->fnum == curframe()) {
  242.             build_dreg(curfunc->reg);
  243.         fxn_select(curfunc->opcode);
  244.         fxn_init();
  245.         fxn_eval();
  246.         }
  247.         log_perm();
  248.         return rhead;
  249.     }
  250.     }
  251. }
  252.  
  253. char     *
  254. parse_op(rhead, log)
  255.     char     *rhead;
  256.     struct logent *log;
  257. {
  258.     char     *tmp;
  259.  
  260.     rhead += strlen("OPCODE");
  261.     log->opcode = atoi(rhead);
  262.     tmp = index(rhead, '\n');
  263.     rhead = tmp + 1;
  264.  
  265.     return rhead;
  266. }
  267.  
  268. char     *
  269. parse_id(rhead, log)
  270.     char     *rhead;
  271.     struct logent *log;
  272. {
  273.     char     *tmp;
  274.  
  275.     rhead += strlen("ID");
  276.     log->id = atoi(rhead);
  277.     tmp = index(rhead, '\n');
  278.     rhead = tmp + 1;
  279.     return rhead;
  280. }
  281.  
  282. char     *
  283. parse_fnum(rhead, log)
  284.     char     *rhead;
  285.     struct logent *log;
  286. {
  287.     char     *tmp;
  288.  
  289.     rhead += strlen("FNUM");
  290.     log->fnum = atoi(rhead);
  291.     tmp = index(rhead, '\n');
  292.     rhead = tmp + 1;
  293.     return rhead;
  294. }
  295.  
  296. char     *
  297. parse_regrec(rhead, log)
  298.     char     *rhead;
  299.     struct logent *log;
  300. {
  301.     struct region *reg;
  302.     char     *tmp;
  303.     int       type;
  304.  
  305.     rhead += strlen("REGREC B\n");
  306.     rhead += strlen("REGTYPE");
  307.     type = atoi(rhead);
  308.     setplim(MAXPOINTS);
  309.     reg = newreg(type);
  310.     tmp = index(rhead, '\n');
  311.     rhead = tmp + 1;
  312.     rhead += strlen("REGFLAGS");
  313.     reg->r_flags = atoi(rhead);
  314.     tmp = index(rhead, '\n');
  315.     rhead = tmp + 1;
  316.     if (type != NOREG) {
  317.     free(reg->r_plist);
  318.     rhead += strlen("PLEN");
  319.     reg->r_plen = atoi(rhead);
  320.     tmp = index(rhead, '\n');
  321.     rhead = tmp + 1;
  322.     if ((reg->r_plist = (struct plist *)
  323.          malloc(sizeof(struct plist *))) == NULL) {
  324.         perror("malloc");
  325.         exit(1);
  326.     }
  327.     rhead = parse_plist(rhead,log);
  328.     flush_cpl(reg);
  329.     }
  330.     if (type == AN_TEXT) {
  331.     rhead = parse_text(rhead);
  332.     }
  333.     rhead += strlen("REGREC E\n");
  334.     log->reg = reg;
  335.     return rhead;
  336. }
  337.  
  338. char     *
  339. parse_plist(rhead,log)
  340.     char     *rhead;
  341.     struct logent *log;
  342. {
  343.     XPoint    pt;
  344.  
  345.     rhead += strlen("PLIST B\n");
  346.     while (strncmp(rhead, "PLREC B\n", strlen("PLREC B\n")) == 0) {
  347.     rhead += strlen("PLREC B\n");
  348.     pt.x = (short) atoi(rhead);
  349.     rhead += 4;        /* format is %4d */
  350.     pt.y = (short) atoi(rhead);
  351.     rhead += 4;        /* format is %4d */
  352.     if (log->fnum == curframe())
  353.         add_point(pt.x, pt.y);
  354.     else
  355.         add_log_point(pt.x, pt.y);
  356.     rhead = fstring(rhead, "PLREC E\n");
  357.     rhead += strlen("PLREC E\n");
  358.     }
  359.     rhead += strlen("PLIST E\n");
  360.     return rhead;
  361. }
  362.  
  363. char     *
  364. parse_text(rhead)
  365.     char     *rhead;
  366. {
  367.     char     *tmp;
  368.     char      tstr[256];
  369.     int       len;
  370.  
  371.     rhead += strlen("ATEXT") + 1;
  372.     tmp = index(rhead, '\n');
  373.  
  374.     len = (int) (tmp - rhead);
  375.     bcopy(rhead, tstr, (unsigned) len);
  376.     tstr[len++] = '\0';
  377.     /* tstr now contains the string */
  378.     /*
  379.      * add string is a custom tailored version of add_char() for efficient
  380.      * loading of strings.  see regions/text.c
  381.      */
  382.     add_string(tstr);
  383.     rhead = tmp + 1;
  384.     return rhead;
  385. }
  386.  
  387. char     *
  388. parse_auxd(rhead, log)
  389.     char     *rhead;
  390.     struct logent *log;
  391. {
  392.     char     *tmp;
  393.     unsigned char *data;
  394.     int       count = 0, idx;
  395.  
  396.     rhead += strlen("AUXDSIZE");
  397.     log->auxdsize = atoi(rhead);
  398.     tmp = index(rhead, '\n');
  399.     rhead = tmp + 1;
  400.     if ((log->auxdata = (unsigned char *) malloc(log->auxdsize)) == NULL) {
  401.     perror("malloc");
  402.     exit(0);
  403.     }
  404.     data = (unsigned char *) log->auxdata;
  405.     while (count < log->auxdsize) {
  406.     for (idx = 0; (idx <= 23) && (count < log->auxdsize); idx++, count++) {
  407.         *data++ = readhex(rhead);
  408.         rhead += 3;
  409.     }
  410.     tmp = index(rhead, '\n');
  411.     rhead = tmp + 1;
  412.     }
  413.     return rhead;
  414. }
  415.