ok so I'm starting over and combining some code I wrote before:
processLines.c (has main)
Code:
#include <stdio.h>
#include <ctype.h>
#include "table.h"
#define MAXLINELEN 100
#define MAXWORDLEN 20
FILE *input, *output;
int nextWord (char *, int, char *, int, struct table *p);
int getline(char s[], char l[], int max);
int getline2(char *, int max);
void processLine(char pline, char line);
int emptyLine (char line[]);
int main ( ) {
char line[MAXLINELEN];
char pline[MAXLINELEN];
char word[MAXWORDLEN];
int pos;
input = fopen("table.test", "r");
output = fopen("table.out", "w");
struct table *p = newTable();
int linecount = 1;
while (getline (line, pline, MAXLINELEN-1)) {
if (emptyLine(line) != -1) {
pos = 0;
while (1) {
if ((pos = nextWord (line, pos, word, linecount, p)) == -1) {
//here is where it loops and prints weird
printTable(p, output);
break;
}
}
linecount++;
}
}
printTable(p, output);
fclose(input);
fclose(output);
return 0;
}
int emptyLine (char line[]) {
int i=0;
while (1) {
if (line[i] == '\0' || line[i] == '\n') {
return -1;
}
else if (line[i] != ' ') {
return i;
}
else {
i++;
}
}
}
int nextWord (char *str, int pos, char *word, int linecount, struct table *p) {
int i=0;
//fprintf(output, "pos is %d\n", pos);
while (1) {
if (*str == '\n' || *str == '\0' || *str == '#')
return -1;
else if (i == pos) {
break;
}
else {
i++;
str++;
}
}
//fprintf(output, "str is now %s\n", str);
int r = pos;
char* word2 = word;
int j;
for (j=0; *str!='\n' || *str!=EOF; str++) {
if (*str != ' ') {
char c = *str;
while (c != '\n' && c != ' ') {
r++;
*word = c;
word++;
str++;
c = *str;
}
*word = '\0';
char last = *--word;
if (last != ':') {
addUse(word2, linecount, p);
here is where I'm calling the functions from table.c
return r;
}
else {
addDef(word2, linecount, p);
}
//fprintf(output, "%s %d\n", word2, linecount);
break;
}
else {
r++;
j++;
}
}
word = word2;
if (emptyLine(word) != -1)
printf("%s\t%d\n", word, linecount);
return r;
}
int getline(char s[], char l[], int lim) {
int c, i=0;
while (--lim>0 && (c=getc(input))!=EOF && c!='\n' && c!='#')
s[i++] = c;
if (c =='\n')
s[i++] = c;
else if (c == '#') {
c=getc(input);
while (c!='\n') {
c=getc(input);
s[i++] = '\n';
}
}
s[i] = '\0';
return i;
}
my table.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "table.h"
struct lineNode {
int lineoccur;
struct lineNode *next;
};
/*struct entryNode {
struct entryNode *next;
struct entry *row;
};*/
struct entry {
char * symbol;
int linedef;
struct lineNode *occur;
struct entry *next;
};
struct table {
int entrycount;
struct entry *first;
};
/* returns an empty table */
struct table * newTable( ) {
struct table * table1 = malloc(sizeof(struct table));
table1->first = NULL;
table1->entrycount = 0;
return table1;
}
struct entry *firstSymbol(struct table * symbols) {
//printf("the symbol is %s\n", symbols->first->symbol);
return symbols->first;
}
/* returns the result of adding a definition for the given symbol
in the line with the given number to the table */
struct table * addDef (char * symbol, int lineNum, struct table * symbols){
printf("should be abc is %s\n", symbols->first->symbol);
printf("!!!adding %s\n", symbol);
printf("should be abc is %s\n", symbols->first->symbol);
struct entry *newEntry = malloc(sizeof(struct entry));
newEntry->symbol = symbol;
newEntry->linedef = lineNum;
struct lineNode *o = malloc(sizeof(struct lineNode));
o->lineoccur = -1;
newEntry->occur = o;
//struct entryNode *newEntryNode = malloc(sizeof(struct entryNode));
//newEntryNode->row = newEntry;
printf("still adding %s\n", symbol);
if (symbols->entrycount == 0) {
symbols->first = newEntry;
symbols->entrycount = 1;
printf("1added here %s\n", symbols->first->symbol);
//printTable(symbols, "out");
}
else {
struct entry *nEntry = symbols->first;
while ( nEntry->next != NULL ) {
nEntry = nEntry->next;
}
nEntry->next = newEntry;
(symbols->entrycount)++;
}
return symbols;
}
struct table * addUse (char * symbol, int lineNum, struct table * symbols) {
printf("starting to add %s\n", symbol);
//printf("lineNum is %d\n", lineNum);
struct entry *nEntry = symbols->first;
if (symbols->first == NULL) {
//printf("going here\n");
struct entry *newEntry = malloc(sizeof(struct entry));
newEntry->symbol = symbol;
newEntry->linedef = -1;
struct lineNode *o = malloc(sizeof(struct lineNode));
o->lineoccur = lineNum;
newEntry->occur = o;
symbols->first = newEntry;
printf("added word!!!! %s\n", symbols->first->symbol);
symbols->entrycount = 1;
//printTable(symbols, "nothing");
}
else {
while ( nEntry->symbol != symbol) {
if (nEntry->next == NULL) {
struct entry *newEntry = malloc(sizeof(struct entry));
newEntry->symbol = symbol;
newEntry->linedef = -1;
struct lineNode *o = malloc(sizeof(struct lineNode));
o->lineoccur = lineNum;
newEntry->occur = o;
nEntry->next = newEntry;
(symbols->entrycount)++;
break;
}
nEntry = nEntry->next;
}
struct lineNode *o = nEntry->occur;
while ( (o->next != NULL) && (o->lineoccur != -1) ) {
o = o->next;
}
if (o->lineoccur == -1) {
o->lineoccur = lineNum;
}
else {
struct lineNode *newLineNode = malloc(sizeof(struct lineNode));
newLineNode->lineoccur = lineNum;
newLineNode->next = NULL;
o->next = newLineNode;
}
}
printf("finished adding use %s\n", symbols->first->symbol);
return symbols;
}
void processSymbol (char * symbol) {
char* word2 = symbol;
while (1) {
if (*symbol == '\n' || *symbol == ' ') {
*symbol = '\0';
symbol = word2;
break;
}
else {
symbol++;
}
}
printf("processed %s\n", symbol);
}
/* print the table, in the format described above, to the given file */
void printTable (struct table * symbols, FILE * out) {
struct entry *n = symbols->first;
printf("FIRST IS %s\n", symbols->first->symbol);
//printf("%s", n->symbol);
while (n != NULL) {
//processSymbol(n->symbol);
printf("%s\t", n->symbol);
//printf("after");
printf("%d\t", n->linedef);
struct lineNode *nline = n->occur;
while ((nline != NULL) && (nline->lineoccur != -1)) {
printf("%d\t", nline->lineoccur);
nline = nline->next;
}
printf("\n");
fprintf(out, "%s\t", n->symbol);
fprintf(out, "%d\t\n", n->linedef);
n = n->next;
}
printf("finished printing \n");
}
starting to add abc
added word!!!! abc
finished adding use abc
FIRST IS abc <-- I added "abc" to the table
abc -1 1 <-- table is fine with one entry
finished printing
should be abc is y: <--this should have been the first, "abc", as called from addDef
!!!adding y:
should be abc is y:
still adding y:
y: 2
FIRST IS y:
y: -1 1
y: 2
finished printing
should be abc is abc:
!!!adding abc:
should be abc is abc:
still adding abc:
abc: 3
FIRST IS abc:
abc: -1 1
abc: 2
abc: 3
finished printing
starting to add y
finished adding use y
FIRST IS y
y -1 1 4
y 2
y 3
finished printing
starting to add y
finished adding use y
FIRST IS y
y -1 1 4 5
y 2
y 3
finished printing
FIRST IS y
y -1 1 4 5
y 2
y 3
finished printing
why are my symbols getting replaced? when I just call the functions in table.c without reading from an input file, it adds and prints fine.