parse.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/dbmi.h>
  5. #include "globals.h"
  6. #include "proto.h"
  7. #include <grass/glocale.h>
  8. /*
  9. * \brief Parse connection string in form:
  10. * 1) 'database_name'
  11. * 2) 'host=xx,port=xx,dbname=xx'
  12. *
  13. * returns: DB_OK - OK
  14. * DB_FAILED - error
  15. */
  16. int parse_conn(const char *str, PGCONN * pgconn)
  17. {
  18. int i;
  19. char **tokens, delm[2];
  20. /* reset */
  21. pgconn->host = NULL;
  22. pgconn->port = NULL;
  23. pgconn->options = NULL;
  24. pgconn->tty = NULL;
  25. pgconn->dbname = NULL;
  26. pgconn->user = NULL;
  27. pgconn->password = NULL;
  28. pgconn->schema = NULL;
  29. G_debug(3, "parse_conn : %s", str);
  30. if (strchr(str, '=') == NULL) { /*db name only */
  31. pgconn->dbname = G_store(str);
  32. }
  33. else {
  34. delm[0] = ',';
  35. delm[1] = '\0';
  36. tokens = G_tokenize(str, delm);
  37. i = 0;
  38. while (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. append_error(_("Unknown option in database definition for PostgreSQL: "));
  60. append_error(tokens[i]);
  61. return DB_FAILED;
  62. }
  63. i++;
  64. }
  65. G_free_tokens(tokens);
  66. }
  67. return DB_OK;
  68. }