jack-compiler/misc/util.h

38 lines
921 B
C
Raw Normal View History

2020-12-20 13:58:10 -05:00
#ifndef UTIL_H
#define UTIL_H
#include <stdio.h>
2020-12-22 11:18:54 -05:00
#include <stdbool.h>
2020-12-20 13:58:10 -05:00
2020-12-21 13:05:49 -05:00
/* util
* Random utilities. */
2020-12-20 13:58:10 -05:00
2020-12-21 16:11:23 -05:00
// Macros
#define eprintf(...) fprintf (stderr, __VA_ARGS__)
#define count(array, type) ((sizeof(array)) / (sizeof(type)))
#define strcount(array) count(array, char*)
2020-12-22 14:00:49 -05:00
#define mkstrlist(name, array) STRINGARRAY name = { .items = array, .size = strcount(array) }
2020-12-21 16:11:23 -05:00
2020-12-21 13:05:49 -05:00
typedef struct stringlist {
2020-12-27 16:52:28 -05:00
char* content;
2020-12-21 13:05:49 -05:00
struct stringlist* next;
} STRINGLIST;
2020-12-20 13:58:10 -05:00
2020-12-21 19:50:55 -05:00
typedef struct {
const char** items;
const int size;
} STRINGARRAY;
char* heapstr(const char* str, int len);
char* ezheapstr(const char* str);
2020-12-21 13:05:49 -05:00
int countplaces(int n);
2020-12-20 13:58:10 -05:00
char* itoa(int i);
2020-12-21 19:50:55 -05:00
void* copy(void* v, int size);
2020-12-21 13:05:49 -05:00
2020-12-24 14:22:22 -05:00
STRINGLIST* onestr(const char* str);
2020-12-21 19:50:55 -05:00
STRINGLIST* initstrlist(const char** strs, int count);
2020-12-21 13:05:49 -05:00
void printstrlist(STRINGLIST* strlist, FILE* stream);
void freestrlist(STRINGLIST* strlist);
2020-12-22 11:18:54 -05:00
bool existsinarray(STRINGARRAY* arr, const char* item);
2020-12-20 13:58:10 -05:00
#endif