File: gui_recode.c

package info (click to toggle)
gretl 2019a-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 53,708 kB
  • sloc: ansic: 367,137; sh: 4,416; makefile: 2,636; cpp: 2,499; xml: 580; perl: 364
file content (294 lines) | stat: -rw-r--r-- 6,772 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 *  gretl -- Gnu Regression, Econometrics and Time-series Library
 *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "gretl.h"
#include "gui_recode.h"

#include <glib/gstdio.h>

/* Used when, e.g. loading a script into a GTK window: the
   script might be in a locale encoding other than UTF-8
   if it was created in a third-party editor.
*/

static gchar *real_my_locale_to_utf8 (const gchar *src,
				      int starting)
{
    static int errcount;
    const gchar *charset = NULL;
    gsize bytes;
    GError *err = NULL;
    GError **errp = NULL;
    gchar *ret;

    if (starting) {
	errcount = 0;
    }

    /* avoid multiple repetition of error message */
    errp = errcount == 0 ? &err : NULL;

    if (g_get_charset(&charset)) {
	/* we're in a UTF-8 locale */
	ret = g_convert(src, -1, "UTF-8", "ISO-8859-15",
			NULL, &bytes, errp);
    } else {
	ret = g_locale_to_utf8(src, -1, NULL, &bytes, errp);
    }

    if (err) {
	errbox(err->message);
	g_error_free(err);
	errcount++;
    }

    return ret;
}

/* wrappers to avoid repeated error messages where one
   will make the point
*/

gchar *my_locale_to_utf8 (const gchar *src)
{
    return real_my_locale_to_utf8(src, 1);
}

gchar *my_locale_to_utf8_next (const gchar *src)
{
    return real_my_locale_to_utf8(src, 0);
}

/* We're not totally sure what charset we're supposed to be converting
   from, but we'll make a guess!
*/

static gchar *gp_locale_to_utf8 (const gchar *src, int starting)
{
    static int errcount;
    static const char *from;
    gsize read, wrote;
    GError *err = NULL;
    gchar *ret;

    if (from == NULL) {
	if (iso_latin_version() == 2) {
#ifdef G_OS_WIN32
	    from = "CP1250";
#else
	    from = "ISO-8859-2";
#endif
	} else {
	    from = "ISO-8859-1";
	}
    }

    if (starting) {
	errcount = 0;
	ret = g_convert(src, -1, "UTF-8", from,
			&read, &wrote, &err);
    } else if (errcount == 0) {
	ret = g_convert(src, -1, "UTF-8", from,
			&read, &wrote, &err);
    } else {
	ret = g_convert(src, -1, "UTF-8", from,
			&read, &wrote, NULL);
    }

    if (err != NULL) {
	errbox(err->message);
	g_error_free(err);
	errcount++;
    }

    return ret;
}

/* If need be, fix up an old, pre-cairo PNG terminal line:
   we come here only if a plot line starts "set term png"
   followed by a space rather than "cairo".
*/

static int maybe_adjust_cairo (char *line)
{
    char *s = line + 12; /* skip "set term png" */
    int ret = 0;

    if (strlen(line) < 512 - 5) {
	/* substitute the preferred cairo PNG term */
	char *p, tmp[512];

	strcpy(tmp, "set term pngcairo");
	s += strspn(s, " ");
	if (!strncmp(s, "truecolor ", 10)) {
	    /* invalid */
	    s += 10;
	}
	if (!strncmp(s, "font", 4)) {
	    /* attempt to fix up old gnuplot font spec */
	    char fname[32], fsize[6];

	    s += 4;
	    s += strspn(s, " ");
	    if (*s != '"') {
		if (sscanf(s, "%31s %5s", fname, fsize) == 2) {
		    strcat(tmp, " font \"");
		    strcat(tmp, fname);
		    strcat(tmp, ",");
		    strcat(tmp, fsize);
		    strcat(tmp, "\" ");
		}
	    }
	    p = strstr(s, " size ");
	    if (p != NULL) {
		strcat(tmp, p + 1);
	    } else {
		strcat(tmp, "\n");
	    }
	} else {
	    /* hope for the best */
	    strcat(tmp, s);
	}
	strcpy(line, tmp);
	ret = 1;
    }

    return ret;
}

static void do_fix_xrange (char *line, int fix, FILE *fp)
{
    double xmin, xmax;
    int n;

    gretl_push_c_numeric_locale();

    n = sscanf(line, "set xrange [%lf:%lf]", &xmin, &xmax);

    /* note: if we fail here, we'll suppress the printing of
       the xrange specification */

    if (n == 2) {
	double offset = 946684800;

	if (fix == 1) {
	    /* old gnuplot to new */
	    xmin += offset;
	    xmax += offset;
	} else {
	    /* new gnuplot to old */
	    xmin -= offset;
	    xmax -= offset;
	}
	fprintf(fp, "set xrange [%.12g:%.12g]\n", xmin, xmax);
    }

    gretl_pop_c_numeric_locale();
}

/* Backward compatibility for gnuplot command files as saved in
   sessions: if the file is non-ascii and non-UTF-8, convert to UTF-8,
   since we have now (2008-01) standardized on UTF-8 as the encoding
   for all gnuplot files that are "internal" to gretl.

   While we're at it, check for a couple of other things.  If we have
   a "set missing" or "set datafile missing" line, ensure that it's in
   sync with the installed gnuplot.  Also, do we have a commented-out
   "set term" line?  If so, this may be out of date with respect to
   current gnuplot, so we'll strip it out.
*/

int maybe_rewrite_gp_file (const char *fname)
{
    FILE *fin, *fout;
    gchar *trbuf, *modname = NULL;
    char line[512];
    int modified = 0;
    int recoded = 0;
    int fix_xrange = 0;
    int err = 0;

    fin = gretl_fopen(fname, "r");
    if (fin == NULL) {
	return 1;
    }

    modname = g_strdup_printf("%s.tr", fname);

    fout = gretl_fopen(modname, "w");
    if (fout == NULL) {
	fclose(fin);
	g_free(modname);
	return 1;
    }

    while (fgets(line, sizeof line, fin)) {
	int modline = 0;

	if (!strncmp(line, "set missing", 11)) {
	    fputs("set datafile missing \"?\"\n", fout);
	    modline = 1;
	} else if (!strncmp(line, "# set term", 10)) {
	    /* skip it */
	    modline = 1;
	} else if (!strncmp(line, "set term png ", 13)) {
	    if (maybe_adjust_cairo(line)) {
		fputs(line, fout);
		modline = 1;
	    }
	} else if (!strncmp(line, "set xdata time", 14)) {
	    if (strstr(line, "ZERO_YEAR=2000")) {
		fputs("set xdata time\n", fout);
		modline = 1;
		fix_xrange = 1;
	    }
	} else if (!strncmp(line, "set xrange", 10) && fix_xrange) {
	    do_fix_xrange(line, fix_xrange, fout);
	    modline = 1;
	}

	if (!modline && !g_utf8_validate(line, -1, NULL)) {
	    trbuf = gp_locale_to_utf8(line, !recoded);
	    if (trbuf != NULL) {
		fputs(trbuf, fout);
		g_free(trbuf);
	    }
	    modline = recoded = 1;
	}

	if (modline) {
	    modified = 1;
	} else {
	    /* pass old line through */
	    fputs(line, fout);
	}
    }

    fclose(fin);
    fclose(fout);

    if (modified) {
	err = copyfile(modname, fname);
    }

    gretl_remove(modname);
    g_free(modname);

    return err;
}