metadata.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "internal.h"
  34. #include "cgrpc.h"
  35. #include "stdlib.h"
  36. #include "string.h"
  37. cgrpc_metadata_array *cgrpc_metadata_array_create() {
  38. cgrpc_metadata_array *metadata = (cgrpc_metadata_array *) malloc(sizeof(cgrpc_metadata_array));
  39. memset(metadata, 0, sizeof(cgrpc_metadata_array));
  40. return metadata;
  41. }
  42. void cgrpc_metadata_array_destroy(cgrpc_metadata_array *array) {
  43. grpc_metadata_array_destroy(array);
  44. }
  45. size_t cgrpc_metadata_array_get_count(cgrpc_metadata_array *array) {
  46. return array->count;
  47. }
  48. const char *cgrpc_metadata_array_get_key_at_index(cgrpc_metadata_array *array, size_t index) {
  49. return array->metadata[index].key;
  50. }
  51. const char *cgrpc_metadata_array_get_value_at_index(cgrpc_metadata_array *array, size_t index) {
  52. return array->metadata[index].value;
  53. }
  54. void cgrpc_metadata_array_move_metadata(cgrpc_metadata_array *destination,
  55. cgrpc_metadata_array *source) {
  56. destination->count = source->count;
  57. destination->capacity = source->capacity;
  58. destination->metadata = source->metadata;
  59. source->count = 0;
  60. source->capacity = 0;
  61. source->metadata = NULL;
  62. }
  63. void cgrpc_metadata_array_append_metadata(cgrpc_metadata_array *metadata, const char *key, const char *value) {
  64. if (!metadata->count) {
  65. metadata->metadata = (grpc_metadata *) malloc(10 * sizeof(grpc_metadata));
  66. metadata->count = 0;
  67. metadata->capacity = 10;
  68. }
  69. if (metadata->count < metadata->capacity) {
  70. size_t i = metadata->count;
  71. metadata->metadata[i].key = strdup(key);
  72. metadata->metadata[i].value = strdup(value);
  73. metadata->metadata[i].value_length = strlen(value);
  74. metadata->count++;
  75. }
  76. }