C/Read an ini file

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
m (Created page with '<source lang="c"> #include <stdio.h> #include <stdlib.h> #include <string.h> #define CHUNK_SIZE 128 enum lineTypes { unknown, empty, comment, sectionHead, variableOnly, v…')
 
m (Attie moved page Read an ini file to C/Read an ini file)
 
(6 intermediate revisions by one user not shown)
Line 7: Line 7:
  
 
enum lineTypes {
 
enum lineTypes {
unknown,
+
  unknown,
empty,
+
  empty,
comment,
+
  comment,
sectionHead,
+
  sectionHead,
variableOnly,
+
  variableOnly,
variableValue
+
  variableValue
 
};
 
};
  
 
struct lineByLine {
 
struct lineByLine {
char *line;
+
  char *line;
int length;
+
  int length;
enum lineTypes type;
+
  enum lineTypes type;
 
};
 
};
  
 
int main(int argc, char *argv[]) {
 
int main(int argc, char *argv[]) {
  
FILE *f;
+
  FILE *f;
void *p;
+
  void *p;
+
 
struct lineByLine *lines;
+
  struct lineByLine *lines;
struct lineByLine *tline;
+
  struct lineByLine *tline;
int linec;
+
  int linec;
+
 
int isNewline;
+
  int isNewline;
char *c; /* contents*/
+
  char *c; /* contents*/
int i;
+
  int i;
int l; /* length */
+
  int l; /* length */
int r; /* amount read */
+
  int r; /* amount read */
int s; /* memory size */
+
  int s; /* memory size */
 +
  int printedLines;
  
f = fopen("php.ini","r");
+
  f = fopen("php.ini","r");
if (!f) {
+
  if (!f) {
perror("fopen()");
+
    perror("fopen()");
exit(1);
+
    goto die;
}
+
  }
  
c = NULL;
+
  c = NULL;
l = 0;
+
  l = 0;
s = 0;
+
  s = 0;
while (!feof(f)) {
+
  while (!feof(f)) {
s += CHUNK_SIZE;
+
    s += CHUNK_SIZE;
p = realloc(c, sizeof(char) * s);
+
    p = realloc(c, sizeof(char) * s);
if (!p) {
+
    if (!p) {
perror("realloc()");
+
      perror("realloc()");
goto die1;
+
      goto die1;
}
+
    }
c = p;
+
    c = p;
r = fread(&(c[l]), 1, s - l, f);
+
    r = fread(&(c[l]), 1, s - l, f);
if (!r) {
+
    if (!r) {
fprintf(stderr,"EOF?\n");
+
      if (feof(f)) break;
break;
+
      /* else error */
} else if (r < 0) {
+
      fprintf(stderr,"fread(): An error occured\n");
perror("fread()");
+
      goto die1;
break;
+
    } else if (r < 0) {
}
+
      perror("fread()");
l += r;
+
      break;
}
+
    }
+
    l += r;
p = realloc(c, sizeof(char) * (l + 1));
+
  }
if (!p) {
+
 
perror("realloc()");
+
  p = realloc(c, sizeof(char) * (l + 1));
} else {
+
  if (!p) {
c = p;
+
    perror("realloc()");
}
+
  } else {
+
    c = p;
c[l] = '\0';
+
  }
+
 
lines = NULL;
+
  c[l] = '\0';
linec = 0;
+
 
isNewline = 1;
+
  lines = NULL;
for (i = 0; i <= l; i++) {
+
  linec = 0;
if (isNewline) {
+
  isNewline = 1;
isNewline = 0;
+
  for (i = 0; i <= l; i++) {
p = realloc(lines, sizeof(struct lineByLine) * (linec + 1));
+
    if (isNewline) {
if (!p) {
+
      isNewline = 0;
perror("realloc()");
+
      p = realloc(lines, sizeof(struct lineByLine) * (linec + 1));
goto die2;
+
      if (!p) {
}
+
        perror("realloc()");
lines = p;
+
        goto die2;
tline = &(lines[linec]);
+
      }
tline->line = &(c[i]);
+
      lines = p;
tline->length = -1;
+
      tline = &(lines[linec]);
tline->type = unknown;
+
      tline->line = &(c[i]);
linec++;
+
      tline->length = -1;
}
+
      tline->type = unknown;
if (i == l || c[i] == '\n') {
+
      linec++;
isNewline = 1;
+
    }
if (i < l) c[i] = '\0';
+
    if (i == l || c[i] == '\n') {
if (linec > 0) {
+
      isNewline = 1;
tline = &(lines[linec-1]);
+
      if (i < l) c[i] = '\0';
tline->length = strlen(tline->line);
+
      if (linec > 0) {
switch (tline->line[0]) {
+
        tline = &(lines[linec-1]);
case '\0': tline->type = empty; break;
+
        tline->length = strlen(tline->line);
case ';':  tline->type = comment; break;
+
        switch (tline->line[0]) {
case '[':  tline->type = sectionHead; break;
+
          case '\0': tline->type = empty; break;
default:
+
          case ';':  tline->type = comment; break;
if (strchr(tline->line,'=')) {
+
          case '[':  tline->type = sectionHead; break;
tline->type = variableValue;
+
          default:
} else {
+
            if (strchr(tline->line,'=')) {
tline->type = variableOnly;
+
              tline->type = variableValue;
}
+
            } else {
}
+
              tline->type = variableOnly;
}
+
            }
}
+
        }
}
+
      }
+
    }
for (i = 0; i < linec; i++) {
+
  }
tline = &(lines[i]);
+
 
printf("%6d-%d-%3d: %s\n",
+
  printedLines = 0;
i,
+
  for (i = 0; i < linec; i++) {
tline->type,
+
    tline = &(lines[i]);
tline->length,
+
    switch (tline->type) {
tline->line);
+
      case sectionHead:
}
+
        if (printedLines) printf("\n");
+
      case variableOnly:
return 0;
+
      case variableValue:
 +
        printf("%s\n", tline->line);
 +
        printedLines++;
 +
        break;
 +
      case empty:
 +
      case unknown:
 +
      case comment:
 +
        break;
 +
    }
 +
  }
 +
 
 +
  return 0;
 
die2:
 
die2:
if (lines) free(lines);
+
  if (lines) free(lines);
 
die1:
 
die1:
if (c) free(c);
+
  if (c) free(c);
 
die:
 
die:
return 1;
+
  return 1;
 
}
 
}
 
