~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to vector/v.mapcalc/any.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <stdlib.h>
3
 
#include <string.h>
4
 
#include <grass/gis.h>
5
 
#include <grass/glocale.h>
6
 
#include "list.h"
7
 
#include "mapcalc.h"
8
 
#include "any.h"
9
 
 
10
 
/*
11
 
 * This is a backdoor for strange types only. We only handle void pointer
12
 
 * references and make no kind of typecheck; lot's of errors can be made
13
 
 * by the user. But at least it's possible to deal with minor issues
14
 
 * without touching the parser.
15
 
 * Memory for "any" should be one single block.
16
 
 */
17
 
 
18
 
typedef ANY *(*a_func) (void);
19
 
typedef ANY *(*a_func_a) (void *a0);
20
 
typedef ANY *(*a_func_aa) (void *a0, void *a1);
21
 
 
22
 
typedef struct Anyfunc
23
 
{
24
 
    char *name;
25
 
    void *func;
26
 
    char *proto;
27
 
} ANYFUNC;
28
 
 
29
 
static ANY *mkstring(void *a);
30
 
void setany(SYMBOL * var, SYMBOL * any);
31
 
SYMBOL *mkanyvar(SYMBOL * var, SYMBOL * any);
32
 
SYMBOL *anyfunc(SYMBOL * func, SYMBOL * arglist);
33
 
 
34
 
static ANYFUNC af[] = {
35
 
    {"mkstring", mkstring, "a"},
36
 
    {NULL, NULL, NULL}
37
 
};
38
 
 
39
 
/***************************************************************************
40
 
 * Test function only
41
 
 */
42
 
static ANY *mkstring(void *a)
43
 
{
44
 
    ANY *str;
45
 
 
46
 
    str = (ANY *) listitem(sizeof(ANY));
47
 
    str->type = st_str;
48
 
    str->any = (char *)strdup(a);
49
 
    str->refcnt = 1;
50
 
    fprintf(stdout, "\t%s\n", (char *)a);
51
 
 
52
 
    return str;
53
 
}
54
 
 
55
 
/*
56
 
 * End test function only
57
 
 ***************************************************************************/
58
 
 
59
 
void init_any(void)
60
 
{
61
 
    SYMBOL *sym;
62
 
    int i;
63
 
 
64
 
    for (i = 0; af[i].name; i++) {
65
 
        sym = putsym(af[i].name);
66
 
        sym->type = sym->itype = st_afunc;
67
 
        sym->v.p = af[i].func;
68
 
        sym->proto = af[i].proto;
69
 
        sym->rettype = st_any;
70
 
    }
71
 
}
72
 
 
73
 
void setany(SYMBOL * var, SYMBOL * any)
74
 
{
75
 
    SYMBOL *sym;
76
 
 
77
 
    if (var->name) {
78
 
        sym = getsym(var->name);
79
 
        if (sym) {
80
 
            if (--((ANY *) sym->v.p)->refcnt < 1)
81
 
                G_free(sym->v.p);
82
 
            sym->v.p = any->v.p;
83
 
        }
84
 
    }
85
 
 
86
 
    if (--((ANY *) var->v.p)->refcnt < 1)
87
 
        G_free(var->v.p);
88
 
    var->v.p = NULL;
89
 
    freesym(var);
90
 
 
91
 
    /* can't show an anyvar */
92
 
    any->v.p = NULL;
93
 
    freesym(any);
94
 
}
95
 
 
96
 
SYMBOL *mkanyvar(SYMBOL * var, SYMBOL * any)
97
 
{
98
 
    var->type = var->itype = st_any;
99
 
    var->name = var->v.p;
100
 
    var->v.p = any->v.p;
101
 
    any->v.p = NULL;
102
 
    symtab = (SYMBOL *) listadd((LIST *) symtab, (LIST *) var, cmpsymsym);
103
 
 
104
 
    /* can't show any */
105
 
 
106
 
    return var;
107
 
}
108
 
 
109
 
SYMBOL *anyfunc(SYMBOL * func, SYMBOL * arglist)
110
 
{
111
 
    SYMBOL *sym;
112
 
    ANY *res = NULL;
113
 
    int argc = -1;
114
 
 
115
 
    if (!func || !func->v.p || func->type != st_afunc) {
116
 
        parseerror = 1;
117
 
        G_warning(_("Can't call bad any-function"));
118
 
    }
119
 
    else
120
 
        argc = listcnt((LIST *) arglist);
121
 
 
122
 
    if (argc == 0 && (!func->proto || !*func->proto))
123
 
        res = (*(a_func) func->v.p) ();
124
 
    else if (argc == 1 && !strcmp(func->proto, "a"))
125
 
        res = (*(a_func_a) func->v.p) (arglist->v.p);
126
 
    else if (argc == 2 && !strcmp(func->proto, "aa"))
127
 
        res = (*(a_func_aa) func->v.p) (arglist->v.p, arglist->next->v.p);
128
 
    else {
129
 
        G_warning(_("Bad arguments to anyfunc %s (argc = %d)"), func->name,
130
 
                  argc);
131
 
        parseerror = 1;
132
 
    }
133
 
 
134
 
    listdelall((LIST *) func, freesym);
135
 
    listdelall((LIST *) arglist, freesym);
136
 
 
137
 
    sym = (SYMBOL *) listitem(sizeof(SYMBOL));
138
 
    sym->type = st_any;
139
 
    sym->v.p = res;
140
 
 
141
 
    return sym;
142
 
}