File: filelists.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 (640 lines) | stat: -rw-r--r-- 13,866 bytes parent folder | download
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
 *  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 "filelists.h"
#include "menustate.h"
#include "toolbar.h"
#include "fnsave.h"
#include "libset.h"

#ifdef G_OS_WIN32
# include "gretlwin32.h"
#endif

#define FDEBUG 0

#define NFILELISTS 5

/* lists of recently opened files */
static char datalist[MAXRECENT][MAXSTR];
static char sessionlist[MAXRECENT][MAXSTR];
static char scriptlist[MAXRECENT][MAXSTR];
static char gfnlist[MAXRECENT][MAXSTR];
static char wdirlist[MAXRECENT][MAXSTR];

/* and pointers to same */
static char *datap[MAXRECENT];
static char *sessionp[MAXRECENT];
static char *scriptp[MAXRECENT];
static char *gfnp[MAXRECENT];
static char *wdirp[MAXRECENT];

/* and ui_ids for same (apart from wdirlist) */
static guint data_id[MAXRECENT];
static guint session_id[MAXRECENT];
static guint script_id[MAXRECENT];
static guint gfn_id[MAXRECENT];

static void real_add_files_to_menus (int ftype);

void initialize_file_lists (void)
{
    int i;

    /* initialize lists of recently opened files */
    for (i=0; i<MAXRECENT; i++) {
	datalist[i][0] = '\0';
	sessionlist[i][0] = '\0';
	scriptlist[i][0] = '\0';
	gfnlist[i][0] = '\0';
	wdirlist[i][0] = '\0';
    }
}

void init_fileptrs (void)
{
    int i;

    for (i=0; i<MAXRECENT; i++) {
	datap[i] = datalist[i];
	sessionp[i] = sessionlist[i];
	scriptp[i] = scriptlist[i];
	gfnp[i] = gfnlist[i];
	wdirp[i] = wdirlist[i];
    }
}

static char **get_file_list (int filetype)
{
    if (filetype == FILE_LIST_DATA) {
	return datap;
    } else if (filetype == FILE_LIST_SESSION) {
	return sessionp;
    } else if (filetype == FILE_LIST_SCRIPT) {
	return scriptp;
    } else if (filetype == FILE_LIST_GFN) {
	return gfnp;
    } else if (filetype == FILE_LIST_WDIR) {
	return wdirp;
    } else {
	return NULL;
    }
}

static const char *file_sections[] = {
    "recent_data_files",
    "recent_session_files",
    "recent_script_files",
    "recent_gfn_files",
    "recent_working_dirs"
};

/* Note: the Windows-special stuff below should eventually be
   unnecessary, but for now we want to allow for the possibility
   that we got a locale-encoded filename on reading the "recent"
   files from the gretl rc file; we now want to ensure that all
   such filenames are in UTF-8. (2018-09-16)
*/

static void write_filename_to_list (int filetype, int i, char *fname)
{
#ifdef G_OS_WIN32
    gchar *fconv = NULL;
#endif

    if (i >=  MAXRECENT) {
	return;
    }

#ifdef G_OS_WIN32
    /* guard against old locale-encoded filenames in rc file */
    if (!g_utf8_validate(fname, -1, NULL)) {
	gsize bytes;

	fconv = g_locale_to_utf8(fname, -1, NULL, &bytes, NULL);
	if (fconv != NULL) {
	    fname = fconv;
	}
    }
#endif

    if (filetype == FILE_LIST_DATA) {
	strcpy(datalist[i], fname);
    } else if (filetype == FILE_LIST_SESSION) {
	strcpy(sessionlist[i], fname);
    } else if (filetype == FILE_LIST_SCRIPT) {
	strcpy(scriptlist[i], fname);
    } else if (filetype == FILE_LIST_GFN) {
	strcpy(gfnlist[i], fname);
    } else if (filetype == FILE_LIST_WDIR) {
	strcpy(wdirlist[i], fname);
    }

#ifdef G_OS_WIN32
    g_free(fconv);
#endif
}

/* We come here on finding a "recent ..." line in
   the user's .gretl2rc. We process that line (@prev)
   first, then look for more.

   Return 1 if we manage to read any "recent files",
   else 0.
*/

