1 #include <stdlib.h>
2 #include <string.h>
3
4 #include <grass/gis.h>
5 #include <grass/glocale.h>
6
7 #include "local_proto.h"
8
9 static int print_status_file(const char *, int);
10 static void print_key_value(const char *, const char *, int);
11 static void check_required_options(struct Key_Value *, int);
12
print_status(int shell)13 void print_status(int shell)
14 {
15 /* try to print OGR first then PG if needed */
16 if (print_status_file("OGR", shell))
17 return;
18
19 if (print_status_file("PG", shell))
20 return;
21
22 if (shell)
23 fprintf(stdout, "format=%s\n", "native");
24 else
25 fprintf(stdout, _("format: native\n"));
26 }
27
print_key_value(const char * key,const char * value,int shell)28 void print_key_value(const char *key, const char *value, int shell)
29 {
30 if (!value && !shell)
31 return;
32
33 if (shell)
34 fprintf(stdout, "%s=%s\n", key, value ? value : "");
35 else
36 fprintf(stdout, "%s: %s\n", key, value);
37 }
38
print_status_file(const char * file,int shell)39 int print_status_file(const char *file, int shell)
40 {
41 int i;
42 FILE *fp;
43
44 struct Key_Value *key_val;
45
46 fp = G_fopen_old("", file, G_mapset());
47 if (!fp)
48 return FALSE;
49
50 key_val = G_fread_key_value(fp);
51 fclose(fp);
52
53 /* check required options */
54 check_required_options(key_val, strcmp(file, "OGR") == 0);
55
56 /* print all options */
57 for (i = 0; i < key_val->nitems; i++)
58 print_key_value(key_val->key[i], key_val->value[i], shell);
59
60 G_free_key_value(key_val);
61
62 return TRUE;
63 }
64
save_status_file(const struct Option * file)65 int save_status_file(const struct Option *file)
66 {
67 int use_ogr;
68 FILE *fp_input, *fp_output;
69
70 struct Key_Value *key_val;
71
72 /* read settings file */
73 use_ogr = FALSE;
74 fp_input = G_fopen_old("", "PG", G_mapset());
75 if (!fp_input) {
76 use_ogr = TRUE;
77 fp_input = G_fopen_old("", "OGR", G_mapset());
78 }
79 if (!fp_input)
80 G_fatal_error(_("No settings defined"));
81
82 /* read settings from file */
83 key_val = G_fread_key_value(fp_input);
84 fclose(fp_input);
85
86 /* check required options */
87 check_required_options(key_val, use_ogr);
88
89 /* open output file */
90 fp_output = G_open_option_file(file);
91
92 /* write settings to output file */
93 G_fwrite_key_value(fp_output, key_val);
94
95 G_close_option_file(fp_output);
96
97 G_free_key_value(key_val);
98
99 return TRUE;
100 }
101
check_required_options(struct Key_Value * key_val,int use_ogr)102 void check_required_options(struct Key_Value *key_val, int use_ogr)
103 {
104 const char *p;
105
106 /* format (required) */
107 p = G_find_key_value("format", key_val);
108 if (!p)
109 G_fatal_error(_("Format not defined"));
110
111 /* check required options */
112 if (use_ogr) { /* OGR */
113 /* dsn (required) */
114 p = G_find_key_value("dsn", key_val);
115 if (!p)
116 G_fatal_error(_("OGR datasource (dsn) not defined"));
117 }
118 else { /* PG */
119 char dsn_name[GNAME_MAX];
120
121 /* conninfo (required) */
122 p = G_find_key_value("conninfo", key_val);
123 if (!p)
124 G_fatal_error(_("PG connection info (conninfo) not defined"));
125
126 /* add dsn for compatibility */
127 sprintf(dsn_name, "PG:%s", p);
128 G_set_key_value("dsn", dsn_name, key_val);
129 }
130 }
131
read_status_file(const struct Option * file)132 int read_status_file(const struct Option *file)
133 {
134 int use_ogr;
135 const char *format;
136 FILE *fp_input, *fp_output;
137
138 struct Key_Value *key_val;
139
140 /* read settings file */
141 fp_input = G_open_option_file(file);
142
143 /* read settings from file */
144 key_val = G_fread_key_value(fp_input);
145 G_close_option_file(fp_input);
146
147 format = G_find_key_value("format", key_val);
148 if (!format)
149 G_fatal_error(_("Format not defined"));
150 use_ogr = is_ogr(format);
151
152 /* check required options */
153 check_required_options(key_val, use_ogr);
154
155 /* write settings to output file */
156 fp_output = G_fopen_new("", use_ogr ? "OGR" : "PG");
157 if (!fp_output)
158 G_fatal_error(_("Unable to create settings file"));
159 if (G_fwrite_key_value(fp_output, key_val) < 0)
160 G_fatal_error(_("Error writing settings file"));
161 fclose(fp_output);
162
163 G_free_key_value(key_val);
164
165 return TRUE;
166 }
167