get_projname.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: GRASS 5 gis library, get_projname.c
  5. * AUTHOR(S): unknown
  6. * PURPOSE: Get projection name from user
  7. * COPYRIGHT: (C) 2000 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 <string.h>
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <grass/gis.h>
  18. #include <grass/glocale.h>
  19. int G_ask_proj_name(char *proj_id, char *proj_name)
  20. {
  21. char path[GPATH_MAX], buff[GPATH_MAX], answer[50], *a;
  22. struct Key_Value *in_proj_keys;
  23. char *Tmp_file;
  24. FILE *Tmp_fd = NULL;
  25. int in_stat, i, npr;
  26. sprintf(path, "%s/etc/projections", G_gisbase());
  27. while (access(path, 0) != 0)
  28. G_fatal_error(_("%s not found"), path);
  29. in_proj_keys = G_read_key_value_file(path, &in_stat);
  30. if (in_stat != 0)
  31. G_fatal_error(_("ERROR in reading %s"), path);
  32. npr = in_proj_keys->nitems;
  33. Tmp_file = G_tempfile();
  34. if (NULL == (Tmp_fd = fopen(Tmp_file, "w"))) {
  35. G_fatal_error(_("Cannot open temp file"));
  36. }
  37. for (i = 0; i < npr; i++) {
  38. fprintf(Tmp_fd, "%s -- %s\n", in_proj_keys->key[i],
  39. in_proj_keys->value[i]);
  40. }
  41. fclose(Tmp_fd);
  42. for (;;) {
  43. do {
  44. fprintf(stderr, _("\n\nPlease specify projection name\n"));
  45. fprintf(stderr,
  46. _
  47. ("Enter 'list' for the list of available projections\n"));
  48. fprintf(stderr, _("Hit RETURN to cancel request\n"));
  49. fprintf(stderr, ">");
  50. } while (!G_gets(answer));
  51. G_strip(answer);
  52. if (strlen(answer) == 0)
  53. return -1;
  54. if (strcmp(answer, "list") == 0) {
  55. char *pager;
  56. pager = getenv("GRASS_PAGER");
  57. if (!pager || strlen(pager) == 0)
  58. pager = "cat";
  59. /* Always print interactive output to stderr */
  60. sprintf(buff, "%s \"%s\" 1>&2", pager,
  61. G_convert_dirseps_to_host(Tmp_file));
  62. G_system(buff);
  63. }
  64. else {
  65. a = G_find_key_value(answer, in_proj_keys);
  66. if (a == NULL) {
  67. fprintf(stderr, _("\ninvalid projection\n"));
  68. }
  69. else
  70. break;
  71. }
  72. }
  73. sprintf(proj_id, "%s", answer);
  74. sprintf(proj_name, "%s", a);
  75. remove(Tmp_file);
  76. return 1;
  77. }