forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
At some point this should be optimized further at the moment there is some 20 byte overhead for each entered key.
- Loading branch information
Showing
10 changed files
with
179 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "buffer.h" | ||
#include "util.h" | ||
|
||
#define BUF_SIZE 1024 | ||
|
||
bool buffer_alloc(Buffer *buf, size_t size) { | ||
if (size < BUF_SIZE) | ||
size = BUF_SIZE; | ||
if (buf->size < size) { | ||
if (buf->size > 0) | ||
size *= 2; | ||
buf->data = realloc(buf->data, size); | ||
if (!buf->data) { | ||
buf->size = 0; | ||
buf->len = 0; | ||
return false; | ||
} | ||
buf->size = size; | ||
} | ||
return true; | ||
} | ||
|
||
void buffer_truncate(Buffer *buf) { | ||
buf->len = 0; | ||
} | ||
|
||
void buffer_free(Buffer *buf) { | ||
if (!buf) | ||
return; | ||
free(buf->data); | ||
buf->data = NULL; | ||
buf->len = 0; | ||
buf->size = 0; | ||
} | ||
|
||
bool buffer_put(Buffer *buf, void *data, size_t len) { | ||
if (!buffer_alloc(buf, len)) | ||
return false; | ||
memcpy(buf->data, data, len); | ||
buf->len = len; | ||
return true; | ||
} | ||
|
||
bool buffer_append(Buffer *buf, void *data, size_t len) { | ||
size_t rem = buf->size - buf->len; | ||
if (len > rem && !buffer_alloc(buf, buf->size + len - rem)) | ||
return false; | ||
memcpy(buf->data + buf->len, data, len); | ||
buf->len += len; | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef BUFFER_H | ||
#define BUFFER_H | ||
|
||
#include <stddef.h> | ||
#include <stdbool.h> | ||
#include "text.h" | ||
|
||
typedef struct { | ||
char *data; /* NULL if empty */ | ||
size_t len; /* current length of data */ | ||
size_t size; /* maximal capacity of the buffer */ | ||
} Buffer; | ||
|
||
void buffer_free(Buffer *buf); | ||
bool buffer_alloc(Buffer *buf, size_t size); | ||
void buffer_truncate(Buffer *buf); | ||
bool buffer_put(Buffer *buf, void *data, size_t len); | ||
bool buffer_append(Buffer *buf, void *data, size_t len); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef MACRO_H | ||
#define MACRO_H | ||
|
||
#include "buffer.h" | ||
|
||
typedef Buffer Macro; | ||
#define macro_free buffer_free | ||
#define macro_reset buffer_truncate | ||
#define macro_append buffer_append | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters