File: frmt.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (163 lines) | stat: -rw-r--r-- 4,460 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
/*****************************************************************************
 *
 * MODULE:       Vector library
 *
 * AUTHOR(S):    Radim Blazek
 *
 * PURPOSE:      Lower level functions for reading/writing/manipulating vectors.
 *
 * COPYRIGHT:    (C) 2001, 2012 by the GRASS Development Team
 *
 *               This program is free software under the GNU General
 *               Public License (>=v2). Read the file COPYING that
 *               comes with GRASS for details.
 *
 *****************************************************************************/

#include <string.h>
#include <stdio.h>

#include <grass/vector.h>
#include <grass/glocale.h>

/*!
   \brief Read external vector format file

   \param dascii format file (frmt)
   \param[out] finfo pointer to Format_info structure

   \return format code
   \return -1 on error
 */
int dig_read_frmt_ascii(FILE *dascii, struct Format_info *finfo)
{
    char buff[2001], buf1[2001];
    char *ptr;
    int frmt = -1;

    G_debug(3, "dig_read_frmt_ascii()");

    /* read first line which must be FORMAT: */
    if (G_getl2(buff, 2000, dascii)) {
        G_chop(buff);

        if (!(ptr = strchr(buff, ':'))) {
            G_warning(_("Vector format not recognized: %s"), buff);
            return -1;
        }

        strcpy(buf1, buff);
        buf1[ptr - buff] = '\0';

        ptr++; /* Search for the start of text */
        while (*ptr == ' ')
            ptr++;

        if (G_strcasecmp(buf1, "FORMAT") == 0) {
#ifdef HAVE_OGR
            if (G_strcasecmp(ptr, "ogr") == 0) {
                frmt = GV_FORMAT_OGR;
            }
#endif
#ifdef HAVE_POSTGRES
            if (G_strcasecmp(ptr, "postgis") == 0) {
                frmt = GV_FORMAT_POSTGIS;
            }
#endif
        }
    }
    if (frmt == -1) {
        G_warning(_("Vector format not recognized: %s"), buff);
        return -1;
    }

    /* init format info values */
#ifdef HAVE_OGR
    G_zero(&(finfo->ogr), sizeof(struct Format_info_ogr));
#else
    if (frmt == GV_FORMAT_OGR) {
        G_warning(_("Vector format '%s' not supported"), ptr);
        return -1;
    }
#endif

#ifdef HAVE_POSTGRES
    G_zero(&(finfo->pg), sizeof(struct Format_info_pg));
#else
    if (frmt == GV_FORMAT_POSTGIS) {
        G_warning(_("Vector format '%s' not supported"), ptr);
        return -1;
    }
#endif

    while (G_getl2(buff, 2000, dascii)) {
        G_chop(buff);

        if (!(ptr = strchr(buff, ':'))) {
            G_warning(_("Format definition is not correct: %s"), buff);
            continue;
        }

        strcpy(buf1, buff);
        buf1[ptr - buff] = '\0';

        ptr++; /* Search for the start of text */
        while (*ptr == ' ')
            ptr++;

#ifdef HAVE_OGR
        if (frmt == GV_FORMAT_OGR) {
            if (G_strcasecmp(buf1, "DSN") == 0)
                finfo->ogr.dsn = G_store(ptr);
            if (G_strcasecmp(buf1, "LAYER") == 0)
                finfo->ogr.layer_name = G_store(ptr);
            if (G_strcasecmp(buf1, "WHERE") == 0)
                finfo->ogr.where = G_store(ptr);
        }
#endif
#ifdef HAVE_POSTGRES
        if (frmt == GV_FORMAT_POSTGIS) {
            if (G_strcasecmp(buf1, "CONNINFO") == 0)
                finfo->pg.conninfo = G_store(ptr);
            if (G_strcasecmp(buf1, "SCHEMA") == 0)
                finfo->pg.schema_name = G_store(ptr);
            if (G_strcasecmp(buf1, "TABLE") == 0)
                finfo->pg.table_name = G_store(ptr);
            if (G_strcasecmp(buf1, "FID") == 0)
                finfo->pg.fid_column = G_store(ptr);
            if (G_strcasecmp(buf1, "WHERE") == 0)
                finfo->pg.where = G_store(ptr);
        }
#endif
    }

#ifdef HAVE_POSTGRES
    /* if schema not defined, use 'public' */
    if (frmt == GV_FORMAT_POSTGIS && !finfo->pg.schema_name) {
        finfo->pg.schema_name = G_store("public");
    }

    /* if fid column not defined, use default value */
    if (frmt == GV_FORMAT_POSTGIS && !finfo->pg.fid_column) {
        finfo->pg.fid_column = G_store(GV_PG_FID_COLUMN);
    }
#endif

    return frmt;
}

/* Write vector format, currently does not work
 *  Parse also connection string.
 *
 *  Returns: 0 OK
 *           -1 on error
 */
int dig_write_frmt_ascii(FILE *dascii UNUSED, struct Format_info *finfo UNUSED,
                         int format UNUSED)
{
    G_debug(3, "dig_write_frmt_ascii()");

    G_fatal_error("Format not supported by dig_write_frmt_ascii()");

    return 0;
}