Improve file-count logs
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 51s

This commit is contained in:
Stephen Seo 2024-07-26 16:18:07 +09:00
parent 4479fdce62
commit 49bd4b5c76
4 changed files with 32 additions and 4 deletions

View file

@ -40,6 +40,8 @@
#include "helpers.h" #include "helpers.h"
#define TEMP_FILENAME_CMP "%s%ssimple_archiver_compressed_%u.tmp" #define TEMP_FILENAME_CMP "%s%ssimple_archiver_compressed_%u.tmp"
#define FILE_COUNTS_OUTPUT_FORMAT_STR_0 "\nFile %%%uu of %%%uu.\n"
#define FILE_COUNTS_OUTPUT_FORMAT_STR_1 "[%%%uu/%%%uu]\n"
#if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \ #if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \ SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC || \
@ -871,7 +873,10 @@ int write_files_fn(void *data, void *ud) {
simple_archiver_list_free(&to_write); simple_archiver_list_free(&to_write);
} }
fprintf(stderr, "[%10u/%10u]\n", ++(state->count), state->max); char format_str[64];
snprintf(format_str, 64, FILE_COUNTS_OUTPUT_FORMAT_STR_1, state->digits,
state->digits);
fprintf(stderr, format_str, ++(state->count), state->max);
return 0; return 0;
} }
@ -996,6 +1001,9 @@ SDArchiverState *simple_archiver_init_state(const SDArchiverParsed *parsed) {
state->parsed = parsed; state->parsed = parsed;
state->out_f = NULL; state->out_f = NULL;
state->map = NULL; state->map = NULL;
state->count = 0;
state->max = 0;
state->digits = 10;
return state; return state;
} }
@ -1105,6 +1113,7 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
state->max = filenames->count; state->max = filenames->count;
state->out_f = out_f; state->out_f = out_f;
state->map = abs_filenames; state->map = abs_filenames;
state->digits = simple_archiver_helper_num_digits(state->max);
fprintf(stderr, "Begin archiving...\n"); fprintf(stderr, "Begin archiving...\n");
__attribute__(( __attribute__((
cleanup(simple_archiver_internal_chdir_back2))) char *original_cwd = NULL; cleanup(simple_archiver_internal_chdir_back2))) char *original_cwd = NULL;
@ -1118,7 +1127,10 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
} }
#endif #endif
} }
fprintf(stderr, "[%10u/%10u]\n", state->count, state->max); char format_str[64];
snprintf(format_str, 64, FILE_COUNTS_OUTPUT_FORMAT_STR_1, state->digits,
state->digits);
fprintf(stderr, format_str, state->count, state->max);
if (simple_archiver_list_get(filenames, write_files_fn, state)) { if (simple_archiver_list_get(filenames, write_files_fn, state)) {
// Error occurred. // Error occurred.
fprintf(stderr, "Error ocurred writing file(s) to archive.\n"); fprintf(stderr, "Error ocurred writing file(s) to archive.\n");
@ -1226,7 +1238,10 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
simple_archiver_helper_32_bit_be(&u32); simple_archiver_helper_32_bit_be(&u32);
fprintf(stderr, "File count is %u\n", u32); fprintf(stderr, "File count is %u\n", u32);
uint32_t size = u32; const uint32_t size = u32;
const unsigned int 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 skip = 0;
__attribute__((cleanup(simple_archiver_hash_map_free))) __attribute__((cleanup(simple_archiver_hash_map_free)))
SDArchiverHashMap *hash_map = NULL; SDArchiverHashMap *hash_map = NULL;
@ -1244,7 +1259,7 @@ int simple_archiver_parse_archive_info(FILE *in_f, int do_extract,
} }
} }
for (uint32_t idx = 0; idx < size; ++idx) { for (uint32_t idx = 0; idx < size; ++idx) {
fprintf(stderr, "\nFile %10u of %10u.\n", idx + 1, size); fprintf(stderr, format_str, idx + 1, size);
if (feof(in_f) || ferror(in_f)) { if (feof(in_f) || ferror(in_f)) {
return SDAS_INVALID_FILE; return SDAS_INVALID_FILE;
} else if (fread(&u16, 2, 1, in_f) != 1) { } else if (fread(&u16, 2, 1, in_f) != 1) {

View file

@ -34,6 +34,7 @@ typedef struct SDArchiverState {
SDArchiverHashMap *map; SDArchiverHashMap *map;
unsigned int count; unsigned int count;
unsigned int max; unsigned int max;
unsigned int digits;
} SDArchiverState; } SDArchiverState;
enum SDArchiverStateReturns { enum SDArchiverStateReturns {

View file

@ -216,3 +216,13 @@ char *simple_archiver_helper_cut_substr(const char *s, unsigned int start_idx,
return new_s; return new_s;
} }
} }
unsigned int simple_archiver_helper_num_digits(unsigned int value) {
unsigned int digits = 0;
do {
++digits;
value /= 10;
} while (value != 0);
return digits;
}

View file

@ -52,4 +52,6 @@ int simple_archiver_helper_make_dirs(const char *file_path);
char *simple_archiver_helper_cut_substr(const char *s, unsigned int start_idx, char *simple_archiver_helper_cut_substr(const char *s, unsigned int start_idx,
unsigned int end_idx); unsigned int end_idx);
unsigned int simple_archiver_helper_num_digits(unsigned int value);
#endif #endif