proj.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <grass/gis.h>
  5. #include <grass/glocale.h>
  6. #include "local_proto.h"
  7. struct proj_unit *get_proj_unit(const char *arg)
  8. {
  9. char buf[4096];
  10. struct proj_unit *unit;
  11. FILE *fp;
  12. sprintf(buf, "%s/etc/proj/units.table", G_gisbase());
  13. fp = fopen(buf, "r");
  14. if (!fp)
  15. return NULL;
  16. for (unit = NULL; !unit;) {
  17. char plural[16], singular[16];
  18. double factor;
  19. struct proj_unit *unit;
  20. if (!G_getl2(buf, sizeof(buf), fp))
  21. break;
  22. if (sscanf(buf, "%[^:]:%[^:]:%lf", plural, singular, &factor) != 3)
  23. continue;
  24. if (G_strcasecmp(arg, plural) != 0)
  25. continue;
  26. unit = G_malloc(sizeof(struct proj_unit));
  27. unit->units = G_store(plural);
  28. unit->unit = G_store(singular);
  29. unit->fact = factor;
  30. }
  31. fclose(fp);
  32. return unit;
  33. }
  34. struct proj_desc *get_proj_desc(const char *arg)
  35. {
  36. char buf[4096];
  37. struct proj_desc *res;
  38. FILE *fp;
  39. sprintf(buf, "%s/etc/proj/desc.table", G_gisbase());
  40. fp = fopen(buf, "r");
  41. if (!fp)
  42. return NULL;
  43. for (res = NULL; !res;) {
  44. char name[16], type[16], key[16], desc[100];
  45. if (!G_getl2(buf, sizeof(buf), fp))
  46. break;
  47. if (sscanf(buf, "%[^:]:%[^:]:%[^:]:%[^\n]", name, type, key, desc) !=
  48. 4)
  49. continue;
  50. if (G_strcasecmp(arg, name) != 0)
  51. continue;
  52. res = G_malloc(sizeof(struct proj_desc));
  53. res->name = G_store(name);
  54. res->type = G_store(type);
  55. res->key = G_store(key);
  56. res->desc = G_store(desc);
  57. }
  58. fclose(fp);
  59. return res;
  60. }
  61. struct proj_parm *get_proj_parms(const char *arg)
  62. {
  63. char buf[4096];
  64. struct proj_parm *parm_table = NULL;
  65. int parm_num = 0;
  66. int parm_max = 0;
  67. char *data;
  68. int done;
  69. FILE *fp;
  70. sprintf(buf, "%s/etc/proj/parms.table", G_gisbase());
  71. fp = fopen(buf, "r");
  72. if (!fp)
  73. return NULL;
  74. for (data = NULL; !data;) {
  75. char *p;
  76. if (!G_getl2(buf, sizeof(buf), fp))
  77. break;
  78. for (p = buf; *p && *p != ':'; p++) ;
  79. if (*p != ':')
  80. break;
  81. *p++ = '\0';
  82. if (G_strcasecmp(buf, arg) != 0)
  83. continue;
  84. for (; *p && *p != ':'; p++) ;
  85. if (*p != ':')
  86. break;
  87. *p++ = '\0';
  88. data = p;
  89. }
  90. fclose(fp);
  91. if (!data)
  92. return NULL;
  93. for (done = 0; !done;) {
  94. char name[16], ask[8], dfl[32];
  95. struct proj_parm *parm;
  96. char *p;
  97. for (p = data; *p && *p != ';'; p++) ;
  98. if (*p == ';')
  99. *p++ = '\0';
  100. else
  101. done = 1;
  102. if (sscanf(data, "%[^=]=%[^,],%s", name, ask, dfl) != 3) {
  103. data = p;
  104. continue;
  105. }
  106. data = p;
  107. if (parm_num + 1 >= parm_max) {
  108. parm_max += 16;
  109. parm_table =
  110. G_realloc(parm_table, parm_max * sizeof(struct proj_parm));
  111. }
  112. parm = &parm_table[parm_num++];
  113. parm->name = G_store(name);
  114. if (strcmp(ask, "ask") == 0)
  115. parm->ask = 1;
  116. else if (strcmp(ask, "noask") == 0)
  117. parm->ask = 0;
  118. else {
  119. parm->ask = 1;
  120. G_warning(_("Unrecognized 'ask' value in parms.table: %s"),
  121. ask);
  122. }
  123. if (strcmp(dfl, "nodfl") == 0)
  124. parm->def_exists = 0;
  125. else if (sscanf(dfl, "%lf", &parm->deflt) == 1)
  126. parm->def_exists = 1;
  127. else {
  128. parm->def_exists = 0;
  129. G_warning(_("Unrecognized default value in parms.table: %s"),
  130. dfl);
  131. }
  132. }
  133. parm_table[parm_num].name = NULL;
  134. return parm_table;
  135. }