had too google this one
+62 −16 · 3 files changed
-
mylib/include/myhash.h+2 −2 -
mylib/src/myhash.c+47 −10 -
mylib/test/t_hash.c+13 −4
@@ -23,6 +23,6 @@ extern HashTable *h_create ( unsigned int size ); extern HashNode *h_search (HashTable *hTable, char *key); extern void h_delete (HashTable *hTable, char *key); extern int openFile(const char *fn); -void readFile(int fd); +//void readFile(int fd); extern int closefile(int fd); -ssize_t readLine(char *buf, size_t sz, char *fn, off_t *offset); +extern ssize_t readLine(char *buf, size_t sz, char *fn, off_t *offset); @@ -194,7 +194,7 @@ int openFile (const char *fn) { } return inputFd; } - +/* void readFile (int fd) { // Read bytes from fd int numRead for read () 3rd arg int countRead; @@ -205,12 +205,12 @@ void readFile (int fd) { // Set buffer for void *buf arg char buf[BUFF]; // extra buffer to fill end of line. - char extraBuffer + //char extraBuffer // Keep track of offset after each '\n' - off_t offset = 0; + //off_t offset = 0; // store last string until '\m' - //if + //if //off_t lseek(int fd, off_t offset, int whence); //off_t newPos = lseek(fd,,SEEK_DATA) while ( (countRead = read (fd, buf, BUFF)) > 0) @@ -220,24 +220,61 @@ void readFile (int fd) { fprintf(stderr,"Could not write whole buffer.\n"); //fprintf(stderr,"%s", pch); } -/* readline `sz` bytes from file `fn`, begin at file `offset` + + * readline `sz` bytes from file `fn`, begin at file `offset` * storing all chars in `buf`. `buf` is terminated at the first * newline found . * return -1 on error, EOF with 0 chars read , success number of chars read */ -ssize_t readLine(char *buf, size_t sz, char *fn, off_t *offset){ + + +ssize_t readLine(char *buf, size_t sz, char *fn, off_t *offset){ // Open the file as read only int fd = openFile(fn); - + + ssize_t nchr = 0; + ssize_t idx = 0; + char *p = NULL; + + /* position fd & read line */ + if ( (nchr = lseek(fd, *offset, SEEK_SET)) !=-1 ) + nchr = read (fd, buf, sz); + closeFile(fd); + + /* read error */ + if (nchr == -1) + fprintf(stderr, "%s(), error: read failure in '%s'.\n", __func__, fn); + + /* end of file - no characters read */ + if (nchr == 0) + return -1; + + /* check each character */ + p = buf; + while( (idx < nchr) && (*p != '\n') ) + p++, idx++; + *p = 0; + + /* newline not found */ + if (idx == nchr) { + *offset += nchr; + + /* check file for missing newline at end */ + return nchr < (ssize_t) sz ? nchr : 0; + + } + // set *offset to 1+idx + *offset += idx + 1; + + return idx; } int closeFile(int fd){ int closeFd; - closeFd = close(fd); - if ( closeFd == -1) + if ( (closeFd = close(fd)) < 0) perror("Error on close\n"); else if(closeFd == 0) { - fprintf(stderr, "Closed file descriptior\n"); + fprintf(stderr, "Closed file descriptior\n"); return closeFd; } } @@ -2,7 +2,6 @@ #include <stdio.h> #include <assert.h> #include "../include/myhash.h" - // edit library implement search func. // test h_search - call on same keys and verify that is same result as the initial call to insert @@ -18,6 +17,7 @@ static void dumpTable (HashTable *table) { // A new nash node - can be null or something else HashNode *node; + do { // Derefenrce one level and set it to node node = *hNodePtr; @@ -65,11 +65,20 @@ int main(int argc, char** argv){ int x = openFile("../testdata"); fprintf(stderr,"%d\n",x); // read the file - readFile(x); + //readFile(x); // close the file - int r = closeFile(x); - printf(stderr,"%d\n",r); + closeFile(x); + //printf(stderr,"%d\n",r); + + + char line[128] = {0}; + off_t offset = 0; + ssize_t len = 0; + size_t i = 0; + /* using open/read, read each line in file into 'line' */ + while ((len = readLine (line, 128, "../testdata" , &offset)) != -1) + printf (" line[%2zu] : %s (%zd chars)\n", i++, line, len); #if 0 int i = 0;