parse.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. } else {
  33. delm[0] = ','; delm[1] = '\0';
  34. tokens = G_tokenize ( str, delm );
  35. i = 0;
  36. while ( tokens[i] ) {
  37. G_debug (3, "token %d : %s", i, tokens[i] );
  38. if ( strncmp(tokens[i], "host", 4 ) == 0 )
  39. pgconn->host = G_store ( tokens[i] + 5 );
  40. else if ( strncmp(tokens[i], "port", 4 ) == 0 )
  41. pgconn->port = G_store ( tokens[i] + 5 );
  42. else if ( strncmp(tokens[i], "options", 7 ) == 0 )
  43. pgconn->options = G_store ( tokens[i] + 8 );
  44. else if ( strncmp(tokens[i], "tty", 3 ) == 0 )
  45. pgconn->tty = G_store ( tokens[i] + 4 );
  46. else if ( strncmp(tokens[i], "dbname", 6 ) == 0 )
  47. pgconn->dbname = G_store ( tokens[i] + 7 );
  48. else if ( strncmp(tokens[i], "user", 4 ) == 0 )
  49. G_warning ( _("'user' in database definition is not supported, use db.login") );
  50. /* pgconn->user = G_store ( tokens[i] + 5 ); */
  51. else if ( strncmp(tokens[i], "password", 8 ) == 0 )
  52. /* pgconn->password = G_store ( tokens[i] + 9 ); */
  53. G_warning ( _("'password' in database definition is not supported, use db.login") );
  54. else if ( strncmp(tokens[i], "schema", 6 ) == 0 )
  55. pgconn->schema = G_store ( tokens[i] + 7 );
  56. else {
  57. append_error ( _("Unknown option in database definition for PostgreSQL: "));
  58. append_error ( tokens[i] );
  59. return DB_FAILED;
  60. }
  61. i++;
  62. }
  63. G_free_tokens ( tokens );
  64. }
  65. return DB_OK;
  66. }