</source>
 
</source>

Latest revision as of 17:07, 14 February 2013

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define CHUNK_SIZE 128
 
enum lineTypes {
  unknown,
  empty,
  comment,
  sectionHead,
  variableOnly,
  variableValue
};
 
struct lineByLine {
  char *line;
  int length;
  enum lineTypes type;
};
 
int main(int argc, char *argv[]) {
 
  FILE *f;
  void *p;
 
  struct lineByLine *lines;
  struct lineByLine *tline;
  int linec;
 
  int isNewline;
  char *c; /* contents*/
  int i;
  int l; /* length */
  int r; /* amount read */
  int s; /* memory size */
  int printedLines;
 
  f = fopen("php.ini","r");
  if (!f) {
    perror("fopen()");
    goto die;
  }
 
  c = NULL;
  l = 0;
  s = 0;
  while (!feof(f)) {
    s += CHUNK_SIZE;
    p = realloc(c, sizeof(char) * s);
    if (!p) {
      perror("realloc()");
      goto die1;
    }
    c = p;
    r = fread(&(c[l]), 1, s - l, f);
    if (!r) {
      if (feof(f)) break;
      /* else error */
      fprintf(stderr,"fread(): An error occured\n");
      goto die1;
    } else if (r < 0) {
      perror("fread()");
      break;
    }
    l += r;
  }
 
  p = realloc(c, sizeof(char) * (l + 1));
  if (!p) {
    perror("realloc()");
  } else {
    c = p;
  }
 
  c[l] = '\0';
 
  lines = NULL;
  linec = 0;
  isNewline = 1;
  for (i = 0; i <= l; i++) {
    if (isNewline) {
      isNewline = 0;
      p = realloc(lines, sizeof(struct lineByLine) * (linec + 1));
      if (!p) {
        perror("realloc()");
        goto die2;
      }
      lines = p;
      tline = &(lines[linec]);
      tline->line = &(c[i]);
      tline->length = -1;
      tline->type = unknown;
      linec++;
    }
    if (i == l || c[i] == '\n') {
      isNewline = 1;
      if (i < l) c[i] = '\0';
      if (linec > 0) {
        tline = &(lines[linec-1]);
        tline->length = strlen(tline->line);
        switch (tline->line[0]) {
          case '\0': tline->type = empty; break;
          case ';':  tline->type = comment; break;
          case '[':  tline->type = sectionHead; break;
          default:
            if (strchr(tline->line,'=')) {
              tline->type = variableValue;
            } else {
              tline->type = variableOnly;
            }
        }
      }
    }
  }
 
  printedLines = 0;
  for (i = 0; i < linec; i++) {
    tline = &(lines[i]);
    switch (tline->type) {
      case sectionHead:
        if (printedLines) printf("\n");
      case variableOnly:
      case variableValue:
        printf("%s\n", tline->line);
        printedLines++;
        break;
      case empty:
      case unknown:
      case comment:
        break;
    }
  }
 
  return 0;
die2:
  if (lines) free(lines);
die1:
  if (c) free(c);
die:
  return 1;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox