parse.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**********************************************************
  2. * MODULE: mysql
  3. * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  4. * PURPOSE: MySQL database driver
  5. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  6. * This program is free software under the
  7. * GNU General Public License (>=v2).
  8. * Read the file COPYING that comes with GRASS
  9. * for details.
  10. **********************************************************/
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <grass/gis.h>
  14. #include <grass/dbmi.h>
  15. #include <grass/glocale.h>
  16. #include "globals.h"
  17. #include "proto.h"
  18. /*
  19. * \brief Parse connection string in form:
  20. * 1) 'database_name'
  21. * 2) 'host=xx,port=xx,dbname=xx'
  22. * \return DB_OK Success
  23. * \return DB_FAILED Failed to parse database
  24. */
  25. int parse_conn(const char *str, CONNPAR * conn)
  26. {
  27. int i;
  28. char **tokens, delm[2];
  29. G_debug(3, "parse_conn : %s", str);
  30. /* reset */
  31. conn->host = NULL;
  32. conn->port = 0;
  33. conn->dbname = NULL;
  34. conn->user = NULL;
  35. conn->password = NULL;
  36. if (strchr(str, '=') == NULL) { /*db name only */
  37. conn->dbname = G_store(str);
  38. }
  39. else {
  40. delm[0] = ',';
  41. delm[1] = '\0';
  42. tokens = G_tokenize(str, delm);
  43. i = 0;
  44. while (tokens[i]) {
  45. G_debug(3, "token %d : %s", i, tokens[i]);
  46. if (strncmp(tokens[i], "host", 4) == 0) {
  47. conn->host = G_store(tokens[i] + 5);
  48. }
  49. else if (strncmp(tokens[i], "port", 4) == 0) {
  50. long port = atol(tokens[i] + 5);
  51. if (port <= 0) {
  52. db_d_append_error("%s %s",
  53. _("Wrong port number in MySQL database "
  54. "definition: "),
  55. tokens[i] + 5);
  56. return DB_FAILED;
  57. }
  58. conn->port = (unsigned int)port;
  59. }
  60. else if (strncmp(tokens[i], "dbname", 6) == 0) {
  61. conn->dbname = G_store(tokens[i] + 7);
  62. }
  63. else if (strncmp(tokens[i], "user", 4) == 0) {
  64. G_warning(_("'user' in database definition "
  65. "is not supported, use db.login"));
  66. }
  67. else if (strncmp(tokens[i], "password", 8) == 0) {
  68. G_warning(_("'password' in database definition "
  69. "is not supported, use db.login"));
  70. }
  71. else {
  72. db_d_append_error("%s %s",
  73. _("Unknown option in database definition "
  74. "for MySQL: "),
  75. tokens[i]);
  76. return DB_FAILED;
  77. }
  78. i++;
  79. }
  80. G_free_tokens(tokens);
  81. }
  82. return DB_OK;
  83. }