54 lines
1.1 KiB
C
54 lines
1.1 KiB
C
#include "string.h"
|
|
#include "stdlib.h"
|
|
#include "ctype.h"
|
|
#include "stdio.h"
|
|
#include "futil.h"
|
|
|
|
char* g_attribs=(char*)0;
|
|
const char** g_attrib_index=(const char**)0;
|
|
int g_attribs_count=0;
|
|
int g_attribs_len=0;
|
|
|
|
/*
|
|
* load and index an attributes file
|
|
*/
|
|
int load_attrib_file(const char* file) {
|
|
FILE* stream=fopen(file,"r");
|
|
g_attribs_len=file_len(stream);
|
|
g_attribs_count=fcountc(stream,',')+1;
|
|
g_attribs=calloc(g_attribs_len,sizeof(char)+1);
|
|
g_attrib_index=calloc(sizeof(char*),g_attribs_count);
|
|
const char** ind_writer = g_attrib_index;
|
|
int n='\0';
|
|
char* writer=g_attribs;
|
|
*ind_writer=writer;
|
|
++ind_writer;
|
|
while((n=fgetc(stream))!=EOF) {
|
|
char c=(char)n;
|
|
printf("%c",c);
|
|
switch(c) {
|
|
case ',':
|
|
*writer='\0';
|
|
if(isspace((char)fpeek(stream)))
|
|
*ind_writer=writer+2;
|
|
else
|
|
*ind_writer=writer+1;
|
|
++ind_writer;
|
|
break;
|
|
case '\t':
|
|
case '\n':
|
|
*writer=' ';
|
|
break;
|
|
default:
|
|
*writer=c;
|
|
break;
|
|
}
|
|
++writer;
|
|
}
|
|
*writer='\0';
|
|
for(int i=0;i<g_attribs_count;++i) {
|
|
printf("%d) %s\n",i+1,g_attrib_index[i]);
|
|
}
|
|
return 0;
|
|
}
|