Skip to content

Commit

Permalink
Merge pull request #555 from dota17/chang_format_3
Browse files Browse the repository at this point in the history
Format json-c with clang-format tool
  • Loading branch information
hawicz committed Apr 3, 2020
2 parents ed54353 + 8b162c4 commit 31f1ab2
Show file tree
Hide file tree
Showing 55 changed files with 3,328 additions and 3,084 deletions.
53 changes: 53 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
BasedOnStyle: LLVM
# If true, clang-format will attempt to re-flow comments
ReflowComments: false
# The column limit.
ColumnLimit: 100
# Indent width for line continuations.
ContinuationIndentWidth: 4
# The number of columns to use for indentation.
IndentWidth: 8
# The number of columns used for tab stops.
TabWidth: 8
UseTab: ForIndentation

# Options for aligning backslashes in escaped newlines.
AlignEscapedNewlines: Left
# Short Block Style
AllowShortBlocksOnASingleLine: true
# If true, short case labels will be contracted to a single line.
AllowShortCaseLabelsOnASingleLine: true
# Dependent on the value, int f() { return 0; } can be put on a single line.
AllowShortFunctionsOnASingleLine: Empty
# The brace breaking style to use.
BreakBeforeBraces: Custom
# Control of individual brace wrapping cases.
BraceWrapping:
# Wrap class definition.
AfterClass: true
# Wrap control statements
AfterControlStatement: true
# Wrap enum definitions.
AfterEnum: true
# Wrap function definitions.
AfterFunction: true
# Wrap namespace definitions.
AfterNamespace: true
# Wrap struct definitions.
AfterStruct: true
# Wrap union definitions.
AfterUnion: true
# Wrap extern blocks.
AfterExternBlock: false
# Wrap before catch.
BeforeCatch: true
# Wrap before else.
BeforeElse: true
# Indent the wrapped braces themselves.
IndentBraces: false
# If false, empty function body can be put on a single line.
SplitEmptyFunction: false
# If false, empty record (e.g. class, struct or union) body can be put on a single line.
SplitEmptyRecord: false
# If false, empty namespace body can be put on a single line.
SplitEmptyNamespace: false
154 changes: 79 additions & 75 deletions arraylist.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
#include <limits.h>

#ifdef STDC_HEADERS
# include <stdlib.h>
# include <string.h>
#include <stdlib.h>
#include <string.h>
#endif /* STDC_HEADERS */

#if defined(HAVE_STRINGS_H) && !defined(_STRING_H) && !defined(__USE_BSD)
# include <strings.h>
#include <strings.h>
#endif /* HAVE_STRINGS_H */

#ifndef SIZE_T_MAX
Expand All @@ -36,111 +36,115 @@

#include "arraylist.h"

struct array_list*
array_list_new(array_list_free_fn *free_fn)
struct array_list *array_list_new(array_list_free_fn *free_fn)
{
struct array_list *arr;

arr = (struct array_list*)calloc(1, sizeof(struct array_list));
if(!arr) return NULL;
arr->size = ARRAY_LIST_DEFAULT_SIZE;
arr->length = 0;
arr->free_fn = free_fn;
if(!(arr->array = (void**)calloc(arr->size, sizeof(void*)))) {
free(arr);
return NULL;
}
return arr;
struct array_list *arr;

arr = (struct array_list *)calloc(1, sizeof(struct array_list));
if (!arr)
return NULL;
arr->size = ARRAY_LIST_DEFAULT_SIZE;
arr->length = 0;
arr->free_fn = free_fn;
if (!(arr->array = (void **)calloc(arr->size, sizeof(void *))))
{
free(arr);
return NULL;
}
return arr;
}

extern void
array_list_free(struct array_list *arr)
extern void array_list_free(struct array_list *arr)
{
size_t i;
for(i = 0; i < arr->length; i++)
if(arr->array[i]) arr->free_fn(arr->array[i]);
free(arr->array);
free(arr);
size_t i;
for (i = 0; i < arr->length; i++)
if (arr->array[i])
arr->free_fn(arr->array[i]);
free(arr->array);
free(arr);
}

void*
array_list_get_idx(struct array_list *arr, size_t i)
void *array_list_get_idx(struct array_list *arr, size_t i)
{
if(i >= arr->length) return NULL;
return arr->array[i];
if (i >= arr->length)
return NULL;
return arr->array[i];
}

static int array_list_expand_internal(struct array_list *arr, size_t max)
{
void *t;
size_t new_size;

if(max < arr->size) return 0;
/* Avoid undefined behaviour on size_t overflow */
if( arr->size >= SIZE_T_MAX / 2 )
new_size = max;
else
{
new_size = arr->size << 1;
if (new_size < max)
new_size = max;
}
if (new_size > (~((size_t)0)) / sizeof(void*)) return -1;
if (!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1;
arr->array = (void**)t;
(void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*));
arr->size = new_size;
return 0;
void *t;
size_t new_size;

if (max < arr->size)
return 0;
/* Avoid undefined behaviour on size_t overflow */
if (arr->size >= SIZE_T_MAX / 2)
new_size = max;
else
{
new_size = arr->size << 1;
if (new_size < max)
new_size = max;
}
if (new_size > (~((size_t)0)) / sizeof(void *))
return -1;
if (!(t = realloc(arr->array, new_size * sizeof(void *))))
return -1;
arr->array = (void **)t;
(void)memset(arr->array + arr->size, 0, (new_size - arr->size) * sizeof(void *));
arr->size = new_size;
return 0;
}

int
array_list_put_idx(struct array_list *arr, size_t idx, void *data)
int array_list_put_idx(struct array_list *arr, size_t idx, void *data)
{
if (idx > SIZE_T_MAX - 1 ) return -1;
if(array_list_expand_internal(arr, idx+1)) return -1;
if(idx < arr->length && arr->array[idx])
arr->free_fn(arr->array[idx]);
arr->array[idx] = data;
if(arr->length <= idx) arr->length = idx + 1;
return 0;
if (idx > SIZE_T_MAX - 1)
return -1;
if (array_list_expand_internal(arr, idx + 1))
return -1;
if (idx < arr->length && arr->array[idx])
arr->free_fn(arr->array[idx]);
arr->array[idx] = data;
if (arr->length <= idx)
arr->length = idx + 1;
return 0;
}

int
array_list_add(struct array_list *arr, void *data)
int array_list_add(struct array_list *arr, void *data)
{
return array_list_put_idx(arr, arr->length, data);
return array_list_put_idx(arr, arr->length, data);
}

void
array_list_sort(struct array_list *arr, int(*compar)(const void *, const void *))
void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *))
{
qsort(arr->array, arr->length, sizeof(arr->array[0]), compar);
qsort(arr->array, arr->length, sizeof(arr->array[0]), compar);
}

void* array_list_bsearch(const void **key, struct array_list *arr,
int (*compar)(const void *, const void *))
void *array_list_bsearch(const void **key, struct array_list *arr,
int (*compar)(const void *, const void *))
{
return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]),
compar);
return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]), compar);
}

size_t
array_list_length(struct array_list *arr)
size_t array_list_length(struct array_list *arr)
{
return arr->length;
return arr->length;
}

int
array_list_del_idx( struct array_list *arr, size_t idx, size_t count )
int array_list_del_idx(struct array_list *arr, size_t idx, size_t count)
{
size_t i, stop;

stop = idx + count;
if ( idx >= arr->length || stop > arr->length ) return -1;
for ( i = idx; i < stop; ++i ) {
if ( arr->array[i] ) arr->free_fn( arr->array[i] );
if (idx >= arr->length || stop > arr->length)
return -1;
for (i = idx; i < stop; ++i)
{
if (arr->array[i])
arr->free_fn(arr->array[i]);
}
memmove( arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void*) );
memmove(arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void *));
arr->length -= count;
return 0;
}
39 changes: 15 additions & 24 deletions arraylist.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,35 @@ extern "C" {

#define ARRAY_LIST_DEFAULT_SIZE 32

typedef void (array_list_free_fn) (void *data);
typedef void(array_list_free_fn)(void *data);

struct array_list
{
void **array;
size_t length;
size_t size;
array_list_free_fn *free_fn;
void **array;
size_t length;
size_t size;
array_list_free_fn *free_fn;
};
typedef struct array_list array_list;

extern struct array_list*
array_list_new(array_list_free_fn *free_fn);
extern struct array_list *array_list_new(array_list_free_fn *free_fn);

extern void
array_list_free(struct array_list *al);
extern void array_list_free(struct array_list *al);

extern void*
array_list_get_idx(struct array_list *al, size_t i);
extern void *array_list_get_idx(struct array_list *al, size_t i);

extern int
array_list_put_idx(struct array_list *al, size_t i, void *data);
extern int array_list_put_idx(struct array_list *al, size_t i, void *data);

extern int
array_list_add(struct array_list *al, void *data);
extern int array_list_add(struct array_list *al, void *data);

extern size_t
array_list_length(struct array_list *al);
extern size_t array_list_length(struct array_list *al);

extern void
array_list_sort(struct array_list *arr, int(*compar)(const void *, const void *));
extern void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *));

extern void*
array_list_bsearch(const void **key, struct array_list *arr,
int (*compar)(const void *, const void *));
extern void *array_list_bsearch(const void **key, struct array_list *arr,
int (*compar)(const void *, const void *));

extern int
array_list_del_idx(struct array_list *arr, size_t idx, size_t count);
extern int array_list_del_idx(struct array_list *arr, size_t idx, size_t count);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 31f1ab2

Please sign in to comment.