xaizek / vifm (License: GPLv2+) (since 2018-12-07)
Vifm is a file manager with curses interface, which provides Vi[m]-like environment for managing objects within file systems, extended with some useful ideas from mutt.
<root> / src / utils / string_array.c (7db9ea58d346c844913ff433a130890ebcc4c315) (9,479B) (mode 100644) [raw]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
/* vifm
 * Copyright (C) 2011 xaizek.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "string_array.h"

#include <assert.h> /* assert() */
#include <stddef.h> /* NULL size_t */
#include <stdio.h> /* FILE SEEK_END SEEK_SET fclose() fprintf() fread()
                      ftell() fseek() */
#include <stdlib.h> /* free() malloc() realloc() */
#include <string.h> /* strcspn() */

#include "../compat/os.h"
#include "../compat/reallocarray.h"
#include "file_streams.h"

static char * read_whole_file(const char filepath[], size_t *read);
static char * read_seekable_stream(FILE *fp, size_t *read);
static char * read_stream(FILE *fp, size_t *read, int drop_bom, progress_cb cb,
		const void *arg);
static size_t get_remaining_stream_size(FILE *fp);
static char ** text_to_lines(char text[], size_t text_len, int *nlines,
		int null_sep);

int
add_to_string_array(char ***array, int len, const char item[])
{
	char **p = reallocarray(*array, len + 1, sizeof(*p));
	if(p == NULL)
	{
		return len;
	}
	*array = p;

	if(item == NULL)
	{
		p[len++] = NULL;
	}
	else if((p[len] = strdup(item)) != NULL)
	{
		++len;
	}

	return len;
}

int
put_into_string_array(char ***array, int len, char item[])
{
	char **const arr = reallocarray(*array, len + 1, sizeof(char *));
	if(arr != NULL)
	{
		*array = arr;
		arr[len++] = item;
	}
	return len;
}

void
remove_from_string_array(char **array, size_t len, int pos)
{
	free(array[pos]);
	memmove(array + pos, array + pos + 1, sizeof(char *)*((len - 1) - pos));
}

int
is_in_string_array(char *array[], size_t len, const char item[])
{
	const int pos = string_array_pos(array, len, item);
	return pos >= 0;
}

int
is_in_string_array_case(char *array[], size_t len, const char item[])
{
	const int pos = string_array_pos_case(array, len, item);
	return pos >= 0;
}

int
is_in_string_array_os(char *array[], size_t len, const char item[])
{
#ifndef _WIN32
	return is_in_string_array(array, len, item);
#else
	return is_in_string_array_case(array, len, item);
#endif
}

char **
copy_string_array(char **array, size_t len)
{
	char **result = reallocarray(NULL, len, sizeof(char *));
	size_t i;
	for(i = 0U; i < len; ++i)
	{
		result[i] = strdup(array[i]);
	}
	return result;
}

int
string_array_pos(char *array[], size_t len, const char item[])
{
	size_t i = len;
	if(item != NULL)
	{
		for(i = 0U; i < len; ++i)
		{
			if(strcmp(array[i], item) == 0)
			{
				break;
			}
		}
	}
	return (i < len) ? (int)i : -1;
}

int
string_array_pos_case(char *array[], size_t len, const char item[])
{
	size_t i = len;
	if(item != NULL)
	{
		for(i = 0; i < len; i++)
		{
			if(strcasecmp(array[i], item) == 0)
			{
				break;
			}
		}
	}
	return (i < len) ? (int)i : -1;
}

int
string_array_equal(char *first[], size_t first_len, char *second[],
		size_t second_len)
{
	if(first_len != second_len)
	{
		return 0;
	}

	size_t i;
	for(i = 0U; i < first_len; ++i)
	{
		if(strcmp(first[i], second[i]) != 0)
		{
			return 0;
		}
	}

	return 1;
}

void
free_string_array(char *array[], size_t len)
{
	if(array != NULL)
	{
		free_strings(array, len);
		free(array);
	}
}

void
free_strings(char *array[], size_t len)
{
	size_t i;
	for(i = 0; i < len; ++i)
	{
		free(array[i]);
	}
}

int
count_strings(char *array[])
{
	int count = 0;
	while(*array++ != NULL)
	{
		++count;
	}
	return count;
}

char **
read_file_of_lines(const char filepath[], int *nlines)
{
	size_t text_len;
	char *const text = read_whole_file(filepath, &text_len);
	if(text == NULL)
	{
		return NULL;
	}

	char **list = text_to_lines(text, text_len, nlines, 0);
	if(list == NULL)
	{
		list = malloc(sizeof(*list));
		*nlines = 0;
	}
	return list;
}

/* Reads file specified by filepath into null terminated string.  Returns
 * string of length *read to be freed by caller on success, otherwise NULL is
 * returned. */
static char *
read_whole_file(const char filepath[], size_t *read)
{
	char *content = NULL;
	FILE *fp;

	*read = 0U;

	if((fp = os_fopen(filepath, "rb")) != NULL)
	{
		content = read_seekable_stream(fp, read);
		fclose(fp);
	}

	return content;
}

char **
read_file_lines(FILE *f, int *nlines)
{
	size_t text_len;
	char *const text = read_seekable_stream(f, &text_len);
	return text_to_lines(text, text_len, nlines, 0);
}

