File: gfn_arglists.c

package info (click to toggle)
gretl 2016d-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 48,620 kB
  • ctags: 22,779
  • sloc: ansic: 345,830; sh: 4,648; makefile: 2,712; xml: 570; perl: 364
file content (133 lines) | stat: -rw-r--r-- 2,621 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
/* 
 *  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 "gfn_arglists.h"

/* apparatus for remembering the list of arguments given to
   packaged functions (gfn)
*/

struct arglist_ {
    char pkgname[32];
    int argc;
    char **argv;
};

static arglist **arglists;
static int n_arglists;

static int arglist_push (arglist *a)
{
    int n = n_arglists + 1;
    arglist **A;

    A = realloc(arglists, n * sizeof *A);

    if (A == NULL) {
	return E_ALLOC;
    } else {
	arglists = A;
	arglists[n-1] = a;
	n_arglists = n;
    }

    return 0;
}

static void arglist_destroy (arglist *a)
{
    if (a != NULL) {
	strings_array_free(a->argv, a->argc);
	free(a);
    }
}

void arglist_cleanup (void)
{
    int i;
    
    for (i=0; i<n_arglists; i++) {
	arglist_destroy(arglists[i]);
    }

    free(arglists);
    arglists = NULL;
    n_arglists = 0;
}

arglist *arglist_new (const char *pkgname, int argc)
{
    arglist *a = malloc(sizeof *a);

    if (a != NULL) {
	a->argv = strings_array_new(argc);
	if (a->argv == NULL) {
	    free(a);
	    a = NULL;
	} else {
	    int err;
	    
	    *a->pkgname = '\0';
	    strncat(a->pkgname, pkgname, 31);
	    a->argc = argc;
	    err = arglist_push(a);
	    if (err) {
		arglist_destroy(a);
		a = NULL;
	    }
	}
    }

    return a;
}

arglist *arglist_lookup (const char *pkgname)
{
    int i;

    for (i=0; i<n_arglists; i++) {
	if (!strcmp(arglists[i]->pkgname, pkgname)) {
	    return arglists[i];
	}
    }

    return NULL;
}

int arglist_record_arg (arglist *a, int i, const char *val)
{
    if (a == NULL || i < 0 || i > a->argc) {
	return E_DATA;
    } else {
	free(a->argv[i]);
	a->argv[i] = gretl_strdup(val);
	return 0;
    }
}

const char *arglist_lookup_val (arglist *a, int i)
{
    if (a == NULL || i < 0 || i > a->argc) {
	return NULL;
    } else {
	return a->argv[i];
    }
}