parse.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*!
  2. \file db/driver/postgres/parse.c
  3. \brief DBMI - Low Level PostgreSQL database driver - parse connection string
  4. This program is free software under the GNU General Public License
  5. (>=v2). Read the file COPYING that comes with GRASS for details.
  6. \author Radim Blazek
  7. */
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <grass/gis.h>
  11. #include <grass/dbmi.h>
  12. #include "globals.h"
  13. #include "proto.h"
  14. #include <grass/glocale.h>
  15. /*
  16. \brief Parse connection string in form:
  17. 1) 'database_name'
  18. 2) 'host=xx,port=xx,dbname=xx'
  19. \returns DB_OK on success
  20. \return DB_FAILED on failure
  21. */
  22. int parse_conn(const char *str, PGCONN * pgconn)
  23. {
  24. int i;
  25. char **tokens, delm[2];
  26. /* reset */
  27. G_zero(pgconn, sizeof(PGCONN));
  28. G_debug(3, "parse_conn: '%s'", str);
  29. if (strchr(str, '=') == NULL) { /* db name only */
  30. pgconn->dbname = G_store(str);
  31. }
  32. else {
  33. delm[0] = ',';
  34. delm[1] = '\0';
  35. tokens = G_tokenize(str, delm);
  36. i = 0;
  37. while (tokens[i]) {
  38. G_chop(tokens[i]);
  39. G_debug(3, "token %d : %s", i, tokens[i]);
  40. if (strncmp(tokens[i], "host", 4) == 0)
  41. pgconn->host = G_store(tokens[i] + 5);
  42. else if (strncmp(tokens[i], "port", 4) == 0)
  43. pgconn->port = G_store(tokens[i] + 5);
  44. else if (strncmp(tokens[i], "options", 7) == 0)
  45. pgconn->options = G_store(tokens[i] + 8);
  46. else if (strncmp(tokens[i], "tty", 3) == 0)
  47. pgconn->tty = G_store(tokens[i] + 4);
  48. else if (strncmp(tokens[i], "dbname", 6) == 0)
  49. pgconn->dbname = G_store(tokens[i] + 7);
  50. else if (strncmp(tokens[i], "user", 4) == 0)
  51. G_warning(_("'user' in database definition is not supported, use db.login"));
  52. /* pgconn->user = G_store ( tokens[i] + 5 ); */
  53. else if (strncmp(tokens[i], "password", 8) == 0)
  54. /* pgconn->password = G_store ( tokens[i] + 9 ); */
  55. G_warning(_("'password' in database definition is not supported, use db.login"));
  56. else if (strncmp(tokens[i], "schema", 6) == 0)
  57. pgconn->schema = G_store(tokens[i] + 7);
  58. else {
  59. db_d_append_error("%s %s",
  60. _("Unknown option in database definition "
  61. "for PostgreSQL: "),
  62. tokens[i]);
  63. return DB_FAILED;
  64. }
  65. i++;
  66. }
  67. G_free_tokens(tokens);
  68. }
  69. return DB_OK;
  70. }