LCOV - code coverage report
Current view: top level - lib - config.c (source / functions) Hit Total Coverage
Test: popd.info Lines: 19 28 67.9 %
Date: 2015-11-25 19:05:59 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* This file is part of the cmail project (http://cmail.rocks/)
       2             :  * (c) 2015 Fabian "cbdev" Stumpf
       3             :  * License: Simplified BSD (2-Clause)
       4             :  * For further information, consult LICENSE.txt
       5             :  */
       6             : 
       7           1 : int config_parse(LOGGER log, void* config_data, char* conf_file){
       8             :         char line_buffer[MAX_CFGLINE + 1];
       9           1 :         char* line_data = NULL;
      10             :         int offset;
      11             : 
      12           1 :         if(!conf_file){
      13           0 :                 logprintf(log, LOG_ERROR, "Null pointer passed as config file\n");
      14           0 :                 return -1;
      15             :         }
      16             : 
      17           1 :         FILE* conf_stream = fopen(conf_file, "r");
      18             : 
      19           1 :         if(!conf_stream){
      20           0 :                 logprintf(log, LOG_ERROR, "Failed to read configuration file: %s\n", strerror(errno));
      21           0 :                 return -1;
      22             :         }
      23             : 
      24          19 :         while(fgets(line_buffer, MAX_CFGLINE, conf_stream) != NULL){
      25             :                 //preprocess line
      26          17 :                 line_data = line_buffer;
      27             : 
      28             :                 //remove trailing characters
      29          35 :                 for(offset = strlen(line_data) - 1; offset >=0 && !(isalnum(line_data[offset]) || ispunct(line_data[offset])); offset--){
      30          18 :                         line_data[offset] = 0;
      31             :                 }
      32             : 
      33             :                 //skip leading spaces
      34          17 :                 for(; isspace(line_data[0]); line_data++){
      35             :                 }
      36             : 
      37             :                 //handle inline commecnts
      38         897 :                 for(offset = 0; line_data[offset]; offset++){
      39         884 :                         if(line_data[offset] == '#'){
      40           4 :                                 line_data[offset] = 0;
      41           4 :                                 break;
      42             :                         }
      43             :                 }
      44             : 
      45             :                 //ignore empty lines & comments
      46          17 :                 if(line_data[0]){
      47             :                         //handle configuration
      48          10 :                         if(config_line(config_data, line_data) < 0){
      49           0 :                                 fclose(conf_stream);
      50           0 :                                 return -1;
      51             :                         }
      52             :                 }
      53             :         }
      54             : 
      55           1 :         if(errno != 0){
      56           0 :                 logprintf(log, LOG_ERROR, "Error while reading configuration file: %s\n", strerror(errno));
      57           0 :                 fclose(conf_stream);
      58           0 :                 return -1;
      59             :         }
      60             : 
      61           1 :         fclose(conf_stream);
      62           1 :         return 0;
      63             : }

Generated by: LCOV version 1.11