Guest User

parser.c

a guest
Jun 29th, 2025
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | Source Code | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define MAX_LINE_LENGTH 255
  6.  
  7. typedef struct
  8. {
  9.     int exposureTime;
  10.     int flashPin;
  11.     int triggerPin;
  12.     int lightPin;
  13.     double disDropLight;
  14.     double disLightCam;
  15. } Config;
  16.  
  17. typedef struct
  18. {
  19.     char key[255];
  20.     char value[255];
  21. } KeyValuePair;
  22.  
  23. // declarations
  24. void addToConfig(Config *config, KeyValuePair pair);
  25. KeyValuePair getKeyValuePair(char line[MAX_LINE_LENGTH]);
  26. Config readConfigFromFile();
  27.  
  28. int main()
  29. {
  30.     Config config = readConfigFromFile();
  31.  
  32.     printf("%s: %d\n", "exposureTime", config.exposureTime);
  33.     printf("%s: %d\n", "flashPin", config.flashPin);
  34.     printf("%s: %d\n", "triggerPin", config.triggerPin);
  35.     printf("%s: %d\n", "lightPin", config.lightPin);
  36.     printf("%s: %f\n", "disDropLight", config.disDropLight);
  37.     printf("%s: %f\n", "disLightCam", config.disLightCam);
  38.  
  39.     return 0;
  40. }
  41.  
  42. Config readConfigFromFile()
  43. {
  44.     FILE *file;
  45.     Config config;
  46.  
  47.     file = fopen("init", "r");
  48.     if (file == NULL)
  49.     {
  50.         perror("fopen()");
  51.         return config;
  52.     }
  53.  
  54.     // read config file line by line
  55.     char line[MAX_LINE_LENGTH];
  56.     while (fgets(line, MAX_LINE_LENGTH, file))
  57.     {
  58.         // remove trailing newline
  59.         line[strcspn(line, "\n")] = 0;
  60.  
  61.         // extract key value pair
  62.         KeyValuePair pair = getKeyValuePair(line);
  63.  
  64.         // fill config struct
  65.         addToConfig(&config, pair);
  66.     }
  67.  
  68.     fclose(file);
  69.  
  70.     return config;
  71. }
  72.  
  73. KeyValuePair getKeyValuePair(char line[MAX_LINE_LENGTH])
  74. {
  75.     char *delimiter = " ";
  76.     KeyValuePair pair;
  77.  
  78.     int currentToken = 0; // ugh, i hate that - but I need to know which token I'm at and couldn't find a nicer way
  79.     for (char *token = strtok(line, delimiter); token != NULL; token = strtok(NULL, delimiter))
  80.     {
  81.         // take the first token as key ...
  82.         if (currentToken == 0)
  83.         {
  84.             strcpy(pair.key, token);
  85.         }
  86.         // ... and the second as value ...
  87.         else if (currentToken == 1)
  88.         {
  89.             strcpy(pair.value, token);
  90.         }
  91.         // ... and ignore all other tokens.
  92.         else
  93.         {
  94.             break;
  95.         }
  96.  
  97.         currentToken++;
  98.     }
  99.  
  100.     return pair;
  101. }
  102.  
  103. void addToConfig(Config *config, KeyValuePair pair)
  104. {
  105.     if (strcmp(pair.key, "exposureTime") == 0)
  106.     {
  107.         config->exposureTime = atof(pair.value);
  108.     }
  109.     else if (strcmp(pair.key, "flashPin") == 0)
  110.     {
  111.         config->lightPin = atof(pair.value);
  112.     }
  113.     else if (strcmp(pair.key, "triggerPin") == 0)
  114.     {
  115.         config->triggerPin = atof(pair.value);
  116.     }
  117.     else if (strcmp(pair.key, "lightPin") == 0)
  118.     {
  119.         config->lightPin = atof(pair.value);
  120.     }
  121.     else if (strcmp(pair.key, "disDropLight") == 0)
  122.     {
  123.         config->disDropLight = atof(pair.value);
  124.     }
  125.     else if (strcmp(pair.key, "disLightCam") == 0)
  126.     {
  127.         config->disLightCam = atof(pair.value);
  128.     }
  129.     else
  130.     {
  131.         printf("unknown config key: %s\n", pair.key);
  132.     }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment