Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

char* readline (FILE* f); void print_pair (const char* key, const char* val, void* arg) { printf (%s:%s , key, val); } void write_pair (const char*

char* readline (FILE* f);

void print_pair (const char* key, const char* val, void* arg) { printf ("%s:%s ", key, val); } void write_pair (const char* key, const char* val, void* arg) {

FILE* f = arg; fprintf (f,"%s:%s ", key, val);

}

int main (int argc, char* argv[]) { dict_t* dict = dict_create (); if (argc > 1){ int numFiles = argc - 1; char* files[numFiles]; int index = 0; for(index = 1; index < argc; index++){ files[index - 1] = argv[index-1]; } index = 0; printf("read file"); printf(">"); char * cmd; while((cmd=readline(stdin))!=NULL){ for(index = 0; index < numFiles;index++){ if(strncmp(cmd,"dmp",3)==0){ printf ("BEGIN_DUMP "); char* filename = files[index]; FILE* f = fopen (filename, "r"); dict_apply(dict,&print_pair,f); printf ("END_DUMP ");

} else if(strncmp(cmd,"svf",3)==0){ char* filename = files[index]; FILE* f = fopen (filename, "w"); if(!f){ error("could not open %s for writing ",filename); }

dict_apply(dict,&write_pair ,f); fclose(f); printf ("SAVED "); } else if(strncmp(cmd,"ldf",3)==0 /*&& cmd[3]==' '*/) { char* filename = files[index]; FILE* f = fopen (filename, "r"); if(!f){ error("could not open %s for writing ",filename); } char*val; char*key; while ((key=readline(f))!=NULL){ for (val=key;*val && *val != ':';val++); if(*val==0){ error("syntax error in file "); } *(val++)=0; dict_put(dict,key,val); free(key); } fclose(f); printf("LOADED "); } else error("syntax error:%s ",cmd); printf(">"); free(cmd); } dict_destroy (dict); } } else { printf(">"); char * cmd; while((cmd=readline(stdin))!=NULL){ if (strncmp(cmd,"get",3)==0 && cmd[3]==' '){ char* key = cmd +4; char* val = dict_get(dict,key); printf("%s ",val?val : ""); } else if (strncmp(cmd,"put",3)==0 && cmd[3]==' '){ char* key = cmd +4; char* val; for (val=key;*val && *val != ':';val++) ; if (*val == 0) error("put entered without colon in %s ",cmd); *(val++) = 0; dict_put(dict,key,val); } else if(strncmp(cmd,"del",3)==0 && cmd[3]==' '){ char* key = cmd +4; dict_del(dict,key);break; } else if(strncmp(cmd,"dmp",3)==0){ printf ("BEGIN_DUMP "); FILE*f=stdout; dict_apply(dict,&print_pair,f); printf ("END_DUMP ");

} else if(strncmp(cmd,"svf",3)==0){ char* filename = cmd +4; FILE* f = fopen (filename, "w"); if(!f){ error("could not open %s for writing ",filename); }

dict_apply(dict,&write_pair ,f); fclose(f); printf ("SAVED "); } else if(strncmp(cmd,"ldf",3)==0 && cmd[3]==' ') { char* filename = cmd +4; FILE* f = fopen (filename, "r"); if(!f){ error("could not open %s for writing ",filename); } char*val; char*key; while ((key=readline(f))!=NULL){ for (val=key;*val && *val != ':';val++); if(*val==0){ error("syntax error in file "); } *(val++)=0; dict_put(dict,key,val); free(key); } fclose(f); printf("LOADED "); } //char * line = NULL;

//size_t len = 0; //ssize_t read;

//while ((read = getline(&line, &len, f)) != -1) //{ // char * token = strtok(line, ":"); // int i = 0; // char* key = NULL; // char* val = NULL;

// while( token != NULL ) { // if(i == 0) key = token; // else if(i == 1) val = token; // // token = strtok(NULL, ":"); // i++; // } // // //split buffer into a key and value based on where the colon is // dict_put(dict,key,val); // } // // fclose(f); //if(line) free(line); // printf ("LOADED "); //}

else error("syntax error:%s ",cmd); printf(">"); free(cmd); } dict_destroy (dict); } }

#define BUFLEN 1024

char* readline (FILE* f) { char* buf = NULL; size_t alloc_len = 0; ssize_t pos = -1; while (1) { int c = fgetc(f);

if (c == EOF) //if (feof(stdin)) return buf; ++pos; if (alloc_len <= pos) { // Reading one more char, and no more space. alloc_len += BUFLEN; buf = realloc (buf, alloc_len); } if (c == (int) ' ') { buf[pos] = 0; return buf; } buf[pos] = (char) c; }

return buf; } I am having issue to find an error at this code .Driving program returning time out , so i probably have an infinite loop

Here is dictinary function that the above main is running :

#include #include #include "dict.h"

typedef void (*dict_apply_fun_t) (const char* key, const char* val, void* arg);

typedef struct dict_list { char* key; char* val; struct dict_list* next; } dict_list_t;

typedef struct dict { dict_list_t* head; size_t size; } dict_t;

dict_t* dict_create () { dict_t*ret = (dict_t*)malloc(sizeof(dict_t)); ret ->size = 0; ret ->head = NULL; return ret; } void dict_put (dict_t* dict, const char* key, const char* val) { dict_list_t*el; size_t val_len = strlen(val);

for (el =dict ->head;el;el = el->next) if(strcmp(el->key, key) == 0) break; if(el){ free(el->val); el->val=(char *)malloc(val_len+1); memcpy(el->val,val,val_len+1); return; }

el=(dict_list_t*)malloc(sizeof(dict_list_t)); el->key = strdup(key); el->val = strdup(val); el->next = dict->head; dict->head = el; dict->size++; char* dict_get (const dict_t* dict, const char* key) { dict_list_t*el;

for (el =dict ->head;(el);el = el->next) if(strcmp(el->key, key) == 0) break; if(el) return el->val; else return NULL; }

void dict_del (dict_t* dict, const char* key) { dict_list_t *el,*prev; for (el = dict->head, prev = NULL; el != NULL; prev = el, el = el->next) { if (strcmp(el->key, key) == 0) { if (el->next != NULL) { if (prev == NULL) { dict->head = el->next; } else { prev->next = el->next; } } else if (prev != NULL) { prev->next = NULL; } else { dict->head = NULL; } free(el->key); free(el->val); free(el);

return; } } } size_t dict_size (const dict_t* dict) { return dict->size; } void dict_clear (dict_t* dict) { dict_list_t* el,*t; el = dict->head; while(el != NULL){ t = el; el = el->next; free(t->key); free(t->val); free(t); }

}

void dict_destroy (dict_t* dict) { dict_clear(dict); free(dict); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

The brain system is composed of...........?

Answered: 1 week ago

Question

What connects two hemisphere of the brain?

Answered: 1 week ago

Question

Fluid filled cavity in the brain is called as........?

Answered: 1 week ago

Question

Which part of the brain controls emotions experience?

Answered: 1 week ago

Question

What part of a neuron responsible for receiving information?

Answered: 1 week ago