Compare commits
4 commits
2252b7d732
...
5068a1b90d
Author | SHA1 | Date | |
---|---|---|---|
5068a1b90d | |||
bb574d2a2c | |||
ed85ded967 | |||
6b862ab022 |
14 changed files with 304 additions and 252 deletions
104
src/archiver.c
104
src/archiver.c
|
@ -280,9 +280,9 @@ int write_files_fn(void *data, void *ud) {
|
|||
// Write file to pipe, and read from other pipe.
|
||||
char write_buf[1024];
|
||||
char read_buf[1024];
|
||||
int write_again = 0;
|
||||
int write_done = 0;
|
||||
int read_done = 0;
|
||||
int_fast8_t write_again = 0;
|
||||
int_fast8_t write_done = 0;
|
||||
int_fast8_t read_done = 0;
|
||||
size_t write_count;
|
||||
size_t read_count;
|
||||
ssize_t ret;
|
||||
|
@ -412,7 +412,7 @@ int write_files_fn(void *data, void *ud) {
|
|||
temp_to_write->buf = malloc(4);
|
||||
temp_to_write->size = 4;
|
||||
for (size_t idx = 0; idx < temp_to_write->size; ++idx) {
|
||||
((unsigned char *)temp_to_write->buf)[idx] = 0;
|
||||
((uint8_t *)temp_to_write->buf)[idx] = 0;
|
||||
}
|
||||
|
||||
// Get file stats.
|
||||
|
@ -431,31 +431,31 @@ int write_files_fn(void *data, void *ud) {
|
|||
}
|
||||
|
||||
if ((stat_buf.st_mode & S_IRUSR) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x2;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x2;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IWUSR) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x4;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x4;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IXUSR) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x8;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x8;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IRGRP) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x10;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x10;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IWGRP) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x20;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x20;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IXGRP) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x40;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x40;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IROTH) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x80;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x80;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IWOTH) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[1] |= 0x1;
|
||||
((uint8_t *)temp_to_write->buf)[1] |= 0x1;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IXOTH) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[1] |= 0x2;
|
||||
((uint8_t *)temp_to_write->buf)[1] |= 0x2;
|
||||
}
|
||||
|
||||
simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write);
|
||||
|
@ -544,7 +544,7 @@ int write_files_fn(void *data, void *ud) {
|
|||
temp_to_write->buf = malloc(4);
|
||||
temp_to_write->size = 4;
|
||||
for (size_t idx = 0; idx < temp_to_write->size; ++idx) {
|
||||
((unsigned char *)temp_to_write->buf)[idx] = 0;
|
||||
((uint8_t *)temp_to_write->buf)[idx] = 0;
|
||||
}
|
||||
|
||||
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
|
||||
|
@ -568,35 +568,35 @@ int write_files_fn(void *data, void *ud) {
|
|||
}
|
||||
|
||||
if ((stat_buf.st_mode & S_IRUSR) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x2;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x2;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IWUSR) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x4;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x4;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IXUSR) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x8;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x8;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IRGRP) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x10;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x10;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IWGRP) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x20;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x20;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IXGRP) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x40;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x40;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IROTH) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x80;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x80;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IWOTH) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[1] |= 0x1;
|
||||
((uint8_t *)temp_to_write->buf)[1] |= 0x1;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IXOTH) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[1] |= 0x2;
|
||||
((uint8_t *)temp_to_write->buf)[1] |= 0x2;
|
||||
}
|
||||
#else
|
||||
// Unsupported platform. Just set the permission bits for user.
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0xE;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0xE;
|
||||
#endif
|
||||
|
||||
simple_archiver_list_add(to_write, temp_to_write, free_internal_to_write);
|
||||
|
@ -685,11 +685,11 @@ int write_files_fn(void *data, void *ud) {
|
|||
temp_to_write->buf = malloc(4);
|
||||
temp_to_write->size = 4;
|
||||
for (size_t idx = 0; idx < temp_to_write->size; ++idx) {
|
||||
((unsigned char *)temp_to_write->buf)[idx] = 0;
|
||||
((uint8_t *)temp_to_write->buf)[idx] = 0;
|
||||
}
|
||||
|
||||
// Set "is symbolic link" flag.
|
||||
((unsigned char *)temp_to_write->buf)[0] = 1;
|
||||
((uint8_t *)temp_to_write->buf)[0] = 1;
|
||||
|
||||
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
|
||||
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
|
||||
|
@ -705,35 +705,35 @@ int write_files_fn(void *data, void *ud) {
|
|||
}
|
||||
|
||||
if ((stat_buf.st_mode & S_IRUSR) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x2;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x2;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IWUSR) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x4;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x4;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IXUSR) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x8;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x8;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IRGRP) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x10;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x10;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IWGRP) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x20;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x20;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IXGRP) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x40;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x40;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IROTH) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0x80;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0x80;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IWOTH) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[1] |= 0x1;
|
||||
((uint8_t *)temp_to_write->buf)[1] |= 0x1;
|
||||
}
|
||||
if ((stat_buf.st_mode & S_IXOTH) != 0) {
|
||||
((unsigned char *)temp_to_write->buf)[1] |= 0x2;
|
||||
((uint8_t *)temp_to_write->buf)[1] |= 0x2;
|
||||
}
|
||||
#else
|
||||
// Unsupported platform. Just set the permission bits for user.
|
||||
((unsigned char *)temp_to_write->buf)[0] |= 0xE;
|
||||
((uint8_t *)temp_to_write->buf)[0] |= 0xE;
|
||||
#endif
|
||||
|
||||
// Need to get abs_path for checking/setting a flag before storing flags.
|
||||
|
@ -781,7 +781,7 @@ int write_files_fn(void *data, void *ud) {
|
|||
// link_substr, dest_substr);
|
||||
|
||||
// Generate the relative path.
|
||||
int has_slash = 0;
|
||||
int_fast8_t has_slash = 0;
|
||||
idx = 0;
|
||||
do {
|
||||
for (; link_substr[idx] != '/' && link_substr[idx] != 0; ++idx);
|
||||
|
@ -810,7 +810,7 @@ int write_files_fn(void *data, void *ud) {
|
|||
fprintf(stderr,
|
||||
"NOTICE: abs_path exists, \"--no-abs-symlink\" not specified, "
|
||||
"and link refers to file NOT in archive; preferring abs_path.\n");
|
||||
((unsigned char *)temp_to_write->buf)[1] |= 0x4;
|
||||
((uint8_t *)temp_to_write->buf)[1] |= 0x4;
|
||||
}
|
||||
|
||||
// Store the 4 byte bit-flags for file.
|
||||
|
@ -1063,7 +1063,7 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
|
|||
return SDAS_NO_COMPRESSOR;
|
||||
} else if (state->parsed->compressor && state->parsed->decompressor) {
|
||||
// Write the four flag bytes with first bit set.
|
||||
unsigned char c = 1;
|
||||
uint8_t c = 1;
|
||||
if (fwrite(&c, 1, 1, out_f) != 1) {
|
||||
return SDAS_FAILED_TO_WRITE;
|
||||
}
|
||||
|
@ -1116,7 +1116,7 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
|
|||
}
|
||||
} else {
|
||||
// Write the four flag bytes with first bit NOT set.
|
||||
unsigned char c = 0;
|
||||
uint8_t c = 0;
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
if (fwrite(&c, 1, 1, out_f) != 1) {
|
||||
return SDAS_FAILED_TO_WRITE;
|
||||
|
@ -1171,14 +1171,14 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
|
|||
return SDAS_SUCCESS;
|
||||
}
|
||||
|
||||
int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
||||
int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract,
|
||||
const SDArchiverState *state) {
|
||||
unsigned char buf[1024];
|
||||
uint8_t buf[1024];
|
||||
memset(buf, 0, 1024);
|
||||
uint16_t u16;
|
||||
uint32_t u32;
|
||||
uint64_t u64;
|
||||
int is_compressed = 0;
|
||||
int_fast8_t is_compressed = 0;
|
||||
|
||||
if (fread(buf, 1, 18, in_f) != 18) {
|
||||
return SDAS_INVALID_FILE;
|
||||
|
@ -1226,7 +1226,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
__attribute__((
|
||||
cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf =
|
||||
malloc(u16 + 1);
|
||||
unsigned char *uc_heap_buf = heap_buf;
|
||||
uint8_t *uc_heap_buf = heap_buf;
|
||||
if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
|
||||
return SDAS_INVALID_FILE;
|
||||
}
|
||||
|
@ -1253,7 +1253,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
__attribute__((
|
||||
cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf =
|
||||
malloc(u16 + 1);
|
||||
unsigned char *uc_heap_buf = heap_buf;
|
||||
uint8_t *uc_heap_buf = heap_buf;
|
||||
if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
|
||||
return SDAS_INVALID_FILE;
|
||||
}
|
||||
|
@ -1274,7 +1274,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
const size_t digits = simple_archiver_helper_num_digits(size);
|
||||
char format_str[128];
|
||||
snprintf(format_str, 128, FILE_COUNTS_OUTPUT_FORMAT_STR_0, digits, digits);
|
||||
int skip = 0;
|
||||
int_fast8_t skip = 0;
|
||||
__attribute__((cleanup(simple_archiver_hash_map_free)))
|
||||
SDArchiverHashMap *hash_map = NULL;
|
||||
if (state && state->parsed->working_files &&
|
||||
|
@ -1333,7 +1333,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
__attribute__((
|
||||
cleanup(simple_archiver_helper_cleanup_malloced))) void *heap_buf =
|
||||
malloc(u16 + 1);
|
||||
unsigned char *uc_heap_buf = heap_buf;
|
||||
uint8_t *uc_heap_buf = heap_buf;
|
||||
if (fread(uc_heap_buf, 1, u16 + 1, in_f) != (size_t)u16 + 1) {
|
||||
return SDAS_INVALID_FILE;
|
||||
}
|
||||
|
@ -1465,7 +1465,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
fprintf(stderr, " File size: %lu\n", u64);
|
||||
}
|
||||
|
||||
int skip_due_to_map = 0;
|
||||
int_fast8_t skip_due_to_map = 0;
|
||||
if (hash_map != NULL && out_f_name) {
|
||||
if (simple_archiver_hash_map_get(hash_map, out_f_name,
|
||||
strlen(out_f_name) + 1) == NULL) {
|
||||
|
@ -1573,9 +1573,9 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
}
|
||||
|
||||
uint64_t compressed_file_size = u64;
|
||||
int write_again = 0;
|
||||
int write_pipe_done = 0;
|
||||
int read_pipe_done = 0;
|
||||
int_fast8_t write_again = 0;
|
||||
int_fast8_t write_pipe_done = 0;
|
||||
int_fast8_t read_pipe_done = 0;
|
||||
size_t fread_ret;
|
||||
char recv_buf[1024];
|
||||
size_t amount_to_read;
|
||||
|
@ -1747,7 +1747,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
|||
} else {
|
||||
// Is a symbolic link.
|
||||
|
||||
int abs_preferred = (buf[1] & 0x4) != 0 ? 1 : 0;
|
||||
int_fast8_t abs_preferred = (buf[1] & 0x4) != 0 ? 1 : 0;
|
||||
fprintf(stderr, " Absolute path is %s\n",
|
||||
(abs_preferred ? "preferred" : "NOT preferred"));
|
||||
|
||||
|
|
|
@ -19,8 +19,11 @@
|
|||
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_ARCHIVER_H_
|
||||
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_ARCHIVER_H_
|
||||
|
||||
// Standard library includes.
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Local includes.
|
||||
#include "data_structures/hash_map.h"
|
||||
#include "data_structures/linked_list.h"
|
||||
#include "parser.h"
|
||||
|
@ -28,7 +31,7 @@
|
|||
typedef struct SDArchiverState {
|
||||
/*
|
||||
*/
|
||||
unsigned int flags;
|
||||
uint32_t flags;
|
||||
const SDArchiverParsed *parsed;
|
||||
FILE *out_f;
|
||||
SDArchiverHashMap *map;
|
||||
|
@ -63,7 +66,7 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
|
|||
const SDArchiverLinkedList *filenames);
|
||||
|
||||
/// Returns zero on success.
|
||||
int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
|
||||
int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract,
|
||||
const SDArchiverState *state);
|
||||
|
||||
/// Returns zero on success.
|
||||
|
|
|
@ -80,7 +80,7 @@ unsigned long long simple_archiver_hash_map_internal_key_to_hash(
|
|||
unsigned long long temp = 0;
|
||||
size_t count = 0;
|
||||
for (size_t idx = 0; idx < key_size; ++idx) {
|
||||
temp |= ((unsigned long long)*((unsigned char *)key + idx)) << (8 * count);
|
||||
temp |= ((unsigned long long)*((uint8_t *)key + idx)) << (8 * count);
|
||||
++count;
|
||||
if (count >= 8) {
|
||||
count = 0;
|
||||
|
@ -159,16 +159,21 @@ SDArchiverHashMap *simple_archiver_hash_map_init(void) {
|
|||
return hash_map;
|
||||
}
|
||||
|
||||
void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map) {
|
||||
if (hash_map && *hash_map) {
|
||||
for (size_t idx = 0; idx < (*hash_map)->buckets_size; ++idx) {
|
||||
SDArchiverLinkedList **linked_list = (*hash_map)->buckets + idx;
|
||||
void simple_archiver_hash_map_free_single_ptr(SDArchiverHashMap *hash_map) {
|
||||
if (hash_map) {
|
||||
for (size_t idx = 0; idx < hash_map->buckets_size; ++idx) {
|
||||
SDArchiverLinkedList **linked_list = hash_map->buckets + idx;
|
||||
simple_archiver_list_free(linked_list);
|
||||
}
|
||||
|
||||
free((*hash_map)->buckets);
|
||||
free(*hash_map);
|
||||
free(hash_map->buckets);
|
||||
free(hash_map);
|
||||
}
|
||||
}
|
||||
|
||||
void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map) {
|
||||
if (hash_map && *hash_map) {
|
||||
simple_archiver_hash_map_free_single_ptr(*hash_map);
|
||||
*hash_map = NULL;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,8 +21,11 @@
|
|||
|
||||
#define SC_SA_DS_HASH_MAP_START_BUCKET_SIZE 32
|
||||
|
||||
// Standard library includes.
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Local includes.
|
||||
#include "linked_list.h"
|
||||
|
||||
typedef struct SDArchiverHashMap {
|
||||
|
@ -32,6 +35,11 @@ typedef struct SDArchiverHashMap {
|
|||
} SDArchiverHashMap;
|
||||
|
||||
SDArchiverHashMap *simple_archiver_hash_map_init(void);
|
||||
|
||||
/// It is recommended to use the double-pointer version of hash-map free as
|
||||
/// that will ensure the variable holding the pointer will end up pointing to
|
||||
/// NULL after free.
|
||||
void simple_archiver_hash_map_free_single_ptr(SDArchiverHashMap *hash_map);
|
||||
void simple_archiver_hash_map_free(SDArchiverHashMap **hash_map);
|
||||
|
||||
/// Returns zero on success.
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
|
||||
#include "linked_list.h"
|
||||
|
||||
// Standard library includes.
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
SDArchiverLinkedList *simple_archiver_list_init(void) {
|
||||
|
@ -41,9 +43,9 @@ SDArchiverLinkedList *simple_archiver_list_init(void) {
|
|||
return list;
|
||||
}
|
||||
|
||||
void simple_archiver_list_free(SDArchiverLinkedList **list) {
|
||||
if (list && *list) {
|
||||
SDArchiverLLNode *node = (*list)->head;
|
||||
void simple_archiver_list_free_single_ptr(SDArchiverLinkedList *list) {
|
||||
if (list) {
|
||||
SDArchiverLLNode *node = list->head;
|
||||
SDArchiverLLNode *prev;
|
||||
while (node) {
|
||||
prev = node;
|
||||
|
@ -58,7 +60,13 @@ void simple_archiver_list_free(SDArchiverLinkedList **list) {
|
|||
}
|
||||
}
|
||||
|
||||
free(*list);
|
||||
free(list);
|
||||
}
|
||||
}
|
||||
|
||||
void simple_archiver_list_free(SDArchiverLinkedList **list) {
|
||||
if (list && *list) {
|
||||
simple_archiver_list_free_single_ptr(*list);
|
||||
*list = NULL;
|
||||
}
|
||||
}
|
||||
|
@ -112,10 +120,10 @@ int simple_archiver_list_remove(SDArchiverLinkedList *list,
|
|||
return 0;
|
||||
}
|
||||
|
||||
int removed_count = 0;
|
||||
int32_t removed_count = 0;
|
||||
|
||||
SDArchiverLLNode *node = list->head;
|
||||
int iter_removed = 0;
|
||||
int32_t iter_removed = 0;
|
||||
while (node) {
|
||||
if (iter_removed == 0) {
|
||||
node = node->next;
|
||||
|
|
|
@ -35,6 +35,11 @@ typedef struct SDArchiverLinkedList {
|
|||
} SDArchiverLinkedList;
|
||||
|
||||
SDArchiverLinkedList *simple_archiver_list_init(void);
|
||||
|
||||
/// It is recommended to use the double-pointer version of list free as that
|
||||
/// will ensure the variable holding the pointer will end up pointing to NULL
|
||||
/// after free.
|
||||
void simple_archiver_list_free_single_ptr(SDArchiverLinkedList *list);
|
||||
void simple_archiver_list_free(SDArchiverLinkedList **list);
|
||||
|
||||
/// Returns 0 on success. Puts data at the end of the list
|
||||
|
|
|
@ -21,32 +21,32 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
void simple_archiver_priority_heap_internal_realloc(
|
||||
SDArchiverPHeap **priority_heap) {
|
||||
SDArchiverPHeap *new_priority_heap = malloc(sizeof(SDArchiverPHeap));
|
||||
SDArchiverPHeap *priority_heap) {
|
||||
SDArchiverPHeap new_priority_heap;
|
||||
|
||||
new_priority_heap->capacity = (*priority_heap)->capacity * 2;
|
||||
new_priority_heap->size = 0;
|
||||
new_priority_heap->less_fn = (*priority_heap)->less_fn;
|
||||
new_priority_heap.capacity = priority_heap->capacity * 2;
|
||||
new_priority_heap.size = 0;
|
||||
new_priority_heap.less_fn = priority_heap->less_fn;
|
||||
|
||||
new_priority_heap->nodes =
|
||||
calloc(new_priority_heap->capacity, sizeof(SDArchiverPHNode));
|
||||
new_priority_heap.nodes =
|
||||
calloc(new_priority_heap.capacity, sizeof(SDArchiverPHNode));
|
||||
|
||||
for (size_t idx = 1; idx < (*priority_heap)->size + 1; ++idx) {
|
||||
if ((*priority_heap)->nodes[idx].is_valid != 0) {
|
||||
for (size_t idx = 1; idx < priority_heap->size + 1; ++idx) {
|
||||
if (priority_heap->nodes[idx].is_valid != 0) {
|
||||
simple_archiver_priority_heap_insert(
|
||||
&new_priority_heap, (*priority_heap)->nodes[idx].priority,
|
||||
(*priority_heap)->nodes[idx].data,
|
||||
(*priority_heap)->nodes[idx].data_cleanup_fn);
|
||||
(*priority_heap)->nodes[idx].is_valid = 0;
|
||||
&new_priority_heap, priority_heap->nodes[idx].priority,
|
||||
priority_heap->nodes[idx].data,
|
||||
priority_heap->nodes[idx].data_cleanup_fn);
|
||||
priority_heap->nodes[idx].is_valid = 0;
|
||||
}
|
||||
}
|
||||
|
||||
simple_archiver_priority_heap_free(priority_heap);
|
||||
free(priority_heap->nodes);
|
||||
|
||||
*priority_heap = new_priority_heap;
|
||||
}
|
||||
|
||||
int simple_archiver_priority_heap_default_less(long long a, long long b) {
|
||||
int simple_archiver_priority_heap_default_less(int64_t a, int64_t b) {
|
||||
return a < b ? 1 : 0;
|
||||
}
|
||||
|
||||
|
@ -64,7 +64,7 @@ SDArchiverPHeap *simple_archiver_priority_heap_init(void) {
|
|||
}
|
||||
|
||||
SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn(
|
||||
int (*less_fn)(long long, long long)) {
|
||||
int (*less_fn)(int64_t, int64_t)) {
|
||||
SDArchiverPHeap *priority_heap = malloc(sizeof(SDArchiverPHeap));
|
||||
|
||||
priority_heap->capacity = SC_SA_DS_PRIORITY_HEAP_START_SIZE;
|
||||
|
@ -77,54 +77,59 @@ SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn(
|
|||
return priority_heap;
|
||||
}
|
||||
|
||||
void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap) {
|
||||
if (priority_heap && *priority_heap) {
|
||||
for (size_t idx = 1; idx < (*priority_heap)->size + 1; ++idx) {
|
||||
if ((*priority_heap)->nodes[idx].is_valid != 0) {
|
||||
if ((*priority_heap)->nodes[idx].data_cleanup_fn) {
|
||||
(*priority_heap)
|
||||
->nodes[idx]
|
||||
.data_cleanup_fn((*priority_heap)->nodes[idx].data);
|
||||
void simple_archiver_priority_heap_free_single_ptr(
|
||||
SDArchiverPHeap *priority_heap) {
|
||||
if (priority_heap) {
|
||||
for (size_t idx = 1; idx < priority_heap->size + 1; ++idx) {
|
||||
if (priority_heap->nodes[idx].is_valid != 0) {
|
||||
if (priority_heap->nodes[idx].data_cleanup_fn) {
|
||||
priority_heap->nodes[idx].data_cleanup_fn(
|
||||
priority_heap->nodes[idx].data);
|
||||
} else {
|
||||
free((*priority_heap)->nodes[idx].data);
|
||||
free(priority_heap->nodes[idx].data);
|
||||
}
|
||||
(*priority_heap)->nodes[idx].is_valid = 0;
|
||||
priority_heap->nodes[idx].is_valid = 0;
|
||||
}
|
||||
}
|
||||
|
||||
free((*priority_heap)->nodes);
|
||||
free(*priority_heap);
|
||||
free(priority_heap->nodes);
|
||||
free(priority_heap);
|
||||
}
|
||||
}
|
||||
|
||||
void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap) {
|
||||
if (priority_heap && *priority_heap) {
|
||||
simple_archiver_priority_heap_free_single_ptr(*priority_heap);
|
||||
*priority_heap = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void simple_archiver_priority_heap_insert(SDArchiverPHeap **priority_heap,
|
||||
long long priority, void *data,
|
||||
void simple_archiver_priority_heap_insert(SDArchiverPHeap *priority_heap,
|
||||
int64_t priority, void *data,
|
||||
void (*data_cleanup_fn)(void *)) {
|
||||
if (!priority_heap || !*priority_heap) {
|
||||
if (!priority_heap) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((*priority_heap)->size + 1 >= (*priority_heap)->capacity) {
|
||||
if (priority_heap->size + 1 >= priority_heap->capacity) {
|
||||
simple_archiver_priority_heap_internal_realloc(priority_heap);
|
||||
}
|
||||
|
||||
size_t hole = (*priority_heap)->size + 1;
|
||||
size_t hole = priority_heap->size + 1;
|
||||
|
||||
while (hole > 1 &&
|
||||
(*priority_heap)
|
||||
->less_fn(priority,
|
||||
(*priority_heap)->nodes[hole / 2].priority) != 0) {
|
||||
(*priority_heap)->nodes[hole] = (*priority_heap)->nodes[hole / 2];
|
||||
priority_heap->less_fn(priority,
|
||||
priority_heap->nodes[hole / 2].priority) != 0) {
|
||||
priority_heap->nodes[hole] = priority_heap->nodes[hole / 2];
|
||||
hole /= 2;
|
||||
}
|
||||
|
||||
(*priority_heap)->nodes[hole].priority = priority;
|
||||
(*priority_heap)->nodes[hole].data = data;
|
||||
(*priority_heap)->nodes[hole].data_cleanup_fn = data_cleanup_fn;
|
||||
(*priority_heap)->nodes[hole].is_valid = 1;
|
||||
priority_heap->nodes[hole].priority = priority;
|
||||
priority_heap->nodes[hole].data = data;
|
||||
priority_heap->nodes[hole].data_cleanup_fn = data_cleanup_fn;
|
||||
priority_heap->nodes[hole].is_valid = 1;
|
||||
|
||||
++(*priority_heap)->size;
|
||||
++priority_heap->size;
|
||||
}
|
||||
|
||||
void *simple_archiver_priority_heap_top(SDArchiverPHeap *priority_heap) {
|
||||
|
|
|
@ -19,10 +19,13 @@
|
|||
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_PRIORITY_HEAP_H_
|
||||
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_DATA_STRUCTURE_PRIORITY_HEAP_H_
|
||||
|
||||
// Standard library includes.
|
||||
#include <stdint.h>
|
||||
|
||||
#define SC_SA_DS_PRIORITY_HEAP_START_SIZE 32
|
||||
|
||||
typedef struct SDArchiverPHNode {
|
||||
long long priority;
|
||||
int64_t priority;
|
||||
void *data;
|
||||
void (*data_cleanup_fn)(void *);
|
||||
/// Is non-zero if valid.
|
||||
|
@ -31,23 +34,29 @@ typedef struct SDArchiverPHNode {
|
|||
|
||||
typedef struct SDArchiverPHeap {
|
||||
SDArchiverPHNode *nodes;
|
||||
unsigned long long capacity;
|
||||
unsigned long long size;
|
||||
int (*less_fn)(long long, long long);
|
||||
uint64_t capacity;
|
||||
uint64_t size;
|
||||
int (*less_fn)(int64_t, int64_t);
|
||||
} SDArchiverPHeap;
|
||||
|
||||
/// Default "less" function to determine if a has higher priority than b.
|
||||
/// Returns non-zero if "less".
|
||||
int simple_archiver_priority_heap_default_less(long long a, long long b);
|
||||
int simple_archiver_priority_heap_default_less(int64_t a, int64_t b);
|
||||
|
||||
SDArchiverPHeap *simple_archiver_priority_heap_init(void);
|
||||
SDArchiverPHeap *simple_archiver_priority_heap_init_less_fn(
|
||||
int (*less_fn)(long long, long long));
|
||||
int (*less_fn)(int64_t, int64_t));
|
||||
|
||||
/// It is recommended to use the double-pointer version of priority-heap free
|
||||
/// as that will ensure the variable holding the pointer will end up pointing
|
||||
/// to NULL after free.
|
||||
void simple_archiver_priority_heap_free_single_ptr(
|
||||
SDArchiverPHeap *priority_heap);
|
||||
void simple_archiver_priority_heap_free(SDArchiverPHeap **priority_heap);
|
||||
|
||||
/// If data_cleanup_fn is NULL, then "free()" is used on data when freed.
|
||||
void simple_archiver_priority_heap_insert(SDArchiverPHeap **priority_heap,
|
||||
long long priority, void *data,
|
||||
void simple_archiver_priority_heap_insert(SDArchiverPHeap *priority_heap,
|
||||
int64_t priority, void *data,
|
||||
void (*data_cleanup_fn)(void *));
|
||||
|
||||
/// Returns NULL if empty or if priority_heap is NULL.
|
||||
|
|
|
@ -16,10 +16,13 @@
|
|||
//
|
||||
// `data_structures/test.c` is the source for testing data structure code.
|
||||
|
||||
// Standard library includes.
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Local includes.
|
||||
#include "../algorithms/linear_congruential_gen.h"
|
||||
#include "hash_map.h"
|
||||
#include "linked_list.h"
|
||||
|
@ -27,8 +30,8 @@
|
|||
|
||||
#define SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE 100
|
||||
|
||||
static int checks_checked = 0;
|
||||
static int checks_passed = 0;
|
||||
static int32_t checks_checked = 0;
|
||||
static int32_t checks_passed = 0;
|
||||
|
||||
#define CHECK_TRUE(x) \
|
||||
do { \
|
||||
|
@ -63,7 +66,7 @@ int get_three_fn(void *data, __attribute__((unused)) void *ud) {
|
|||
return strcmp(data, "three") == 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
int more_fn(long long a, long long b) { return a > b ? 1 : 0; }
|
||||
int more_fn(int64_t a, int64_t b) { return a > b ? 1 : 0; }
|
||||
|
||||
int hash_map_iter_check_fn(__attribute__((unused)) const void *key,
|
||||
__attribute__((unused)) size_t key_size,
|
||||
|
@ -138,37 +141,37 @@ int main(void) {
|
|||
hash_map = simple_archiver_hash_map_init();
|
||||
|
||||
{
|
||||
int *value, *key;
|
||||
int32_t *value, *key;
|
||||
|
||||
for (unsigned int idx = 0; idx < 20; ++idx) {
|
||||
value = malloc(sizeof(int));
|
||||
key = malloc(sizeof(int));
|
||||
for (uint32_t idx = 0; idx < 20; ++idx) {
|
||||
value = malloc(sizeof(int32_t));
|
||||
key = malloc(sizeof(int32_t));
|
||||
*value = idx;
|
||||
*key = idx;
|
||||
simple_archiver_hash_map_insert(hash_map, value, key, sizeof(int), NULL,
|
||||
NULL);
|
||||
simple_archiver_hash_map_insert(hash_map, value, key, sizeof(int32_t),
|
||||
NULL, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
int value, key;
|
||||
int32_t value, key;
|
||||
void *value_ptr;
|
||||
|
||||
for (value = 0, key = 0; value < 20 && key < 20; ++value, ++key) {
|
||||
value_ptr = simple_archiver_hash_map_get(hash_map, &key, sizeof(int));
|
||||
value_ptr = simple_archiver_hash_map_get(hash_map, &key, sizeof(int32_t));
|
||||
CHECK_TRUE(value_ptr != NULL);
|
||||
CHECK_TRUE(memcmp(value_ptr, &value, sizeof(int)) == 0);
|
||||
CHECK_TRUE(memcmp(value_ptr, &value, sizeof(int32_t)) == 0);
|
||||
}
|
||||
|
||||
key = 5;
|
||||
simple_archiver_hash_map_remove(hash_map, &key, sizeof(int));
|
||||
simple_archiver_hash_map_remove(hash_map, &key, sizeof(int32_t));
|
||||
key = 15;
|
||||
simple_archiver_hash_map_remove(hash_map, &key, sizeof(int));
|
||||
simple_archiver_hash_map_remove(hash_map, &key, sizeof(int32_t));
|
||||
|
||||
for (value = 0, key = 0; value < 20 && key < 20; ++value, ++key) {
|
||||
value_ptr = simple_archiver_hash_map_get(hash_map, &key, sizeof(int));
|
||||
value_ptr = simple_archiver_hash_map_get(hash_map, &key, sizeof(int32_t));
|
||||
if (key != 5 && key != 15) {
|
||||
CHECK_TRUE(value_ptr != NULL);
|
||||
CHECK_TRUE(memcmp(value_ptr, &value, sizeof(int)) == 0);
|
||||
CHECK_TRUE(memcmp(value_ptr, &value, sizeof(int32_t)) == 0);
|
||||
} else {
|
||||
CHECK_TRUE(value_ptr == NULL);
|
||||
}
|
||||
|
@ -178,14 +181,14 @@ int main(void) {
|
|||
|
||||
// Rehash test for Memcheck.
|
||||
hash_map = simple_archiver_hash_map_init();
|
||||
for (unsigned int idx = 0; idx < SC_SA_DS_HASH_MAP_START_BUCKET_SIZE + 1;
|
||||
for (uint32_t idx = 0; idx < SC_SA_DS_HASH_MAP_START_BUCKET_SIZE + 1;
|
||||
++idx) {
|
||||
unsigned int *copy_value = malloc(sizeof(unsigned int));
|
||||
uint32_t *copy_value = malloc(sizeof(uint32_t));
|
||||
*copy_value = idx;
|
||||
unsigned int *copy_key = malloc(sizeof(unsigned int));
|
||||
uint32_t *copy_key = malloc(sizeof(uint32_t));
|
||||
*copy_key = idx;
|
||||
simple_archiver_hash_map_insert(hash_map, copy_value, copy_key,
|
||||
sizeof(unsigned int), NULL, NULL);
|
||||
sizeof(uint32_t), NULL, NULL);
|
||||
}
|
||||
simple_archiver_hash_map_free(&hash_map);
|
||||
|
||||
|
@ -202,16 +205,14 @@ int main(void) {
|
|||
CHECK_TRUE(simple_archiver_hash_map_iter(hash_map, hash_map_iter_check_fn,
|
||||
found) == 0);
|
||||
|
||||
for (unsigned int idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE;
|
||||
++idx) {
|
||||
for (uint32_t idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE; ++idx) {
|
||||
CHECK_TRUE(found[idx] == 1);
|
||||
}
|
||||
|
||||
CHECK_TRUE(simple_archiver_hash_map_iter(hash_map, hash_map_iter_check_fn2,
|
||||
found) == 2);
|
||||
|
||||
for (unsigned int idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE;
|
||||
++idx) {
|
||||
for (uint32_t idx = 0; idx < SDARCHIVER_DS_TEST_HASH_MAP_ITER_SIZE; ++idx) {
|
||||
CHECK_TRUE(found[idx] == 1);
|
||||
}
|
||||
|
||||
|
@ -226,13 +227,13 @@ int main(void) {
|
|||
priority_heap = simple_archiver_priority_heap_init();
|
||||
|
||||
// Just 3 elements.
|
||||
for (unsigned int idx = 0; idx < 3; ++idx) {
|
||||
unsigned int *data = malloc(sizeof(unsigned int));
|
||||
for (uint32_t idx = 0; idx < 3; ++idx) {
|
||||
uint32_t *data = malloc(sizeof(uint32_t));
|
||||
*data = idx;
|
||||
simple_archiver_priority_heap_insert(&priority_heap, idx, data, NULL);
|
||||
simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
|
||||
}
|
||||
for (unsigned int idx = 0; idx < 3; ++idx) {
|
||||
unsigned int *data = simple_archiver_priority_heap_top(priority_heap);
|
||||
for (uint32_t idx = 0; idx < 3; ++idx) {
|
||||
uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
|
||||
CHECK_TRUE(*data == idx);
|
||||
if (*data != idx) {
|
||||
printf("idx is %u, data is %u\n", idx, *data);
|
||||
|
@ -246,16 +247,16 @@ int main(void) {
|
|||
}
|
||||
|
||||
// 100 elements.
|
||||
unsigned int max = 100;
|
||||
uint32_t max = 100;
|
||||
|
||||
for (unsigned int idx = 0; idx < max; ++idx) {
|
||||
unsigned int *data = malloc(sizeof(unsigned int));
|
||||
for (uint32_t idx = 0; idx < max; ++idx) {
|
||||
uint32_t *data = malloc(sizeof(uint32_t));
|
||||
*data = idx;
|
||||
simple_archiver_priority_heap_insert(&priority_heap, idx, data, NULL);
|
||||
simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
|
||||
}
|
||||
|
||||
for (unsigned int idx = 0; idx < max; ++idx) {
|
||||
unsigned int *data = simple_archiver_priority_heap_top(priority_heap);
|
||||
for (uint32_t idx = 0; idx < max; ++idx) {
|
||||
uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
|
||||
CHECK_TRUE(*data == idx);
|
||||
data = simple_archiver_priority_heap_pop(priority_heap);
|
||||
CHECK_TRUE(*data == idx);
|
||||
|
@ -263,14 +264,14 @@ int main(void) {
|
|||
}
|
||||
|
||||
// Insert in reverse order.
|
||||
for (unsigned int idx = max; idx-- > 0;) {
|
||||
unsigned int *data = malloc(sizeof(unsigned int));
|
||||
for (uint32_t idx = max; idx-- > 0;) {
|
||||
uint32_t *data = malloc(sizeof(uint32_t));
|
||||
*data = idx;
|
||||
simple_archiver_priority_heap_insert(&priority_heap, idx, data, NULL);
|
||||
simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
|
||||
}
|
||||
|
||||
for (unsigned int idx = 0; idx < max; ++idx) {
|
||||
unsigned int *data = simple_archiver_priority_heap_top(priority_heap);
|
||||
for (uint32_t idx = 0; idx < max; ++idx) {
|
||||
uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
|
||||
CHECK_TRUE(*data == idx);
|
||||
data = simple_archiver_priority_heap_pop(priority_heap);
|
||||
CHECK_TRUE(*data == idx);
|
||||
|
@ -278,30 +279,30 @@ int main(void) {
|
|||
}
|
||||
|
||||
// Insert in random order.
|
||||
unsigned int *array = malloc(sizeof(unsigned int) * max);
|
||||
for (unsigned int idx = 0; idx < max; ++idx) {
|
||||
uint32_t *array = malloc(sizeof(uint32_t) * max);
|
||||
for (uint32_t idx = 0; idx < max; ++idx) {
|
||||
array[idx] = idx;
|
||||
}
|
||||
|
||||
// Deterministic randomization.
|
||||
for (unsigned int idx = max - 1; idx-- > 0;) {
|
||||
unsigned int other_idx = simple_archiver_algo_lcg_defaults(idx) %
|
||||
for (uint32_t idx = max - 1; idx-- > 0;) {
|
||||
uint32_t other_idx = simple_archiver_algo_lcg_defaults(idx) %
|
||||
(unsigned long long)(idx + 1);
|
||||
if (max - 1 != other_idx) {
|
||||
unsigned int temp = array[max - 1];
|
||||
uint32_t temp = array[max - 1];
|
||||
array[max - 1] = array[other_idx];
|
||||
array[other_idx] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// Insert the deterministically randomized array.
|
||||
for (unsigned int idx = 0; idx < max; ++idx) {
|
||||
simple_archiver_priority_heap_insert(&priority_heap, array[idx],
|
||||
for (uint32_t idx = 0; idx < max; ++idx) {
|
||||
simple_archiver_priority_heap_insert(priority_heap, array[idx],
|
||||
array + idx, no_free_fn);
|
||||
}
|
||||
|
||||
for (unsigned int idx = 0; idx < max; ++idx) {
|
||||
unsigned int *data = simple_archiver_priority_heap_top(priority_heap);
|
||||
for (uint32_t idx = 0; idx < max; ++idx) {
|
||||
uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
|
||||
CHECK_TRUE(*data == idx);
|
||||
if (*data != idx) {
|
||||
printf("idx is %u, data is %u\n", idx, *data);
|
||||
|
@ -318,24 +319,24 @@ int main(void) {
|
|||
|
||||
// Insert, don't pop, do free, for memcheck.
|
||||
priority_heap = simple_archiver_priority_heap_init();
|
||||
for (unsigned int idx = 0; idx < max; ++idx) {
|
||||
unsigned int *data = malloc(sizeof(unsigned int));
|
||||
for (uint32_t idx = 0; idx < max; ++idx) {
|
||||
uint32_t *data = malloc(sizeof(uint32_t));
|
||||
*data = idx;
|
||||
simple_archiver_priority_heap_insert(&priority_heap, idx, data, NULL);
|
||||
simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
|
||||
}
|
||||
simple_archiver_priority_heap_free(&priority_heap);
|
||||
|
||||
// Reverse priority.
|
||||
priority_heap = simple_archiver_priority_heap_init_less_fn(more_fn);
|
||||
|
||||
for (unsigned int idx = 0; idx < max; ++idx) {
|
||||
unsigned int *data = malloc(sizeof(unsigned int));
|
||||
for (uint32_t idx = 0; idx < max; ++idx) {
|
||||
uint32_t *data = malloc(sizeof(uint32_t));
|
||||
*data = idx;
|
||||
simple_archiver_priority_heap_insert(&priority_heap, idx, data, NULL);
|
||||
simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL);
|
||||
}
|
||||
|
||||
for (unsigned int idx = max; idx-- > 0;) {
|
||||
unsigned int *data = simple_archiver_priority_heap_top(priority_heap);
|
||||
for (uint32_t idx = max; idx-- > 0;) {
|
||||
uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
|
||||
CHECK_TRUE(*data == idx);
|
||||
data = simple_archiver_priority_heap_pop(priority_heap);
|
||||
CHECK_TRUE(*data == idx);
|
||||
|
@ -346,30 +347,30 @@ int main(void) {
|
|||
|
||||
// Insert in random order with reverse-priority-heap.
|
||||
priority_heap = simple_archiver_priority_heap_init_less_fn(more_fn);
|
||||
array = malloc(sizeof(unsigned int) * max);
|
||||
for (unsigned int idx = 0; idx < max; ++idx) {
|
||||
array = malloc(sizeof(uint32_t) * max);
|
||||
for (uint32_t idx = 0; idx < max; ++idx) {
|
||||
array[idx] = idx;
|
||||
}
|
||||
|
||||
// Deterministic randomization.
|
||||
for (unsigned int idx = max - 1; idx-- > 0;) {
|
||||
unsigned int other_idx = simple_archiver_algo_lcg_defaults(idx) %
|
||||
for (uint32_t idx = max - 1; idx-- > 0;) {
|
||||
uint32_t other_idx = simple_archiver_algo_lcg_defaults(idx) %
|
||||
(unsigned long long)(idx + 1);
|
||||
if (max - 1 != other_idx) {
|
||||
unsigned int temp = array[max - 1];
|
||||
uint32_t temp = array[max - 1];
|
||||
array[max - 1] = array[other_idx];
|
||||
array[other_idx] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
// Insert the deterministically randomized array.
|
||||
for (unsigned int idx = 0; idx < max; ++idx) {
|
||||
simple_archiver_priority_heap_insert(&priority_heap, array[idx],
|
||||
for (uint32_t idx = 0; idx < max; ++idx) {
|
||||
simple_archiver_priority_heap_insert(priority_heap, array[idx],
|
||||
array + idx, no_free_fn);
|
||||
}
|
||||
|
||||
for (unsigned int idx = max; idx-- > 0;) {
|
||||
unsigned int *data = simple_archiver_priority_heap_top(priority_heap);
|
||||
for (uint32_t idx = max; idx-- > 0;) {
|
||||
uint32_t *data = simple_archiver_priority_heap_top(priority_heap);
|
||||
CHECK_TRUE(*data == idx);
|
||||
if (*data != idx) {
|
||||
printf("idx is %u, data is %u\n", idx, *data);
|
||||
|
|
|
@ -80,41 +80,41 @@ int simple_archiver_helper_is_big_endian(void) {
|
|||
|
||||
void simple_archiver_helper_16_bit_be(uint16_t *value) {
|
||||
if (simple_archiver_helper_is_big_endian() == 0) {
|
||||
unsigned char c = ((unsigned char *)value)[0];
|
||||
((unsigned char *)value)[0] = ((unsigned char *)value)[1];
|
||||
((unsigned char *)value)[1] = c;
|
||||
uint8_t c = ((uint8_t *)value)[0];
|
||||
((uint8_t *)value)[0] = ((uint8_t *)value)[1];
|
||||
((uint8_t *)value)[1] = c;
|
||||
}
|
||||
}
|
||||
|
||||
void simple_archiver_helper_32_bit_be(uint32_t *value) {
|
||||
if (simple_archiver_helper_is_big_endian() == 0) {
|
||||
for (unsigned int i = 0; i < 2; ++i) {
|
||||
unsigned char c = ((unsigned char *)value)[i];
|
||||
((unsigned char *)value)[i] = ((unsigned char *)value)[3 - i];
|
||||
((unsigned char *)value)[3 - i] = c;
|
||||
for (uint32_t i = 0; i < 2; ++i) {
|
||||
uint8_t c = ((uint8_t *)value)[i];
|
||||
((uint8_t *)value)[i] = ((uint8_t *)value)[3 - i];
|
||||
((uint8_t *)value)[3 - i] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void simple_archiver_helper_64_bit_be(uint64_t *value) {
|
||||
if (simple_archiver_helper_is_big_endian() == 0) {
|
||||
for (unsigned int i = 0; i < 4; ++i) {
|
||||
unsigned char c = ((unsigned char *)value)[i];
|
||||
((unsigned char *)value)[i] = ((unsigned char *)value)[7 - i];
|
||||
((unsigned char *)value)[7 - i] = c;
|
||||
for (uint32_t i = 0; i < 4; ++i) {
|
||||
uint8_t c = ((uint8_t *)value)[i];
|
||||
((uint8_t *)value)[i] = ((uint8_t *)value)[7 - i];
|
||||
((uint8_t *)value)[7 - i] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char **simple_archiver_helper_cmd_string_to_argv(const char *cmd) {
|
||||
unsigned int capacity = 16;
|
||||
unsigned int idx = 0;
|
||||
uint32_t capacity = 16;
|
||||
uint32_t idx = 0;
|
||||
// Size of every pointer is the same, so using size of (void*) should be ok.
|
||||
char **args = malloc(sizeof(void *) * capacity);
|
||||
memset(args, 0, sizeof(void *) * capacity);
|
||||
|
||||
unsigned int word_capacity = 16;
|
||||
unsigned int word_idx = 0;
|
||||
uint32_t word_capacity = 16;
|
||||
uint32_t word_idx = 0;
|
||||
char *word = malloc(word_capacity);
|
||||
memset(word, 0, word_capacity);
|
||||
for (const char *c = cmd; *c != 0; ++c) {
|
||||
|
|
|
@ -19,10 +19,11 @@
|
|||
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_HELPERS_H_
|
||||
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_HELPERS_H_
|
||||
|
||||
// Standard library includes.
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static const unsigned int MAX_SYMBOLIC_LINK_SIZE = 512;
|
||||
static const uint32_t MAX_SYMBOLIC_LINK_SIZE = 512;
|
||||
|
||||
/// Returns non-zero if this system is big-endian.
|
||||
int simple_archiver_helper_is_big_endian(void);
|
||||
|
|
|
@ -201,7 +201,7 @@ int simple_archiver_parse_args(int argc, const char **argv,
|
|||
--argc;
|
||||
++argv;
|
||||
|
||||
int is_remaining_args = 0;
|
||||
int_fast8_t is_remaining_args = 0;
|
||||
|
||||
while (argc > 0) {
|
||||
if (!is_remaining_args) {
|
||||
|
@ -356,7 +356,7 @@ void simple_archiver_free_parsed(SDArchiverParsed *parsed) {
|
|||
}
|
||||
if (parsed->working_files) {
|
||||
char **ptr = parsed->working_files;
|
||||
unsigned int idx = 0;
|
||||
uint32_t idx = 0;
|
||||
while (ptr[idx]) {
|
||||
free(ptr[idx]);
|
||||
++idx;
|
||||
|
@ -585,8 +585,8 @@ SDArchiverLinkedList *simple_archiver_parsed_to_filenames(
|
|||
}
|
||||
|
||||
// Remove "./" entries inside the file path.
|
||||
int slash_found = 0;
|
||||
int dot_found = 0;
|
||||
int_fast8_t slash_found = 0;
|
||||
int_fast8_t dot_found = 0;
|
||||
for (idx = strlen(file_info->filename); idx-- > 0;) {
|
||||
if (file_info->filename[idx] == '/') {
|
||||
if (dot_found) {
|
||||
|
|
|
@ -19,6 +19,10 @@
|
|||
#ifndef SEODISPARATE_COM_SIMPLE_ARCHIVER_PARSER_H_
|
||||
#define SEODISPARATE_COM_SIMPLE_ARCHIVER_PARSER_H_
|
||||
|
||||
// Standard library includes.
|
||||
#include <stdint.h>
|
||||
|
||||
// Local includes.
|
||||
#include "data_structures/linked_list.h"
|
||||
|
||||
typedef struct SDArchiverParsed {
|
||||
|
@ -31,7 +35,7 @@ typedef struct SDArchiverParsed {
|
|||
/// 0b xxxx 1xxx - Allow extract overwrite.
|
||||
/// 0b xxx1 xxxx - Create archive to stdout or read archive from stdin.
|
||||
/// 0b xx1x xxxx - Do not save absolute paths for symlinks.
|
||||
unsigned int flags;
|
||||
uint32_t flags;
|
||||
/// Null-terminated string.
|
||||
char *filename;
|
||||
/// Null-terminated string.
|
||||
|
|
95
src/test.c
95
src/test.c
|
@ -16,15 +16,18 @@
|
|||
//
|
||||
// `test.c` is the source for testing code.
|
||||
|
||||
// Standard library includes.
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Local includes.
|
||||
#include "helpers.h"
|
||||
#include "parser_internal.h"
|
||||
|
||||
static int checks_checked = 0;
|
||||
static int checks_passed = 0;
|
||||
static int32_t checks_checked = 0;
|
||||
static int32_t checks_passed = 0;
|
||||
|
||||
#define CHECK_TRUE(x) \
|
||||
do { \
|
||||
|
@ -57,7 +60,7 @@ static int checks_passed = 0;
|
|||
int main(void) {
|
||||
// Test parser.
|
||||
{
|
||||
unsigned int idx =
|
||||
size_t idx =
|
||||
simple_archiver_parser_internal_get_first_non_current_idx("test");
|
||||
CHECK_TRUE(idx == 0);
|
||||
|
||||
|
@ -132,58 +135,58 @@ int main(void) {
|
|||
// Only if system is little-endian.
|
||||
if (simple_archiver_helper_is_big_endian() == 0) {
|
||||
uint16_t u16 = 0x0102;
|
||||
CHECK_TRUE(((unsigned char *)&u16)[0] == 2);
|
||||
CHECK_TRUE(((unsigned char *)&u16)[1] == 1);
|
||||
CHECK_TRUE(((uint8_t *)&u16)[0] == 2);
|
||||
CHECK_TRUE(((uint8_t *)&u16)[1] == 1);
|
||||
simple_archiver_helper_16_bit_be(&u16);
|
||||
CHECK_TRUE(((unsigned char *)&u16)[0] == 1);
|
||||
CHECK_TRUE(((unsigned char *)&u16)[1] == 2);
|
||||
CHECK_TRUE(((uint8_t *)&u16)[0] == 1);
|
||||
CHECK_TRUE(((uint8_t *)&u16)[1] == 2);
|
||||
simple_archiver_helper_16_bit_be(&u16);
|
||||
CHECK_TRUE(((unsigned char *)&u16)[0] == 2);
|
||||
CHECK_TRUE(((unsigned char *)&u16)[1] == 1);
|
||||
CHECK_TRUE(((uint8_t *)&u16)[0] == 2);
|
||||
CHECK_TRUE(((uint8_t *)&u16)[1] == 1);
|
||||
|
||||
uint32_t u32 = 0x01020304;
|
||||
CHECK_TRUE(((unsigned char *)&u32)[0] == 4);
|
||||
CHECK_TRUE(((unsigned char *)&u32)[1] == 3);
|
||||
CHECK_TRUE(((unsigned char *)&u32)[2] == 2);
|
||||
CHECK_TRUE(((unsigned char *)&u32)[3] == 1);
|
||||
CHECK_TRUE(((uint8_t *)&u32)[0] == 4);
|
||||
CHECK_TRUE(((uint8_t *)&u32)[1] == 3);
|
||||
CHECK_TRUE(((uint8_t *)&u32)[2] == 2);
|
||||
CHECK_TRUE(((uint8_t *)&u32)[3] == 1);
|
||||
simple_archiver_helper_32_bit_be(&u32);
|
||||
CHECK_TRUE(((unsigned char *)&u32)[0] == 1);
|
||||
CHECK_TRUE(((unsigned char *)&u32)[1] == 2);
|
||||
CHECK_TRUE(((unsigned char *)&u32)[2] == 3);
|
||||
CHECK_TRUE(((unsigned char *)&u32)[3] == 4);
|
||||
CHECK_TRUE(((uint8_t *)&u32)[0] == 1);
|
||||
CHECK_TRUE(((uint8_t *)&u32)[1] == 2);
|
||||
CHECK_TRUE(((uint8_t *)&u32)[2] == 3);
|
||||
CHECK_TRUE(((uint8_t *)&u32)[3] == 4);
|
||||
simple_archiver_helper_32_bit_be(&u32);
|
||||
CHECK_TRUE(((unsigned char *)&u32)[0] == 4);
|
||||
CHECK_TRUE(((unsigned char *)&u32)[1] == 3);
|
||||
CHECK_TRUE(((unsigned char *)&u32)[2] == 2);
|
||||
CHECK_TRUE(((unsigned char *)&u32)[3] == 1);
|
||||
CHECK_TRUE(((uint8_t *)&u32)[0] == 4);
|
||||
CHECK_TRUE(((uint8_t *)&u32)[1] == 3);
|
||||
CHECK_TRUE(((uint8_t *)&u32)[2] == 2);
|
||||
CHECK_TRUE(((uint8_t *)&u32)[3] == 1);
|
||||
|
||||
uint64_t u64 = 0x010203040a0b0c0d;
|
||||
CHECK_TRUE(((unsigned char *)&u64)[0] == 0xd);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[1] == 0xc);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[2] == 0xb);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[3] == 0xa);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[4] == 0x4);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[5] == 0x3);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[6] == 0x2);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[7] == 0x1);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[0] == 0xd);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[1] == 0xc);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[2] == 0xb);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[3] == 0xa);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[4] == 0x4);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[5] == 0x3);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[6] == 0x2);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[7] == 0x1);
|
||||
simple_archiver_helper_64_bit_be(&u64);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[0] == 0x1);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[1] == 0x2);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[2] == 0x3);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[3] == 0x4);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[4] == 0xa);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[5] == 0xb);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[6] == 0xc);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[7] == 0xd);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[0] == 0x1);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[1] == 0x2);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[2] == 0x3);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[3] == 0x4);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[4] == 0xa);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[5] == 0xb);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[6] == 0xc);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[7] == 0xd);
|
||||
simple_archiver_helper_64_bit_be(&u64);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[0] == 0xd);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[1] == 0xc);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[2] == 0xb);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[3] == 0xa);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[4] == 0x4);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[5] == 0x3);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[6] == 0x2);
|
||||
CHECK_TRUE(((unsigned char *)&u64)[7] == 0x1);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[0] == 0xd);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[1] == 0xc);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[2] == 0xb);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[3] == 0xa);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[4] == 0x4);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[5] == 0x3);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[6] == 0x2);
|
||||
CHECK_TRUE(((uint8_t *)&u64)[7] == 0x1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +211,7 @@ int main(void) {
|
|||
// Test helpers cut substr.
|
||||
{
|
||||
const char *s = "one two three.";
|
||||
unsigned int s_len = strlen(s);
|
||||
uint32_t s_len = strlen(s);
|
||||
// Invalid range.
|
||||
char *out = simple_archiver_helper_cut_substr(s, 1, 0);
|
||||
CHECK_FALSE(out);
|
||||
|
|
Loading…
Reference in a new issue