FreeRDP
Loading...
Searching...
No Matches
strlst.c
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
19#include <winpr/config.h>
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include <winpr/strlst.h>
26#include <winpr/string.h>
27
28void string_list_free(char** string_list)
29{
30 for (size_t i = 0; string_list[i]; i++)
31 {
32 free(string_list[i]);
33 }
34
35 free((void*)string_list);
36}
37
38int string_list_length(const char* const* string_list)
39{
40 int i = 0;
41 for (; string_list[i]; i++)
42 ;
43
44 return i;
45}
46
47char** string_list_copy(const char* const* string_list)
48{
49 int length = string_list_length(string_list);
50 char** copy = (char**)calloc(WINPR_ASSERTING_INT_CAST(size_t, length) + 1, sizeof(char*));
51
52 if (!copy)
53 {
54 return 0;
55 }
56
57 for (int i = 0; i < length; i++)
58 {
59 copy[i] = _strdup(string_list[i]);
60 }
61
62 copy[length] = 0;
63 return copy;
64}
65
66void string_list_print(FILE* out, const char* const* string_list)
67{
68 for (int j = 0; string_list[j]; j++)
69 {
70 (void)fprintf(out, "[%2d]: %s\n", j, string_list[j]);
71 }
72
73 (void)fflush(out);
74}