qLibc
qgrow.c File Reference

Grow container that handles growable objects. More...

Go to the source code of this file.

Functions

qgrow_t * qgrow (int options)
 Initialize grow.
 
bool qgrow_add (qgrow_t *grow, const void *data, size_t size)
 qgrow->add(): Stack object
 
bool qgrow_addstr (qgrow_t *grow, const char *str)
 qgrow->addstr(): Stack string
 
bool qgrow_addstrf (qgrow_t *grow, const char *format,...)
 qgrow->addstrf(): Stack formatted string
 
size_t qgrow_size (qgrow_t *grow)
 qgrow->size(): Returns the number of elements in this grow.
 
size_t qgrow_datasize (qgrow_t *grow)
 qgrow->datasize(): Returns the sum of total element size in this grow.
 
void * qgrow_toarray (qgrow_t *grow, size_t *size)
 qgrow->toarray(): Returns the serialized chunk containing all the elements in this grow.
 
char * qgrow_tostring (qgrow_t *grow)
 qgrow->tostring(): Returns a string representation of this grow, containing string representation of each element.
 
void qgrow_clear (qgrow_t *grow)
 qgrow->clear(): Removes all of the elements from this grow.
 
bool qgrow_debug (qgrow_t *grow, FILE *out)
 qgrow->debug(): Print out stored elements for debugging purpose.
 
void qgrow_free (qgrow_t *grow)
 qgrow->free(): De-allocate grow
 

Detailed Description

Grow container that handles growable objects.

qgrow container is a grow implementation. It implements a growable array of objects and it extends qlist container that allow a linked-list to be treated as a grow.

[Code sample - Object]
qgrow_t *grow = qgrow(QGROW_THREADSAFE);
// add elements
grow->addstr(grow, "AB"); // no need to supply size
grow->addstrf(grow, "%d", 12); // for formatted string
grow->addstr(grow, "CD");
// get the chunk as a string
char *final = grow->tostring(grow);
// print out
printf("Number of elements = %zu\n", grow->size(grow));
printf("Final string = %s\n", final);
// release
free(final);
grow->free(grow);
[Result]
Number of elements = 3
Final string = AB12CD
qgrow_t * qgrow(int options)
Initialize grow.
Definition qgrow.c:134
[Code sample - Object]
// sample object
struct sampleobj {
int num;
char str[10];
};
// get new grow
qgrow_t *grow = qgrow();
// add objects
int i;
struct sampleobj obj;
for(i = 0; i < 3; i++) {
// filling object with sample data
obj.num = i;
sprintf(obj.str, "hello%d", i);
// stack
grow->add(grow, (void *)&obj, sizeof(struct sampleobj));
}
// final
struct sampleobj *final;
final = (struct sampleobj *)grow->toarray(grow, NULL);
// print out
printf("Number of Objects = %zu\n", grow->size(grow));
for(i = 0; i < grow->size(grow); i++) {
printf("Object%d %d, %s\n", i+1, final[i].num, final[i].str);
}
// release
free(final);
grow->free(grow);
[Result]
Number of Objects = 3
Object1 0, hello0
Object2 1, hello1
Object3 2, hello2

Definition in file qgrow.c.

Function Documentation

◆ qgrow()

qgrow_t * qgrow ( int  options)

Initialize grow.

Parameters
optionscombination of initialization options.
Returns
qgrow_t container pointer.
Return values
errnowill be set in error condition.
  • ENOMEM : Memory allocation failure.
// allocate memory
qgrow_t *grow = qgrow(QGROW_THREADSAFE);
grow->free(grow);
Note
Available options:
  • QGROW_THREADSAFE - make it thread-safe.

Definition at line 134 of file qgrow.c.

◆ qgrow_add()

bool qgrow_add ( qgrow_t *  grow,
const void *  data,
size_t  size 
)

qgrow->add(): Stack object

Parameters
growqgrow_t container pointer.
objecta pointer of object data
sizesize of object
Returns
true if successful, otherwise returns false
Return values
errnowill be set in error condition.
  • EINVAL : Invalid argument.
  • ENOMEM : Memory allocation failure.

Definition at line 178 of file qgrow.c.

◆ qgrow_addstr()

bool qgrow_addstr ( qgrow_t *  grow,
const char *  str 
)

qgrow->addstr(): Stack string

Parameters
growqgrow_t container pointer.
stra pointer of string
Returns
true if successful, otherwise returns false
Return values
errnowill be set in error condition.
  • EINVAL : Invalid argument.
  • ENOMEM : Memory allocation failure.

Definition at line 193 of file qgrow.c.

◆ qgrow_addstrf()

bool qgrow_addstrf ( qgrow_t *  grow,
const char *  format,
  ... 
)

qgrow->addstrf(): Stack formatted string

Parameters
growqgrow_t container pointer.
formatstring format
Returns
true if successful, otherwise returns false
Return values
errnowill be set in error condition.
  • EINVAL : Invalid argument.
  • ENOMEM : Memory allocation failure.

Definition at line 208 of file qgrow.c.

◆ qgrow_size()

size_t qgrow_size ( qgrow_t *  grow)

qgrow->size(): Returns the number of elements in this grow.

Parameters
growqgrow_t container pointer.
Returns
the number of elements in this grow.

Definition at line 229 of file qgrow.c.

◆ qgrow_datasize()

size_t qgrow_datasize ( qgrow_t *  grow)

qgrow->datasize(): Returns the sum of total element size in this grow.

Parameters
growqgrow_t container pointer.
Returns
the sum of total element size in this grow.

Definition at line 241 of file qgrow.c.

◆ qgrow_toarray()

void * qgrow_toarray ( qgrow_t *  grow,
size_t *  size 
)

qgrow->toarray(): Returns the serialized chunk containing all the elements in this grow.

Parameters
growqgrow_t container pointer.
sizeif size is not NULL, merged object size will be stored.
Returns
a pointer of finally merged elements(malloced), otherwise returns NULL
Return values
errnowill be set in error condition.
  • ENOENT : empty.
  • ENOMEM : Memory allocation failure.

Definition at line 258 of file qgrow.c.

◆ qgrow_tostring()

char * qgrow_tostring ( qgrow_t *  grow)

qgrow->tostring(): Returns a string representation of this grow, containing string representation of each element.

Parameters
growqgrow_t container pointer.
Returns
a pointer of finally merged strings(malloced), otherwise returns NULL
Return values
errnowill be set in error condition.
  • ENOENT : empty.
  • ENOMEM : Memory allocation failure.
Note
Return string is always terminated by '\0'.

Definition at line 276 of file qgrow.c.

◆ qgrow_clear()

void qgrow_clear ( qgrow_t *  grow)

qgrow->clear(): Removes all of the elements from this grow.

Parameters
growqgrow_t container pointer.

Definition at line 285 of file qgrow.c.

◆ qgrow_debug()

bool qgrow_debug ( qgrow_t *  grow,
FILE *  out 
)

qgrow->debug(): Print out stored elements for debugging purpose.

Parameters
growqgrow_t container pointer.
outoutput stream FILE descriptor such like stdout, stderr.
Returns
true if successful, otherwise returns false.
Return values
errnowill be set in error condition.
  • EIO : Invalid output stream.

Definition at line 299 of file qgrow.c.

◆ qgrow_free()

void qgrow_free ( qgrow_t *  grow)

qgrow->free(): De-allocate grow

Parameters
growqgrow_t container pointer.

Definition at line 308 of file qgrow.c.