Advertisement
Guest User

Untitled

a guest
Feb 7th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. int module_prev_state, channel_prev_state;
  2. int module_count = 0;
  3. int channel_count = 0;
  4.  
  5. static int handler(void* user, const char* section, const char* name,
  6.                    const char* value)
  7. {
  8.     settings* pconfig = (settings*)user;
  9.     #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
  10.  
  11.     if (strcmp(section, "module") == 0) {
  12.         if (MATCH("module", "name")) {
  13.             pconfig->module[module_count].name = strdup(value);     ///< Filling module name
  14.             module_prev_state = module_count;
  15.             ++module_count;
  16.         } else if (MATCH("module", "imitationType")) {
  17.             pconfig->module[module_prev_state].imitation_type = strdup(value);  ///< Filling imitation type
  18.         } else if (MATCH("module", "tract")) {
  19.             pconfig->module[module_prev_state].tract = atoi(value); ///< Filling physical address
  20.         } else if (MATCH("module", "channel")) {
  21.             pconfig->module[module_prev_state].channel = atoi(value); ///< Filling channel number
  22.         } else {
  23.             module_prev_state = module_count;
  24.             return 0;  /* unknown section/name, error */
  25.         }
  26.     }
  27.  
  28.     if (strcmp(section, "channel") == 0) {
  29.         if (MATCH("channel", "number")) {
  30.             pconfig->channel[channel_count].channel_number = atoi(value);   ///< Filling channel number
  31.             channel_prev_state = channel_count;
  32.             ++channel_count;    /// Counting channels by names
  33.         } else if (MATCH("channel", "working")) {
  34.             pconfig->channel[channel_prev_state].isWorking = atoi(value);   ///< Filling working state
  35.         } else if (MATCH("channel", "frequency")) {
  36.             pconfig->channel[channel_prev_state].frequency = atoi(value);   ///< Filling channel frequency
  37.         } else if (MATCH("channel", "moduleCount")) {
  38.             pconfig->channel[channel_prev_state].moduleCount = atoi(value); ///< Filling module amount
  39.         } else {
  40.             channel_prev_state = channel_count;
  41.             return 0;  /* unknown section/name, error */
  42.         }      
  43.     }
  44.  
  45.     /// Reallocating memory for next iterations
  46.     pconfig = (settings *) realloc(pconfig, (module_count + channel_count) * sizeof(channel_config));
  47.     pconfig->module = (module_config *) realloc(pconfig->module, module_count * sizeof(module_config));
  48.     pconfig->channel = (channel_config *) realloc(pconfig->channel, channel_count * sizeof(channel_config));
  49.  
  50.     return 1;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement