Skip to content

Commit

Permalink
Class to read config files, please move global variables and function…
Browse files Browse the repository at this point in the history
… header (and maybe also any includes I have in this class) to cs352proxy header file that John is making.
  • Loading branch information
zoukai957 committed Nov 16, 2012
1 parent d6e0357 commit 851aedd
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 0 deletions.
108 changes: 108 additions & 0 deletions configFile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//This is a tokenizer file I made for 214 and needed it for this
//program, so this program would rely on tokenizer.c
#include "tokenizer.h"
#include "linkedlist.h"

//MACROS that should be added into header
#define TAPDEVICESIZE 10

//Global variables that should be added into header
char tapDevice[TAPDEVICESIZE];
int linkPeriod;
int linkTimeout;
int quitAfter;


/**readConfigFile reads the configuration file required to run the VLAN
* program.
* Parameters:
* FILE *configFile - pointer to the configuration file, it should
* already be opened, if NULL error will return.
* linkedListHolder peer - pointer to a linklist of peers, if NULL creates
* a link list of peers
* Return: 0 on success, 1 on failure
* NOTE: Lines that are longer than 279 characters will be cut short
* NOTE: Also assumes text file was created using linux OS**/
int readConfigFile(FILE *configFile, linkedListHolder peer){
if (configFile == NULL){
printf("Error: Bad Config File\n");
return 1;
}

//reads a line from file
char *buffer = (char*)malloc(sizeof(char)*280);
fgets(buffer, 280,configFile);

//Sees if file is blank
if(strlen(buffer) == 0 && ftell(configFile)){
printf("Error: File is blank\n");
return 1;
}
int keyC=0;
while(strlen(buffer) != 0){
//Sees if file read entire line
int position = ftell(configFile);
position--;
fseek(configFile, position, SEEK_SET);
char breakBool = fgetc(configFile);
if (breakBool != '\n' && breakBool != EOF){

//Reads from file until next line
breakBool = fgetc(configFile);
while(breakBool != '\n'&& breakBool != EOF){
breakBool = fgetc(configFile);
}
}

//Checks if line is only 1 character long
if(strlen(buffer)==1){
printf("Error: One of the lines in the file is in incorrect format\n");
return 1;
}

//Checks if line is a comment
if(buffer[0] == '/' && buffer[1] == '/'){
memset(buffer, '\0', sizeof(buffer));
fgets(buffer, 280, configFile);
continue;
}

TokenizerT words = TKCreate(" ",buffer);
//Sees if last charcter in token is '\n' if so it removes it
if(((words->next)->token)[strlen(((words->next)->token))-1] == '\n'){
((words->next)->token)[strlen(((words->next)->token))-1] = ((words->next)->token)[strlen(((words->next)->token))];
}
if(strcmp((words->token),"tapDevice") == 0){
memcpy(tapDevice,(words->next)->token, strlen((words->next)->token));
}else if(strcmp((words->token),"linkPeriod") == 0){
linkPeriod = atoi((words->next)->token);
}else if(strcmp((words->token),"linkTimeout") == 0){
linkTimeout = atoi((words->next)->token);
}else if(strcmp((words->token),"peer") == 0){
char *peerName = (char*)malloc(sizeof(char)*strlen((words->next)->token));
memcpy(peerName,(words->next)->token,strlen((words->next)->token)+1);
add(peer, peerName, keyC);
keyC++;
}else if(strcmp((words->token),"quitAfter") == 0){
quitAfter = atoi((words->next)->token);
}else{
printf("Error: One of the lines in the file is in incorrect format\n");
return 1;
}

//Cleanup
memset(buffer, '\0', sizeof(buffer));
fgets(buffer, 280, configFile);

}

//Cleanup
free(buffer);

return 0;

}

69 changes: 69 additions & 0 deletions linkedlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "linkedlist.h"

linkedListHolder create() {
linkedListHolder ans = (linkedListHolder)malloc(sizeof(struct llholder));
ans->head = 0;
return ans;
}

void add(linkedListHolder list, void * data, int key) {
linkedList insert = (linkedList)malloc(sizeof(struct ll));
insert->key = key;
insert->data = data;
if (list->head == 0) {
insert->next = insert;
insert->prev = insert;
list->head = insert;
return;
}
linkedList last = list->head->prev;
insert->prev = last;
last->next = insert;
insert->next = list->head;
list->head->prev = insert;
return;
}
void * get(linkedListHolder list, int key) {
linkedList pointer = list->head;
do {
if (pointer->key == key) {
return pointer->data;
}
pointer = pointer->next;
} while (pointer != list->head);
return 0;
}

int removeNode(linkedListHolder list, int key) {
if (list->head->key == key) {
if (list->head->next == list->head) {
free(list->head);
list->head = 0;
return 1;
}
else {
list->head->next->prev = list->head->prev;
list->head->prev->next = list->head->next;
linkedList temp = list->head;
list->head = list->head->next;
free(temp);
return 1;
}
}
linkedList pointer = list->head->next;
while (pointer != list->head) {
if (pointer->key == key) {
pointer->prev->next = pointer->next;
pointer->next->prev = pointer->prev;
free(pointer);
return 1;
}
pointer = pointer->next;
}
return 0;
}


25 changes: 25 additions & 0 deletions linkedlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct ll {
struct ll * prev;
struct ll * next;
int key;
void * data;
};

typedef struct ll * linkedList;

struct llholder {
linkedList head;
};
typedef struct llholder * linkedListHolder;

linkedListHolder create();

void add(linkedListHolder list, void * data, int key);

void * get(linkedListHolder list, int key);

int removeNode(linkedListHolder list, int key);
178 changes: 178 additions & 0 deletions tokenizer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "tokenizer.h"



/*
* TKCreate creates a new TokenizerT object for a given set of separator
* characters (given as a string) and a token stream (given as a string).
*
* TKCreate should copy the two arguments so that it is not dependent on
* them staying immutable after returnning. (In the future, this may change
* to increase efficiency.)
*
* If the function succeeds, it returns a non-NULL TokenizerT.
* Else it returns NULL.
*
* You need to fill in this function as part of your implementation.
*/

TokenizerT TKCreate(char *separators, char *ts) {

/*Tests to make sure separators and ts exists*/
if (separators==NULL || ts==NULL) {
return NULL;
}

char *sep; /*seperator characters*/
sep = malloc(sizeof(char)*(strlen(separators)+1));
char *tokenString; /*String your trying to seperate*/
tokenString = malloc(sizeof(char)*(strlen(ts)+1));
int length; /*length of each token*/
int charptrTS; /*points to the char location in the tokenString*/
TokenizerT rtrn=NULL; /*return tokenizer*/
TokenizerT rtrnPTR = NULL; /*location of latest node in the returning tokenizer*/
char *begins; /*points to the beginning of the token*/

/*copies sperators and ts to sep and tokenString*/
memcpy(sep,separators,((strlen(separators)+1)*sizeof(char)));
memcpy(tokenString,ts,((strlen(ts)+1)*sizeof(char)));

/*starts to seperate tokenString*/
length=0;
charptrTS=0;
begins=&tokenString[0];

while (charptrTS!= strlen(tokenString)){
int charptrSEP=0;
for (; charptrSEP < strlen(sep); charptrSEP++) {
if (tokenString[charptrTS]==sep[charptrSEP]){
if (length==0){
begins=&tokenString[charptrTS+1];
break;
}else {
TokenizerT temp = malloc(sizeof(struct TokenizerT_));
char *String= malloc(sizeof(char)*(length+1));
strncpy(String,begins,length);
String[length]='\0';
temp->token=String;
temp->next=NULL;
if (rtrn==NULL){
rtrn=temp;
} else {
rtrnPTR->next=temp;
}
rtrnPTR=temp;
}
begins=&tokenString[charptrTS+1];
length=0;
}else {
length++;
}
}
charptrTS++;
}


/*Gets the last word*/
TokenizerT temp = malloc(sizeof(struct TokenizerT_));
char *String= malloc(sizeof(char)*(length+1));
strncpy(String,begins,length);
String[length]='\0';
temp->token=String;
temp->next=NULL;
if (rtrn==NULL){
rtrn=temp;
} else {
rtrnPTR->next=temp;
}
rtrnPTR=temp;

/*frees unnecessary mallocs*/
free(sep);
free(tokenString);

return rtrn;
}

/*
* TKDestroy destroys a TokenizerT object. It should free all dynamically
* allocated memory that is part of the object being destroyed.
*
* You need to fill in this function as part of your implementation.
*/

void TKDestroy(TokenizerT tk) {
TokenizerT destroyPTR; /*Pointer we want to destroy*/
TokenizerT nextPTR; /*Pointer to the next Token in object*/

/*Frees all malloced space within TK and declares the items within tokenizerT as NULL*/
destroyPTR=tk;
nextPTR=tk;

while(destroyPTR!=NULL){
nextPTR=nextPTR->next;
destroyPTR->token=NULL;
destroyPTR->next=NULL;
free(destroyPTR);
destroyPTR=nextPTR;
}
}

/*
* TKGetNextToken returns the next token from the token stream as a
* character string. Space for the returned token should be dynamically
* allocated. The caller is responsible for freeing the space once it is
* no longer needed.
*
* If the function succeeds, it returns a C string (delimited by '\0')
* containing the token. Else it returns 0.
*
* You need to fill in this function as part of your implementation.
*/

char *TKGetNextToken(TokenizerT tk) {
/*NOTE FOR GRADER: I returned the token for the given TokenizerT.
* I didn't see a use to return the next token, but I will add notes as to
* what should be different to return the NEXT token string.*/

/*if tk == NULL returns 0*/
if (tk->token==NULL) {
return "0";
}

/*returning String*/
char *rtrn=NULL;
rtrn=malloc(sizeof(char)*(strlen(tk->token)+1));
/*(if returning next token the tk->token should be tk->next->token*/

strcpy(rtrn,tk->token);
/*if returning next token the tk->token should be tk->next->token*/

return rtrn;
}

/*
* main will have two string arguments (in argv[1] and argv[2]).
* The first string contains the separator characters.
* The second string contains the tokens.
* Print out the tokens in the second string in left-to-right order.
* Each token should be printed on a separate line.
*/

void printList(TokenizerT tk){
TokenizerT printPTR=tk; /*points to TokenizerT*/

/*Prints the resulting list of TKCreate*/
while (printPTR!=NULL) {
if (strcmp(TKGetNextToken(printPTR),"0")!=0){
printf("\t%s\n",TKGetNextToken(printPTR));
}
printPTR=printPTR->next;
}
}

Loading

0 comments on commit 851aedd

Please sign in to comment.