replace.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**********************************************************
  2. * MODULE: mysql
  3. * AUTHOR(S): Radim Blazek (radim.blazek@gmail.com)
  4. * PURPOSE: MySQL database driver
  5. * COPYRIGHT: (C) 2001,2008 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/dbmi.h>
  14. #include <grass/gis.h>
  15. #include <grass/glocale.h>
  16. #include "globals.h"
  17. #include "proto.h"
  18. int replace_variables(char *in, char **datadir, char **database)
  19. {
  20. *datadir = NULL;
  21. *database = NULL;
  22. /* parse/replace variables in input string */
  23. char tmp[2000];
  24. char **tokens;
  25. int no_tokens, n;
  26. if (!strchr(in, '/')) { /* no path */
  27. *datadir = G_store("./");
  28. *database = G_store(in);
  29. }
  30. else {
  31. tokens = G_tokenize(in, "/");
  32. no_tokens = G_number_of_tokens(tokens);
  33. G_debug(3, "no_tokens = %d", no_tokens);
  34. tmp[0] = '\0';
  35. for (n = 0; n < no_tokens - 1; n++) {
  36. if (n > 0)
  37. strcat(tmp, "/");
  38. G_debug(3, "tokens[%d] = %s", n, tokens[n]);
  39. if (tokens[n][0] == '$') {
  40. G_strchg(tokens[n], '$', ' ');
  41. G_chop(tokens[n]);
  42. strcat(tmp, G__getenv(tokens[n]));
  43. G_debug(3, " -> %s", G__getenv(tokens[n]));
  44. }
  45. else {
  46. strcat(tmp, tokens[n]);
  47. }
  48. }
  49. *datadir = G_store(tmp);
  50. *database = G_store(tokens[n]);
  51. G_free_tokens(tokens);
  52. }
  53. G_debug(2, "datadir = '%s'", *datadir);
  54. G_debug(2, "database = '%s'", *database);
  55. return 1;
  56. }