metadata.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "internal.h"
  17. #include "cgrpc.h"
  18. #include "stdlib.h"
  19. #include "string.h"
  20. cgrpc_metadata_array *cgrpc_metadata_array_create() {
  21. cgrpc_metadata_array *metadata = (cgrpc_metadata_array *) malloc(sizeof(cgrpc_metadata_array));
  22. memset(metadata, 0, sizeof(cgrpc_metadata_array));
  23. return metadata;
  24. }
  25. void cgrpc_metadata_array_destroy(cgrpc_metadata_array *array) {
  26. grpc_metadata_array_destroy(array);
  27. }
  28. size_t cgrpc_metadata_array_get_count(cgrpc_metadata_array *array) {
  29. return array->count;
  30. }
  31. char *cgrpc_metadata_array_copy_key_at_index(cgrpc_metadata_array *array, size_t index) {
  32. size_t length = GRPC_SLICE_LENGTH(array->metadata[index].key);
  33. char *str = (char *) malloc(length + 1);
  34. memcpy(str, GRPC_SLICE_START_PTR(array->metadata[index].key), length);
  35. str[length] = 0;
  36. return str;
  37. }
  38. char *cgrpc_metadata_array_copy_value_at_index(cgrpc_metadata_array *array, size_t index) {
  39. size_t length = GRPC_SLICE_LENGTH(array->metadata[index].value);
  40. char *str = (char *) malloc(length + 1);
  41. memcpy(str, GRPC_SLICE_START_PTR(array->metadata[index].value), length);
  42. str[length] = 0;
  43. return str;
  44. }
  45. size_t cgrpc_metadata_array_get_value_length_at_index(cgrpc_metadata_array *array, size_t index) {
  46. return GRPC_SLICE_LENGTH(array->metadata[index].value);
  47. /*
  48. int length = GRPC_SLICE_LENGTH(array->metadata[index].value);
  49. char *str = (char *) malloc(length + 1);
  50. memcpy(str, GRPC_SLICE_START_PTR(array->metadata[index].value), length);
  51. str[length] = 0;
  52. return str;
  53. */
  54. }
  55. void cgrpc_metadata_array_move_metadata(cgrpc_metadata_array *destination,
  56. cgrpc_metadata_array *source) {
  57. destination->count = source->count;
  58. destination->capacity = source->capacity;
  59. destination->metadata = source->metadata;
  60. source->count = 0;
  61. source->capacity = 0;
  62. source->metadata = NULL;
  63. }
  64. void cgrpc_metadata_array_append_metadata(cgrpc_metadata_array *metadata, const char *key, const char *value) {
  65. if (!metadata->count) {
  66. metadata->metadata = (grpc_metadata *) malloc(10 * sizeof(grpc_metadata));
  67. metadata->count = 0;
  68. metadata->capacity = 10;
  69. }
  70. if (metadata->count < metadata->capacity) {
  71. size_t i = metadata->count;
  72. metadata->metadata[i].key = grpc_slice_from_copied_string(key);
  73. metadata->metadata[i].value = grpc_slice_from_copied_string(value);
  74. metadata->count++;
  75. }
  76. }