GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
frmt.c
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * MODULE: Vector library
4 *
5 * AUTHOR(S): Radim Blazek
6 *
7 * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
8 *
9 * COPYRIGHT: (C) 2001, 2012 by the GRASS Development Team
10 *
11 * This program is free software under the GNU General
12 * Public License (>=v2). Read the file COPYING that
13 * comes with GRASS for details.
14 *
15 *****************************************************************************/
16
17#include <string.h>
18#include <stdio.h>
19
20#include <grass/vector.h>
21#include <grass/glocale.h>
22#include <grass/gis.h>
23
24/*!
25 \brief Read external vector format file
26
27 \param dascii format file (frmt)
28 \param[out] finfo pointer to Format_info structure
29
30 \return format code
31 \return -1 on error
32 */
34{
35 char buff[2001], buf1[2001];
36 char *ptr;
37 int frmt = -1;
38 size_t len;
39
40 G_debug(3, "dig_read_frmt_ascii()");
41
42 /* read first line which must be FORMAT: */
43 if (G_getl2(buff, 2000, dascii)) {
44 G_chop(buff);
45
46 if (!(ptr = strchr(buff, ':'))) {
47 G_warning(_("Vector format not recognized: %s"), buff);
48 return -1;
49 }
50
51 len = G_strlcpy(buf1, buff, sizeof(buf1));
52 if (len >= sizeof(buf1)) {
53 G_warning(_("Line <%s> is too long"), buff);
54 return -1;
55 }
56 buf1[ptr - buff] = '\0';
57
58 ptr++; /* Search for the start of text */
59 while (*ptr == ' ')
60 ptr++;
61
62 if (G_strcasecmp(buf1, "FORMAT") == 0) {
63 if (G_strcasecmp(ptr, "ogr") == 0) {
65 }
66#ifdef HAVE_POSTGRES
67 if (G_strcasecmp(ptr, "postgis") == 0) {
69 }
70#endif
71 }
72 }
73 if (frmt == -1) {
74 G_warning(_("Vector format not recognized: %s"), buff);
75 return -1;
76 }
77
78 /* init format info values */
79 G_zero(&(finfo->ogr), sizeof(struct Format_info_ogr));
80
81#ifdef HAVE_POSTGRES
82 G_zero(&(finfo->pg), sizeof(struct Format_info_pg));
83#else
84 if (frmt == GV_FORMAT_POSTGIS) {
85 G_warning(_("Vector format '%s' not supported"), ptr);
86 return -1;
87 }
88#endif
89
90 while (G_getl2(buff, 2000, dascii)) {
91 G_chop(buff);
92
93 if (!(ptr = strchr(buff, ':'))) {
94 G_warning(_("Format definition is not correct: %s"), buff);
95 continue;
96 }
97
98 len = G_strlcpy(buf1, buff, sizeof(buf1));
99 if (len >= sizeof(buf1)) {
100 G_warning(_("Line <%s> is too long"), buff);
101 return -1;
102 }
103 buf1[ptr - buff] = '\0';
104
105 ptr++; /* Search for the start of text */
106 while (*ptr == ' ')
107 ptr++;
108
109 if (frmt == GV_FORMAT_OGR) {
110 if (G_strcasecmp(buf1, "DSN") == 0)
111 finfo->ogr.dsn = G_store(ptr);
112 if (G_strcasecmp(buf1, "LAYER") == 0)
113 finfo->ogr.layer_name = G_store(ptr);
114 if (G_strcasecmp(buf1, "WHERE") == 0)
115 finfo->ogr.where = G_store(ptr);
116 }
117#ifdef HAVE_POSTGRES
118 if (frmt == GV_FORMAT_POSTGIS) {
119 if (G_strcasecmp(buf1, "CONNINFO") == 0)
120 finfo->pg.conninfo = G_store(ptr);
121 if (G_strcasecmp(buf1, "SCHEMA") == 0)
122 finfo->pg.schema_name = G_store(ptr);
123 if (G_strcasecmp(buf1, "TABLE") == 0)
124 finfo->pg.table_name = G_store(ptr);
125 if (G_strcasecmp(buf1, "FID") == 0)
126 finfo->pg.fid_column = G_store(ptr);
127 if (G_strcasecmp(buf1, "WHERE") == 0)
128 finfo->pg.where = G_store(ptr);
129 }
130#endif
131 }
132
133#ifdef HAVE_POSTGRES
134 /* if schema not defined, use 'public' */
135 if (frmt == GV_FORMAT_POSTGIS && !finfo->pg.schema_name) {
136 finfo->pg.schema_name = G_store("public");
137 }
138
139 /* if fid column not defined, use default value */
140 if (frmt == GV_FORMAT_POSTGIS && !finfo->pg.fid_column) {
141 finfo->pg.fid_column = G_store(GV_PG_FID_COLUMN);
142 }
143#endif
144
145 return frmt;
146}
147
148/* Write vector format, currently does not work
149 * Parse also connection string.
150 *
151 * Returns: 0 OK
152 * -1 on error
153 */
155 int format UNUSED)
156{
157 G_debug(3, "dig_write_frmt_ascii()");
158
159 G_fatal_error("Format not supported by dig_write_frmt_ascii()");
160
161 return 0;
162}
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
Definition getl.c:60
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition gis/zero.c:23
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition strings.c:47
char * G_chop(char *)
Chop leading and trailing white spaces.
Definition strings.c:332
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:87
int G_debug(int, const char *,...) __attribute__((format(printf
size_t G_strlcpy(char *, const char *, size_t)
Safe string copy function.
Definition strlcpy.c:52
#define GV_FORMAT_POSTGIS
PostGIS format.
Definition dig_defines.h:89
#define GV_FORMAT_OGR
OGR format.
Definition dig_defines.h:85
#define GV_PG_FID_COLUMN
GRASS-PostGIS data provider - default fid column.
int dig_read_frmt_ascii(FILE *dascii, struct Format_info *finfo)
Read external vector format file.
Definition frmt.c:33
int dig_write_frmt_ascii(FILE *dascii, struct Format_info *finfo, int format)
Definition frmt.c:154
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:46
#define _(str)
Definition glocale.h:10
Non-native format info (OGR)
Non-native format info (PostGIS)
Non-native format info (currently only OGR is implemented)