Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Friday, October 16, 2026

Diff for libcfg+/src/cfgfile.c between version 1.4 and 1.5

version 1.4, 2001/12/05 23:58:48 version 1.5, 2001/12/08 18:23:52
Line 1 
Line 1 
   #include <unistd.h>
   
 #include "str/dynfgets.h"  #include "str/dynfgets.h"
   
 static int cfg_cfgfile_get_next_opt(const CFG_CONTEXT con);  static int cfg_cfgfile_get_next_opt(const CFG_CONTEXT con);
 static int cfgfile_set_currents(char *buf);  static int cfgfile_set_currents(const CFG_CONTEXT con, char *buf);
 static char *get_multi_line(const CFG_CONTEXT con);  static int get_multi_line(const CFG_CONTEXT con, char **buf);
 static void trim_spaces(char *buf);  
   
 static int cfg_cfgfile_get_next_opt(const CFG_CONTEXT con)  static int cfg_cfgfile_get_next_opt(const CFG_CONTEXT con)
 {  {
         char *buf;          char *buf;
           int ret_val;
           register int ret;
   
         if (con->fhandle == NULL) {          if (con->fhandle == NULL) {
                 con->fhandle = fopen(con->filename, "r");                  con->fhandle = fopen(con->filename, "r");
Line 20  static int cfg_cfgfile_get_next_opt(cons
Line 23  static int cfg_cfgfile_get_next_opt(cons
         }          }
   
         while (1) {          while (1) {
                   ret = get_multi_line(con, &buf);
   
                 buf = get_multi_line(con);                  if (ret != CFG_OK) {
                           if (buf != NULL)
                                   free(buf);
   
                 if (buf == NULL)                          return ret;
                         return CFG_ERROR_NOMEM;                  }
   
 #if defined(DEBUG) && DEBUG                  if (feof(con->fhandle)) {
                           if (buf != NULL)
                                   free(buf);
   
                           return CFG_OK;
                   }
   
   #if 0 && defined(DEBUG) && DEBUG
                 printf("read_multi_line<%s>\n", buf);                  printf("read_multi_line<%s>\n", buf);
 #endif  #endif
                   free_currents(con);
   
                 if (cfgfile_set_currents(buf) == 0)                  if (cfgfile_set_currents(con, buf) == 0)
                         return CFG_ERROR_NOMEM;                          return CFG_ERROR_NOMEM;
   
                 free(buf);                  free(buf);
   
                   ret = process_currents(con, &ret_val, NULL);
   
                   if (ret != CFG_OK)
                           return ret;
   
                   if (ret_val > 0)
                           return ret_val;
         }          }
   
         return 0;          return 0;
Line 47  static int cfg_cfgfile_get_next_opt(cons
Line 69  static int cfg_cfgfile_get_next_opt(cons
  * Returns 1 on success, 0 on error (not enough memory).   * Returns 1 on success, 0 on error (not enough memory).
  */   */
   
 static int cfgfile_set_currents(char *buf)  static int cfgfile_set_currents(const CFG_CONTEXT con, char *buf)
 {  {
           register char **pos;
           register char *s_sep, *s_tmp;
           register int size;
   
           for (s_sep = NULL, size = 0, pos = con->file_option_arg_separator;
                           pos != NULL && *pos != NULL;
                           pos++) {
   
                   if ((s_tmp = strstr(buf, *pos)) != NULL)
                           if (s_sep == NULL || s_tmp < s_sep
                                           || (s_tmp == s_sep && strlen(*pos) > size)) {
                                   s_sep = s_tmp;
                                   size = strlen(*pos);
                           }
           }
   
           if (s_sep == NULL) {
                   con->cur_arg = NULL;
   
                   con->cur_opt = strdup(buf);
                   if (con->cur_opt == NULL)
                           return 0;
           }
           else {
                   con->cur_opt = (char *) malloc((s_sep - buf + 1) * sizeof(char));
                   if (con->cur_opt == NULL)
                           return 0;
   
                   strncpy(con->cur_opt, buf, s_sep - buf);
                   con->cur_opt[s_sep - buf] = '\0';
   
                   if ((con->cur_arg = strdup(s_sep + size)) == NULL)
                           return 0;
   
                   str_right_trim(con->cur_opt);
                   str_left_trim(con->cur_arg);
           }
   
         return 1;          return 1;
 }  }
Line 56  static int cfgfile_set_currents(char *bu
Line 115  static int cfgfile_set_currents(char *bu
 /*  /*
  * get_multi_line()   * get_multi_line()
  *   *
  * Gets single line from file. If line continues on next line,   * Gets single line from file. If line continues on next line, returns
  * returns the whole line concatenated.   * the whole line concatenated. Coments, remarks and empty lines are
    * skipped. If end of file is reached CFG_OK is returned and higher
    * level should determine that fact using feof() function.
  */   */
   
 static char *get_multi_line(const CFG_CONTEXT con)  int get_multi_line(const CFG_CONTEXT con, char **buf)
 {  {
         register char **ar = NULL;          register char **ar = NULL;
         register char *buf = NULL;          register char *my_buf = NULL;
   
         register int state = 0;          register int state = 0;
   
           *buf = NULL;
   
         if ((ar = strdyn_create()) == NULL)          if ((ar = strdyn_create()) == NULL)
                 return NULL;                  return CFG_ERROR_NOMEM;
   
         while (1) {          while (1) {
                 if (buf != NULL)                  if (state > 1)
                         free(buf);                          state = 1;
   
                 buf = dynamic_fgets(con->fhandle);                  if (my_buf != NULL)
                 if (buf == NULL)                          free(my_buf);
                         return NULL;  
   
                 //trim_spaces(buf);                  my_buf = dynamic_fgets(con->fhandle);
                   if (my_buf == NULL) {
                           if (feof(con->fhandle))
                                   return CFG_OK;
   
                 if (strlen(buf) == 0 || strdyn_str(buf, con->comment_str) == buf) {                          return CFG_ERROR_NOMEM;
                   }
   
                   str_trim(my_buf);
   
                   if (strlen(my_buf) == 0
                                   || strdyn_str(my_buf, con->comment_prefix) == my_buf) {
                         if (state == 0)                          if (state == 0)
                                 continue;                                  continue;
                         else                          else
                                 break;                                  break;
                 }                  }
   
                 if (buf[strlen(buf) - 1] == '\\') {                  /* Multi line detection. */
                         buf[strlen(buf) - 1] = '\0';                  {
                         //trim_spaces(buf);                          register char **pos;
                         state = 1;                          register int max_len = 0;
                           register int len;
   
                           for (pos = con->multi_line_postfix;
                                           pos != NULL && *pos != NULL;
                                           pos++) {
   
                                   len = strlen(my_buf) - strlen(*pos);
   
                                   if (len > max_len && ! strcmp(*pos, my_buf + len))
                                           max_len = len;
                           }
   
                           /* Multi line postfix found? */
                           if (max_len > 0) {
                                   my_buf[max_len] = '\0';
                                   state = 2;
   
                                   len = strlen(my_buf);
                                   str_right_trim(my_buf);
                                   if (len - strlen(my_buf) > 0) {
                                           /* Could be replaced with
                                              strcpy(my_buf + strlen(my_buf), " "); */
                                           my_buf[strlen(my_buf) + 1] = '\0';
                                           my_buf[strlen(my_buf)] = ' ';
                                   }
                           }
                 }                  }
   
                 ar = strdyn_add(ar, buf);                  ar = strdyn_add(ar, my_buf);
                 if (ar == NULL)                  if (ar == NULL)
                         return NULL;                          return CFG_ERROR_NOMEM;
   
                 if (state == 0)                  if (state != 2)
                         break;                          break;
         }          }
   
         if (buf != NULL)          if (my_buf != NULL)
                 free(buf);                  free(my_buf);
   
         buf = strdyn_implode(ar, "");  
         if (buf == NULL)  
                 return NULL;  
   
         return buf;  
 }  
   
 /*          my_buf = str_right_trim(strdyn_implode(ar, ""));
  * trim_str()          strdyn_free(ar);
  *  
  * Removes white characters from beginning and end of string.  
  */  
   
 static void trim_spaces(char *buf)          if (my_buf == NULL)
 {                  return CFG_ERROR_NOMEM;
   
         buf = buf;          *buf = my_buf;
   
         return;          return CFG_OK;
 }  }
   

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top