Advertisement
Lekensteyn

fieldsize.c

Oct 25th, 2011
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.29 KB | None | 0 0
  1. /**
  2.  * Copyright (C) 2011  Peter Lekensteyn <lekensteyn@gmail.com>
  3.  *
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <string.h>
  21. #include <strings.h>
  22. #include <stdlib.h>
  23.  
  24. #define BUFSIZE 64
  25.  
  26. void parse_error(char *msg, char *line, char *current) {
  27.     fprintf(stderr, "%s\n", msg);
  28.     fprintf(stderr, " at pos %lu in line %s\n",
  29.         1 + current - line, line);
  30.     exit(1);
  31. }
  32. #define SKIP_SPACES(str) while (isspace(*str)) str++;
  33.  
  34.  
  35. // stores the name segment from str in name
  36. void nameSeg(char *str, char *name) {
  37.     int i;
  38.     memset(name, 0, sizeof(name));
  39.     for (i=0; i<4; i++, str++) {
  40.         if (!*str)
  41.             break;
  42.         // NameSeg = Alpha (Alpha | digits){0,3}
  43.         if ((i && isalpha(*str)) ||
  44.                   isalnum(*str))
  45.             name[i] = *str;
  46.         else
  47.             break;
  48.     }
  49. }
  50.  
  51. // returns the number read from *str and moves the pointer
  52. int readNumber(char **str) {
  53.     if (strlen(*str) >= 2 && !strncasecmp("0x", *str, 2)) {
  54.         (*str) += 2;
  55.         return strtol(*str, str, 16);
  56.     } else {
  57.         return strtol(*str, str, 10);
  58.     }
  59. }
  60.  
  61. // get the comment contents found in str
  62. // unused, implement if there is a need for retrieving comments
  63. void get_comment(char *str, char **target) {
  64.     // begin comment?
  65.     if (*str == '/') {
  66.         if (!*(++str)) return;
  67.         // begin single-line comment
  68.         if (*str == '/') {
  69.             str++;
  70.             SKIP_SPACES(str);
  71.             strcpy(*target, str);
  72.         } else if (*str == '*') {
  73.             str++;
  74.             SKIP_SPACES(str);
  75.             // XXX copy & remove */
  76.         }
  77.     }
  78. }
  79.  
  80. int main(int argc, char **argv) {
  81.  
  82.     char line[BUFSIZE];
  83.     //char comment[BUFSIZE];
  84.     // index in bits
  85.     int index = 0;
  86.  
  87.     while (fgets(line, sizeof(line), stdin) != NULL) {
  88.         char *c = line;
  89.  
  90.         // skip leading spaces
  91.         SKIP_SPACES(c);
  92.  
  93.         // skip empty lines and comments
  94.         if (!*c || *c == '/')
  95.             continue;
  96.  
  97.         if (strlen(c) > 8 && !strncasecmp("AccessAs", c, 8)) /* ignore */;
  98.         else if (strlen(c) > 9 && !strncasecmp("Offset", c, 6)) {
  99.             // matching for Offset(x)
  100.             c += 6;
  101.             int offset;
  102.  
  103.             SKIP_SPACES(c);
  104.             if (!*c || *(c++) != '(')
  105.                 parse_error("Expected a parenthese after Offset.", line, c);
  106.  
  107.             offset = readNumber(&c);
  108.             if (offset < 0)
  109.                 parse_error("Offset cannot be negative", line, c);
  110.             index = 8 * offset;
  111.  
  112.             SKIP_SPACES(c);
  113.             if (*c != ')')
  114.                 parse_error("Expected a parenthese after Offset.", line, c);
  115.             printf("Offset (0x%02X), // byte 0x%02X  bit %i\n", offset,
  116.                 index / 8, index % 8);
  117.         } else {
  118.             char name[5];
  119.             nameSeg(c, name);
  120.             int size;
  121.             c += strlen(name);
  122.  
  123.             // if no name was found, ignore it, probably alignment?
  124.  
  125.             SKIP_SPACES(c);
  126.             if (*c != ',')
  127.                 parse_error("Expected a CommaChar but encountered invalid data.", line, c);
  128.             c++;
  129.             SKIP_SPACES(c);
  130.  
  131.             size = readNumber(&c);
  132.             if (size < 0)
  133.                 parse_error("BitLength must not be negative", line, c);
  134.  
  135.             SKIP_SPACES(c);
  136.             if (*c && *c != ',' && *c != '/')
  137.                 parse_error("Expected a comma, comment or null but encountered data.", line, c);
  138.             printf("%4s,   %2i,    // byte 0x%02X  bit %i\n", name, size,
  139.                 index / 8, index % 8);
  140.  
  141.             index += size;
  142.         }
  143.     }
  144.     return 0;
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement