main.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /****************************************************************************
  2. *
  3. * MODULE: db.databases
  4. * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
  5. * Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
  6. * PURPOSE: lists all databases for a given driver
  7. * COPYRIGHT: (C) 2002-2006 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. *****************************************************************************/
  14. #include <stdlib.h>
  15. #include <grass/dbmi.h>
  16. #include <grass/gis.h>
  17. #include <grass/codes.h>
  18. #include <grass/glocale.h>
  19. struct
  20. {
  21. char *driver;
  22. char *location;
  23. } parms;
  24. /* function prototypes */
  25. static void parse_command_line(int, char **);
  26. int main(int argc, char **argv)
  27. {
  28. dbDriver *driver;
  29. dbHandle *handles;
  30. dbString locations;
  31. int nlocs = 0;
  32. int count, i;
  33. db_init_string(&locations);
  34. parse_command_line(argc, argv);
  35. if (parms.location) {
  36. db_set_string(&locations, parms.location);
  37. nlocs = 1;
  38. }
  39. driver = db_start_driver(parms.driver);
  40. if (driver == NULL)
  41. G_fatal_error(_("Unable to start driver <%s>"), parms.driver);
  42. if (db_list_databases(driver, &locations, nlocs, &handles, &count) !=
  43. DB_OK)
  44. G_fatal_error(_("Unable to list databases"));
  45. db_shutdown_driver(driver);
  46. for (i = 0; i < count; i++) {
  47. fprintf(stdout, "%s", db_get_handle_dbname(&handles[i]));
  48. fprintf(stdout, "\n");
  49. }
  50. exit(EXIT_SUCCESS);
  51. }
  52. static void parse_command_line(int argc, char **argv)
  53. {
  54. struct Option *driver, *location;
  55. struct GModule *module;
  56. /* Initialize the GIS calls */
  57. G_gisinit(argv[0]);
  58. driver = G_define_standard_option(G_OPT_DB_DRIVER);
  59. driver->options = db_list_drivers();
  60. location = G_define_option();
  61. location->key = "location";
  62. location->type = TYPE_STRING;
  63. location->required = NO;
  64. location->multiple = YES;
  65. location->description = _("Location name");
  66. /* Set description */
  67. module = G_define_module();
  68. G_add_keyword(_("database"));
  69. G_add_keyword(_("SQL"));
  70. module->description =
  71. _("List all databases for a given driver and location.");
  72. if (G_parser(argc, argv))
  73. exit(EXIT_FAILURE);
  74. parms.driver = driver->answer;
  75. parms.location = location->answer;
  76. }