char **
read_stream_lines(FILE *f, int *nlines, int null_sep_heuristic, progress_cb cb,
		const void *arg)
{
	int null;
	size_t text_len;
	char *const text = read_nonseekable_stream(f, &text_len, cb, arg);
	if(text == NULL)
	{
		return NULL;
	}
	null = (null_sep_heuristic && strlen(text) != text_len);
	return text_to_lines(text, text_len, nlines, null);
}

char *
read_nonseekable_stream(FILE *fp, size_t *read, progress_cb cb, const void *arg)
{
	return read_stream(fp, read, 1, cb, arg);
}

/* Reads content of the fp stream that supports seek operation (points to a
 * file) until end-of-file into null terminated string.  Returns string of
 * length *read to be freed by caller on success, otherwise NULL is returned and
 * *read is set to 0UL. */
static char *
read_seekable_stream(FILE *fp, size_t *read)
{
	char *content;
	size_t len;

	skip_bom(fp);
	len = get_remaining_stream_size(fp);

	if(len == 0)
	{
		/* Give this file another shot in case it's a virtual file that doesn't
		 * report its size ahead of time. */
		return read_stream(fp, read, 0, NULL, NULL);
	}

	*read = 0UL;

	if((content = malloc(len + 1U)) == NULL)
	{
		return NULL;
	}

	if(fread(content, len, 1U, fp) == 1U)
	{
		content[len] = '\0';
		*read = len;
	}
	else
	{
		free(content);
		content = NULL;
	}

	return content;
}

/* Reads content of the fp stream that doesn't support seek operation (e.g. it
 * points to a pipe) until end-of-file into null terminated string.  cb can be
 * NULL.  Returns string of length *read to be freed by caller on success,
 * otherwise NULL is returned. */
static char *
read_stream(FILE *fp, size_t *read, int drop_bom, progress_cb cb,
		const void *arg)
{
	enum { PIECE_LEN = 4096 };
	char *content = malloc(PIECE_LEN + 1);

	if(content != NULL)
	{
		char *last_allocated_block = content;
		size_t len = 0U, piece_len;
		if(drop_bom)
		{
			skip_bom(fp);
		}
		while((piece_len = fread(content + len, 1, PIECE_LEN, fp)) != 0U)
		{
			const size_t new_size = len + piece_len + PIECE_LEN + 1U;
			last_allocated_block = realloc(content, new_size);
			if(last_allocated_block == NULL)
			{
				break;
			}

			content = last_allocated_block;
			len += piece_len;

			if(cb != NULL)
			{
				cb(arg);
			}
		}
		content[len] = '\0';

		if(last_allocated_block == NULL)
		{
			free(content);
			content = NULL;
		}
		else
		{
			*read = len;
		}
	}

	if(content == NULL)
	{
		*read = 0U;
	}

	return content;
}

/* Calculates remaining size of the stream.  Assumes that the fp stream supports
 * seek operation.  Returns the size. */
static size_t
get_remaining_stream_size(FILE *fp)
{
	size_t remaining_size;
	const long pos = ftell(fp);
	assert(pos >= 0 && "Stream expected to support seek operation.");

	(void)fseek(fp, 0, SEEK_END);
	remaining_size = ftell(fp) - pos;
	(void)fseek(fp, pos, SEEK_SET);

	return remaining_size;
}

/* Converts text of length text_len into an array of strings.  Always frees
 * piece of memory pointed to by the text.  Returns non-NULL on success,
 * otherwise NULL is returned, *nlines is untouched.  For empty file non-NULL
 * will be returned, but *nlines will be zero. */
static char **
text_to_lines(char text[], size_t text_len, int *nlines, int null_sep)
{
	char **list = NULL;

	if(text != NULL)
	{
		list = break_into_lines(text, text_len, nlines, null_sep);
		free(text);
	}
	return list;
}

char **
break_into_lines(char text[], size_t text_len, int *nlines, int null_sep)
{
	const char *const seps = null_sep ? "" : "\n\r";
	const char *const end = text + text_len;
	char **list = NULL;

	*nlines = 0;
	while(text < end)
	{
		const size_t line_len = strcspn(text, seps);

		char *after_line = text + line_len;
		if(after_line[0] == '\n')
		{
			after_line += 1;
		}
		else if(after_line[0] == '\r')
		{
			after_line += (after_line[1] == '\n') ? 2 : 1;
		}
		else if(after_line[0] == '\0')
		{
			do
			{
				++after_line;
			}
			while(after_line < end && after_line[0] == '\0');
		}

		text[line_len] = '\0';
		*nlines = add_to_string_array(&list, *nlines, text);

		text = after_line;
	}

	return list;
}

int
write_file_of_lines(const char filepath[], char *strs[], size_t nstrs)
{
	FILE *const fp = os_fopen(filepath, "w");
	if(fp == NULL)
	{
		return 1;
	}

	write_lines_to_file(fp, strs, nstrs);
	fclose(fp);
	return 0;
}

void
write_lines_to_file(FILE *fp, char *strs[], size_t nstrs)
{
	size_t i;
	for(i = 0U; i < nstrs; ++i)
	{
		fputs(strs[i], fp);
		putc('\n', fp);
	}
}

/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */
/* vim: set cinoptions+=t0 filetype=c : */
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/vifm

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/vifm

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master