1 /* vifm
2  * Copyright (C) 2011 xaizek.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #ifndef VIFM__UTILS__STRING_ARRAY_H__
20 #define VIFM__UTILS__STRING_ARRAY_H__
21 
22 #include <stddef.h> /* size_t wchar_t */
23 #include <stdio.h> /* FILE */
24 
25 /* Convenience structure (not used here directly, but is related
26  * functionally). */
27 typedef struct strlist_t
28 {
29 	int nitems;   /* Number of items in the list. */
30 	char **items; /* The list itself. */
31 }
32 strlist_t;
33 
34 /* Type of callback function to get notification on reading another portion of
35  * data. */
36 typedef void (*progress_cb)(const void *arg);
37 
38 /* Adds copy of a string to a string array.  Input pointer can be NULL.  Returns
39  * new length of the array, which is unchanged on allocation failure. */
40 int add_to_string_array(char ***array, int len, const char item[]);
41 
42 /* Puts pointer into string array without making a copy.  Reallocates *array.
43  * item can be NULL.  Returns new size of the array, which can be equal to len
44  * on reallocation failure. */
45 int put_into_string_array(char **array[], int len, char item[]);
46 
47 void remove_from_string_array(char **array, size_t len, int pos);
48 
49 /* Checks whether item is in the array.  Always uses case sensitive comparison.
50  * Returns non-zero on successful search, otherwise zero is returned. */
51 int is_in_string_array(char *array[], size_t len, const char item[]);
52 
53 /* Checks whether item is in the array.  Always uses case insensitive
54  * comparison.  Returns non-zero on successful search, otherwise zero is
55  * returned. */
56 int is_in_string_array_case(char *array[], size_t len, const char item[]);
57 
58 /* Checks whether item is in the array.  Uses case sensitive or case insensitive
59  * comparison depending on OS case sensitivity.  Returns non-zero on successful
60  * search, otherwise zero is returned. */
61 int is_in_string_array_os(char *array[], size_t len, const char item[]);
62 
63 char ** copy_string_array(char **array, size_t len);
64 
65 /* Returns position of the item in the array, -1 if no match found. */
66 int string_array_pos(char *array[], size_t len, const char item[]);
67 
68 /* Returns position of the item in the array, -1 if no match found. */
69 int string_array_pos_case(char *array[], size_t len, const char item[]);
70 
71 /* Frees memory of all array items and from the array itself.  Does nothing for
72  * NULL arrays. */
73 void free_string_array(char *array[], size_t len);
74 
75 /* Frees memory of all array items, but not from the array itself. */
76 void free_strings(char *array[], size_t len);
77 
78 /* Counts number of elements of a null terminated array of strings.  Returns the
79  * count. */
80 int count_strings(char *array[]);
81 
82 /* Reads file specified by filepath into an array of strings.  Returns non-NULL
83  * on success, otherwise NULL is returned, *nlines is untouched and errno
84  * contains error code.  For empty file non-NULL will be returned, but *nlines
85  * will be zero. */
86 char ** read_file_of_lines(const char filepath[], int *nlines);
87 
88 /* Reads content of the file stream (required to be seekable) f into array of
89  * strings.  Returns NULL for an empty file stream. */
90 char ** read_file_lines(FILE *f, int *nlines);
91 
92 /* Reads content of the stream (not required to be seekable) f into array of
93  * strings.  The flag enables heuristic according to which if stream contents
94  * includes null character, it's taken as a separator instead of regular newline
95  * characters.  cb can be NULL.  Returns NULL for an empty file stream. */
96 char ** read_stream_lines(FILE *f, int *nlines, int null_sep_heuristic,
97 		progress_cb cb, const void *arg);
98 
99 /* Reads content of the fp stream that doesn't support seek operation (e.g. it
100  * points to a pipe) until end-of-file into null terminated string.  cb can be
101  * NULL.  Returns string of length *read to be freed by caller on success,
102  * otherwise NULL is returned. */
103 char * read_nonseekable_stream(FILE *fp, size_t *read, progress_cb cb,
104 		const void *arg);
105 
106 /* Converts text of length text_len into an array of strings.  Returns non-NULL
107  * on success, otherwise NULL is returned, *nlines is untouched.  For empty file
108  * non-NULL will be returned, but *nlines will be zero. */
109 char ** break_into_lines(char text[], size_t text_len, int *nlines,
110 		int null_sep);
111 
112 /* Overwrites file specified by filepath with lines.  Returns zero on success,
113  * otherwise non-zero is returned and errno contains error code. */
114 int write_file_of_lines(const char filepath[], char *strs[], size_t nstrs);
115 
116 #endif /* VIFM__UTILS__STRING_ARRAY_H__ */
117 
118 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
119 /* vim: set cinoptions+=t0 filetype=c : */
120