strlst.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * String List Utils
  3. *
  4. * Copyright 2018 Pascal Bourguignon <pjb@informatimago.com>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <winpr/strlst.h>
  25. #include <winpr/string.h>
  26. void string_list_free(char** string_list)
  27. {
  28. int i;
  29. for (i = 0; string_list[i]; i++)
  30. {
  31. free(string_list[i]);
  32. }
  33. free(string_list);
  34. }
  35. int string_list_length(const char* const* string_list)
  36. {
  37. int i;
  38. for (i = 0; string_list[i]; i++)
  39. ;
  40. return i;
  41. }
  42. char** string_list_copy(const char* const* string_list)
  43. {
  44. int i;
  45. int length = string_list_length(string_list);
  46. char** copy = calloc(length + 1, sizeof(char*));
  47. if (!copy)
  48. {
  49. return 0;
  50. }
  51. for (i = 0; i < length; i++)
  52. {
  53. copy[i] = _strdup(string_list[i]);
  54. }
  55. copy[length] = 0;
  56. return copy;
  57. }
  58. void string_list_print(FILE* out, const char* const* string_list)
  59. {
  60. int j;
  61. for (j = 0; string_list[j]; j++)
  62. {
  63. fprintf(out, "[%2d]: %s\n", j, string_list[j]);
  64. }
  65. fflush(out);
  66. }