]> git.draconx.ca Git - gob-dx.git/blob - src/out.c
b6371f6ad1c63e936ad10d1e0135d122708e9326
[gob-dx.git] / src / out.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <glib.h>
4
5 #include "main.h"
6
7 #include "out.h"
8
9 int outline = 1;
10 static gboolean in_out = TRUE;
11 int outhline = 1;
12 static gboolean in_outh = TRUE;
13 int outphline = 1;
14 static gboolean in_outph = TRUE;
15
16 extern char file_sep;
17
18 static int
19 strchrcnt(char *s, char c)
20 {
21         int cnt = 0;
22         for(;*s;s++)
23                 if(*s == c)
24                         cnt++;
25         return cnt;
26 }
27
28 void
29 out_printf(FILE *fp, const char *format,...)
30 {
31         va_list ap;
32         char *s;
33
34         if (no_write)
35                 return;
36
37         va_start(ap, format);
38         s = g_strdup_vprintf(format, ap);
39         va_end(ap);
40
41         if(fp == out)
42                 outline += strchrcnt(s, '\n');
43         else if(fp == outh)
44                 outhline += strchrcnt(s, '\n');
45         else if(fp == outph)
46                 outphline += strchrcnt(s, '\n');
47         else
48                 g_assert_not_reached();
49
50         fprintf(fp, "%s", s);
51         g_free(s);
52 }
53
54 void
55 out_addline_infile(FILE *fp, int line)
56 {
57         if(no_lines || no_write)
58                 return;
59
60         if(fp == out) {
61                 outline++;
62                 in_out = FALSE;
63         } else if(fp == outh) {
64                 outhline++;
65                 in_outh = FALSE;
66         } else if(fp == outph) {
67                 outphline++;
68                 in_outph = FALSE;
69         } else
70                 g_assert_not_reached();
71
72         fprintf(fp, "#line %d \"%s\"\n", line, filename);
73 }
74
75 void
76 out_addline_outfile(FILE *fp)
77 {
78         if(no_lines || no_write)
79                 return;
80
81         if(fp == out) {
82                 if(in_out) return;
83                 outline++;
84                 if(!for_cpp)
85                         fprintf(fp,"#line %d \"%s.c\"\n",outline,filebase);
86                 else
87                         fprintf(fp,"#line %d \"%s.cc\"\n",outline,filebase);
88                 in_out = TRUE;
89         } else if(fp == outh) {
90                 if(in_outh) return;
91                 outhline++;
92                 fprintf(fp,"#line %d \"%s.h\"\n",outhline,filebase);
93                 in_outh = TRUE;
94         } else if(fp == outph) {
95                 if(in_outph) return;
96                 outphline++;
97                 fprintf(fp,"#line %d \"%s%cprivate.h\"\n",outphline,filebase,file_sep);
98                 in_outph = TRUE;
99         } else
100                 g_assert_not_reached();
101 }