summaryrefslogtreecommitdiff
path: root/src/jsapi-binding.c
blob: 51a0df701e0e94d27f28c6ffdc681648880762fb (plain)
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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "jsapi-binding.h"

#define HDR_COMMENT_PREABLE "/* Generated by nsgenjsapi\n"

static FILE *outfile = NULL;
static char *hdr_comments = NULL;
static bool hdr_comments_output = false;

int genjsbind_outputopen(char *outfilename)
{
        /* open output file */
	if (outfilename == NULL) {
		outfile = stdout;
	} else {
		outfile = fopen(outfilename, "w");
	}

	if (!outfile) {
		fprintf(stderr, "Error opening output %s: %s\n",
			outfilename, 
			strerror(errno));
		return 4;
	}

	hdr_comments = strdup(HDR_COMMENT_PREABLE);
	return 0;
}

int genjsbind_outputclose()
{
	fclose(outfile);
	return 0;
}

int genjsbind_header_comment(char *comment)
{
	char *fullstr;
	int fulllen;
	fulllen = strlen(hdr_comments) + strlen(comment) + 2;
	fullstr = malloc(fulllen);
	snprintf(fullstr, fulllen, "%s\n%s", hdr_comments , comment);
	free(hdr_comments);
	free(comment);
	hdr_comments  = fullstr;

	if (hdr_comments_output) {
		fprintf(stderr, "Warning: adding header comments after output already started\n");
	}
	return 0;
}

int genjsbind_output_interface(const char *interface)
{
	if (!hdr_comments_output) {
		fprintf(outfile, "%s\n*/\n\n", hdr_comments);
		hdr_comments_output = true;
	}
	fprintf(outfile, "/* interface %s */\n\n", interface);
	return 0;
}