-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserialize.c
64 lines (53 loc) · 1.3 KB
/
serialize.c
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
#include <stdio.h>
#include <string.h>
#include "postgres.h"
#include "lib/stringinfo.h"
#include "kitesdk.h"
#include "kite_fdw.h"
/*
typedef struct kite_handle_t kite_handle_t;
typedef struct kite_filespec_t {
char fmt[MAX_FILESPEC_FMT_LEN];
union {
struct {
char delim;
char quote;
char escape;
int8_t header_line;
char nullstr[MAX_CSV_NULLSTR_LEN];
} csv;
} u;
} kite_filespec_t;
*/
extern void filespec_serialize(kite_filespec_t *fspec, StringInfo s) {
appendStringInfo(s, "%s\n", fspec->fmt);
appendStringInfo(s, "%c", fspec->u.csv.delim);
appendStringInfo(s, "%c", fspec->u.csv.quote);
appendStringInfo(s, "%c", fspec->u.csv.escape);
appendStringInfo(s, "%c", (fspec->u.csv.header_line ? 'T' : 'F'));
appendStringInfo(s, "%s", fspec->u.csv.nullstr);
}
extern kite_filespec_t *filespec_deserialize(char *src) {
kite_filespec_t *fspec = (kite_filespec_t *) palloc0(sizeof(kite_filespec_t));
char *s;
char *p = pstrdup(src);
int len = 0;
s = strchr(p, '\n');
if (!s) {
return 0;
}
len = s-p;
strncpy(fspec->fmt, p, s-p);
fspec->fmt[len] = 0;
s++;
fspec->u.csv.delim = *s;
s++;
fspec->u.csv.quote = *s;
s++;
fspec->u.csv.escape = *s;
s++;
fspec->u.csv.header_line = (*s == 'T' ? 1 : 0);
s++;
strcpy(fspec->u.csv.nullstr, s);
return fspec;
}