field.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*!
  2. \file field.c
  3. \brief Vector library - field(layer) related fns.
  4. Higher level functions for reading/writing/manipulating vectors.
  5. TODO: see Vect_read_dblinks; activate auto-FID detection once
  6. OGR_L_GetFIDColumn() is working or solution found if FID not available
  7. (C) 2001-2008 by the GRASS Development Team
  8. This program is free software under the
  9. GNU General Public License (>=v2).
  10. Read the file COPYING that comes with GRASS
  11. for details.
  12. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  13. Update to GRASS 5.7 Radim Blazek and David D. Gray.
  14. \date 2001-2008
  15. */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <grass/glocale.h>
  20. #include <grass/gis.h>
  21. #include <grass/dbmi.h>
  22. #include <grass/Vect.h>
  23. #include <gdal_version.h> /* needed for FID detection */
  24. /*!
  25. \brief Create and init new dblinks ctructure
  26. \param void
  27. \return pointer to new dblinks structure
  28. */
  29. struct dblinks *Vect_new_dblinks_struct(void)
  30. {
  31. struct dblinks *p;
  32. p = (struct dblinks *)G_malloc(sizeof(struct dblinks));
  33. if (p) {
  34. p->alloc_fields = p->n_fields = 0;
  35. p->field = NULL;
  36. }
  37. return p;
  38. }
  39. /*!
  40. \brief Reset dblinks structure
  41. \param p pointer to existing dblinks structure
  42. \return void
  43. */
  44. void Vect_reset_dblinks(struct dblinks *p)
  45. {
  46. p->n_fields = 0;
  47. }
  48. /*!
  49. \brief Add new db connection to Map_info structure
  50. \param Map vector map
  51. \param number layer number
  52. \param name layer name
  53. \param table table name
  54. \param key key name
  55. \param db database name
  56. \param driver driver name
  57. \return 0 OK
  58. \return -1 error
  59. */
  60. int
  61. Vect_map_add_dblink(struct Map_info *Map, int number, const char *name,
  62. const char *table, const char *key, const char *db,
  63. const char *driver)
  64. {
  65. int ret;
  66. if (number == 0) {
  67. G_warning(_("Layer number must be 1 or greater"));
  68. return -1;
  69. }
  70. if (Map->mode != GV_MODE_WRITE && Map->mode != GV_MODE_RW) {
  71. G_warning(_("Unable to add database link, map is not opened in WRITE mode"));
  72. return -1;
  73. }
  74. ret = Vect_add_dblink(Map->dblnk, number, name, table, key, db, driver);
  75. if (ret == -1) {
  76. G_warning(_("Unable to add database link"));
  77. return -1;
  78. }
  79. /* write it immediately otherwise it is lost if module crashes */
  80. ret = Vect_write_dblinks(Map);
  81. if (ret == -1) {
  82. G_warning(_("Unable to write database links"));
  83. return -1;
  84. }
  85. return 0;
  86. }
  87. /*!
  88. \brief Delete db connection from Map_info structure
  89. \param Map vector map
  90. \param field layer number
  91. \return 0 deleted
  92. \return -1 error
  93. */
  94. int Vect_map_del_dblink(struct Map_info *Map, int field)
  95. {
  96. int i, j, ret;
  97. struct dblinks *links;
  98. G_debug(4, "Vect_map_del_dblink() field = %d", field);
  99. links = Map->dblnk;
  100. ret = -1;
  101. for (i = 0; i < links->n_fields; i++) {
  102. if (links->field[i].number == field) { /* field found */
  103. for (j = i; j < links->n_fields - 1; j++) {
  104. links->field[j].number = links->field[j + 1].number;
  105. links->field[j].name = links->field[j + 1].name;
  106. links->field[j].table = links->field[j + 1].table;
  107. links->field[j].key = links->field[j + 1].key;
  108. links->field[j].database = links->field[j + 1].database;
  109. links->field[j].driver = links->field[j + 1].driver;
  110. }
  111. ret = 0;
  112. links->n_fields--;
  113. }
  114. }
  115. if (ret == -1)
  116. return -1;
  117. /* write it immediately otherwise it is lost if module crashes */
  118. ret = Vect_write_dblinks(Map);
  119. if (ret == -1) {
  120. G_warning(_("Unable to write database links"));
  121. return -1;
  122. }
  123. return 0;
  124. }
  125. /*!
  126. \brief Check if db connection exists in dblinks structure
  127. \param Map vector map
  128. \param field layer number
  129. \return 1 dblink for field exists
  130. \return 0 dblink does not exist for field
  131. */
  132. int Vect_map_check_dblink(struct Map_info *Map, int field)
  133. {
  134. return Vect_check_dblink(Map->dblnk, field);
  135. }
  136. /*!
  137. \brief Check if db connection exists in dblinks structure
  138. \param p pointer to existing dblinks structure
  139. \param field layer number
  140. \return 1 dblink for field exists
  141. \return 0 dblink does not exist for field
  142. */
  143. int Vect_check_dblink(struct dblinks *p, int field)
  144. {
  145. int i;
  146. G_debug(3, "Vect_check_dblink: field %d", field);
  147. for (i = 0; i < p->n_fields; i++) {
  148. if (p->field[i].number == field) {
  149. return 1;
  150. }
  151. }
  152. return 0;
  153. }
  154. /*!
  155. \brief Add new db connection to dblinks structure
  156. \param p pointer to existing dblinks structure
  157. \param number layer number
  158. \param name layer name
  159. \param key key name
  160. \param db database name
  161. \param driver driver name
  162. \return 0 OK
  163. \return -1 error
  164. */
  165. int
  166. Vect_add_dblink(struct dblinks *p, int number, const char *name,
  167. const char *table, const char *key, const char *db,
  168. const char *driver)
  169. {
  170. int ret;
  171. G_debug(3, "Field number <%d>, name <%s>", number, name);
  172. ret = Vect_check_dblink(p, number);
  173. if (ret == 1) {
  174. G_warning(_("Layer number %d or name <%s> already exists"), number,
  175. name);
  176. return -1;
  177. }
  178. if (p->n_fields == p->alloc_fields) {
  179. p->alloc_fields += 10;
  180. p->field = (struct field_info *)G_realloc((void *)p->field,
  181. p->alloc_fields *
  182. sizeof(struct field_info));
  183. }
  184. p->field[p->n_fields].number = number;
  185. if (name != NULL)
  186. p->field[p->n_fields].name = G_store(name);
  187. else
  188. p->field[p->n_fields].name = NULL;
  189. if (table != NULL)
  190. p->field[p->n_fields].table = G_store(table);
  191. else
  192. p->field[p->n_fields].table = NULL;
  193. if (key != NULL)
  194. p->field[p->n_fields].key = G_store(key);
  195. else
  196. p->field[p->n_fields].key = NULL;
  197. if (db != NULL)
  198. p->field[p->n_fields].database = G_store(db);
  199. else
  200. p->field[p->n_fields].database = NULL;
  201. if (driver != NULL)
  202. p->field[p->n_fields].driver = G_store(driver);
  203. else
  204. p->field[p->n_fields].driver = NULL;
  205. p->n_fields++;
  206. return 0;
  207. }
  208. /*!
  209. \brief Get default information about link to database for new dblink
  210. \param Map vector map
  211. \param field layer number
  212. \param field_name layer name
  213. \param type how many tables are linked to map: GV_1TABLE / GV_MTABLE
  214. \return pointer to new field_info structure
  215. */
  216. struct field_info
  217. *Vect_default_field_info(struct Map_info *Map,
  218. int field, const char *field_name, int type)
  219. {
  220. struct field_info *fi;
  221. char buf[1000], buf2[1000];
  222. const char *schema;
  223. const char *drv, *db;
  224. dbConnection connection;
  225. G_debug(1, "Vect_default_field_info(): map = %s field = %d", Map->name,
  226. field);
  227. db_get_connection(&connection);
  228. drv = G__getenv2("DB_DRIVER", G_VAR_MAPSET);
  229. db = G__getenv2("DB_DATABASE", G_VAR_MAPSET);
  230. G_debug(2, "drv = %s db = %s", drv, db);
  231. if (!connection.driverName && !connection.databaseName) {
  232. /* Set default values and create dbf db dir */
  233. db_set_default_connection();
  234. db_get_connection(&connection);
  235. G_warning(_("Default driver / database set to:\n"
  236. "driver: %s\ndatabase: %s"), connection.driverName,
  237. connection.databaseName);
  238. }
  239. /* they must be a matched pair, so if one is set but not the other
  240. then give up and let the user figure it out */
  241. else if (!connection.driverName) {
  242. G_fatal_error(_("Default driver is not set"));
  243. }
  244. else if (!connection.databaseName) {
  245. G_fatal_error(_("Default database is not set"));
  246. }
  247. drv = connection.driverName;
  248. db = connection.databaseName;
  249. fi = (struct field_info *)G_malloc(sizeof(struct field_info));
  250. fi->number = field;
  251. if (field_name != NULL)
  252. fi->name = G_store(field_name);
  253. else
  254. fi->name = NULL;
  255. /* Table name */
  256. if (type == GV_1TABLE) {
  257. sprintf(buf, "%s", Map->name);
  258. }
  259. else {
  260. if (field_name != NULL && strlen(field_name) > 0)
  261. sprintf(buf, "%s_%s", Map->name, field_name);
  262. else
  263. sprintf(buf, "%s_%d", Map->name, field);
  264. }
  265. schema = connection.schemaName;
  266. if (schema && strlen(schema) > 0) {
  267. sprintf(buf2, "%s.%s", schema, buf);
  268. fi->table = G_store(buf2);
  269. }
  270. else {
  271. fi->table = G_store(buf);
  272. }
  273. fi->key = G_store("cat"); /* Should be: id/fid/gfid/... ? */
  274. fi->database = G_store(db);
  275. fi->driver = G_store(drv);
  276. return (fi);
  277. }
  278. /*!
  279. \brief Get information about link to database.
  280. Variables are substituted by values, link is index to array of
  281. dblinks
  282. \param Map vector map
  283. \param link link id
  284. \return pointer to new field_info structure
  285. */
  286. struct field_info *Vect_get_dblink(struct Map_info *Map, int link)
  287. {
  288. struct field_info *fi;
  289. G_debug(1, "Vect_get_dblink(): link = %d", link);
  290. if (link >= Map->dblnk->n_fields) {
  291. G_warning(_("Requested dblink %d, maximum link number %d"), link,
  292. Map->dblnk->n_fields - 1);
  293. return NULL;
  294. }
  295. fi = (struct field_info *)malloc(sizeof(struct field_info));
  296. fi->number = Map->dblnk->field[link].number;
  297. if (Map->dblnk->field[link].name != NULL)
  298. fi->name = G_store(Map->dblnk->field[link].name);
  299. else
  300. fi->name = NULL;
  301. fi->table = G_store(Map->dblnk->field[link].table);
  302. fi->key = G_store(Map->dblnk->field[link].key);
  303. fi->database = Vect_subst_var(Map->dblnk->field[link].database, Map);
  304. fi->driver = G_store(Map->dblnk->field[link].driver);
  305. return fi;
  306. }
  307. /*!
  308. \brief Get information about link to database.
  309. Variables are substituted by values,
  310. field is number of requested field
  311. \param Map vector map
  312. \param field layer number
  313. \return pointer to new field_info structure
  314. \return NULL if not found
  315. */
  316. struct field_info *Vect_get_field(struct Map_info *Map, int field)
  317. {
  318. int i;
  319. struct field_info *fi = NULL;
  320. G_debug(1, "Vect_get_field(): field = %d", field);
  321. for (i = 0; i < Map->dblnk->n_fields; i++) {
  322. if (Map->dblnk->field[i].number == field) {
  323. fi = Vect_get_dblink(Map, i);
  324. break;
  325. }
  326. }
  327. return fi;
  328. }
  329. /*!
  330. \brief Read dblinks to existing structure.
  331. Variables are not substituted by values.
  332. \param Map vector map
  333. \return number of links read
  334. \return -1 on error
  335. */
  336. int Vect_read_dblinks(struct Map_info *Map)
  337. {
  338. int ndef;
  339. FILE *fd;
  340. char file[1024], buf[2001];
  341. char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname;
  342. int fld;
  343. char *c;
  344. int row, rule;
  345. struct dblinks *dbl;
  346. G_debug(1, "Vect_read_dblinks(): map = %s, mapset = %s", Map->name,
  347. Map->mapset);
  348. dbl = Map->dblnk;
  349. Vect_reset_dblinks(dbl);
  350. G_debug(3, "Searching for FID column in OGR DB");
  351. if (Map->format == GV_FORMAT_OGR) {
  352. #if GDAL_VERSION_NUM > 1320 /* seems to be fixed after 1320 release */
  353. int layer, nLayers;
  354. OGRDataSourceH Ogr_ds;
  355. OGRLayerH Ogr_layer = NULL;
  356. OGRFeatureDefnH Ogr_featuredefn;
  357. char ogr_fid_col[1024];
  358. G_debug(3, "GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
  359. /* we open the connection to fetch the FID column name */
  360. OGRRegisterAll();
  361. /*Data source handle */
  362. Ogr_ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
  363. if (Ogr_ds == NULL)
  364. G_fatal_error("Cannot open OGR data source '%s'",
  365. Map->fInfo.ogr.dsn);
  366. Map->fInfo.ogr.ds = Ogr_ds;
  367. /* Layer number */
  368. layer = -1;
  369. nLayers = OGR_DS_GetLayerCount(Ogr_ds); /* Layers = Maps in OGR DB */
  370. G_debug(3, "%d layers (maps) found in data source", nLayers);
  371. G_debug(3, "Trying to open OGR layer: %s", Map->fInfo.ogr.layer_name);
  372. Ogr_layer = OGR_DS_GetLayerByName(Ogr_ds, Map->fInfo.ogr.layer_name);
  373. if (Ogr_layer == NULL) {
  374. OGR_DS_Destroy(Ogr_ds);
  375. G_fatal_error("Cannot open layer '%s'",
  376. Map->fInfo.ogr.layer_name);
  377. }
  378. Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
  379. G_debug(3, "layer %s, FID col name: %s",
  380. OGR_FD_GetName(Ogr_featuredefn),
  381. OGR_L_GetFIDColumn(Ogr_layer));
  382. Map->fInfo.ogr.layer = Ogr_layer;
  383. G_debug(3, "OGR Map->fInfo.ogr.layer %p opened",
  384. Map->fInfo.ogr.layer);
  385. /* TODO what to do if OGR_L_GetFIDColumn() doesn't return FID name */
  386. sprintf(ogr_fid_col, "%s", OGR_L_GetFIDColumn(Map->fInfo.ogr.layer));
  387. G_debug(3, "Using FID column <%s> in OGR DB", ogr_fid_col);
  388. Vect_add_dblink(dbl, 1, NULL, Map->fInfo.ogr.layer_name, ogr_fid_col,
  389. Map->fInfo.ogr.dsn, "ogr");
  390. #else
  391. dbDriver *driver;
  392. dbCursor cursor;
  393. dbString sql;
  394. int FID = 0, OGC_FID = 0, OGR_FID = 0, GID = 0;
  395. G_debug(3, "GDAL_VERSION_NUM: %d", GDAL_VERSION_NUM);
  396. /* FID is not available for all OGR drivers */
  397. db_init_string(&sql);
  398. driver = db_start_driver_open_database("ogr", Map->fInfo.ogr.dsn);
  399. if (driver == NULL) {
  400. G_warning(_("Unable to open OGR DBMI driver"));
  401. return -1;
  402. }
  403. /* this is a bit stupid, but above FID auto-detection doesn't work yet...: */
  404. db_auto_print_errors(0);
  405. sprintf(buf, "select FID from %s where FID > 0",
  406. Map->fInfo.ogr.layer_name);
  407. db_set_string(&sql, buf);
  408. if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
  409. DB_OK) {
  410. /* FID not available, so we try ogc_fid */
  411. G_debug(3, "Failed. Now searching for ogc_fid column in OGR DB");
  412. sprintf(buf, "select ogc_fid from %s where ogc_fid > 0",
  413. Map->fInfo.ogr.layer_name);
  414. db_set_string(&sql, buf);
  415. if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) !=
  416. DB_OK) {
  417. /* Neither FID nor ogc_fid available, so we try ogr_fid */
  418. G_debug(3,
  419. "Failed. Now searching for ogr_fid column in OGR DB");
  420. sprintf(buf, "select ogr_fid from %s where ogr_fid > 0",
  421. Map->fInfo.ogr.layer_name);
  422. db_set_string(&sql, buf);
  423. if (db_open_select_cursor
  424. (driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
  425. /* Neither FID nor ogc_fid available, so we try gid */
  426. G_debug(3,
  427. "Failed. Now searching for gid column in OGR DB");
  428. sprintf(buf, "select gid from %s where gid > 0",
  429. Map->fInfo.ogr.layer_name);
  430. db_set_string(&sql, buf);
  431. if (db_open_select_cursor
  432. (driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) {
  433. /* neither FID nor ogc_fid nor ogr_fid nor gid available */
  434. G_warning(_("All FID tests failed. Neither 'FID' nor 'ogc_fid' "
  435. "nor 'ogr_fid' nor 'gid' available in OGR DB table"));
  436. db_close_database_shutdown_driver(driver);
  437. return 0;
  438. }
  439. else
  440. GID = 1;
  441. }
  442. else
  443. OGR_FID = 1;
  444. }
  445. else
  446. OGC_FID = 1;
  447. }
  448. else
  449. FID = 1;
  450. G_debug(3, "FID: %d, OGC_FID: %d, OGR_FID: %d, GID: %d", FID, OGC_FID,
  451. OGR_FID, GID);
  452. db_close_cursor(&cursor);
  453. db_close_database_shutdown_driver(driver);
  454. db_auto_print_errors(1);
  455. if (FID) {
  456. G_debug(3, "Using FID column in OGR DB");
  457. Vect_add_dblink(dbl, 1, NULL, Map->fInfo.ogr.layer_name, "FID",
  458. Map->fInfo.ogr.dsn, "ogr");
  459. }
  460. else {
  461. if (OGC_FID) {
  462. G_debug(3, "Using ogc_fid column in OGR DB");
  463. Vect_add_dblink(dbl, 1, NULL, Map->fInfo.ogr.layer_name,
  464. "ogc_fid", Map->fInfo.ogr.dsn, "ogr");
  465. }
  466. else {
  467. if (OGR_FID) {
  468. G_debug(3, "Using ogr_fid column in OGR DB");
  469. Vect_add_dblink(dbl, 1, NULL, Map->fInfo.ogr.layer_name,
  470. "ogr_fid", Map->fInfo.ogr.dsn, "ogr");
  471. }
  472. else {
  473. if (GID) {
  474. G_debug(3, "Using gid column in OGR DB");
  475. Vect_add_dblink(dbl, 1, NULL,
  476. Map->fInfo.ogr.layer_name, "gid",
  477. Map->fInfo.ogr.dsn, "ogr");
  478. }
  479. }
  480. }
  481. }
  482. #endif /* GDAL_VERSION_NUM > 1320 */
  483. return (1);
  484. }
  485. else if (Map->format != GV_FORMAT_NATIVE) {
  486. G_fatal_error(_("Don't know how to read links for format %d"),
  487. Map->format);
  488. }
  489. sprintf(file, "%s/%s/%s/%s/%s/%s", Map->gisdbase, Map->location,
  490. Map->mapset, GRASS_VECT_DIRECTORY, Map->name,
  491. GRASS_VECT_DBLN_ELEMENT);
  492. G_debug(1, "dbln file: %s", file);
  493. fd = fopen(file, "r");
  494. if (fd == NULL) { /* This may be correct, no tables defined */
  495. G_debug(1, "Cannot open vector database definition file");
  496. return (-1);
  497. }
  498. row = 0;
  499. rule = 0;
  500. while (G_getl2(buf, 2000, fd)) {
  501. row++;
  502. G_chop(buf);
  503. G_debug(1, "dbln: %s", buf);
  504. c = (char *)strchr(buf, '#');
  505. if (c != NULL)
  506. *c = '\0';
  507. if (strlen(buf) == 0)
  508. continue;
  509. ndef = sscanf(buf, "%s %s %s %s %s", fldstr, tab, col, db, drv);
  510. if (ndef < 2 || (ndef < 5 && rule < 1)) {
  511. G_warning(_("Error in rule on row %d in %s"), row, file);
  512. continue;
  513. }
  514. /* get field and field name */
  515. fldname = strchr(fldstr, '/');
  516. if (fldname != NULL) { /* field has name */
  517. fldname[0] = 0;
  518. fldname++;
  519. }
  520. fld = atoi(fldstr);
  521. Vect_add_dblink(dbl, fld, fldname, tab, col, db, drv);
  522. G_debug(1,
  523. "field = %d name = %s, table = %s, key = %s, database = %s, driver = %s",
  524. fld, fldname, tab, col, db, drv);
  525. rule++;
  526. }
  527. fclose(fd);
  528. G_debug(1, "Dblinks read");
  529. return (rule);
  530. }
  531. /*!
  532. \brief Write dblinks to file
  533. \param Map vector map
  534. \return 0 OK
  535. \return -1 on error
  536. */
  537. int Vect_write_dblinks(struct Map_info *Map)
  538. {
  539. int i;
  540. FILE *fd;
  541. char file[GPATH_MAX], buf[GPATH_MAX];
  542. struct dblinks *dbl;
  543. G_debug(1, "Vect_write_dblinks(): map = %s, mapset = %s", Map->name,
  544. Map->mapset);
  545. dbl = Map->dblnk;
  546. sprintf(file, "%s/%s/%s/%s/%s/%s", Map->gisdbase, Map->location,
  547. Map->mapset, GRASS_VECT_DIRECTORY, Map->name,
  548. GRASS_VECT_DBLN_ELEMENT);
  549. G_debug(1, "dbln file: %s", file);
  550. fd = fopen(file, "w");
  551. if (fd == NULL) { /* This may be correct, no tables defined */
  552. G_warning(_("Unable to open vector database definition file '%s'"),
  553. file);
  554. return (-1);
  555. }
  556. for (i = 0; i < dbl->n_fields; i++) {
  557. if (dbl->field[i].name != NULL)
  558. sprintf(buf, "%d/%s", dbl->field[i].number, dbl->field[i].name);
  559. else
  560. sprintf(buf, "%d", dbl->field[i].number);
  561. fprintf(fd, "%s %s %s %s %s\n", buf, dbl->field[i].table,
  562. dbl->field[i].key, dbl->field[i].database,
  563. dbl->field[i].driver);
  564. G_debug(1, "%s %s %s %s %s", buf, dbl->field[i].table,
  565. dbl->field[i].key, dbl->field[i].database,
  566. dbl->field[i].driver);
  567. }
  568. fclose(fd);
  569. G_debug(1, "Dblinks written");
  570. return 0;
  571. }
  572. /*!
  573. \brief Substitute variable in string
  574. \param in current string
  575. \param Map vector map
  576. \return pointer to new string
  577. */
  578. char *Vect_subst_var(const char *in, struct Map_info *Map)
  579. {
  580. char *c;
  581. char buf[1000], str[1000];
  582. G_debug(3, "Vect_subst_var(): in = %s, map = %s, mapset = %s", in,
  583. Map->name, Map->mapset);
  584. strcpy(str, in);
  585. strcpy(buf, str);
  586. c = (char *)strstr(buf, "$GISDBASE");
  587. if (c != NULL) {
  588. *c = '\0';
  589. sprintf(str, "%s%s%s", buf, Map->gisdbase, c + 9);
  590. }
  591. strcpy(buf, str);
  592. c = (char *)strstr(buf, "$LOCATION_NAME");
  593. if (c != NULL) {
  594. *c = '\0';
  595. sprintf(str, "%s%s%s", buf, Map->location, c + 14);
  596. }
  597. strcpy(buf, str);
  598. c = (char *)strstr(buf, "$MAPSET");
  599. if (c != NULL) {
  600. *c = '\0';
  601. sprintf(str, "%s%s%s", buf, Map->mapset, c + 7);
  602. }
  603. strcpy(buf, str);
  604. c = (char *)strstr(buf, "$MAP");
  605. if (c != NULL) {
  606. *c = '\0';
  607. sprintf(str, "%s%s%s", buf, Map->name, c + 4);
  608. }
  609. G_debug(3, " -> %s", str);
  610. return (G_store(str));
  611. }
  612. /*!
  613. \brief Rewrite 'dbln' file
  614. Should be used by GRASS modules which update
  615. database tables, so that other applications know that tables
  616. were changed and can reload data.
  617. \param Map vector map
  618. \return void
  619. */
  620. void Vect_set_db_updated(struct Map_info *Map)
  621. {
  622. if (strcmp(Map->mapset, G_mapset()) != 0) {
  623. G_fatal_error(_("Bug: attempt to update map which is not in current mapset"));
  624. }
  625. Vect_write_dblinks(Map);
  626. }