occlientparams.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
  2. See the COPYRIGHT file for more information. */
  3. #include "config.h"
  4. #include "ocinternal.h"
  5. #include "ocdebug.h"
  6. #define LBRACKET '['
  7. #define RBRACKET ']'
  8. /*
  9. Client parameters are assumed to be
  10. one or more instances of bracketed pairs:
  11. e.g "[...][...]...".
  12. The bracket content in turn is assumed to be a
  13. comma separated list of <name>=<value> pairs.
  14. e.g. x=y,z=,a=b.
  15. If the same parameter is specifed more than once,
  16. then the first occurrence is used; this is so that
  17. is possible to forcibly override user specified
  18. parameters by prefixing.
  19. IMPORTANT: client parameter string is assumed to
  20. have blanks compress out.
  21. */
  22. int
  23. ocparamdecode(OCstate* state)
  24. {
  25. int i;
  26. i = ocuridecodeparams(state->uri);
  27. return i?OC_NOERR:OC_EBADURL;
  28. }
  29. const char*
  30. ocparamlookup(OCstate* state, const char* key)
  31. {
  32. if(state == NULL || key == NULL || state->uri == NULL) return NULL;
  33. return ocurilookup(state->uri,key);
  34. }
  35. int
  36. ocparamset(OCstate* state, const char* params)
  37. {
  38. int i;
  39. i = ocurisetparams(state->uri,params);
  40. return i?OC_NOERR:OC_EBADURL;
  41. }
  42. #ifdef OCIGNORE
  43. void
  44. ocparamfree(OClist* params)
  45. {
  46. int i;
  47. if(params == NULL) return;
  48. for(i=0;i<oclistlength(params);i++) {
  49. char* s = (char*)oclistget(params,i);
  50. if(s != NULL) free((void*)s);
  51. }
  52. oclistfree(params);
  53. }
  54. /*
  55. Delete the entry.
  56. return value = 1 => found and deleted;
  57. 0 => param not found
  58. */
  59. int
  60. ocparamdelete(OClist* params, const char* clientparam)
  61. {
  62. int i,found = 0;
  63. if(params == NULL || clientparam == NULL) return 0;
  64. for(i=0;i<oclistlength(params);i+=2) {
  65. char* name = (char*)oclistget(params,i);
  66. if(strcmp(clientparam,name)==0) {found=1; break;}
  67. }
  68. if(found) {
  69. oclistremove(params,i+1); /* remove value */
  70. oclistremove(params,i); /* remove name */
  71. }
  72. return found;
  73. }
  74. /*
  75. Insert new client param (name,value);
  76. return value = 1 => not already defined
  77. 0 => param already defined (no change)
  78. */
  79. int
  80. ocparaminsert(OClist* params, const char* clientparam, const char* value)
  81. {
  82. int i;
  83. if(params == NULL || clientparam == NULL) return 0;
  84. for(i=0;i<oclistlength(params);i+=2) {
  85. char* name = (char*)oclistget(params,i);
  86. if(strcmp(clientparam,name)==0) return 0;
  87. }
  88. /* not found, append */
  89. oclistpush(params,(ocelem)nulldup(clientparam));
  90. oclistpush(params,(ocelem)nulldup(value));
  91. return 1;
  92. }
  93. /*
  94. Replace new client param (name,value);
  95. return value = 1 => replacement performed
  96. 0 => insertion performed
  97. */
  98. int
  99. ocparamreplace(OClist* params, const char* clientparam, const char* value)
  100. {
  101. int i;
  102. if(params == NULL || clientparam == NULL) return 0;
  103. for(i=0;i<oclistlength(params);i+=2) {
  104. char* name = (char*)oclistget(params,i);
  105. if(strcmp(clientparam,name)==0) {
  106. oclistinsert(params,i+1,(ocelem)nulldup(value));
  107. return 1;
  108. }
  109. }
  110. ocparaminsert(params,clientparam,value);
  111. return 0;
  112. }
  113. #endif