From 28b00896a8cb94663a3d35313ee7d771d4af11e0 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Thu, 14 Mar 2024 17:16:22 +0100 Subject: use cstdint types --- src/array.cpp | 28 ++++++++++++++-------------- src/array.h | 21 +++++++++++---------- src/config.cpp | 14 +++++++------- src/config.h | 4 ++-- src/export.cpp | 12 ++++++------ src/image.cpp | 6 +++--- src/image.h | 9 +++++---- src/main.cpp | 16 ++++++++-------- src/memory_bucket.cpp | 10 +++++----- src/memory_bucket.h | 8 ++++---- src/mutex.h | 2 +- src/platform.h | 2 +- src/search.cpp | 10 +++++----- src/search.h | 26 +++++++++++++------------- 14 files changed, 85 insertions(+), 83 deletions(-) (limited to 'src') diff --git a/src/array.cpp b/src/array.cpp index 4eb50bd..6cdc8f4 100644 --- a/src/array.cpp +++ b/src/array.cpp @@ -3,7 +3,7 @@ #include #include -ts_array ts_array_create(int entry_size) +ts_array ts_array_create(uint32_t entry_size) { ts_array new_ts_array; new_ts_array.length = 0; @@ -16,7 +16,7 @@ ts_array ts_array_create(int entry_size) return new_ts_array; } -int ts_array_push(ts_array *ts_array, void *data) +uint32_t ts_array_push(ts_array *ts_array, void *data) { ASSERT(ts_array); ASSERT(data); @@ -40,12 +40,12 @@ int ts_array_push(ts_array *ts_array, void *data) memcpy((char*)ts_array->data + ((ts_array->length-1) * ts_array->entry_size), data, ts_array->entry_size); - int result = ts_array->length -1; + uint32_t result = ts_array->length -1; ts_mutex_unlock(&ts_array->mutex); return result; } -int ts_array_push_size(ts_array *ts_array, void *data, int data_size) +uint32_t ts_array_push_size(ts_array *ts_array, void *data, uint32_t data_size) { ASSERT(ts_array); ASSERT(data); @@ -72,22 +72,22 @@ int ts_array_push_size(ts_array *ts_array, void *data, int data_size) // fill remaining space with 0 if (ts_array->entry_size > data_size) { - int remaining = ts_array->entry_size - data_size; + uint32_t remaining = ts_array->entry_size - data_size; memset((char*)ts_array->data + ((ts_array->length-1) * ts_array->entry_size) + data_size, 0, remaining); } - int result = ts_array->length -1; + uint32_t result = ts_array->length -1; ts_mutex_unlock(&ts_array->mutex); return result; } -void ts_array_reserve(ts_array *ts_array, int reserve_count) +void ts_array_reserve(ts_array *ts_array, uint32_t reserve_count) { ASSERT(ts_array); ts_mutex_lock(&ts_array->mutex); - int reserved_count = ts_array->reserved_length - ts_array->length; + uint32_t reserved_count = ts_array->reserved_length - ts_array->length; reserve_count -= reserved_count; if (reserve_count > 0) @@ -106,7 +106,7 @@ void ts_array_reserve(ts_array *ts_array, int reserve_count) ts_mutex_unlock(&ts_array->mutex); } -void ts_array_remove_at(ts_array *ts_array, int at) +void ts_array_remove_at(ts_array *ts_array, uint32_t at) { ASSERT(ts_array); ASSERT(at >= 0); @@ -115,8 +115,8 @@ void ts_array_remove_at(ts_array *ts_array, int at) ts_mutex_lock(&ts_array->mutex); if (ts_array->length > 1) { - int offset = at * ts_array->entry_size; - int size = (ts_array->length - at - 1) * ts_array->entry_size; + uint32_t offset = at * ts_array->entry_size; + uint32_t size = (ts_array->length - at - 1) * ts_array->entry_size; memcpy((char*)ts_array->data + offset, (char*)ts_array->data + offset + ts_array->entry_size, size); @@ -133,7 +133,7 @@ void ts_array_remove(ts_array *ts_array, void *ptr) ts_mutex_lock(&ts_array->mutex); size_t offset = (char*)ptr - (char*)ts_array->data; size_t at = offset / ts_array->entry_size; - ts_array_remove_at(ts_array, (int)at); + ts_array_remove_at(ts_array, (uint32_t)at); ts_mutex_unlock(&ts_array->mutex); } @@ -142,7 +142,7 @@ void ts_array_remove_by(ts_array *ts_array, void *data) ASSERT(ts_array); ts_mutex_lock(&ts_array->mutex); - for (int i = 0; i < ts_array->length; i++) + for (uint32_t i = 0; i < ts_array->length; i++) { void *d = ts_array_at(ts_array, i); if (memcmp(d, data, ts_array->entry_size) == 0) @@ -154,7 +154,7 @@ void ts_array_remove_by(ts_array *ts_array, void *data) ts_mutex_unlock(&ts_array->mutex); } -void *ts_array_at(ts_array *ts_array, int at) +void *ts_array_at(ts_array *ts_array, uint32_t at) { ts_mutex_lock(&ts_array->mutex); ASSERT(ts_array); diff --git a/src/array.h b/src/array.h index f2707a8..6697548 100644 --- a/src/array.h +++ b/src/array.h @@ -4,26 +4,27 @@ #define ASSERT(e_) {if(!(e_)){*(int*)0=0;}} #include "mutex.h" +#include typedef struct t_ts_array { - int length; - int reserved_length; - int entry_size; - int reserve_jump; + uint32_t length; + uint32_t reserved_length; + uint32_t entry_size; + uint32_t reserve_jump; void *data; ts_mutex mutex; } ts_array; -ts_array ts_array_create(int entry_size); -int ts_array_push(ts_array *ts_array, void *data); -int ts_array_push_size(ts_array *ts_array, void *data, int data_size); -void ts_array_remove_at(ts_array *ts_array, int at); +ts_array ts_array_create(uint32_t entry_size); +uint32_t ts_array_push(ts_array *ts_array, void *data); +uint32_t ts_array_push_size(ts_array *ts_array, void *data, uint32_t data_size); +void ts_array_remove_at(ts_array *ts_array, uint32_t at); void ts_array_remove(ts_array *ts_array, void *ptr); void ts_array_remove_by(ts_array *ts_array, void *data); -void* ts_array_at(ts_array *ts_array, int at); +void* ts_array_at(ts_array *ts_array, uint32_t at); void ts_array_destroy(ts_array *ts_array); -void ts_array_reserve(ts_array *ts_array, int reserve_count); +void ts_array_reserve(ts_array *ts_array, uint32_t reserve_count); ts_array ts_array_copy(ts_array *ts_array); #endif \ No newline at end of file diff --git a/src/config.cpp b/src/config.cpp index 04ad58e..ef3f4be 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -10,17 +10,17 @@ utf8_int8_t save_path[MAX_INPUT_LENGTH]; utf8_int8_t path_buffer[MAX_INPUT_LENGTH]; utf8_int8_t filter_buffer[MAX_INPUT_LENGTH]; utf8_int8_t query_buffer[MAX_INPUT_LENGTH]; -int ts_thread_count = 4; -int max_file_size = 100; // in MBs +uint16_t ts_thread_count = 4; +uint32_t max_file_size = 100; // in MBs bool respect_capitalization = false; -static void _ts_config_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line) +static void _ts_config_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const utf8_int8_t* line) { - uint8_t path[MAX_INPUT_LENGTH]; - uint8_t filter[MAX_INPUT_LENGTH]; - uint8_t query[MAX_INPUT_LENGTH]; + utf8_int8_t path[MAX_INPUT_LENGTH]; + utf8_int8_t filter[MAX_INPUT_LENGTH]; + utf8_int8_t query[MAX_INPUT_LENGTH]; - int threads = 1, maxSize = 100, matchCase = 0; + uint32_t threads = 1, maxSize = 100, matchCase = 0; #if defined(_WIN32) if (sscanf_s(line, "Path=%s", (char*)&path, MAX_INPUT_LENGTH) == 1) { strncpy_s(path_buffer, MAX_INPUT_LENGTH, (char*)path, MAX_INPUT_LENGTH); } diff --git a/src/config.h b/src/config.h index 2e64755..408cf63 100644 --- a/src/config.h +++ b/src/config.h @@ -24,8 +24,8 @@ extern utf8_int8_t save_path[MAX_INPUT_LENGTH]; extern utf8_int8_t path_buffer[MAX_INPUT_LENGTH]; extern utf8_int8_t filter_buffer[MAX_INPUT_LENGTH]; extern utf8_int8_t query_buffer[MAX_INPUT_LENGTH]; -extern int ts_thread_count; -extern int max_file_size; // in MBs +extern uint16_t ts_thread_count; +extern uint32_t max_file_size; // in MBs extern bool respect_capitalization; void ts_load_config(); \ No newline at end of file diff --git a/src/export.cpp b/src/export.cpp index 6991c94..6adab07 100644 --- a/src/export.cpp +++ b/src/export.cpp @@ -72,7 +72,7 @@ static bool _ts_export_json(ts_search_result* result, const utf8_int8_t* path) { fprintf(write_file, "\"files\": [\n"); // Empty files. - for (int i = 0; i < result->files.length; i++) { + for (uint32_t i = 0; i < result->files.length; i++) { ts_found_file* file = *(ts_found_file **)ts_array_at(&result->files, i); if (file->match_count != 0) continue; fprintf(write_file, "{\n"); @@ -86,7 +86,7 @@ static bool _ts_export_json(ts_search_result* result, const utf8_int8_t* path) { // Files with matches. bool first_match_of_file = true; ts_found_file* prev_file = NULL; - for (int i = 0; i < result->matches.length; i++) { + for (uint32_t i = 0; i < result->matches.length; i++) { ts_file_match* match = (ts_file_match*)ts_array_at(&result->matches, i); if (match->file != prev_file) { @@ -136,7 +136,7 @@ static bool _ts_export_csv(ts_search_result* result, const utf8_int8_t* path) { fprintf(write_file, "TIMESTAMP,%llu\n", result->timestamp); // Empty files. - for (int i = 0; i < result->files.length; i++) { + for (uint32_t i = 0; i < result->files.length; i++) { ts_found_file* file = *(ts_found_file **)ts_array_at(&result->files, i); if (file->match_count != 0) continue; fprintf(write_file, "FILE,%s\n", file->path); @@ -144,7 +144,7 @@ static bool _ts_export_csv(ts_search_result* result, const utf8_int8_t* path) { // Files with matches. ts_found_file* prev_file = NULL; - for (int i = 0; i < result->matches.length; i++) { + for (uint32_t i = 0; i < result->matches.length; i++) { ts_file_match* match = (ts_file_match*)ts_array_at(&result->matches, i); if (match->file != prev_file) { @@ -199,7 +199,7 @@ static bool _ts_export_xml(ts_search_result* result, const utf8_int8_t* path) { fprintf(write_file, "%llu\n", result->timestamp); // Empty files. - for (int i = 0; i < result->files.length; i++) { + for (uint32_t i = 0; i < result->files.length; i++) { ts_found_file* file = *(ts_found_file **)ts_array_at(&result->files, i); if (file->match_count != 0) continue; fprintf(write_file, "\n"); @@ -209,7 +209,7 @@ static bool _ts_export_xml(ts_search_result* result, const utf8_int8_t* path) { // Files with matches. ts_found_file* prev_file = NULL; - for (int i = 0; i < result->matches.length; i++) { + for (uint32_t i = 0; i < result->matches.length; i++) { ts_file_match* match = (ts_file_match*)ts_array_at(&result->matches, i); if (match->file != prev_file) { diff --git a/src/image.cpp b/src/image.cpp index 238232e..6f87eda 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -11,7 +11,7 @@ ts_image img_folder; ts_image img_drop; // Simple helper function to load an image into a OpenGL texture with common settings -static bool _ts_load_texture(unsigned char* data, size_t size, GLuint* out_texture, int* out_width, int* out_height) +static bool _ts_load_texture(unsigned char* data, size_t size, GLuint* out_texture, uint32_t* out_width, uint32_t* out_height) { // Load from file int image_width = 0; @@ -46,8 +46,8 @@ static bool _ts_load_texture(unsigned char* data, size_t size, GLuint* out_textu } static ts_image _ts_load_image(unsigned char* data, size_t size) { - int w = 0; - int h = 0; + uint32_t w = 0; + uint32_t h = 0; GLuint id = 0; _ts_load_texture(data, size, &id, &w, &h); diff --git a/src/image.h b/src/image.h index a3831cb..7bfe5ec 100644 --- a/src/image.h +++ b/src/image.h @@ -8,11 +8,12 @@ #include #include #include +#include typedef struct t_ts_image { GLuint id; - int width; - int height; + uint32_t width; + uint32_t height; } ts_image; #elif defined(__linux__) || defined(__APPLE__) @@ -23,8 +24,8 @@ typedef struct t_ts_image { typedef struct t_ts_image { GLuint id; - int width; - int height; + uint32_t width; + uint32_t height; } ts_image; #endif diff --git a/src/main.cpp b/src/main.cpp index 914bcc4..585d7ff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,10 +39,10 @@ static void _ts_create_popups() { // Settings window if (ImGui::BeginPopupModal("Text-Search settings", &open_settings_window, ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove)) { ImGui::SetWindowSize({300, 0}); - ImGui::DragInt("Threads", &ts_thread_count, 0.1f, 1, 8); + ImGui::DragInt("Threads", (int*)&ts_thread_count, 0.1f, 1, 8); ImGui::SetItemTooltip("Number of threads used to search for text matches"); - ImGui::DragInt("File Size", &max_file_size, 50.0f, 1, 10000, "%dMB"); + ImGui::DragInt("File Size", (int*)&max_file_size, 50.0f, 1, 10000, "%dMB"); ImGui::SetItemTooltip("Files larger than this will not be searched"); ImGui::Dummy({0, 70}); @@ -262,8 +262,8 @@ int _tb_query_input_cb(ImGuiInputTextCallbackData* data) { } void _ts_create_file_match_rows() { - int itemcount = current_search_result == 0 ? 0 : current_search_result->files.length; - for (int item = 0; item < itemcount; item++) + uint32_t itemcount = current_search_result == 0 ? 0 : current_search_result->files.length; + for (uint32_t item = 0; item < itemcount; item++) { ts_found_file *file = *(ts_found_file **)ts_array_at(¤t_search_result->files, item); @@ -303,8 +303,8 @@ const utf8_int8_t* _ts_file_error_to_message(ts_file_open_error err) { } void _ts_create_file_error_rows() { - int itemcount = current_search_result == 0 ? 0 : current_search_result->files.length; - for (int item = 0; item < itemcount; item++) + uint32_t itemcount = current_search_result == 0 ? 0 : current_search_result->files.length; + for (uint32_t item = 0; item < itemcount; item++) { ts_found_file *file = *(ts_found_file **)ts_array_at(¤t_search_result->files, item); if (file->error == FILE_ERROR_NONE) continue; @@ -327,9 +327,9 @@ void _ts_create_file_error_rows() { } void _ts_create_text_match_rows() { - int itemcount = current_search_result == 0 ? 0 : current_search_result->matches.length; + uint32_t itemcount = current_search_result == 0 ? 0 : current_search_result->matches.length; ts_found_file* prev_file = nullptr; - for (int item = 0; item < itemcount; item++) + for (uint32_t item = 0; item < itemcount; item++) { ts_file_match *file = (ts_file_match *)ts_array_at(¤t_search_result->matches, item); diff --git a/src/memory_bucket.cpp b/src/memory_bucket.cpp index 1a1e54d..b199e59 100644 --- a/src/memory_bucket.cpp +++ b/src/memory_bucket.cpp @@ -1,7 +1,7 @@ #include "memory_bucket.h" #include -ts_memory_bucket ts_memory_bucket_init(int bucket_size) +ts_memory_bucket ts_memory_bucket_init(uint32_t bucket_size) { ts_memory_bucket collection; collection.bucket_mutex = ts_mutex_create(); @@ -15,11 +15,11 @@ ts_memory_bucket ts_memory_bucket_init(int bucket_size) return collection; } -void* ts_memory_bucket_reserve(ts_memory_bucket *bucket, int reserve_length) +void* ts_memory_bucket_reserve(ts_memory_bucket *bucket, uint32_t reserve_length) { ts_mutex_lock(&bucket->bucket_mutex); ts_memory_bucket_entry *bucket_entry = 0; - for (int i = 0; i < bucket->buckets.length; i++) + for (uint32_t i = 0; i < bucket->buckets.length; i++) { bucket_entry = (ts_memory_bucket_entry *)ts_array_at(&bucket->buckets, i); @@ -46,7 +46,7 @@ void* ts_memory_bucket_reserve(ts_memory_bucket *bucket, int reserve_length) void ts_memory_bucket_reset(ts_memory_bucket *bucket) { ts_mutex_lock(&bucket->bucket_mutex); - for (int i = 0; i < bucket->buckets.length; i++) + for (uint32_t i = 0; i < bucket->buckets.length; i++) { ts_memory_bucket_entry *bucket_entry = (ts_memory_bucket_entry *)ts_array_at(&bucket->buckets, i); bucket_entry->cursor = 0; @@ -57,7 +57,7 @@ void ts_memory_bucket_reset(ts_memory_bucket *bucket) void ts_memory_bucket_destroy(ts_memory_bucket *bucket) { ts_mutex_lock(&bucket->bucket_mutex); - for (int i = 0; i < bucket->buckets.length; i++) + for (uint32_t i = 0; i < bucket->buckets.length; i++) { ts_memory_bucket_entry *bucket_entry = (ts_memory_bucket_entry *)ts_array_at(&bucket->buckets, i); free(bucket_entry->data); diff --git a/src/memory_bucket.h b/src/memory_bucket.h index 206dff2..e5f18e5 100644 --- a/src/memory_bucket.h +++ b/src/memory_bucket.h @@ -10,8 +10,8 @@ typedef struct t_ts_memory_bucket_entry { char *data; - int length; - int cursor; + uint32_t length; + uint32_t cursor; } ts_memory_bucket_entry; typedef struct t_ts_memory_bucket @@ -20,8 +20,8 @@ typedef struct t_ts_memory_bucket ts_array buckets; } ts_memory_bucket; -ts_memory_bucket ts_memory_bucket_init(int bucket_size); -void* ts_memory_bucket_reserve(ts_memory_bucket *bucket, int reserve_length); +ts_memory_bucket ts_memory_bucket_init(uint32_t bucket_size); +void* ts_memory_bucket_reserve(ts_memory_bucket *bucket, uint32_t reserve_length); void ts_memory_bucket_reset(ts_memory_bucket *bucket); void ts_memory_bucket_destroy(ts_memory_bucket *bucket); diff --git a/src/mutex.h b/src/mutex.h index 323b277..31a0026 100644 --- a/src/mutex.h +++ b/src/mutex.h @@ -18,7 +18,7 @@ typedef struct t_ts_mutex typedef struct t_ts_thread { HANDLE thread; - int valid; + bool valid; } ts_thread; #elif defined(__linux__) || defined(__APPLE__) diff --git a/src/platform.h b/src/platform.h index 5926aeb..5d2711e 100644 --- a/src/platform.h +++ b/src/platform.h @@ -11,7 +11,7 @@ typedef struct t_ts_file_content { size_t content_length; void *content; - int file_error; + uint16_t file_error; } ts_file_content; typedef enum t_ts_file_open_error diff --git a/src/search.cpp b/src/search.cpp index de5b608..d3e34ac 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -33,7 +33,7 @@ ts_array ts_get_filters(utf8_int8_t *pattern) return result; } -int ts_string_match(utf8_int8_t *first, utf8_int8_t *second) +uint32_t ts_string_match(utf8_int8_t *first, utf8_int8_t *second) { // If we reach at the end of both strings, we are done if (*first == '\0' && *second == '\0') @@ -60,7 +60,7 @@ int ts_string_match(utf8_int8_t *first, utf8_int8_t *second) size_t ts_filter_matches(ts_array *filters, char *string, char **matched_filter) { - for (int i = 0; i < filters->length; i++) + for (uint32_t i = 0; i < filters->length; i++) { char *filter = (char *)ts_array_at(filters, i); @@ -256,7 +256,7 @@ static void _ts_search_file(ts_found_file *ref, ts_file_content content, ts_sear if (ts_string_contains((char *)content.content, result->search_text, &text_matches, result->respect_capitalization)) { ts_mutex_lock(&result->matches.mutex); - for (int i = 0; i < text_matches.length; i++) + for (uint32_t i = 0; i < text_matches.length; i++) { ts_text_match *m = (ts_text_match *)ts_array_at(&text_matches, i); @@ -323,7 +323,7 @@ static void *_ts_search_thread(void *args) goto finish_early; ts_mutex_lock(&new_result->files.mutex); - int read_cursor = new_result->file_list_read_cursor; + uint32_t read_cursor = new_result->file_list_read_cursor; if (read_cursor >= new_result->files.length) { ts_mutex_unlock(&new_result->files.mutex); @@ -398,7 +398,7 @@ static void _ts_list_files(ts_search_result* result) ts_thread_detach(&thr); } -void ts_start_search(utf8_int8_t *path, utf8_int8_t *filter, utf8_int8_t *query, int thread_count, int max_file_size, bool respect_capitalization) +void ts_start_search(utf8_int8_t *path, utf8_int8_t *filter, utf8_int8_t *query, uint16_t thread_count, uint32_t max_file_size, bool respect_capitalization) { if (utf8len(query) > 0 && utf8len(query) <= 2) { // need a string of atleast 3 characters return; diff --git a/src/search.h b/src/search.h index bdfdafa..ac4a3eb 100644 --- a/src/search.h +++ b/src/search.h @@ -8,8 +8,8 @@ typedef struct t_ts_found_file { utf8_int8_t *path; - int match_count; - int error; + uint32_t match_count; + uint16_t error; bool collapsed; } ts_found_file; @@ -19,17 +19,17 @@ typedef struct t_ts_search_result ts_array files; ts_array matches; ts_array filters; - int match_count; - int file_count; + uint32_t match_count; + uint32_t file_count; ts_memory_bucket memory; struct t_ts_search_result* prev_result; uint64_t timestamp; // thread syncing ts_mutex mutex; - int completed_match_threads; - int done_finding_files; - int file_list_read_cursor; + uint16_t completed_match_threads; + bool done_finding_files; + uint32_t file_list_read_cursor; bool cancel_search; bool search_completed; bool is_saving; @@ -38,15 +38,15 @@ typedef struct t_ts_search_result utf8_int8_t *directory_to_search; utf8_int8_t *file_filter; utf8_int8_t *search_text; - int max_ts_thread_count; - uint64_t max_file_size; + uint16_t max_ts_thread_count; + uint32_t max_file_size; bool respect_capitalization; } ts_search_result; typedef struct t_ts_file_match { ts_found_file* file; - int line_nr; + uint32_t line_nr; size_t word_match_offset; // nr of bytes, not codepoints. size_t word_match_length; // nr of bytes, not codepoints. utf8_int8_t *line_info; @@ -54,7 +54,7 @@ typedef struct t_ts_file_match typedef struct t_ts_text_match { - int line_nr; + uint32_t line_nr; size_t word_offset; size_t word_match_len; utf8_int8_t *line_start; @@ -65,10 +65,10 @@ extern ts_search_result* current_search_result; ts_array ts_get_filters(utf8_int8_t *pattern); size_t ts_filter_matches(ts_array *filters, utf8_int8_t *string, utf8_int8_t **matched_filter); -int ts_string_match(utf8_int8_t *first, utf8_int8_t *second); +uint32_t ts_string_match(utf8_int8_t *first, utf8_int8_t *second); ts_search_result* ts_create_empty_search_result(); bool ts_string_contains(utf8_int8_t *text_to_search, utf8_int8_t *text_to_find, ts_array *text_matches, bool respect_capitalization); -void ts_start_search(utf8_int8_t *path, utf8_int8_t *filter, utf8_int8_t *query, int thread_count, int max_file_size, bool respect_capitalization); +void ts_start_search(utf8_int8_t *path, utf8_int8_t *filter, utf8_int8_t *query, uint16_t thread_count, uint32_t max_file_size, bool respect_capitalization); void ts_destroy_result(ts_search_result* result); #endif \ No newline at end of file -- cgit v1.2.3-70-g09d2