Files
mediafire-fuse/utils/stringv.c
josch 171fd815f2 allow to change local files
- fuse/filecache:
     * add filecache_upload_patch
     * allow opening files in modes other than RDONLY
 - fuse/hashtbl:
     * add folder_tree_upload_patch
 - fuse/operations:
     * allow opening files in modes other than RDONLY
     * add members to private context which allow tracking of
       not-yet-uploaded files and files opened for writing and
       files opened in read-only mode
 - mfapi/apicalls/upload_patch:
     * supply x-filename and x-filesize headers
 - mfapi/apicalls/upload_simple:
     * do not supply the x-filehash header as it is not used by the
       server
 - utils/hash:
     * hex characters must be lower case for the server
 - utils/strings:
     * clean up unused functions strdup_join, strdup_substr,
       string_chomp
 - utils/stringv:
     * complete rewrite with different string vector implementation
2014-12-18 20:29:47 +01:00

108 lines
2.5 KiB
C

/*
* Copyright (C) 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* 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.
*
*/
#define _POSIX_C_SOURCE 200809L // for strdup
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stddef.h>
#include "stringv.h"
struct stringv {
size_t len;
char **array;
};
stringv *stringv_alloc(void)
{
stringv *sv;
sv = (stringv *) calloc(1, sizeof(stringv));
sv->len = 0;
sv->array = NULL;
return sv;
}
void stringv_free(stringv * sv)
{
size_t i;
for (i = 0; i < sv->len; i++) {
free(sv->array[i]);
}
free(sv->array);
free(sv);
}
bool stringv_mem(stringv * sv, const char *e)
{
size_t i;
for (i = 0; i < sv->len; i++) {
if (strcmp(sv->array[i], e) == 0)
return true;
}
return false;
}
int stringv_add(stringv * sv, const char *e)
{
sv->len++;
sv->array = realloc(sv->array, sizeof(char *) * sv->len);
if (sv->array == NULL) {
fprintf(stderr, "failed to realloc\n");
return -1;
}
sv->array[sv->len - 1] = strdup(e);
if (sv->array[sv->len - 1] == NULL) {
fprintf(stderr, "failed to strdup\n");
return -1;
}
return 0;
}
int stringv_del(stringv * sv, const char *e)
{
size_t i;
for (i = 0; i < sv->len; i++) {
if (strcmp(sv->array[i], e) == 0) {
free(sv->array[i]);
break;
}
}
if (i == sv->len) {
fprintf(stderr, "not found\n");
return -1;
}
// shift the remaining entries one place to the left
memmove(sv->array + i, sv->array + i + 1,
sizeof(char *) * (sv->len - i - 1));
sv->len--;
if (sv->len == 0) {
free(sv->array);
sv->array = NULL;
} else {
sv->array = realloc(sv->array, sizeof(char *) * sv->len);
}
return 0;
}