nsgenbind
Loading...
Searching...
No Matches
output.c
Go to the documentation of this file.
1/* generated output implementation
2 *
3 * This file is part of nsgenbind.
4 * Licensed under the MIT License,
5 * http://www.opensource.org/licenses/mit-license.php
6 * Copyright 2019 Vincent Sanders <vince@netsurf-browser.org>
7 */
8
9#include <stdio.h>
10#include <stdarg.h>
11#include <stdlib.h>
12#include <string.h>
13
14#include "utils.h"
15#include "output.h"
16
17struct opctx {
18 char *filename;
19 FILE *outf;
20 unsigned int lineno;
21};
22
23int output_open(const char *filename, struct opctx **opctx_out)
24{
25 struct opctx *opctx;
26
27 opctx = malloc(sizeof(struct opctx));
28 if (opctx == NULL) {
29 return -1;
30 }
31
32 opctx->filename = strdup(filename);
33 if (opctx->filename == NULL) {
34 free(opctx);
35 return -1;
36 }
37
38 /* open output file */
40 if (opctx->outf == NULL) {
42 free(opctx);
43 return -1;
44 }
45
46 opctx->lineno = 2;
47 *opctx_out = opctx;
48
49 return 0;
50}
51
53{
54 int res;
57 free(opctx);
58 return res;
59}
60
64static char output_buffer[128*1024];
65
66int outputf(struct opctx *opctx, const char *fmt, ...)
67{
68 va_list ap;
69 int res;
70 int idx;
71
72 va_start(ap, fmt);
73 res = vsnprintf(output_buffer, sizeof(output_buffer), fmt, ap);
74 va_end(ap);
75
76 /* account for newlines in output */
77 for (idx = 0; idx < res; idx++) {
78 if (output_buffer[idx] == '\n') {
79 opctx->lineno++;
80 }
81 }
82
83 fwrite(output_buffer, 1, res, opctx->outf);
84
85 return res;
86}
87
88int outputc(struct opctx *opctx, int c)
89{
90 if (c == '\n') {
91 opctx->lineno++;
92 }
93 fputc(c, opctx->outf);
94
95 return 0;
96}
97
99{
100 int res;
101 res = fprintf(opctx->outf,
102 "#line %d \"%s\"\n",
104 opctx->lineno++;
105 return res;
106}
DOMString filename
Definition html.idl:1561
void * malloc(YYSIZE_T)
void free(void *)
int outputc(struct opctx *opctx, int c)
Definition output.c:88
int output_line(struct opctx *opctx)
Definition output.c:98
int outputf(struct opctx *opctx, const char *fmt,...)
Definition output.c:66
int output_close(struct opctx *opctx)
Definition output.c:52
int output_open(const char *filename, struct opctx **opctx_out)
Definition output.c:23
Definition output.c:17
char * filename
Definition output.c:18
FILE * outf
Definition output.c:19
unsigned int lineno
Definition output.c:20
FILE * genb_fopen_tmp(const char *fname)
Definition utils.c:70
int genb_fclose_tmp(FILE *filef_tmp, const char *fname)
Definition utils.c:93