parse.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 ( 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. } else {
  39. delm[0] = ','; delm[1] = '\0';
  40. tokens = G_tokenize ( str, delm );
  41. i = 0;
  42. while ( tokens[i] ) {
  43. G_debug (3, "token %d : %s", i, tokens[i] );
  44. if ( strncmp(tokens[i], "host", 4 ) == 0 )
  45. {
  46. conn->host = G_store ( tokens[i] + 5 );
  47. }
  48. else if ( strncmp(tokens[i], "port", 4 ) == 0 )
  49. {
  50. long port = atol( tokens[i] + 5 );
  51. if ( port <= 0 )
  52. {
  53. append_error ( _("Wrong port number in MySQL database "
  54. "definition: ") );
  55. append_error ( 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. {
  62. conn->dbname = G_store ( tokens[i] + 7 );
  63. }
  64. else if ( strncmp(tokens[i], "user", 4 ) == 0 )
  65. {
  66. G_warning ( _("'user' in database definition "
  67. "is not supported, use db.login") );
  68. }
  69. else if ( strncmp(tokens[i], "password", 8 ) == 0 )
  70. {
  71. G_warning ( _("'password' in database definition "
  72. "is not supported, use db.login") );
  73. }
  74. else
  75. {
  76. append_error ( _("Unknown option in database definition for ") );
  77. append_error ( "MySQL: " );
  78. append_error ( tokens[i] );
  79. return DB_FAILED;
  80. }
  81. i++;
  82. }
  83. G_free_tokens ( tokens );
  84. }
  85. return DB_OK;
  86. }