parse.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_chop(tokens[i]);
  46. G_debug(3, "token %d : %s", i, tokens[i]);
  47. if (strncmp(tokens[i], "host", 4) == 0) {
  48. conn->host = G_store(tokens[i] + 5);
  49. }
  50. else if (strncmp(tokens[i], "port", 4) == 0) {
  51. long port = atol(tokens[i] + 5);
  52. if (port <= 0) {
  53. db_d_append_error("%s %s",
  54. _("Wrong port number in MySQL database "
  55. "definition: "),
  56. tokens[i] + 5);
  57. return DB_FAILED;
  58. }
  59. conn->port = (unsigned int)port;
  60. }
  61. else if (strncmp(tokens[i], "dbname", 6) == 0) {
  62. conn->dbname = G_store(tokens[i] + 7);
  63. }
  64. else if (strncmp(tokens[i], "user", 4) == 0) {
  65. G_warning(_("'user' in database definition "
  66. "is not supported, use db.login"));
  67. }
  68. else if (strncmp(tokens[i], "password", 8) == 0) {
  69. G_warning(_("'password' in database definition "
  70. "is not supported, use db.login"));
  71. }
  72. else {
  73. db_d_append_error("%s %s",
  74. _("Unknown option in database definition "
  75. "for MySQL: "),
  76. tokens[i]);
  77. return DB_FAILED;
  78. }
  79. i++;
  80. }
  81. G_free_tokens(tokens);
  82. }
  83. return DB_OK;
  84. }