parse.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. append_error(_("Wrong port number in MySQL database "
  53. "definition: "));
  54. append_error(tokens[i] + 5);
  55. return DB_FAILED;
  56. }
  57. conn->port = (unsigned int)port;
  58. }
  59. else if (strncmp(tokens[i], "dbname", 6) == 0) {
  60. conn->dbname = G_store(tokens[i] + 7);
  61. }
  62. else if (strncmp(tokens[i], "user", 4) == 0) {
  63. G_warning(_("'user' in database definition "
  64. "is not supported, use db.login"));
  65. }
  66. else if (strncmp(tokens[i], "password", 8) == 0) {
  67. G_warning(_("'password' in database definition "
  68. "is not supported, use db.login"));
  69. }
  70. else {
  71. append_error(_("Unknown option in database definition for "));
  72. append_error("MySQL: ");
  73. append_error(tokens[i]);
  74. return DB_FAILED;
  75. }
  76. i++;
  77. }
  78. G_free_tokens(tokens);
  79. }
  80. return DB_OK;
  81. }