File: jx_export.c

package info (click to toggle)
cctools 9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,624 kB
  • sloc: ansic: 192,539; python: 20,827; cpp: 20,199; sh: 11,719; perl: 4,106; xml: 3,688; makefile: 1,224
file content (256 lines) | stat: -rw-r--r-- 5,981 bytes parent folder | download | duplicates (2)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
Copyright (C) 2015- The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/

#include "jx_export.h"
#include "jx_print.h"

#include "stringtools.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

static char * unquoted_string( struct jx *j )
{
	char *str;
	if(j->type==JX_STRING) {
		str = strdup(j->u.string_value);
	} else {
		str = jx_print_string(j);
	}
	return str;
}

/*
Export a JX object as environment variables in bash format.
*/

void jx_export_shell( struct jx *j, FILE *stream )
{
	struct jx_pair *p;
	for(p=j->u.pairs;p;p=p->next) {
		char *str = unquoted_string(p->value);
		fprintf(stream,"export %s=%s\n",p->key->u.string_value,str);
		free(str);
	}
}


/*
The old nvpair format simply has unquoted data following the key.
*/

void jx_export_nvpair( struct jx *j, FILE *stream )
{
	struct jx_pair *p;
	for(p=j->u.pairs;p;p=p->next) {
		char *str = unquoted_string(p->value);
		fprintf(stream,"%s %s\n",p->key->u.string_value,str);
		free(str);
	}
	fprintf(stream,"\n");
}

/*
The old classad format has quoted strings, symbols, booleans, integers, but not objects or arrays.  So, we quote the latter two types.  Individual ads are separated by newlines.
*/

void jx_export_old_classads( struct jx *j, FILE *stream )
{
	struct jx_pair *p;
	for(p=j->u.pairs;p;p=p->next) {
		char *str = jx_print_string(p->value);
		if(p->value->type==JX_OBJECT || p->value->type==JX_ARRAY) {
			fprintf(stream,"%s = \"%s\"\n",p->key->u.string_value,str);
		} else {
			fprintf(stream,"%s = %s\n",p->key->u.string_value,str);
		}
		free(str);
	}
	fprintf(stream,"\n");
}

/*
For XML encoding, we use plain text for atomic types and tags to structure objects and arrays.
*/

void jx_export_xml( struct jx *j, FILE *stream )
{
	struct jx_pair *p;
	struct jx_item *i;

	switch(j->type) {
	case JX_NULL:
		fprintf(stream,"null");
		break;
	case JX_BOOLEAN:
		fprintf(stream,j->u.boolean_value?"true":"false");
		break;
	case JX_INTEGER:
		fprintf(stream,"%lld",(long long)j->u.integer_value);
		break;
	case JX_DOUBLE:
		fprintf(stream,"%lf",j->u.double_value);
		break;
	case JX_STRING:
		fprintf(stream,"%s",j->u.string_value);
		break;
	case JX_SYMBOL:
		fprintf(stream,"%s",j->u.symbol_name);
		break;
	case JX_OBJECT:
		fprintf(stream,"<object>\n");
		for(p=j->u.pairs;p;p=p->next) {
			fprintf(stream,"<pair><key>%s</key>",p->key->u.string_value);
			fprintf(stream,"<value>");
			jx_export_xml(p->value,stream);
			fprintf(stream,"</value></pair>");
		}
		fprintf(stream,"</object>\n");
		break;
	case JX_ARRAY:
		fprintf(stream,"<array>\n");
		for (i = j->u.items; i; i = i->next) {
			fprintf(stream, "<item>");
			jx_export_xml(i->value, stream);
			fprintf(stream,"</item>");
		}
		fprintf(stream,"</array>\n");
		break;
	case JX_OPERATOR:
		fprintf(stream,"<expr>\n");
		jx_print_stream(j,stream);
		fprintf(stream,"</expr>\n");
		break;
	case JX_ERROR:
		fprintf(stream,"<error>\n");
		jx_print_stream(j,stream);
		fprintf(stream,"</error>\n");
		break;
	}
}

