Sign in to follow this  
dailycoder

Interview assignment

Recommended Posts

#include <stdio.h> #include <string.h> #include "interview-dstr.h" #ifndef NULL #define NULL 0 #endif #define DSTRING_CHUNK_SIZE 512 #define string_length(s) (strlen(s)) #define string_contents(s) ((*s)) /*----- Functions provided ------------------------------------------------*/ /* --- @nf_dstr_init@ --- * * * Arguments: @nf_dstr *d@ = pointer to dynamic string * @nf_arena *a@ = pointer to arena to use * * Returns: Zero on success, or @-1@ on error. * * Use: Initializes a string accumulator. */ int nf_dstr_init(nf_dstr *d, nf_arena *a){ void *p; d->sz=DSTRING_CHUNK_SIZE; d->buf=aALLOC(d->a,d->sz); if (!d->buf) return -1; d->len=0; d->st=0; return 0; } /* --- @nf_dstr_zero@ --- * * * Arguments: @nf_dstr *d@ = pointer to dynamic string * @nf_arena *a@ = pointer to arena to use * * Returns: -- * * Use: Initializes a string accumulator to a valid state, * but does not preallocate any memory. This cannot fail, * and forgetting about the dstr while it is in this state * does not leak any memory. */ void nf_dstr_zero(nf_dstr *d, nf_arena *a){ d->a=NULL; d->buf=NULL; d->len=0; d->sz=0; d->st=0; } /* --- @nf_dstr_destroy@ --- * * * Arguments: @nf_dstr *d@ = pointer to dynamic string * * Returns: --- * * Use: Destroys a dynamic string. * Leaves the string in the same state as @nf_dstr_zero@. */ void nf_dstr_destroy(nf_dstr *d){ void *p; if(d->buff) { d->buff=aFree(d->a,(char*)p) d->buff=NULL; d->st=0; d->len=0; } } /* --- @nf_dstr_check@ --- * * * Arguments: @const nf_dstr *d@ = pointer to dynamic string * * Returns: Zero on success, or @-1@ on error. * * Use: Returns @Status_OK@ if the string is working, or a status * code which explains what'd wrong with it. */ * int nf_dstr_check(const nf_dstr *d){ if (d->len<0) // validating by checking whether the length is less than 0 return -1; if( d->len>=d->sz) // Validing the string length if any additional space required than return error return -1; return 0; } /* --- @nf_dstr_ensure@ --- * * * Arguments: @nf_dstr *d@ = pointer to dynamic string * @size_t sz@ = amount of free data needed * * Returns: Zero on success, or @-1@ on error. * * Use: Ensures that there'd enough free space in the string for * something to be added. */ int nf_dstr_ensure(nf_dstr *d, size_t sz){ if((d->len+sz)< (d->sz)) { d->st=0; return d->st; }else { d->sz+=DSTRING_CHUNK_SIZE; d->buff=aREALLOC(d->a,(char*)p,d->sz); if(!d->buff) return -1 } return 0; } /* --- @nf_dstr_reset@ --- * * * Arguments: @nf_dstr *d@ = pointer to dynamic string * * Returns: --- * * Use: Resets a string. The string becomes empty, and further * additions appear at the beginning. */ void nf_dstr_reset(nf_dstr *d){ if(d->buff){ d->buff=NULL; d->len=0; } } /* --- @nf_dstr_putc@ --- * * * Arguments: @nf_dstr *d@ = pointer to dynamic string * @int ch@ = character to add * * Returns: Zero on success, or @-1@ on error. * * Use: Adds a character to a dynamic string. */ int nf_dstr_putc(nf_dstr *d, int ch){ if ((d->len) >= (d->sz)) { d->sz += DSTRING_CHUNK_SIZE; tmp = (char*) malloc(sizeof(char)*d->sz); (void)memcpy((char*)tmp, (char*)(d->buf), (size_t)(d->len)); if(nf_dstr_reset(d->buf)==0) d->buf = tmp; else return -1; } d->buf[d->len++] = ch; return 0; } /* --- @nf_dstr_putz@ --- * * * Arguments: @nf_dstr *d@ = pointer to dynamic string * * Returns: Zero on success, or @-1@ on error. * * Use: Puts a null terminator on the end of a dynamic string. The * null terminator doesn't count as part of the string, however, * and it'll be overwritten with the next @put@ call. */ int nf_dstr_putz(nf_dstr *d){ if ((d->len) >= (d->sz)) { d->sz += DSTRING_CHUNK_SIZE; tmp = (char*) malloc(sizeof(char)*d->sz); (void)memcpy((char*)tmp, (char*)(d->buf), (size_t)(d->len)); if(nf_dstr_reset(d->buf)==0) d->buf = tmp; else return -1; } d->buf[d->len+1] = '\0'; return 0; } /* --- @nf_dstr_puts@ --- * * * Arguments: @nf_dstr *d@ = pointer to dynamic string * @const char *p@ = pointer to a string to add * * Returns: Zero on success, or @-1@ on error. * * Use: Adds a string to a dynamic string. A null terminator is also * attached. */ int nf_dstr_puts(nf_dstr *d, const char *p){ unsigned nlength = string_length(p); unsigned length = (nlength + (d->len)); if (length > (d->sz)) { char* tmp; while ((d->sz) < length) { d->sz += DSTRING_CHUNK_SIZE; } tmp = (char*) malloc (sizeof(char), d->sz); (void)memcpy((char*)tmp, (char*)(d->buff), (size_t)(d->len)); if(nf_dstr_reset(d->buf)==0) d->buf = tmp; else return -1 } (void)memcpy((char*) & (d->buf[d->len]), (char*)string_contents(p), (size_t)nlength); d->len = length; d->buf[d->len+1]='\0'; return 0; } /* --- @nf_dstr_putm@ --- * * * Arguments: @nf_dstr *d@ = pointer to dynamic string * @const void *p@ = pointer to a block to add * @size_t sz@ = size of the block. * * Returns: Zero on success, or @-1@ on error. * * Use: Adds an arbitrary binary block to a dynamic string. */ int nf_dstr_putm(nf_dstr *d, const void *p, size_t sz){ unsigned nlength = string_length((char*)p); unsigned length = (nlength + (d->len)); if (length > (d->sz)) { char* tmp; while ((d->sz) < length) { d->sz += DSTRING_CHUNK_SIZE; } tmp = (char*) malloc (sizeof(char), d->sz); (void)memcpy((char*)tmp, (char*)(d->buff), (size_t)(d->len)); if(nf_dstr_reset(d->buf)==0) d->buf = tmp; else return -1 } (void)memcpy((char*) & (d->buf[d->len]), (char*)string_contents((char*)p), (size_t)nlength); d->len = length; return 0; } /* --- @nf_dstr_putd@ --- * * * Arguments: @nf_dstr *d@ = pointer to dynamic string * @const nf_dstr *dd@ = pointer to a string to add * * Returns: Zero on success, or @-1@ on error. * * Use: Adds a string to a dynamic string. A null terminator is also * attached. */ int nf_dstr_putd(nf_dstr *d, const nf_dstr *dd){ unsigned nlength = dd->len; unsigned length = (nlength + (d->len)); if (length > (d->sz)) { char* tmp; while ((d->sz) < length) { d->sz += DSTRING_CHUNK_SIZE; } tmp = (char*) malloc (sizeof(char), d->sz); (void)memcpy((char*)tmp, (char*)(d->buff), (size_t)(d->len)); if(nf_dstr_reset(d->buf)==0) d->buf = tmp; else return -1 } (void)memcpy((char*) & (d->buf[d->len]), (char*)&(dd->buf[dd->len], (size_t)nlength); d->len = length; d->buf[d->len+1]='\0'; return 0; } /* --- @nf_dstr_puthex@ --- * * * Arguments: @nf_dstr *d@ = pointer to dynamic string * @const void *p@ = pointer to data * @size_t sz@ = size of the data * * Returns: Zero on success, or @-1@ on error. * * Use: Writes a hex representation of a binary block to a string. */ int nf_dstr_puthex(nf_dstr *d, const void *p, size_t sz){ char *hexa1; sprintf(hexa1,"0x%x",strtoul((char*)p,(char**)NULL,16)-1); unsigned nlength = string_length(hexa1); unsigned length = (nlength + (d->len)); if (length > (d->sz)) { char* tmp; while ((d->sz) < length) { d->sz += DSTRING_CHUNK_SIZE; } tmp = (char*) malloc (sizeof(char), d->sz); (void)memcpy((char*)tmp, (char*)(d->buff), (size_t)(d->len)); if(nf_dstr_reset(d->buf)==0) d->buf = tmp; else return -1 } (void)memcpy((char*) & (d->buf[d->len]), (char*)string_contents(hexa1), (size_t)nlength); d->len = length; return 0; } /* --- @nf_dstr_putline@ --- * * * Arguments: @nf_dstr *d@ = pointer to a dynamic string block * @FILE *fp@ = a stream to read from * * Returns: The number of characters read into the buffer, or @EOF@ if * end-of-file was reached before any characters were read. * * Use: Appends the next line from the given input stream to the * string. The trailing newline is stripped; a trailing null * byte is appended, as for @dstr_putz@ */ int nf_dstr_putFile(nf_dstr *d, File *f){ int chunksize = 4; /* use BUFSIZ-1 for (probably) more efficient program */ char *s; char *temp; int buffersize = chunksize+1; s = malloc(buffersize); /* get the first chunk */ if (s == NULL) { printf("Can't allocate %d bytes\n", buffersize); return NULL; } /* read the first chunk with standard library fgets */ if (fgets(s, chunksize+1, f) == NULL) { printf("fgets returned NULL\n"); } else { printf("\n"); while (strchr(s, '\n') == NULL) { buffersize += chunksize; temp = realloc(s, buffersize); if (temp == NULL) { free(s); /* clean up the previously allocated stuff */ s = NULL; break; } else { s = temp; /*read into the zero byte that was the end of the previous chunk */ if (fgets(s+buffersize-chunksize-1, chunksize+1, f) == NULL) { printf("fgets returned NULL\n"); break; } } } } return nf_dstr_puts(d, s); }

Share this post


Link to post
Share on other sites
Interview-dstr.c file is attached . please let me know whether the code is correct or not. u can find interview-dst.h and itnerview-arean.h is available in the forum

Share this post


Link to post
Share on other sites
No. We will not answer your job/university/whatever application questions for you.

Also, when posting code, please use the source tags. No-one is going to want to decipher that great big lump of poorly formatted code to answer your question even if it were a legitimate question for the forums.

Share this post


Link to post
Share on other sites
I'm not even sure I understand what you want (not that I'm going to actually help). If you have the ability to pose yourself sample interview questions (I assume that is what this is), can you not figure out for yourself if your response is correct or not? I can think of a couple of ways that you might do that. Alternatively, if this is a -real- interview question that someone is actually hoping you'll try your hand at...boy I'm glad I don't potentially work with you.

That is all.

~Shiny

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this