1 #include <string.h>
2
3 #include <grass/gis.h>
4 #include <grass/glocale.h>
5
6 #ifdef HAVE_OGR
7 #include "ogr_api.h"
8 #endif /* HAVE_OGR */
9
10 static int cmp(const void *, const void *);
11 static char **format_list(int *, size_t *);
12
cmp(const void * a,const void * b)13 int cmp(const void *a, const void *b)
14 {
15 return strcmp(*(char **)a, *(char **)b);
16 }
17
format_list(int * count,size_t * len)18 char **format_list(int *count, size_t *len)
19 {
20 int i;
21 char **list;
22
23 list = NULL;
24 *count = 0;
25 if (len)
26 *len = 0;
27
28 #ifdef HAVE_OGR
29 char buf[2000];
30
31 OGRSFDriverH Ogr_driver;
32
33 /* Open OGR DSN */
34 OGRRegisterAll();
35 G_debug(2, "driver count = %d", OGRGetDriverCount());
36 for (i = 0; i < OGRGetDriverCount(); i++) {
37 /* only fetch read/write drivers */
38 if (!OGR_Dr_TestCapability(OGRGetDriver(i), ODrCCreateDataSource))
39 continue;
40
41 Ogr_driver = OGRGetDriver(i);
42 G_debug(2, "driver %d/%d : %s", i, OGRGetDriverCount(),
43 OGR_Dr_GetName(Ogr_driver));
44
45 list = G_realloc(list, ((*count) + 1) * sizeof(char *));
46
47 /* chg white space to underscore in OGR driver names */
48 sprintf(buf, "%s", OGR_Dr_GetName(Ogr_driver));
49 G_strchg(buf, ' ', '_');
50 list[(*count)++] = G_store(buf);
51 if (len)
52 *len += strlen(buf) + 1; /* + ',' */
53 }
54
55 /* order formats by name */
56 qsort(list, *count, sizeof(char *), cmp);
57 #endif
58 #if defined HAVE_POSTGRES && !defined HAVE_OGR
59 list = G_realloc(list, ((*count) + 1) * sizeof(char *));
60 list[(*count)++] = G_store("PostgreSQL");
61 if (len)
62 *len += strlen("PostgreSQL") + 1;
63 #endif
64
65 return list;
66 }
67
format_options()68 char *format_options()
69 {
70 int i, count;
71 char **list, *ret;
72 size_t len;
73
74 ret = NULL;
75 list = format_list(&count, &len);
76
77 if (len > 0) {
78 ret = G_malloc((len + 1) * sizeof(char)); /* \0 */
79 *ret = '\0';
80 for (i = 0; i < count; i++) {
81 if (i > 0)
82 strcat(ret, ",");
83 strcat(ret, list[i]);
84 G_free(list[i]);
85 }
86 G_free(list);
87 }
88 else {
89 ret = G_store("");
90 }
91
92 G_debug(2, "all drivers: %s", ret);
93
94 return ret;
95 }
96
list_formats(void)97 void list_formats(void)
98 {
99 int i, count;
100 char **list;
101
102 G_message(_("Supported formats:"));
103
104 list = format_list(&count, NULL);
105
106 for (i = 0; i < count; i++)
107 fprintf(stdout, "%s\n", list[i]);
108 fflush(stdout);
109
110 G_free(list);
111 }
112