The qDecoder Project: C/C++ Library

- Main Page
- Data Structures
- Files

- File List
- Globals

# qObstack.c File Reference

qDecoder implementation of GNU obstack More...

## Functions
Q_OBSTACK * | qObstackInit (void)
| Initialize object-stack.
bool | qObstackGrow (Q_OBSTACK *obstack, const void *object, size_t size)
| Stack object.
bool | qObstackGrowStr (Q_OBSTACK *obstack, const char *str)
| Stack string.
bool | qObstackGrowStrf (Q_OBSTACK *obstack, const char *format,...)
| Stack formatted string.
void * | qObstackFinish (Q_OBSTACK *obstack)
| Stack formatted string.
void * | qObstackGetFinal (Q_OBSTACK *obstack)
| Finalize objects and get merged single final object pointer.
size_t | qObstackGetSize (Q_OBSTACK *obstack)
| Get stacked objects size.
int | qObstackGetNum (Q_OBSTACK *obstack)
| Get the number of stacked objects.
bool | qObstackFree (Q_OBSTACK *obstack)
| De-allocate obstack.

## Detailed Description

qDecoder implementation of GNU obstack

An qObstack is a pool of memory containing a stack of objects. An qObstack can contain any number of objects of any size.

       [Code sample - String]
       Q_OBSTACK *obstack;

       char *final;
       char *tmp = "CDE";

       // get new obstack
       obstack = qObstackInit();

       // stack
       qObstackGrowStr(obstack, "AB");              // no need to supply size
       qObstackGrowStrf(obstack, "%s", tmp);        // for formatted string
       qObstackGrow(obstack, "FGH", 3);     // same effects as above but this can
                                            // be used for object or binary

       // final
       final = (char *)qObstackFinish(obstack);

       // print out
       printf("Final string = %s\n", final);
       printf("Total Size = %d, Number of Objects = %d\n", qObstackGetSize(obstack), qObstackGetNum(obstack));

       qObstackFree(obstack);

       [Result]
       Final string = ABCDEFGH
       Total Size = 8, Number of Objects = 3

       [Code sample - Object]
       struct sampleobj {
            int     num;
            char    str[10];
       };

       Q_OBSTACK *obstack;
       int i;

       // sample object
       struct sampleobj obj;
       struct sampleobj *final;

       // get new obstack
       obstack = qObstackInit();

       // stack
       for(i = 0; i < 3; i++) {
            // filling object with sample data
            obj.num  = i;
            sprintf(obj.str, "hello%d", i);

            // stack
            qObstackGrow(obstack, (void *)&obj, sizeof(struct sampleobj));
       }

       // final
       final = (struct sampleobj *)qObstackFinish(obstack);

       // print out
       qContentType("text/plain");
       for(i = 0; i < qObstackGetNum(obstack); i++) {
            printf("Object%d final = %d, %s\n", i+1, final[i].num, final[i].str);
       }
       printf("Total Size = %d, Number of Objects = %d\n", qObstackGetSize(obstack), qObstackGetNum(obstack));

       qObstackFree(obstack);

       [Result]
       Object1 final = 0, hello0
       Object2 final = 1, hello1
       Object3 final = 2, hello2
       Total Size = 48, Number of Objects = 3

## Function Documentation

Q_OBSTACK* qObstackInit | ( | void |  | ) |

Initialize object-stack.

Returns:
  a pointer of Q_OBSTACK structure

       // allocate memory
       Q_OBSTACK *obstack = qObstackInit();
       qObstackFree(obstack);

bool qObstackGrow | ( | Q_OBSTACK * | obstack,
|  | const void * | object,
|  | size_t | size |
| ) |  |  |

Stack object.

Parameters:

  | obstack | a pointer of Q_OBSTACK
  | object | a pointer of object data
  | size | size of object

Returns:
  true if successful, otherwise returns false

bool qObstackGrowStr | ( | Q_OBSTACK * | obstack,
|  | const char * | str |
| ) |  |  |

Stack string.

Parameters:

  | obstack | a pointer of Q_OBSTACK
  | str | a pointer of string

Returns:
  true if successful, otherwise returns false

bool qObstackGrowStrf | ( | Q_OBSTACK * | obstack,
|  | const char * | format,
|  |  | ... |
| ) |  |  |

Stack formatted string.

Parameters:

  | obstack | a pointer of Q_OBSTACK
  | format | string format

Returns:
  true if successful, otherwise returns false

void* qObstackFinish | ( | Q_OBSTACK * | obstack | ) |

Stack formatted string.

Parameters:

  | obstack | a pointer of Q_OBSTACK

Returns:
  true if successful, otherwise returns false

void* qObstackGetFinal | ( | Q_OBSTACK * | obstack | ) |

Finalize objects and get merged single final object pointer.

Parameters:

  | obstack | a pointer of Q_OBSTACK

Returns:
  a pointer of finally merged object, otherwise returns NULL

size_t qObstackGetSize | ( | Q_OBSTACK * | obstack | ) |

Get stacked objects size.

Parameters:

  | obstack | a pointer of Q_OBSTACK

Returns:
  a pointer of finally merged object, otherwise returns NULL

int qObstackGetNum | ( | Q_OBSTACK * | obstack | ) |

Get the number of stacked objects.

Parameters:

  | obstack | a pointer of Q_OBSTACK

Returns:
  a number of stacked objects, otherwise returns 0

bool qObstackFree | ( | Q_OBSTACK * | obstack | ) |

De-allocate obstack.

Parameters:

  | obstack | a pointer of Q_OBSTACK

Returns:
  true if successful, otherwise returns false

[Home] [About] [Examples] [Changes] [Download] [SVN Repository] [Install] [Reference]