/*
New classads are quite similar to json, except that the use of [] and {} is reversed.
*/

void jx_export_new_classads( struct jx *j, FILE *stream )
{
	struct jx_pair *p;
	struct jx_item *i;

	switch(j->type) {
		case JX_OBJECT:
			fprintf(stream,"[\n");
			for(p=j->u.pairs;p;p=p->next) {
				fprintf(stream,"%s=",p->key->u.string_value);
				jx_print_stream(p->value,stream);
				fprintf(stream,";\n");
			}
			fprintf(stream,"]\n");
			break;
		case JX_ARRAY:
			fprintf(stream,"{\n");
			for(i=j->u.items;i;i=i->next) {
				jx_print_stream(i->value,stream);
				if(i->next) fprintf(stream,",");
			}
			fprintf(stream,"}\n");
			break;
		default:
			jx_print_stream(j,stream);
			break;
	}
}

#define COLOR_ONE "#aaaaff"
#define COLOR_TWO "#bbbbbb"

static int color_counter = 0;

static const char *align_string(struct jx_table *h)
{
	if(h->align == JX_TABLE_ALIGN_RIGHT) {
		return "right";
	} else {
		return "left";
	}
}

void jx_export_html_solo(struct jx *j, FILE * stream)
{
	fprintf(stream, "<table bgcolor=%s>\n", COLOR_TWO);
	fprintf(stream, "<tr bgcolor=%s>\n", COLOR_ONE);

	color_counter = 0;

	struct jx_pair *p;
	for(p=j->u.pairs;p;p=p->next) {
		fprintf(stream, "<tr bgcolor=%s>\n", color_counter % 2 ? COLOR_ONE : COLOR_TWO);
		color_counter++;
		fprintf(stream, "<td align=left><b>%s</b>\n", p->key->u.string_value);
		char *str = unquoted_string(p->value);
		if(!strcmp(p->key->u.string_value, "url")) {
			fprintf(stream, "<td align=left><a href=%s>%s</a>\n",str,str);
		} else {
			fprintf(stream, "<td align=left>%s\n",str);
		}
		free(str);
	}
	fprintf(stream, "</table>\n");
}

void jx_export_html_header(FILE * s, struct jx_table *h)
{
	fprintf(s, "<table bgcolor=%s>\n", COLOR_TWO);
	fprintf(s, "<tr bgcolor=%s>\n", COLOR_ONE);
	while(h->name) {
		fprintf(s, "<td align=%s><b>%s</b>\n", align_string(h), h->title);
		h++;
	}
	color_counter = 0;
}

void jx_export_html(struct jx *n, FILE * s, struct jx_table *h)
{
	jx_export_html_with_link(n, s, h, 0, 0);
}

void jx_export_html_with_link(struct jx *n, FILE * s, struct jx_table *h, const char *linkname, const char *linktext)
{
	fprintf(s, "<tr bgcolor=%s>\n", color_counter % 2 ? COLOR_ONE : COLOR_TWO);
	color_counter++;
	while(h->name) {
		struct jx *value = jx_lookup(n,h->name);
		char *text;
		if(value) {
			text = unquoted_string(value);
		} else {
			text = strdup("???");
		}
		fprintf(s, "<td align=%s>", align_string(h));
		if(h->mode == JX_TABLE_MODE_URL) {
			fprintf(s, "<a href=%s>%s</a>\n", text, text);
		} else if(h->mode == JX_TABLE_MODE_METRIC) {
			char line[1024];
			string_metric(atof(text), -1, line);
			fprintf(s, "%sB\n", line);
		} else {
			if(linkname && !strcmp(linkname, h->name)) {
				fprintf(s, "<a href=%s>%s</a>\n", linktext, text);
			} else {
				fprintf(s, "%s\n", text);
			}
		}
		free(text);
		h++;
	}
}

void jx_export_html_footer(FILE * s, struct jx_table *h)
{
	fprintf(s, "</table>\n");
}