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