interview assignement
Well,
I've been recently given a mini c project for a work interview. This is about c programing, strings manipulation exactly.
There are the instructions:
"Please find attached 2 C header files, interview-dstr.h and
interview-arena.h.
The task is to provide an implementation, interview-dstr.c, of the
functions declared in interview-dstr.h (There is no need to write an
implementation of interview-arena.h, you may assume that one already
exists.)
You may use any C documentation or other online resources to help you with
the implementation.
You should assume that the implementation is to be used in production
quality software and that it must conform exactly to the comments in
interview-dstr.h. If you find that any comment for a dstr function does
not completely define the functions behaviour, you should decide how to
make the function behave and improve the documentation in interview-dstr.h."
interview-dstr.h file:/* -*-c-*- * * Dynamic string construction interface * */#ifndef DSTR_H#define DSTR_H#ifdef __cplusplus extern "C" {#endif/*----- Header files ------------------------------------------------------*/#include <stdio.h>#include <stdarg.h>#ifndef ARENA_H# include "arena.h"#endif/*----- Data structures ---------------------------------------------------*//* Use these fields directly for read access to the dstr. * Always ensure that 0 <= len <= sz. */typedef struct nf_dstr { nf_arena *a; char *buf; /* contains the current value of the string */ size_t len; /* number of bytes used (not including terminating null) */ size_t sz; /* size of allocation currently in use */ int st; /* current error status */} nf_dstr;#ifndef DISABLE_NONNFtypedef nf_dstr dstr;#endif/*----- 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);/* --- @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);/* --- @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);/* --- @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);/* --- @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);/* --- @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);/* --- @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);/* --- @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);/* --- @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);/* --- @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);/* --- @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);/* --- @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);/* --- @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@. *//*----- That's all, folks -------------------------------------------------*/#ifdef __cplusplus }#endif#endif
[Edited by - jolyqr on October 23, 2007 3:42:16 AM]
interview-arena.h file:/* -*-c-*- * * Memory allocation abstraction * */#ifndef ARENA_H#define ARENA_H#ifdef __cplusplus extern "C" {#endif/*----- Header files ------------------------------------------------------*/#include <stddef.h>/*----- Data structures ---------------------------------------------------*/typedef struct nf_arena { const struct nf_arena_ops *ops;} nf_arena;/* aalloc, afree and arealloc work analogously to malloc, free and realloc */typedef struct nf_arena_ops { void *(*aalloc)(nf_arena *a, size_t sz); void (*afree)(nf_arena *a, void *p); void *(*arealloc)(nf_arena *a, void *p, size_t sz); void (*adestroy)(nf_arena *a);} nf_arena_ops;#ifndef DISABLE_NONNFtypedef nf_arena arena;typedef nf_arena_ops arena_ops;#endif/*----- Standard arena: uses @malloc@/@free@ ------------------------------*/extern nf_arena nf_arena_std;/*----- Handy macros ------------------------------------------------------*/#define aALLOC(a, sz) ((a)->ops->aalloc((a), (sz)))#define aFREE(a, p) ((a)->ops->afree((a), (p)))#define aREALLOC(a, p, sz) ((a)->ops->arealloc((a), (p), (sz)))#define aNEW(a, p) ((p)= (a)->ops->aalloc((a), sizeof(*p)))/* All of the above evaluate their first argument multiple times. *//*----- That's all, folks -------------------------------------------------*/#ifdef __cplusplus }#endif#endif
[Edited by - jolyqr on October 23, 2007 3:43:17 AM]
Of course, I'm not asking you to do it for me.
But I will post what I've done so far and I would like you to give me tips and advices.
Cheers
But I will post what I've done so far and I would like you to give me tips and advices.
Cheers
Tip #4: Write your unit tests *first*, using the ample details provided. If you can do that, the rest is easy.
there is what I've done. i find this assignment pretty difficult...
int nf_dstr_init(nf_dstr *d, nf_arena *a);{ d->a=NULL; d->buf=NULL; d->len=sizeof(d); d->sz= d->st=0; return ;}void nf_dstr_zero(nf_dstr *d, nf_arena *a){ d->st=1;}void nf_dstr_destroy(nf_dstr *d){ free(sizeof(d));}
they're lots of things i don't get.
i don't get what are the purposes of :
-nf_arena
-size_t (where is its implementation?)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement