Oh my jod

This commit is contained in:
Sasha Koshka 2023-11-07 03:13:13 -05:00
parent 20dca2d2e4
commit e57e93c8b6
2 changed files with 50 additions and 6 deletions

View File

@ -9,14 +9,26 @@
variable length strings and files. */ variable length strings and files. */
typedef struct _XmdBuffer XmdBuffer; typedef struct _XmdBuffer XmdBuffer;
/* XmdBufferNew creates a new buffer with the specified element size. */ /* XmdBufferNew creates a new buffer that contains the specified type. */
XmdBuffer *XmdBufferNew (Cardinal size); #define XmdBufferNew(type) _XmdBufferNew(sizeof(type));
XmdBuffer *_XmdBufferNew (Cardinal size);
/* XmdBufferPush adds a new element to the buffer. */ /* XmdBufferPush adds a new element to the buffer. */
void XmdBufferPush (XmdBuffer *buffer, void *element); void XmdBufferPush (XmdBuffer *buffer, void *element);
/* XmdBufferPop copies the last element into element, and removes it from the
buffer. */
void XmdBufferPop (XmdBuffer *buffer, void *element);
/* XmdBufferPoke sets a single element of the buffer. */
void XmdBufferPoke (XmdBuffer *buffer, Cardinal index, void *element);
/* XmdBufferPeek copies the value of a single element of the buffer into
element. */
void XmdBufferPeek (XmdBuffer *buffer, Cardinal index, void *element);
/* XmdBufferBreak frees the buffer without freeing its data. Its data is /* XmdBufferBreak frees the buffer without freeing its data. Its data is
returned and must be freed manually at some point. */ returned and must be freed manually using XtFree() at some point. */
void *XmdBufferBreak (XmdBuffer *buffer); void *XmdBufferBreak (XmdBuffer *buffer);
/* XmdBufferFree frees the buffer and any data associated with it. */ /* XmdBufferFree frees the buffer and any data associated with it. */

View File

@ -16,12 +16,19 @@ static void XmdBufferResize (XmdBuffer *buffer, Cardinal length);
static void XmdBufferFit (XmdBuffer *buffer); static void XmdBufferFit (XmdBuffer *buffer);
static void *XmdBufferOffset (XmdBuffer *buffer, Cardinal index); static void *XmdBufferOffset (XmdBuffer *buffer, Cardinal index);
XmdBuffer *XmdBufferNew (Cardinal size) { XmdBuffer *_XmdBufferNew (Cardinal size) {
XmdBuffer *buffer = XtNew(XmdBuffer); XmdBuffer *buffer = XtNew(XmdBuffer);
if (buffer == NULL) return NULL;
buffer->size = size; buffer->size = size;
buffer->length = 0; buffer->length = 0;
buffer->capacity = XmdBUFFER_INITIAL_CAPACITY; buffer->capacity = XmdBUFFER_INITIAL_CAPACITY;
buffer->data = XtCalloc(buffer->capacity, buffer->size); buffer->data = XtCalloc(buffer->capacity, buffer->size);
if (buffer->data == NULL) {
XmdBufferFree(buffer);
return NULL;
}
XmdBufferFit(buffer); XmdBufferFit(buffer);
return buffer; return buffer;
} }
@ -46,11 +53,34 @@ void *XmdBufferOffset (XmdBuffer *buffer, Cardinal index) {
} }
void XmdBufferPush (XmdBuffer *buffer, void *element) { void XmdBufferPush (XmdBuffer *buffer, void *element) {
Cardinal index = buffer->length;
XmdBufferResize(buffer, buffer->length + 1); XmdBufferResize(buffer, buffer->length + 1);
XmdBufferPoke(buffer, index, element);
}
void XmdBufferPop (XmdBuffer *buffer, void *element) {
Cardinal index = buffer->length - 1;
XmdBufferPeek(buffer, index, element);
XmdBufferResize(buffer, index);
}
void XmdBufferPoke (XmdBuffer *buffer, Cardinal index, void *element) {
if (index > buffer->length) return;
memcpy ( memcpy (
XmdBufferOffset(buffer, buffer->length - 1), XmdBufferOffset(buffer, index),
element, element,
buffer->size); buffer->size);
char test;
XmdBufferPeek(buffer, index, &test);
}
void XmdBufferPeek (XmdBuffer *buffer, Cardinal index, void *element) {
if (index > buffer->length) return;
memcpy (
element,
XmdBufferOffset(buffer, index),
buffer->size);
} }
void *XmdBufferBreak (XmdBuffer *buffer) { void *XmdBufferBreak (XmdBuffer *buffer) {
@ -60,6 +90,8 @@ void *XmdBufferBreak (XmdBuffer *buffer) {
} }
void XmdBufferFree (XmdBuffer *buffer) { void XmdBufferFree (XmdBuffer *buffer) {
XtFree(buffer->data); if (buffer->data != NULL) {
XtFree(buffer->data);
}
XtFree((char *)(buffer)); XtFree((char *)(buffer));
} }