wyatt8740

parser code scrap 1

Jan 3rd, 2017
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.30 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <ctype.h> /* isspace() */
  4.  
  5. /* initial defines - pre-set limits. Subject to future changes.*/
  6. #define MAX_KEY_STRING_LEN 51 /* a 'key' can only be 50 chars long, not
  7.                                * including '[]'. This was chosen arbitrarily
  8.                                * to simplify programming and waste fewer CPU
  9.                                * cycles. '51' includes the null termination
  10.                                * byte.*/
  11.  
  12.  
  13. #define DEBUG 1
  14.  
  15.  
  16. /* global vars */
  17. FILE *fp;    /* file pointer should be accessible anywhere in the parser */
  18.  
  19. int  pos = -1; /* position in file */
  20.  
  21. char chr;    /* value of current char in file, as grabbed by fgetc(). */
  22.  
  23. char key[MAX_KEY_STRING_LEN]; /* char array containing key name being operated
  24.                                * on*/
  25.  
  26. char *value; /* pointer to a string containing the value of a key in string
  27.               * form */
  28.  
  29. /*
  30.  * current offset (position) in file. becomes zero the first time nextChar()
  31.  * is called, and is added to on each subsequent call. This is here in case I
  32.  * need to use fseek() at some point.
  33.  */
  34.  
  35. /* end of global vars */
  36.  
  37. int nextChar() /* set chr to char and return 0 unless EOF reached, skip white
  38.                  * space */
  39. {
  40.   chr = fgetc(fp);
  41.   pos = pos + 1;
  42.  
  43.   /* keep going until we find non-whitespace. characters. */
  44.   while ( isspace(chr) != 0 ) { /* isspace() returns non-zero for whitespace. */
  45.     chr = fgetc(fp);
  46.     pos = pos + 1;
  47.   }
  48.  
  49.   if ( chr == EOF ) {
  50.     return (4); /* End-Of-Transmission character */
  51.   }
  52.  
  53.   return (0);
  54. }
  55.  
  56.  
  57.  
  58. int openFile(char *filename) {
  59.   /* open file (first arg) in read-only mode */
  60.   fp=fopen(filename,"r");
  61.  
  62.   /* make sure file was readable */
  63.   if(fp == NULL) {
  64.     printf("Error reading file. Check permissions!\n");
  65.     return (-1);
  66.   }
  67.   return (0);
  68. }
  69.  
  70. int getKeyName() {
  71.   int i = 0;
  72.  
  73.   /*
  74.    * called to get a key name, when the next byte to be read from the file is
  75.    * immediately proceeding a '['.
  76.    */
  77.   while ( (chr != ']') && (chr != EOF) && ( i < ( MAX_KEY_STRING_LEN ) ) ) {
  78.     /* start grabbing chars */
  79.     nextChar(fp);
  80. /*    pos = pos + 1; */ /* nextChar() handles incrementing. Only needed if
  81.                          * using fgetc() directly. */
  82.  
  83.  
  84.     key[i] = chr;
  85.     i = i + 1;
  86.   }
  87.  
  88.   if (chr == ']') {
  89.     key[i - 1] = '\0'; /* replace ']' with null terminator. */
  90.     return (0);
  91.   }
  92.  
  93.   /* now catch things that go wrong */
  94.  
  95.   if ( chr == EOF ) {
  96.     printf("Malformed save data! Reached end-of-file while looking for ']'\n");
  97.     exit(EXIT_FAILURE);    
  98.   }
  99.  
  100.   if ( i == (MAX_KEY_STRING_LEN) ) {
  101.     key[MAX_KEY_STRING_LEN - 1] = '\0'; /* force null termination */
  102.  
  103.     printf("Malformed save data!\n"
  104.             "key name starting with '%s'\n"
  105.             "is too long!\n"
  106.             "(maximum length: %d characters)\n",key,MAX_KEY_STRING_LEN);
  107.     exit(EXIT_FAILURE);
  108.   }
  109.  
  110.   /* Error if we reached this point. Should be unreachable. */
  111.   return (-1);
  112.  
  113. } /* int getKeyName() */
  114.  
  115.  
  116. int parse(){
  117.   /* first, check if item is an array. This is determined by if a curly brace
  118.    * appears in text prior to a semicolon (if a semicolon is first, it is not
  119.    * an array; just a value).
  120.    */
  121.  
  122.   while ( (nextChar() == 0) ) { /* as long as we are able to read data */
  123.  
  124. /* first we need a 'key,' which is enclosed in square brackets ([]), then we
  125.  * need a 'value,' which is enclosed in double quote marks ("").
  126.  */
  127.  
  128.     if (chr == '[') { /* get a new key name. */
  129.       getKeyName(); /* this function sets 'key' to a string containing the key
  130.                      * value */
  131.  
  132.       #ifdef DEBUG
  133.         printf("key name is %s\n",key);
  134.       #endif
  135.  
  136.     /* done getting key name */
  137.     } /* if(chr == '[') */
  138.  
  139.  
  140.     if (chr == '=') { /* set key to value(s) within the next quote or '{' */
  141.       /* first, seek for the special chars ( '{' and '"' ). */
  142.       while( ( chr != '{' ) && ( chr != '"' ) && ( chr != EOF )) {
  143.         nextChar();
  144.       }
  145.  
  146.  
  147.       /* now, we've found the value presumably.
  148.        * the next byte we read will be the 1st char after the quote or '{'.
  149.        * if it's a quote, we know the next value is part of the stored data
  150.        * string. Otherwise, we have to do the same thing we just did to find
  151.        * another key-value pair, separate from the first one.
  152.        */
  153.  
  154.          
  155.       if ( chr == '"' ) {
  156.  
  157.       /* once we have found quote marks, start getting the value within.
  158.        * IT IS IMPORTANT TO NOT USE NEXTCHAR() FOR THIS PART, AS WITHIN THE
  159.        * QUOTE MARKS, SPACES ARE IMPORTANT. */
  160.        
  161.        
  162.  
  163.       } /* if ( chr == '"' ) */
  164.  
  165.     } /* if (chr == '=') */
  166.  
  167.  
  168.   } /*  while ( (nextChar() == 0) ) */
  169.  
  170.  
  171. /* TODO */
  172. /* if nothing else is true, print a warning message,
  173.  * and try again. If EOF is reached, error. */
  174.  
  175. return 0;  
  176. } /* int parse() */
  177.  
  178.  
  179.  
  180.  
  181.  
  182. int main(int argc, char *argv[]) {
  183.  
  184.   /* make sure file name was given */
  185.   if(argc <=1) { /* no arguments passed */
  186.     printf("Error: No file argument was given.\n");
  187.     exit(EXIT_FAILURE);
  188.   }
  189.  
  190.   /* open file, exit if it goes wrong */
  191.   if(openFile(argv[1]) != 0) {
  192.     /* something went wrong opening the file; check in openFile() function */
  193.     exit(EXIT_FAILURE);
  194.   }
  195.  
  196.   else {
  197.     /* we're golden. start parsing HERE. */
  198.     return parse();  
  199.  
  200.   }
  201. }
  202.  
  203.  
  204. int nextCharInclSpace() {/* basically a wrapper to fgetc(), but it also
  205.                          * increments where we are in the file. */
  206.  
  207.                         /* also it does not return a char, but rather sets
  208.                          * 'chr'. Sorry if you hate it, it made more sense to
  209.                          * me because I'm an insane assembly programmer at
  210.                          * heart. Feel free to fix it, or I might later if
  211.                          * the urge strikes me.
  212.                          */
  213.  
  214.   chr = fgetc(fp);
  215.   pos = pos + 1;
  216.  
  217.   if ( chr == EOF ) {
  218.     return (4); /* End-Of-Transmission character */
  219.   }
  220.  
  221.   return (0);
  222. }
  223.  
  224.  
  225.  
  226. int getValString() {
  227.   int i = 0;
  228.   long int oldpos=ftell(fp); /* starting point to return to once we've
  229.                                 * checked how long our string is.*/
  230.   int len;
  231.   /* first, find out how long the string is */
  232.   while( (chr != '"') &&  (chr != EOF) ) {
  233.     nextCharInclSpace();
  234.     i = i + 1;
  235.   }
  236.   if(chr == '"') {
  237.     len = i; /* not `i - 1` since the end quote will become a null terminator. */
  238.   }
  239.  
  240.   /* now, set a value length to match */
  241.   char val[len];
  242.  
  243.   /* seek back to where we were before doing this and this time read the value
  244.    * into the buffer we've allocated */
  245.  
  246.   fseek(fp,oldpos,SEEK_SET);
  247.   /* could also probably make it SEEK_CUR and move negative `i` places. */
  248.  
  249.   pos = pos - i;
  250.   /* TODO - might need to move one further back sine 'i' starts at 0? */
  251.   /* if there are bugs, this is one of the likely spots. */
  252.  
  253.   i=0;
  254.   while (i<len) {
  255.     val[i]=chr;
  256.     nextCharInclSpace();
  257.     i = i + 1;
  258.   }
  259.  
  260.   val[len - 1] = '\0';
  261.  
  262.   #ifdef DEBUG
  263.     printf("value name is %s\n",val);
  264.   #endif
  265.  
  266.  
  267.  
  268.   /* now catch things that go wrong */
  269.  
  270.   if ( chr == EOF ) {
  271.     printf("Malformed save data! Reached end-of-file while looking for ']'\n");
  272.     exit(EXIT_FAILURE);    
  273.   }
  274.  
  275.   if ( i == (MAX_KEY_STRING_LEN) ) {
  276.     key[MAX_KEY_STRING_LEN - 1] = '\0'; /* force null termination */
  277.  
  278.     printf("Malformed save data!\n"
  279.             "key name starting with '%s'\n"
  280.             "is too long!\n"
  281.             "(maximum length: %d characters)\n",key,MAX_KEY_STRING_LEN);
  282.     exit(EXIT_FAILURE);
  283.   }
  284.  
  285.   if (chr == ']') {
  286.     key[i] = '\0'; /* replace ']' with null terminator. */
  287.     return (0);
  288.   }
  289.  
  290.   /* Error if we reached this point. Should be unreachable. */
  291.   return (-1);
  292.  
  293. } /* int getValString() */
  294.  
  295.  
  296.  
  297.  
  298.  
  299. /* internal memory structure:
  300.  * the first item in any object struct is a string identifying it.
  301.  *
  302.  * the player can hold up to ten items, so the inventory array contains
  303.  * ten item structs, and each item struct contains an item name and an item
  304.  * quantity (a char array and an int).
  305.  */
  306.  
  307. /* this code just checks for hardcoded keys to start work on.
  308.  * arrays/objects cannot be embedded in other arrays/objects to
  309.  * simplify things for me. so for player->inventory, we do a separate
  310.  * key called 'player.inventory'.*/
Advertisement
Add Comment
Please, Sign In to add comment