113 lines
2.1 KiB
C
113 lines
2.1 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <ctype.h>
|
||
|
#include <string.h>
|
||
|
#include "parser.h"
|
||
|
|
||
|
void gountilbrk (FILE* input) {
|
||
|
char c;
|
||
|
while(c = fgetc(input), c != -1) {
|
||
|
if(c == '\n') {
|
||
|
ungetc(c, input);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void getinfo(FILE* input, int* lncount, int* widestln, int* maxtokens) {
|
||
|
char c, nc;
|
||
|
int lns = 0;
|
||
|
int widest = 0;
|
||
|
int currsz = 0;
|
||
|
int tokens = 0;
|
||
|
int maxtoks = 0;
|
||
|
short readsmt = 0;
|
||
|
while(c = fgetc(input), c != -1) {
|
||
|
if(isspace(c) && readsmt) {
|
||
|
tokens++;
|
||
|
if(c == '\n') {
|
||
|
lns++;
|
||
|
if(currsz > widest)
|
||
|
widest = currsz;
|
||
|
if(tokens > maxtoks)
|
||
|
maxtoks = tokens;
|
||
|
currsz = 0;
|
||
|
tokens = 0;
|
||
|
}
|
||
|
readsmt = 0;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if(c == '/') {
|
||
|
nc = fgetc(input);
|
||
|
if(nc == '/') {
|
||
|
gountilbrk(input);
|
||
|
continue;
|
||
|
}
|
||
|
ungetc(nc, input);
|
||
|
}
|
||
|
|
||
|
currsz++;
|
||
|
readsmt = 1;
|
||
|
}
|
||
|
rewind(input);
|
||
|
(*lncount) = lns;
|
||
|
(*widestln) = widest;
|
||
|
(*maxtokens) = maxtoks;
|
||
|
}
|
||
|
|
||
|
struct line** parse(FILE* input, int lncount, int widestln, int maxtokens) {
|
||
|
struct line** lns = (struct line**)malloc(sizeof(struct line*)*lncount);
|
||
|
char c, nc;
|
||
|
short readsmt = 0;
|
||
|
char tmp[widestln];
|
||
|
char* tokens[maxtokens];
|
||
|
int tmpind = 0;
|
||
|
int lnsind = 0;
|
||
|
int tokensind = 0;
|
||
|
int truelncount = 0;
|
||
|
while(c = fgetc(input), c != -1) {
|
||
|
if(isspace(c)) {
|
||
|
if(readsmt) {
|
||
|
tmp[tmpind] = '\0';
|
||
|
char* newtoken = (char*)malloc(sizeof(char)*tmpind+1);
|
||
|
strcpy(newtoken, tmp);
|
||
|
tokens[tokensind] = newtoken;
|
||
|
tmpind = 0;
|
||
|
readsmt = 0;
|
||
|
tokensind++;
|
||
|
}
|
||
|
if(c == '\n') {
|
||
|
truelncount++;
|
||
|
if(tokensind > 0) {
|
||
|
struct line* newln = (struct line*)malloc(sizeof(struct line));
|
||
|
newln->tokens = (char**)malloc(sizeof(char*)*tokensind);
|
||
|
for(int i = 0; i < tokensind; i++) {
|
||
|
newln->tokens[i] = tokens[i];
|
||
|
}
|
||
|
newln->tokenscount = tokensind;
|
||
|
newln->truen = truelncount;
|
||
|
lns[lnsind] = newln;
|
||
|
lnsind++;
|
||
|
tokensind = 0;
|
||
|
}
|
||
|
}
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if(c == '/') {
|
||
|
nc = fgetc(input);
|
||
|
if(nc == '/') {
|
||
|
gountilbrk(input);
|
||
|
continue;
|
||
|
}
|
||
|
ungetc(nc, input);
|
||
|
}
|
||
|
|
||
|
tmp[tmpind] = c;
|
||
|
tmpind++;
|
||
|
readsmt = 1;
|
||
|
}
|
||
|
return lns;
|
||
|
}
|