C/Read an ini file

From Attie's Wiki
(Difference between revisions)
Jump to: navigation, search
m
m
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);
+
exit(1);
  }
+
}
  
  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");
+
fprintf(stderr,"EOF?\n");
      break;
+
break;
    } else if (r < 0) {
+
} else if (r < 0) {
      perror("fread()");
+
perror("fread()");
      break;
+
break;
    }
+
}
    l += r;
+
l += r;
  }
+
}
 
+
  p = realloc(c, sizeof(char) * (l + 1));
+
p = realloc(c, sizeof(char) * (l + 1));
  if (!p) {
+
if (!p) {
    perror("realloc()");
+
perror("realloc()");
  } else {
+
} else {
    c = p;
+
c = p;
  }
+
}
 
+
  c[l] = '\0';
+
c[l] = '\0';
 
+
  lines = NULL;
+
lines = NULL;
  linec = 0;
+
linec = 0;
  isNewline = 1;
+
isNewline = 1;
  for (i = 0; i <= l; i++) {
+
for (i = 0; i <= l; i++) {
    if (isNewline) {
+
if (isNewline) {
      isNewline = 0;
+
isNewline = 0;
      p = realloc(lines, sizeof(struct lineByLine) * (linec + 1));
+
p = realloc(lines, sizeof(struct lineByLine) * (linec + 1));
      if (!p) {
+
if (!p) {
        perror("realloc()");
+
perror("realloc()");
        goto die2;
+
goto die2;
      }
+
}
      lines = p;
+
lines = p;
      tline = &(lines[linec]);
+
tline = &(lines[linec]);
      tline->line = &(c[i]);
+
tline->line = &(c[i]);
      tline->length = -1;
+
tline->length = -1;
      tline->type = unknown;
+
tline->type = unknown;
      linec++;
+
linec++;
    }
+
}
    if (i == l || c[i] == '\n') {
+
if (i == l || c[i] == '\n') {
      isNewline = 1;
+
isNewline = 1;
      if (i < l) c[i] = '\0';
+
if (i < l) c[i] = '\0';
      if (linec > 0) {
+
if (linec > 0) {
        tline = &(lines[linec-1]);
+
tline = &(lines[linec-1]);
        tline->length = strlen(tline->line);
+
tline->length = strlen(tline->line);
        switch (tline->line[0]) {
+
switch (tline->line[0]) {
          case '\0': tline->type = empty; break;
+
case '\0': tline->type = empty; break;
          case ';':  tline->type = comment; break;
+
case ';':  tline->type = comment; break;
          case '[':  tline->type = sectionHead; break;
+
case '[':  tline->type = sectionHead; break;
          default:
+
default:
            if (strchr(tline->line,'=')) {
+
if (strchr(tline->line,'=')) {
              tline->type = variableValue;
+
tline->type = variableValue;
            } else {
+
} else {
              tline->type = variableOnly;
+
tline->type = variableOnly;
            }
+
}
        }
+
}
      }
+
}
    }
+
}
  }
+
}
 
+
  for (i = 0; i < linec; i++) {
+
printedLines = 0;
    tline = &(lines[i]);
+
for (i = 0; i < linec; i++) {
    printf("%6d-%d-%3d: %s\n",
+
tline = &(lines[i]);
            i,
+
switch (tline->type) {
            tline->type,
+
case sectionHead:
            tline->length,
+
if (printedLines) printf("\n");
            tline->line);
+
case variableOnly:
  }
+
case variableValue:
 
+
printf("%s\n", tline->line);
  return 0;
+
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>

Revision as of 16:54, 17 October 2011

#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()");
		exit(1);
	}
 
	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) {
			fprintf(stderr,"EOF?\n");
			break;
		} 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