int rc_read_file_lists (FILE *fp, char *prev)
{
    char line[MAXLEN];
    int i, len, n[NFILELISTS] = {0};
    int ret = 0;

    initialize_file_lists();
    strcpy(line, prev);

    do {
	for (i=0; i<NFILELISTS; i++) {
	    len = strlen(file_sections[i]);
	    if (!strncmp(line, file_sections[i], len)) {
		/* found a known "recent files" section */
		gretl_strstrip(line);
		if (*line != '\0') {
		    write_filename_to_list(i, n[i], line + len + 2);
		    n[i] += 1;
		    ret = 1;
		}
		break;
	    }
	}
    } while (fgets(line, sizeof line, fp) != NULL);

    return ret;
}

static void rc_print_filelist (int filetype, FILE *fp)
{
    char **filep;
    int i;

    filep = get_file_list(filetype);
    if (filep == NULL) {
	return;
    }

    for (i=0; i<MAXRECENT; i++) {
	if (filep[i] != NULL && *filep[i] != 0) {
	    fprintf(fp, "%s%d %s\n", file_sections[filetype], i, filep[i]);
	}
    }
}

void rc_save_file_lists (FILE *fp)
{
    rc_print_filelist(FILE_LIST_DATA, fp);
    rc_print_filelist(FILE_LIST_SESSION, fp);
    rc_print_filelist(FILE_LIST_SCRIPT, fp);
    rc_print_filelist(FILE_LIST_GFN, fp);
    rc_print_filelist(FILE_LIST_WDIR, fp);
}

static void clear_files_list (int ftype, char **filep)
{
    guint *id;
    int i;

    if (mdata == NULL || mdata->ui == NULL) {
	return;
    }

    if (ftype == FILE_LIST_DATA) {
	id = data_id;
    } else if (ftype == FILE_LIST_SESSION) {
	id = session_id;
    } else if (ftype == FILE_LIST_SCRIPT) {
	id = script_id;
    } else if (ftype == FILE_LIST_GFN) {
	id = gfn_id;
    } else {
	return;
    }

    for (i=0; i<MAXRECENT; i++) {
	if (filep[i][0] != '\0') {
	    gtk_ui_manager_remove_ui(mdata->ui, id[i]);
	}
    }
}

static void add_files_to_menu (int ftype)
{
    if (ftype != FILE_LIST_WDIR) {
#if FDEBUG
	fprintf(stderr, "add_files_to_menu: ftype = %d\n", ftype);
#endif
	real_add_files_to_menus(ftype);
    }
}

#ifdef G_OS_WIN32

/* standardize on backslash as path separator */

static char *bslash (char *fname)
{
    char *s = fname;

    while (*s) {
	if (*s == '/') *s = '\\';
	s++;
    }

    return fname;
}

/* make comparison case-insensitive, and also insensitive to
   the distinction between forward and back slashes */

static int fnamencmp (const char *f1, const char *f2, int n)
{
    gchar *u1 = NULL, *u2 = NULL;
    GError *err = NULL;
    gsize bytes;
    int ret = 0;

    if (g_utf8_validate(f1, -1, NULL)) {
	u1 = g_strdup(f1);
    } else {
	u1 = g_locale_to_utf8(f1, -1, NULL, &bytes, &err);
    }

    if (err == NULL) {
	if (g_utf8_validate(f2, -1, NULL)) {
	    u2 = g_strdup(f2);
	} else {
	    u2 = g_locale_to_utf8(f2, -1, NULL, &bytes, &err);
	}
    }

    if (err != NULL) {
	errbox(err->message);
	g_error_free(err);
    } else {
	gchar *c1 = g_utf8_casefold(u1, -1);
	gchar *c2 = g_utf8_casefold(u2, -1);

	trim_slash(bslash(c1));
	trim_slash(bslash(c2));

	if (n > 0) {
	    ret = strncmp(c1, c2, n);
	} else {
	    ret = strcmp(c1, c2);
	}

	g_free(c1);
	g_free(c2);
    }

    g_free(u1);
    g_free(u2);

    return ret;
}

int fnamecmp (const char *f1, const char *f2)
{
    return fnamencmp(f1, f2, -1);
}

#else

int fnamecmp (const char *f1, const char *f2)
{
    gchar *c1 = NULL, *c2 = NULL;
    int ret = 0;

    c1 = g_strdup(f1);
    c2 = g_strdup(f2);

    trim_slash(c1);
    trim_slash(c2);

    ret = strcmp(c1, c2);

    g_free(c1);
    g_free(c2);

    return ret;
}

#endif

/* handle the case where we got an incoming filename without
   a path specification (command-line use)
*/

static char *maybe_expand_path (char *fullname,
				const char *fname,
				size_t len)
{
    gchar *cwd = g_get_current_dir();

    *fullname = '\0';

    if (cwd != NULL && strlen(cwd) + strlen(fname) < len - 1) {
	strcat(fullname, cwd);
	slash_terminate(fullname);
	strcat(fullname, fname);
    } else {
	strncat(fullname, fname, len);
    }

    g_free(cwd);

    return fullname;
}

void mkfilelist (int filetype, char *fname, int get_full)
{
    char fullname[FILENAME_MAX];
    char *tmp[MAXRECENT-1];
    char **filep;
    int i, pos = -1;

#if FDEBUG
    fprintf(stderr, "mkfilelist: type=%d, fname='%s'\n",
	    filetype, fname);
#endif

    *fullname = '\0';
    if (g_path_is_absolute(fname)) {
	strncat(fullname, fname, FILENAME_MAX-1);
    } else {
	maybe_expand_path(fullname, fname, FILENAME_MAX-1);
    }
    gretl_normalize_path(fullname);

#if FDEBUG
    fprintf(stderr, "after normalize_path: '%s'\n", fullname);
#endif

    if (get_full) {
	strcpy(fname, fullname);
    }

    filep = get_file_list(filetype);
    if (filep == NULL) {
	return;
    }

    /* see if this file is already on the list */
    for (i=0; i<MAXRECENT; i++) {
        if (!fnamecmp(filep[i], fullname)) {
            pos = i;
#if FDEBUG
	    fprintf(stderr, "file already on list at pos %d\n", i);
#endif
            break;
        }
    }

    if (pos == 0) {
	/* file is on top: no change is needed */
	return;
    }

    /* clear menu files list before rebuilding */
    clear_files_list(filetype, filep);

    /* save pointers to current order */
    for (i=0; i<MAXRECENT-1; i++) {
	tmp[i] = filep[i];
    }

    /* copy fname into array, if not already present */
    if (pos == -1) {
	/* look for an empty slot */
        for (i=1; i<MAXRECENT; i++) {
            if (filep[i][0] == '\0') {
                strcpy(filep[i], fullname);
                pos = i;
                break;
	    }
	}
	if (pos == -1) {
	    /* no empty slot available */
	    pos = MAXRECENT - 1;
	    strcpy(filep[pos], fullname);
	}
    }

    /* set first pointer to newest file */
    filep[0] = filep[pos];

    /* and rearrange the other pointers */
    for (i=1; i<=pos; i++) {
	filep[i] = tmp[i-1];
    }

    add_files_to_menu(filetype);
}

/* return 0 if this is a np-op, 1 if we actually deleted
   an entry */

int delete_from_filelist (int filetype, const char *fname)
{
    char *tmp[MAXRECENT];
    char **filep;
    int i, match = -1;

    filep = get_file_list(filetype);
    if (filep == NULL) {
	return 0;
    }

    /* save pointers to current order */
    for (i=0; i<MAXRECENT; i++) {
	tmp[i] = filep[i];
	if (!fnamecmp(filep[i], fname)) {
	    match = i;
	}
    }

    if (match == -1) {
	return 0;
    }

    /* clear menu files list before rebuilding */
    clear_files_list(filetype, filep);

    for (i=match; i<MAXRECENT-1; i++) {
	filep[i] = tmp[i+1];
    }

    filep[MAXRECENT-1] = tmp[match];
    filep[MAXRECENT-1][0] = '\0';

    add_files_to_menu(filetype);

    return 1;
}

static void open_file_from_filelist (GtkAction *action)
{
    const gchar *s = gtk_action_get_name(action);
    char ftype[8];
    int i;

    sscanf(s, "%s %d", ftype, &i);

    if (!strcmp(ftype, "Data")) {
	strcpy(tryfile, datap[i]);
#if 0 /* probably not a good idea */
	if (strstr(tryfile, ".csv")) {
	    int resp = csv_options_dialog(OPEN_DATA, GRETL_OBJ_DSET, NULL);

	    if (canceled(resp)) {
		return;
	    }
	}
#endif
	verify_open_data(NULL, 0);
    } else if (!strcmp(ftype, "Script")) {
	strcpy(tryfile, scriptp[i]);
	do_open_script(EDIT_HANSL);
    } else if (!strcmp(ftype, "Session")) {
	strcpy(tryfile, sessionp[i]);
	verify_open_session();
    } else if (!strcmp(ftype, "Gfn")) {
	edit_specified_package(gfnp[i]);
    }
}

static void real_add_files_to_menus (int ftype)
{
    char tmp[MAXSTR];
    const char *fword;
    guint *id;
    GtkActionEntry entry;
    const gchar *mpath[] = {
	"/menubar/File/OpenDataMenu/RecentData",
	"/menubar/File/SessionFiles/RecentSessions",
	"/menubar/File/ScriptFiles/RecentScripts",
	"/menubar/File/Packages/RecentGfns"
    };
    gchar *aname, *alabel, *apath;
    int jmin = 0, jmax = NFILELISTS - 1;
    GtkWidget *w;
    int i, j, k;

    if (mdata == NULL || mdata->ui == NULL) {
	return;
    }

    if (ftype < NFILELISTS - 1) {
	jmin = ftype;
	jmax = jmin + 1;
    }

    action_entry_init(&entry);
    entry.callback = G_CALLBACK(open_file_from_filelist);

    for (j=jmin; j<jmax; j++) {
	char **filep = NULL;

	if (j == FILE_LIST_DATA) {
	    filep = datap;
	    id = data_id;
	    fword = "Data";
	} else if (j == FILE_LIST_SESSION) {
	    filep = sessionp;
	    id = session_id;
	    fword = "Session";
	} else if (j == FILE_LIST_SCRIPT) {
	    filep = scriptp;
	    id = script_id;
	    fword = "Script";
	} else if (j == FILE_LIST_GFN) {
	    filep = gfnp;
	    id = gfn_id;
	    fword = "Gfn";
	}

	/* See if there are any files to add */
	if (filep == NULL || *filep[0] == '\0') {
	    continue;
	}

	/* put the files under the menu separator: ensure valid UTF-8
	   for display purposes */

	k = 0;
	for (i=0; i<MAXRECENT && filep[i][0]; i++) {
	    const char *fname = filep[i];

	    if (!g_utf8_validate(fname, -1, NULL)) {
		/* We got a rubbish filename.  It would be nice to
		   know how that happened, but we'll try to recover by
		   blanking out the rubbish and continuing.
		*/
		fprintf(stderr, "%s %d: got corrupted filename\n",
			mpath[j], i);
		filep[i][0] = '\0';
		continue;
	    } else {
		aname = g_strdup_printf("%s %d", fword, k);
		alabel = g_strdup_printf("%d. %s", k+1,
					 gretl_basename(tmp, fname, 1));
		entry.name = aname;
		entry.label = alabel;
		id[i] = vwin_menu_add_item_unique(mdata, aname, mpath[j], &entry);
		apath = g_strdup_printf("%s/%s", mpath[j], aname);
		w = gtk_ui_manager_get_widget(mdata->ui, apath);
		if (w != NULL) {
		    gretl_tooltips_add(w, fname);
		}
		g_free(aname);
		g_free(alabel);
		g_free(apath);
		k++;
	    }
	}
    }
}

void add_files_to_menus (void)
{
    real_add_files_to_menus(NFILELISTS);
}

GList *get_working_dir_list (void)
{
    GList *list = NULL;
    int i;

    for (i=0; i<MAXRECENT && wdirp[i][0]; i++) {
	if (g_utf8_validate(wdirp[i], -1, NULL)) {
	    list = g_list_append(list, wdirp[i]);
	}
    }

    return list;
}