GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-535c39c9fc
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 
23 /*!
24  \brief Read external vector format file
25 
26  \param dascii format file (frmt)
27  \param[out] finfo pointer to Format_info structure
28 
29  \return format code
30  \return -1 on error
31  */
32 int dig_read_frmt_ascii(FILE *dascii, struct Format_info *finfo)
33 {
34  char buff[2001], buf1[2001];
35  char *ptr;
36  int frmt = -1;
37 
38  G_debug(3, "dig_read_frmt_ascii()");
39 
40  /* read first line which must be FORMAT: */
41  if (G_getl2(buff, 2000, dascii)) {
42  G_chop(buff);
43 
44  if (!(ptr = strchr(buff, ':'))) {
45  G_warning(_("Vector format not recognized: %s"), buff);
46  return -1;
47  }
48 
49  strcpy(buf1, buff);
50  buf1[ptr - buff] = '\0';
51 
52  ptr++; /* Search for the start of text */
53  while (*ptr == ' ')
54  ptr++;
55 
56  if (G_strcasecmp(buf1, "FORMAT") == 0) {
57 #ifdef HAVE_OGR
58  if (G_strcasecmp(ptr, "ogr") == 0) {
59  frmt = GV_FORMAT_OGR;
60  }
61 #endif
62 #ifdef HAVE_POSTGRES
63  if (G_strcasecmp(ptr, "postgis") == 0) {
64  frmt = GV_FORMAT_POSTGIS;
65  }
66 #endif
67  }
68  }
69  if (frmt == -1) {
70  G_warning(_("Vector format not recognized: %s"), buff);
71  return -1;
72  }
73 
74  /* init format info values */
75 #ifdef HAVE_OGR
76  G_zero(&(finfo->ogr), sizeof(struct Format_info_ogr));
77 #else
78  if (frmt == GV_FORMAT_OGR) {
79  G_warning(_("Vector format '%s' not supported"), ptr);
80  return -1;
81  }
82 #endif
83 
84 #ifdef HAVE_POSTGRES
85  G_zero(&(finfo->pg), sizeof(struct Format_info_pg));
86 #else
87  if (frmt == GV_FORMAT_POSTGIS) {
88  G_warning(_("Vector format '%s' not supported"), ptr);
89  return -1;
90  }
91 #endif
92 
93  while (G_getl2(buff, 2000, dascii)) {
94  G_chop(buff);
95 
96  if (!(ptr = strchr(buff, ':'))) {
97  G_warning(_("Format definition is not correct: %s"), buff);
98  continue;
99  }
100 
101  strcpy(buf1, buff);
102  buf1[ptr - buff] = '\0';
103 
104  ptr++; /* Search for the start of text */
105  while (*ptr == ' ')
106  ptr++;
107 
108 #ifdef HAVE_OGR
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 #endif
118 #ifdef HAVE_POSTGRES
119  if (frmt == GV_FORMAT_POSTGIS) {
120  if (G_strcasecmp(buf1, "CONNINFO") == 0)
121  finfo->pg.conninfo = G_store(ptr);
122  if (G_strcasecmp(buf1, "SCHEMA") == 0)
123  finfo->pg.schema_name = G_store(ptr);
124  if (G_strcasecmp(buf1, "TABLE") == 0)
125  finfo->pg.table_name = G_store(ptr);
126  if (G_strcasecmp(buf1, "FID") == 0)
127  finfo->pg.fid_column = G_store(ptr);
128  if (G_strcasecmp(buf1, "WHERE") == 0)
129  finfo->pg.where = G_store(ptr);
130  }
131 #endif
132  }
133 
134 #ifdef HAVE_POSTGRES
135  /* if schema not defined, use 'public' */
136  if (frmt == GV_FORMAT_POSTGIS && !finfo->pg.schema_name) {
137  finfo->pg.schema_name = G_store("public");
138  }
139 
140  /* if fid column not defined, use default value */
141  if (frmt == GV_FORMAT_POSTGIS && !finfo->pg.fid_column) {
143  }
144 #endif
145 
146  return frmt;
147 }
148 
149 /* Write vector format, currently does not work
150  * Parse also connection string.
151  *
152  * Returns: 0 OK
153  * -1 on error
154  */
155 int dig_write_frmt_ascii(FILE *dascii UNUSED, struct Format_info *finfo UNUSED,
156  int format UNUSED)
157 {
158  G_debug(3, "dig_write_frmt_ascii()");
159 
160  G_fatal_error("Format not supported by dig_write_frmt_ascii()");
161 
162  return 0;
163 }
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
Definition: getl.c:65
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
int G_debug(int, const char *,...) __attribute__((format(printf
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
#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.
Definition: dig_defines.h:271
int dig_read_frmt_ascii(FILE *dascii, struct Format_info *finfo)
Read external vector format file.
Definition: frmt.c:32
int dig_write_frmt_ascii(FILE *dascii UNUSED, struct Format_info *finfo UNUSED, int format UNUSED)
Definition: frmt.c:155
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47
#define _(str)
Definition: glocale.h:10
#define strcpy
Definition: parson.c:62
Non-native format info (OGR)
Definition: dig_structs.h:505
char * dsn
OGR datasource name.
Definition: dig_structs.h:513
char * layer_name
OGR layer name.
Definition: dig_structs.h:517
char * where
SQL where statement (to filter features)
Definition: dig_structs.h:521
Non-native format info (PostGIS)
Definition: dig_structs.h:590
char * fid_column
FID column.
Definition: dig_structs.h:614
char * schema_name
Schema name.
Definition: dig_structs.h:602
char * where
SQL where statement (of filter features)
Definition: dig_structs.h:610
char * conninfo
Connection string.
Definition: dig_structs.h:594
char * table_name
Table name.
Definition: dig_structs.h:606
Non-native format info (currently only OGR is implemented)
Definition: dig_structs.h:700
struct Format_info_pg pg
PostGIS info.
Definition: dig_structs.h:712
struct Format_info_ogr ogr
OGR info.
Definition: dig_structs.h:708