-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp.h
104 lines (87 loc) · 2.37 KB
/
mp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
*
* mp.h
* Global declarations and definitions
*
*/
#ifndef MP_H
#define MP_H
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#define MP_BOOL int
#define MP_TRUE 1
#define MP_FALSE 0
#define MP_OK 1
#define MP_BAD 0
#define MP_END -1
#define MP_PRINT_ERROR(frmt, ...) (printf(("Error: " frmt "\n") __VA_OPT__(,) __VA_ARGS__))
#define MP_PRINT_WARNING(frmt, ...) (printf(("Warning: " frmt "\n") __VA_OPT__(,) __VA_ARGS__))
#define MP_PRINT_PROCESS_ERROR(pe, frmt, ...) (MP_PRINT_ERROR(frmt " at offset %u (ln:%u col:%u), while processing file \"%s\"" __VA_OPT__(,) __VA_ARGS__, (pe)->state.srcofs + 1, (pe)->state.ln, (pe)->state.srcofs - (pe)->state.lnsidx + 1, (pe)->fn))
// file
int mp_file_read (FILE* f, const char* filename, char* buff, char** optBuff, long* flenPtr, long offset, size_t readlen, MP_BOOL nullterm);
int mp_file_write (FILE* f, const char* filename, const char* buff, size_t len);
struct mp_String {
char* buff;
size_t len;
};
/*
*
* process
*
*/
enum mp_DelimWhat {
MP_DELIM_ARGS,
MP_DELIM_PARAMS
};
struct mp_Macro {
const char* name;
size_t namelen;
char* def;
size_t deflen;
MP_BOOL isfunc;
struct mp_String* params;
size_t paramc;
MP_BOOL exp; // expanded?
};
struct mp_ProcessState {
size_t srcofs;
size_t outofs;
MP_BOOL eof;
size_t ln;
size_t lnsidx; // line start index
MP_BOOL isinstr;
size_t nllen;
const char* nlstr;
const char* word;
size_t wlen;
const char* writestart;
};
struct mp_ProcessContext {
const char* src;
char* outBuff;
size_t readlen;
int endch;
};
struct mp_ProcessEnv {
const char* fn;
struct mp_ProcessState state;
struct mp_ProcessContext ctx;
size_t macrostop;
struct mp_Macro macros[MP_MAX_MACROS];
size_t stringstop;
struct mp_String strings[MP_MAX_MACROS];
size_t tofreetop;
char* tofree[MP_MAX_MACRO_EXPS];
};
#define MP_ENDCH_NONE SCHAR_MIN - 1
#define MP_ENDCH_NL SCHAR_MIN - 2
void mp_PE_init (struct mp_ProcessEnv* pe, const char* src, const char* fn, char* outBuff, size_t outBuffLen, size_t readlen, int endch);
void mp_PE_free (struct mp_ProcessEnv* pe);
int mp_process (struct mp_ProcessEnv* pe);
// cstr
MP_BOOL mp_cstr_eq (const char* str1, size_t len1, const char* str2, size_t len2);
#endif // MP_H