diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2024-03-03 16:13:02 +0100 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2024-03-03 16:13:02 +0100 |
| commit | b8d8eaa8b2c46f79e8a580a402ec7c472075d073 (patch) | |
| tree | 470b46ba7693c50bd0368c2a69fbe057bb8dac66 /src | |
| parent | ea14b4942c5f260b8a647bec52b85d376f12066a (diff) | |
enforce naming convention
Diffstat (limited to 'src')
| -rw-r--r-- | src/array.cpp | 198 | ||||
| -rw-r--r-- | src/array.h | 30 | ||||
| -rw-r--r-- | src/main.cpp | 8 | ||||
| -rw-r--r-- | src/main_windows.cpp | 26 | ||||
| -rw-r--r-- | src/memory_bucket.cpp | 48 | ||||
| -rw-r--r-- | src/memory_bucket.h | 20 | ||||
| -rw-r--r-- | src/mutex.cpp | 62 | ||||
| -rw-r--r-- | src/mutex.h | 40 | ||||
| -rw-r--r-- | src/platform.h | 14 | ||||
| -rw-r--r-- | src/search.cpp | 269 | ||||
| -rw-r--r-- | src/search.h | 43 |
11 files changed, 384 insertions, 374 deletions
diff --git a/src/array.cpp b/src/array.cpp index b6e0a9b..38b7095 100644 --- a/src/array.cpp +++ b/src/array.cpp @@ -1,186 +1,186 @@ #include "array.h" -array array_create(int entry_size) +ts_array ts_array_create(int entry_size) { - array new_array; - new_array.length = 0; - new_array.reserved_length = 0; - new_array.entry_size = entry_size; - new_array.data = 0; - new_array.reserve_jump = 1; - new_array.mutex = mutex_create_recursive(); - - return new_array; + ts_array new_ts_array; + new_ts_array.length = 0; + new_ts_array.reserved_length = 0; + new_ts_array.entry_size = entry_size; + new_ts_array.data = 0; + new_ts_array.reserve_jump = 1; + new_ts_array.mutex = ts_mutex_create_recursive(); + + return new_ts_array; } -int array_push(array *array, void *data) +int ts_array_push(ts_array *ts_array, void *data) { - ASSERT(array); + ASSERT(ts_array); ASSERT(data); - ASSERT(array->reserve_jump >= 1); + ASSERT(ts_array->reserve_jump >= 1); - mutex_lock(&array->mutex); - array->length++; + ts_mutex_lock(&ts_array->mutex); + ts_array->length++; - if (!array->data) + if (!ts_array->data) { - array->data = malloc(array->entry_size * array->reserve_jump); - array->reserved_length = array->reserve_jump; + ts_array->data = malloc(ts_array->entry_size * ts_array->reserve_jump); + ts_array->reserved_length = ts_array->reserve_jump; } - if (array->reserved_length < array->length) + if (ts_array->reserved_length < ts_array->length) { - array->reserved_length += array->reserve_jump; - array->data = realloc(array->data, (array->reserved_length*array->entry_size)); + ts_array->reserved_length += ts_array->reserve_jump; + ts_array->data = realloc(ts_array->data, (ts_array->reserved_length*ts_array->entry_size)); } - memcpy((char*)array->data + ((array->length-1) * array->entry_size), - data, array->entry_size); + memcpy((char*)ts_array->data + ((ts_array->length-1) * ts_array->entry_size), + data, ts_array->entry_size); - int result = array->length -1; - mutex_unlock(&array->mutex); + int result = ts_array->length -1; + ts_mutex_unlock(&ts_array->mutex); return result; } -int array_push_size(array *array, void *data, int data_size) +int ts_array_push_size(ts_array *ts_array, void *data, int data_size) { - ASSERT(array); + ASSERT(ts_array); ASSERT(data); - ASSERT(array->reserve_jump >= 1); + ASSERT(ts_array->reserve_jump >= 1); - mutex_lock(&array->mutex); - array->length++; + ts_mutex_lock(&ts_array->mutex); + ts_array->length++; - if (!array->data) + if (!ts_array->data) { - array->data = malloc(array->entry_size * array->reserve_jump); - array->reserved_length = array->reserve_jump; + ts_array->data = malloc(ts_array->entry_size * ts_array->reserve_jump); + ts_array->reserved_length = ts_array->reserve_jump; } - if (array->reserved_length < array->length) + if (ts_array->reserved_length < ts_array->length) { - array->reserved_length += array->reserve_jump; - array->data = realloc(array->data, (array->reserved_length*array->entry_size)); + ts_array->reserved_length += ts_array->reserve_jump; + ts_array->data = realloc(ts_array->data, (ts_array->reserved_length*ts_array->entry_size)); } - memcpy((char*)array->data + ((array->length-1) * array->entry_size), + memcpy((char*)ts_array->data + ((ts_array->length-1) * ts_array->entry_size), data, data_size); // fill remaining space with 0 - if (array->entry_size > data_size) + if (ts_array->entry_size > data_size) { - int remaining = array->entry_size - data_size; - memset((char*)array->data + ((array->length-1) * array->entry_size) + data_size, + int 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 = array->length -1; - mutex_unlock(&array->mutex); + int result = ts_array->length -1; + ts_mutex_unlock(&ts_array->mutex); return result; } -void array_reserve(array *array, int reserve_count) +void ts_array_reserve(ts_array *ts_array, int reserve_count) { - ASSERT(array); + ASSERT(ts_array); - mutex_lock(&array->mutex); - int reserved_count = array->reserved_length - array->length; + ts_mutex_lock(&ts_array->mutex); + int reserved_count = ts_array->reserved_length - ts_array->length; reserve_count -= reserved_count; if (reserve_count > 0) { - array->reserved_length += reserve_count; + ts_array->reserved_length += reserve_count; - if (array->data) + if (ts_array->data) { - array->data = realloc(array->data, (array->reserved_length*array->entry_size)); + ts_array->data = realloc(ts_array->data, (ts_array->reserved_length*ts_array->entry_size)); } else { - array->data = malloc(array->reserved_length*array->entry_size); + ts_array->data = malloc(ts_array->reserved_length*ts_array->entry_size); } } - mutex_unlock(&array->mutex); + ts_mutex_unlock(&ts_array->mutex); } -void array_remove_at(array *array, int at) +void ts_array_remove_at(ts_array *ts_array, int at) { - ASSERT(array); + ASSERT(ts_array); ASSERT(at >= 0); - ASSERT(at < array->length); + ASSERT(at < ts_array->length); - mutex_lock(&array->mutex); - if (array->length > 1) + ts_mutex_lock(&ts_array->mutex); + if (ts_array->length > 1) { - int offset = at * array->entry_size; - int size = (array->length - at - 1) * array->entry_size; - memcpy((char*)array->data + offset, - (char*)array->data + offset + array->entry_size, + int offset = at * ts_array->entry_size; + int 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); - //array->data = realloc(array->data, array->length * array->entry_size); + //ts_array->data = realloc(ts_array->data, ts_array->length * ts_array->entry_size); } - array->length--; - mutex_unlock(&array->mutex); + ts_array->length--; + ts_mutex_unlock(&ts_array->mutex); } -void array_remove(array *array, void *ptr) +void ts_array_remove(ts_array *ts_array, void *ptr) { - mutex_lock(&array->mutex); - int offset = (char*)ptr - (char*)array->data; - int at = offset / array->entry_size; - array_remove_at(array, at); - mutex_unlock(&array->mutex); + ts_mutex_lock(&ts_array->mutex); + int offset = (char*)ptr - (char*)ts_array->data; + int at = offset / ts_array->entry_size; + ts_array_remove_at(ts_array, at); + ts_mutex_unlock(&ts_array->mutex); } -void array_remove_by(array *array, void *data) +void ts_array_remove_by(ts_array *ts_array, void *data) { - ASSERT(array); + ASSERT(ts_array); - mutex_lock(&array->mutex); - for (int i = 0; i < array->length; i++) + ts_mutex_lock(&ts_array->mutex); + for (int i = 0; i < ts_array->length; i++) { - void *d = array_at(array, i); - if (memcmp(d, data, array->entry_size) == 0) + void *d = ts_array_at(ts_array, i); + if (memcmp(d, data, ts_array->entry_size) == 0) { - array_remove_at(array, i); + ts_array_remove_at(ts_array, i); return; } } - mutex_unlock(&array->mutex); + ts_mutex_unlock(&ts_array->mutex); } -void *array_at(array *array, int at) +void *ts_array_at(ts_array *ts_array, int at) { - mutex_lock(&array->mutex); - ASSERT(array); + ts_mutex_lock(&ts_array->mutex); + ASSERT(ts_array); ASSERT(at >= 0); - ASSERT(at < array->length); + ASSERT(at < ts_array->length); - void *result = (char*)array->data + (at * array->entry_size); - mutex_unlock(&array->mutex); + void *result = (char*)ts_array->data + (at * ts_array->entry_size); + ts_mutex_unlock(&ts_array->mutex); return result; } -void array_destroy(array *array) +void ts_array_destroy(ts_array *ts_array) { - ASSERT(array); - free(array->data); - mutex_destroy(&array->mutex); + ASSERT(ts_array); + free(ts_array->data); + ts_mutex_destroy(&ts_array->mutex); } -array array_copy(array *arr) +ts_array ts_array_copy(ts_array *arr) { - array new_array; - new_array.length = arr->length; - new_array.reserved_length = arr->reserved_length; - new_array.entry_size = arr->entry_size; - new_array.data = malloc(new_array.entry_size*new_array.reserved_length); - new_array.mutex = mutex_create(); - - mutex_lock(&arr->mutex); - memcpy(new_array.data, arr->data, new_array.entry_size*new_array.reserved_length); - mutex_unlock(&arr->mutex); - return new_array; + ts_array new_ts_array; + new_ts_array.length = arr->length; + new_ts_array.reserved_length = arr->reserved_length; + new_ts_array.entry_size = arr->entry_size; + new_ts_array.data = malloc(new_ts_array.entry_size*new_ts_array.reserved_length); + new_ts_array.mutex = ts_mutex_create(); + + ts_mutex_lock(&arr->mutex); + memcpy(new_ts_array.data, arr->data, new_ts_array.entry_size*new_ts_array.reserved_length); + ts_mutex_unlock(&arr->mutex); + return new_ts_array; }
\ No newline at end of file diff --git a/src/array.h b/src/array.h index f00c220..f2707a8 100644 --- a/src/array.h +++ b/src/array.h @@ -1,29 +1,29 @@ -#ifndef INCLUDE_ARRAY -#define INCLUDE_ARRAY +#ifndef INCLUDE_ts_array +#define INCLUDE_ts_array #define ASSERT(e_) {if(!(e_)){*(int*)0=0;}} #include "mutex.h" -typedef struct t_array +typedef struct t_ts_array { int length; int reserved_length; int entry_size; int reserve_jump; void *data; - mutex mutex; -} array; + ts_mutex mutex; +} ts_array; -array array_create(int entry_size); -int array_push(array *array, void *data); -int array_push_size(array *array, void *data, int data_size); -void array_remove_at(array *array, int at); -void array_remove(array *array, void *ptr); -void array_remove_by(array *array, void *data); -void *array_at(array *array, int at); -void array_destroy(array *array); -void array_reserve(array *array, int reserve_count); -array array_copy(array *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); +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_destroy(ts_array *ts_array); +void ts_array_reserve(ts_array *ts_array, int reserve_count); +ts_array ts_array_copy(ts_array *ts_array); #endif
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 883b8ee..4046004 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,7 @@ utf8_int8_t query_buffer[SEARCH_BUFFER_SIZE]; bool open_settings_window = false; bool open_about_window = false; -int thread_count = 4; +int ts_thread_count = 4; int current_locale_index = 0; int locales_count = 2; char* locales[] = { @@ -36,7 +36,7 @@ static void _ts_create_popups() { // Settings window if (ImGui::BeginPopupModal("Text-Search settings", NULL, ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove)) { ImGui::SetWindowSize({300, 0}); - ImGui::DragInt("Threads", &thread_count, 1.0f, 1, 64); + ImGui::DragInt("Threads", &ts_thread_count, 1.0f, 1, 64); ImGui::Combo("Language", ¤t_locale_index, locales, locales_count); ImGui::Dummy({0, 70}); @@ -203,10 +203,10 @@ void ts_create_gui(int window_w, int window_h) { ImGui::TableHeadersRow(); int itemcount = current_search_result == 0 ? 0 : current_search_result->matches.length; - found_file* prev_file = nullptr; + ts_found_file* prev_file = nullptr; for (int item = 0; item < itemcount; item++) { - file_match *file = (file_match *)array_at(¤t_search_result->matches, item); + ts_file_match *file = (ts_file_match *)ts_array_at(¤t_search_result->matches, item); if (prev_file != file->file) { prev_file = file->file; diff --git a/src/main_windows.cpp b/src/main_windows.cpp index e2fc0eb..5b98eed 100644 --- a/src/main_windows.cpp +++ b/src/main_windows.cpp @@ -224,9 +224,9 @@ LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) return ::DefWindowProcW(hWnd, msg, wParam, lParam); } -file_content platform_read_file(char *path, const char *mode) +ts_file_content ts_platform_read_file(char *path, const char *mode) { - file_content result; + ts_file_content result; result.content = 0; result.content_length = 0; result.file_error = 0; @@ -297,19 +297,19 @@ file_content platform_read_file(char *path, const char *mode) static void *_list_files_thread(void *args) { - search_result *info = (search_result *)args; - platform_list_files_block(info, nullptr); + ts_search_result *info = (ts_search_result *)args; + ts_platform_list_files_block(info, nullptr); info->done_finding_files = true; return 0; } -void platform_list_files(search_result* result) +void ts_platform_list_files(ts_search_result* result) { - thread thr = thread_start(_list_files_thread, (void*)result); - thread_detach(&thr); + ts_thread thr = ts_thread_start(_list_files_thread, (void*)result); + ts_thread_detach(&thr); } -void platform_list_files_block(search_result* result, wchar_t* start_dir) +void ts_platform_list_files_block(ts_search_result* result, wchar_t* start_dir) { // Utf8 to wchar str wchar_t* search_dir = (wchar_t*)malloc(MAX_INPUT_LENGTH); @@ -354,7 +354,7 @@ void platform_list_files_block(search_result* result, wchar_t* start_dir) wcscpy(subdir_buffer_path, search_dir); wcscat(subdir_buffer_path, L"\\"); wcscat(subdir_buffer_path, name); - platform_list_files_block(result, subdir_buffer_path); + ts_platform_list_files_block(result, subdir_buffer_path); } else if ((file_info.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) || (file_info.dwFileAttributes & FILE_ATTRIBUTE_ENCRYPTED) || @@ -368,14 +368,14 @@ void platform_list_files_block(search_result* result, wchar_t* start_dir) wcscat(complete_file_path, L"\\"); wcscat(complete_file_path, name); - found_file f; + ts_found_file f; f.path = (utf8_int8_t*)malloc(MAX_INPUT_LENGTH); f.match_count = 0; WideCharToMultiByte(CP_UTF8,0,complete_file_path,-1,(LPSTR)f.path,MAX_INPUT_LENGTH, NULL, NULL); - mutex_lock(&result->files.mutex); - array_push_size(&result->files, &f, sizeof(found_file)); - mutex_unlock(&result->files.mutex); + ts_mutex_lock(&result->files.mutex); + ts_array_push_size(&result->files, &f, sizeof(ts_found_file)); + ts_mutex_unlock(&result->files.mutex); } } diff --git a/src/memory_bucket.cpp b/src/memory_bucket.cpp index 71cc79a..f6f215a 100644 --- a/src/memory_bucket.cpp +++ b/src/memory_bucket.cpp @@ -1,68 +1,68 @@ #include "memory_bucket.h" -memory_bucket memory_bucket_init(int bucket_size) +ts_memory_bucket ts_memory_bucket_init(int bucket_size) { - memory_bucket collection; - collection.bucket_mutex = mutex_create(); - collection.buckets = array_create(sizeof(memory_bucket_entry)); + ts_memory_bucket collection; + collection.bucket_mutex = ts_mutex_create(); + collection.buckets = ts_array_create(sizeof(ts_memory_bucket_entry)); - memory_bucket_entry bucket; + ts_memory_bucket_entry bucket; bucket.data = (char*)malloc(bucket_size); bucket.length = bucket_size; bucket.cursor = 0; - array_push(&collection.buckets, &bucket); + ts_array_push(&collection.buckets, &bucket); return collection; } -void* memory_bucket_reserve(memory_bucket *bucket, int reserve_length) +void* ts_memory_bucket_reserve(ts_memory_bucket *bucket, int reserve_length) { - mutex_lock(&bucket->bucket_mutex); - memory_bucket_entry *bucket_entry = 0; + ts_mutex_lock(&bucket->bucket_mutex); + ts_memory_bucket_entry *bucket_entry = 0; for (int i = 0; i < bucket->buckets.length; i++) { - bucket_entry = (memory_bucket_entry *)array_at(&bucket->buckets, i); + bucket_entry = (ts_memory_bucket_entry *)ts_array_at(&bucket->buckets, i); if (bucket_entry->length - bucket_entry->cursor < reserve_length) continue; void *space = bucket_entry->data+bucket_entry->cursor; bucket_entry->cursor += reserve_length; - mutex_unlock(&bucket->bucket_mutex); + ts_mutex_unlock(&bucket->bucket_mutex); return space; } // failed to find suitable space, allocate new bucket - memory_bucket_entry new_bucket; + ts_memory_bucket_entry new_bucket; new_bucket.data = (char*)malloc(bucket_entry->length); new_bucket.length = bucket_entry->length; new_bucket.cursor = 0; - array_push(&bucket->buckets, &new_bucket); - mutex_unlock(&bucket->bucket_mutex); + ts_array_push(&bucket->buckets, &new_bucket); + ts_mutex_unlock(&bucket->bucket_mutex); return new_bucket.data; } -inline void memory_bucket_reset(memory_bucket *bucket) +inline void ts_memory_bucket_reset(ts_memory_bucket *bucket) { - mutex_lock(&bucket->bucket_mutex); + ts_mutex_lock(&bucket->bucket_mutex); for (int i = 0; i < bucket->buckets.length; i++) { - memory_bucket_entry *bucket_entry = (memory_bucket_entry *)array_at(&bucket->buckets, i); + ts_memory_bucket_entry *bucket_entry = (ts_memory_bucket_entry *)ts_array_at(&bucket->buckets, i); bucket_entry->cursor = 0; } - mutex_unlock(&bucket->bucket_mutex); + ts_mutex_unlock(&bucket->bucket_mutex); } -inline void memory_bucket_destroy(memory_bucket *bucket) +inline void ts_memory_bucket_destroy(ts_memory_bucket *bucket) { - mutex_lock(&bucket->bucket_mutex); + ts_mutex_lock(&bucket->bucket_mutex); for (int i = 0; i < bucket->buckets.length; i++) { - memory_bucket_entry *bucket_entry = (memory_bucket_entry *)array_at(&bucket->buckets, i); + ts_memory_bucket_entry *bucket_entry = (ts_memory_bucket_entry *)ts_array_at(&bucket->buckets, i); free(bucket_entry->data); } - array_destroy(&bucket->buckets); - mutex_unlock(&bucket->bucket_mutex); + ts_array_destroy(&bucket->buckets); + ts_mutex_unlock(&bucket->bucket_mutex); - mutex_destroy(&bucket->bucket_mutex); + ts_mutex_destroy(&bucket->bucket_mutex); }
\ No newline at end of file diff --git a/src/memory_bucket.h b/src/memory_bucket.h index f203804..206dff2 100644 --- a/src/memory_bucket.h +++ b/src/memory_bucket.h @@ -7,22 +7,22 @@ #include "mutex.h" #include "array.h" -typedef struct t_memory_bucket_entry +typedef struct t_ts_memory_bucket_entry { char *data; int length; int cursor; -} memory_bucket_entry; +} ts_memory_bucket_entry; -typedef struct t_memory_bucket +typedef struct t_ts_memory_bucket { - mutex bucket_mutex; - array buckets; -} memory_bucket; + ts_mutex bucket_mutex; + ts_array buckets; +} ts_memory_bucket; -memory_bucket memory_bucket_init(int bucket_size); -void* memory_bucket_reserve(memory_bucket *bucket, int reserve_length); -void memory_bucket_reset(memory_bucket *bucket); -void memory_bucket_destroy(memory_bucket *bucket); +ts_memory_bucket ts_memory_bucket_init(int bucket_size); +void* ts_memory_bucket_reserve(ts_memory_bucket *bucket, int reserve_length); +void ts_memory_bucket_reset(ts_memory_bucket *bucket); +void ts_memory_bucket_destroy(ts_memory_bucket *bucket); #endif
\ No newline at end of file diff --git a/src/mutex.cpp b/src/mutex.cpp index 089e5bd..f224312 100644 --- a/src/mutex.cpp +++ b/src/mutex.cpp @@ -1,47 +1,47 @@ #include "mutex.h" -mutex mutex_create() +ts_mutex ts_mutex_create() { - mutex result; + ts_mutex result; result.mutex = CreateMutex( NULL, // default security attributes FALSE, // initially not owned - NULL); // unnamed mutex + NULL); // unnamed ts_mutex return result; } -mutex mutex_create_recursive() +ts_mutex ts_mutex_create_recursive() { - return mutex_create(); + return ts_mutex_create(); } -void mutex_lock(mutex *mutex) +void ts_mutex_lock(ts_mutex *ts_mutex) { WaitForSingleObject( - mutex->mutex, // handle to mutex + ts_mutex->mutex, // handle to ts_mutex INFINITE); // no time-out interval } -int mutex_trylock(mutex *mutex) +int ts_mutex_trylock(ts_mutex *ts_mutex) { - return WaitForSingleObject(mutex->mutex, 1) == WAIT_OBJECT_0; + return WaitForSingleObject(ts_mutex->mutex, 1) == WAIT_OBJECT_0; } -void mutex_unlock(mutex *mutex) +void ts_mutex_unlock(ts_mutex *ts_mutex) { - ReleaseMutex(mutex->mutex); + ReleaseMutex(ts_mutex->mutex); } -void mutex_destroy(mutex *mutex) +void ts_mutex_destroy(ts_mutex *ts_mutex) { - CloseHandle(mutex->mutex); + CloseHandle(ts_mutex->mutex); } -thread thread_start(void *(*start_routine) (void *), void *arg) +ts_thread ts_thread_start(void *(*start_routine) (void *), void *arg) { - thread result; + ts_thread result; result.valid = 0; result.thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start_routine, @@ -51,52 +51,52 @@ thread thread_start(void *(*start_routine) (void *), void *arg) return result; } -void thread_join(thread *thread) +void ts_thread_join(ts_thread *ts_thread) { - if (thread->valid) + if (ts_thread->valid) { - WaitForSingleObject(thread->thread, INFINITE); - CloseHandle(thread->thread); + WaitForSingleObject(ts_thread->thread, INFINITE); + CloseHandle(ts_thread->thread); } } -int thread_tryjoin(thread *thread) +int ts_thread_tryjoin(ts_thread *ts_thread) { - if (thread->valid) + if (ts_thread->valid) { - int result = WaitForSingleObject(thread->thread, 0); + int result = WaitForSingleObject(ts_thread->thread, 0); return result == WAIT_OBJECT_0; } return 0; } -void thread_detach(thread *thread) +void ts_thread_detach(ts_thread *ts_thread) { - if (thread->valid) + if (ts_thread->valid) { - CloseHandle(thread->thread); + CloseHandle(ts_thread->thread); } } -void thread_stop(thread *thread) +void ts_thread_stop(ts_thread *ts_thread) { - if (thread->valid) + if (ts_thread->valid) { - SuspendThread(thread->thread); + SuspendThread(ts_thread->thread); } } -int thread_get_id() +int ts_thread_get_id() { return GetCurrentThreadId(); } -void thread_exit() +void ts_thread_exit() { ExitThread(0); } -void thread_sleep(int microseconds) +void ts_thread_sleep(int microseconds) { Sleep(microseconds/1000); }
\ No newline at end of file diff --git a/src/mutex.h b/src/mutex.h index a061c2e..a745947 100644 --- a/src/mutex.h +++ b/src/mutex.h @@ -9,34 +9,32 @@ #include <conio.h> #include <synchapi.h> -typedef struct t_mutex +typedef struct t_ts_mutex { HANDLE mutex; -} mutex; +} ts_mutex; -typedef struct t_thread +typedef struct t_ts_thread { HANDLE thread; int valid; -} thread; +} ts_thread; #endif -thread thread_start(void *(*start_routine) (void *), void *arg); -void thread_join(thread *thread); -int thread_tryjoin(thread *thread); -void thread_detach(thread *thread); -void thread_stop(thread *thread); -int thread_get_id(); -void thread_sleep(int microseconds); -void thread_exit(); - - -mutex mutex_create_recursive(); -mutex mutex_create(); -void mutex_lock(mutex *mutex); -int mutex_trylock(mutex *mutex); -void mutex_unlock(mutex *mutex); -void mutex_destroy(mutex *mutex); - +ts_thread ts_thread_start(void *(*start_routine) (void *), void *arg); +void ts_thread_join(ts_thread *ts_thread); +int ts_thread_tryjoin(ts_thread *ts_thread); +void ts_thread_detach(ts_thread *ts_thread); +void ts_thread_stop(ts_thread *ts_thread); +int ts_thread_get_id(); +void ts_thread_sleep(int microseconds); +void ts_thread_exit(); + +ts_mutex ts_mutex_create_recursive(); +ts_mutex ts_mutex_create(); +void ts_mutex_lock(ts_mutex *ts_mutex); +int ts_mutex_trylock(ts_mutex *ts_mutex); +void ts_mutex_unlock(ts_mutex *ts_mutex); +void ts_mutex_destroy(ts_mutex *ts_mutex); #endif
\ No newline at end of file diff --git a/src/platform.h b/src/platform.h index 66cdcb1..8429058 100644 --- a/src/platform.h +++ b/src/platform.h @@ -6,14 +6,14 @@ #include "search.h" #include "../utf8.h" -typedef struct t_file_content +typedef struct t_ts_file_content { int content_length; void *content; int file_error; -} file_content; +} ts_file_content; -typedef enum t_file_open_error +typedef enum t_ts_file_open_error { FILE_ERROR_TOO_MANY_OPEN_FILES_PROCESS = 1, FILE_ERROR_TOO_MANY_OPEN_FILES_SYSTEM = 2, @@ -26,10 +26,10 @@ typedef enum t_file_open_error FILE_ERROR_STALE = 9, // NFS server file is removed/renamed FILE_ERROR_GENERIC = 10, FILE_ERROR_TOO_BIG = 11, -} file_open_error; +} ts_file_open_error; -file_content platform_read_file(char *path, const char *mode); -void platform_list_files_block(search_result* result, wchar_t* start_dir = nullptr); -void platform_list_files(search_result* result); +ts_file_content ts_platform_read_file(char *path, const char *mode); +void ts_platform_list_files_block(ts_search_result* result, wchar_t* start_dir = nullptr); +void ts_platform_list_files(ts_search_result* result); #endif
\ No newline at end of file diff --git a/src/search.cpp b/src/search.cpp index f312423..9a55105 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -3,27 +3,27 @@ #include <stdio.h> -search_result* current_search_result = nullptr; +ts_search_result *current_search_result = nullptr; -array get_filters(char *pattern) +ts_array ts_get_filters(char *pattern) { - array result = array_create(MAX_INPUT_LENGTH); - + ts_array result = ts_array_create(MAX_INPUT_LENGTH); + char current_filter[MAX_INPUT_LENGTH]; int filter_len = 0; - while(*pattern) + while (*pattern) { char ch = *pattern; - + if (ch == ',') { current_filter[filter_len] = 0; - array_push(&result, current_filter); + ts_array_push(&result, current_filter); filter_len = 0; } else { - if(filter_len < MAX_INPUT_LENGTH-1) + if (filter_len < MAX_INPUT_LENGTH - 1) { current_filter[filter_len++] = ch; } @@ -32,47 +32,46 @@ array get_filters(char *pattern) current_filter[filter_len] = ch; } } - + pattern++; } current_filter[filter_len] = 0; - array_push(&result, current_filter); - + ts_array_push(&result, current_filter); + return result; } -int string_match(char *first, char *second) +int ts_string_match(char *first, char *second) { - // If we reach at the end of both strings, we are done - if (*first == '\0' && *second == '\0') - return 1; - - // Make sure that the characters after '*' are present - // in second string. This function assumes that the first - // string will not contain two consecutive '*' - if (*first == '*' && *(first+1) != '\0' && *second == '\0') + // If we reach at the end of both strings, we are done + if (*first == '\0' && *second == '\0') + return 1; + + // Make sure that the characters after '*' are present + // in second string. This function assumes that the first + // string will not contain two consecutive '*' + if (*first == '*' && *(first + 1) != '\0' && *second == '\0') return 0; - - // If the first string contains '?', or current characters + + // If the first string contains '?', or current characters // of both strings string_match - if (*first == '?' || *first == *second) - return string_match(first+1, second+1); - - // If there is *, then there are two possibilities - // a) We consider current character of second string - // b) We ignore current character of second string. - if (*first == '*') - return string_match(first+1, second) || string_match(first, second+1); - return 0; -} + if (*first == '?' || *first == *second) + return ts_string_match(first + 1, second + 1); + // If there is *, then there are two possibilities + // a) We consider current character of second string + // b) We ignore current character of second string. + if (*first == '*') + return ts_string_match(first + 1, second) || ts_string_match(first, second + 1); + return 0; +} -int filter_matches(array *filters, char *string, char **matched_filter) +int ts_filter_matches(ts_array *filters, char *string, char **matched_filter) { for (int i = 0; i < filters->length; i++) { - char *filter = (char *)array_at(filters, i); - if (string_match(filter, string)) + char *filter = (char *)ts_array_at(filters, i); + if (ts_string_match(filter, string)) { *matched_filter = filter; return strlen(filter); @@ -81,228 +80,239 @@ int filter_matches(array *filters, char *string, char **matched_filter) return -1; } -search_result *create_empty_search_result() +ts_search_result *ts_create_empty_search_result() { - search_result *new_result_buffer = (search_result *)malloc(sizeof(search_result)); + ts_search_result *new_result_buffer = (ts_search_result *)malloc(sizeof(ts_search_result)); new_result_buffer->completed_match_threads = 0; - new_result_buffer->mutex = mutex_create(); + new_result_buffer->mutex = ts_mutex_create(); new_result_buffer->done_finding_files = false; new_result_buffer->file_list_read_cursor = 0; - new_result_buffer->max_thread_count = 4; + new_result_buffer->max_ts_thread_count = 4; new_result_buffer->match_count = 0; new_result_buffer->file_count = 0; new_result_buffer->cancel_search = false; new_result_buffer->max_file_size = megabytes(1000); - - new_result_buffer->files = array_create(sizeof(found_file)); + + new_result_buffer->files = ts_array_create(sizeof(ts_found_file)); new_result_buffer->files.reserve_jump = FILE_RESERVE_COUNT; - array_reserve(&new_result_buffer->files, FILE_RESERVE_COUNT); + ts_array_reserve(&new_result_buffer->files, FILE_RESERVE_COUNT); - new_result_buffer->matches = array_create(sizeof(file_match)); + new_result_buffer->matches = ts_array_create(sizeof(ts_file_match)); new_result_buffer->matches.reserve_jump = FILE_RESERVE_COUNT; - array_reserve(&new_result_buffer->matches, FILE_RESERVE_COUNT); + ts_array_reserve(&new_result_buffer->matches, FILE_RESERVE_COUNT); // filter buffers - new_result_buffer->directory_to_search = (char*)malloc(MAX_INPUT_LENGTH); - new_result_buffer->search_text = (char*)malloc(MAX_INPUT_LENGTH); - + new_result_buffer->directory_to_search = (char *)malloc(MAX_INPUT_LENGTH); + new_result_buffer->search_text = (char *)malloc(MAX_INPUT_LENGTH); + return new_result_buffer; } bool string_is_asteriks(char *text) { utf8_int32_t ch; - while((text = utf8codepoint(text, &ch)) && ch) + while ((text = utf8codepoint(text, &ch)) && ch) { - if (ch != '*') return false; + if (ch != '*') + return false; } return true; } -bool string_contains_ex(char *text_to_search, utf8_int8_t *text_to_find, array *text_matches) +bool ts_string_contains(char *text_to_search, utf8_int8_t *text_to_find, ts_array *text_matches) { bool final_result = false; bool is_asteriks_only = false; - + // * wildcard at the start of text to find is not needed if (string_is_asteriks(text_to_find)) { is_asteriks_only = true; text_to_find += strlen(text_to_find); } - + // remove all asteriks from start utf8_int32_t br; - while(utf8codepoint(text_to_find, &br) && br == '*') + while (utf8codepoint(text_to_find, &br) && br == '*') { text_to_find = utf8codepoint(text_to_find, &br); } - + char *text_to_find_original = text_to_find; bool save_info = (text_matches != 0); - + utf8_int32_t text_to_search_ch = 0; utf8_int32_t text_to_find_ch = 0; size_t text_to_find_char_len = utf8len(text_to_find); - + int line_nr_val = 1; int word_offset_val = 0; int word_match_len_val = 0; - char* line_start_ptr = text_to_search; - + char *line_start_ptr = text_to_search; + int index = 0; - while((text_to_search = utf8codepoint(text_to_search, &text_to_search_ch)) - && text_to_search_ch) + while ((text_to_search = utf8codepoint(text_to_search, &text_to_search_ch)) && text_to_search_ch) { word_offset_val++; - if (text_to_search_ch == '\n') + if (text_to_search_ch == '\n') { line_nr_val++; word_offset_val = 0; line_start_ptr = text_to_search; } - + utf8_int8_t *text_to_search_current_attempt = text_to_search; utf8_int32_t text_to_search_current_attempt_ch = text_to_search_ch; - + bool in_wildcard = false; - + text_to_find = utf8codepoint(text_to_find, &text_to_find_ch); - //text_to_search_current_attempt = utf8codepoint(text_to_search_current_attempt, + // text_to_search_current_attempt = utf8codepoint(text_to_search_current_attempt, //&text_to_search_current_attempt_ch); - + word_match_len_val = 0; - while(text_to_search_current_attempt_ch) + while (text_to_search_current_attempt_ch) { // wildcard, accept any character in text to search if (text_to_find_ch == '?') goto continue_search; - - // character matches, + + // character matches, if (text_to_find_ch == text_to_search_current_attempt_ch && in_wildcard) in_wildcard = false; - + // wildcard, accept any characters in text to search untill next char is found if (text_to_find_ch == '*') { text_to_find = utf8codepoint(text_to_find, &text_to_find_ch); in_wildcard = true; } - + // text to find has reached 0byte, word has been found if (text_to_find_ch == 0) { - done: + done: if (save_info) { - text_match new_match; + ts_text_match new_match; new_match.line_nr = line_nr_val; - new_match.word_offset = word_offset_val-1; + new_match.word_offset = word_offset_val - 1; new_match.word_match_len = word_match_len_val; new_match.line_start = line_start_ptr; new_match.line_info = 0; - array_push(text_matches, &new_match); + ts_array_push(text_matches, &new_match); } - + final_result = true; - + if (is_asteriks_only) { return final_result; } - + break; } - + // character does not match, continue search if (text_to_find_ch != text_to_search_current_attempt_ch && !in_wildcard) break; - - continue_search: + + continue_search: if (!in_wildcard) text_to_find = utf8codepoint(text_to_find, &text_to_find_ch); - + text_to_search_current_attempt = utf8codepoint( text_to_search_current_attempt, &text_to_search_current_attempt_ch); - - if (!text_to_search_current_attempt_ch && !text_to_find_ch) goto done; - + + if (!text_to_search_current_attempt_ch && !text_to_find_ch) + goto done; + word_match_len_val++; } - + text_to_find = text_to_find_original; index++; } - + return final_result; - - set_info_and_return_failure: + +set_info_and_return_failure: return false; } -static void _ts_search_file(found_file* ref, file_content content, search_result* result) { +static void _ts_search_file(ts_found_file *ref, ts_file_content content, ts_search_result *result) +{ if (content.content && !content.file_error) { - array text_matches = array_create(sizeof(text_match)); + ts_array text_matches = ts_array_create(sizeof(ts_text_match)); int search_len = strlen(result->search_text); - if (string_contains_ex((char*)content.content, result->search_text, &text_matches)) + if (ts_string_contains((char *)content.content, result->search_text, &text_matches)) { - mutex_lock(&result->matches.mutex); + ts_mutex_lock(&result->matches.mutex); for (int i = 0; i < text_matches.length; i++) { - text_match *m = (text_match *)array_at(&text_matches, i); + ts_text_match *m = (ts_text_match *)ts_array_at(&text_matches, i); - file_match file_match; + ts_file_match file_match; file_match.file = ref; file_match.line_nr = m->line_nr; file_match.word_match_offset = m->word_offset; file_match.word_match_length = m->word_match_len; - file_match.line_info = (char*)malloc(MAX_INPUT_LENGTH); + file_match.line_info = (char *)malloc(MAX_INPUT_LENGTH); int text_pad_lr = 25; - if (file_match.word_match_offset > text_pad_lr) { + if (file_match.word_match_offset > text_pad_lr) + { m->line_start += file_match.word_match_offset - text_pad_lr; file_match.word_match_offset = text_pad_lr; } int total_len = text_pad_lr + search_len + text_pad_lr; snprintf(file_match.line_info, MAX_INPUT_LENGTH, "%.*s", total_len, m->line_start); - for (int i = 0; i < total_len; i++) { - if (file_match.line_info[i] == '\n') file_match.line_info[i] = ' '; - if (file_match.line_info[i] == '\t') file_match.line_info[i] = ' '; - if (file_match.line_info[i] == '\r') file_match.line_info[i] = ' '; - if (file_match.line_info[i] == '\x0B') file_match.line_info[i] = ' '; + for (int i = 0; i < total_len; i++) + { + if (file_match.line_info[i] == '\n') + file_match.line_info[i] = ' '; + if (file_match.line_info[i] == '\t') + file_match.line_info[i] = ' '; + if (file_match.line_info[i] == '\r') + file_match.line_info[i] = ' '; + if (file_match.line_info[i] == '\x0B') + file_match.line_info[i] = ' '; } - - array_push_size(&result->matches, &file_match, sizeof(file_match)); + + ts_array_push_size(&result->matches, &file_match, sizeof(file_match)); ref->match_count++; result->match_count = result->matches.length; } - mutex_unlock(&result->matches.mutex); + ts_mutex_unlock(&result->matches.mutex); } - - array_destroy(&text_matches); + + ts_array_destroy(&text_matches); } } -static void* _ts_search_thread(void* args) { - search_result* new_result = (search_result *)args; +static void *_ts_search_thread(void *args) +{ + ts_search_result *new_result = (ts_search_result *)args; - keep_going:; +keep_going:; while (new_result->file_list_read_cursor < new_result->files.length) { - if (new_result->cancel_search) goto finish_early; + if (new_result->cancel_search) + goto finish_early; - mutex_lock(&new_result->files.mutex); + ts_mutex_lock(&new_result->files.mutex); int read_cursor = new_result->file_list_read_cursor++; new_result->file_count++; - mutex_unlock(&new_result->files.mutex); + ts_mutex_unlock(&new_result->files.mutex); - if (read_cursor >= new_result->files.length) continue; + if (read_cursor >= new_result->files.length) + continue; - found_file* f = (found_file*)array_at(&new_result->files, read_cursor); - file_content content = platform_read_file(f->path, "rb, ccs=UTF-8"); + ts_found_file *f = (ts_found_file *)ts_array_at(&new_result->files, read_cursor); + ts_file_content content = ts_platform_read_file(f->path, "rb, ccs=UTF-8"); _ts_search_file(f, content, new_result); @@ -312,28 +322,31 @@ static void* _ts_search_thread(void* args) { if (!new_result->done_finding_files) goto keep_going; - finish_early:; - mutex_lock(&new_result->files.mutex); +finish_early:; + ts_mutex_lock(&new_result->files.mutex); new_result->completed_match_threads++; - mutex_unlock(&new_result->files.mutex); + ts_mutex_unlock(&new_result->files.mutex); return 0; } -void ts_start_search(utf8_int8_t* path, utf8_int8_t* filter, utf8_int8_t* query) { - if (current_search_result) { +void ts_start_search(utf8_int8_t *path, utf8_int8_t *filter, utf8_int8_t *query) +{ + if (current_search_result) + { current_search_result->cancel_search = true; } - search_result* new_result = create_empty_search_result(); + ts_search_result *new_result = ts_create_empty_search_result(); snprintf(new_result->directory_to_search, MAX_INPUT_LENGTH, "%s", path); snprintf(new_result->search_text, MAX_INPUT_LENGTH, "%s", query); - - platform_list_files(new_result); - //new_result->max_thread_count - for (int i = 0; i < 1; i++) { - thread thr = thread_start(_ts_search_thread, new_result); - thread_detach(&thr); + + ts_platform_list_files(new_result); + // new_result->max_ts_thread_count + for (int i = 0; i < 1; i++) + { + ts_thread thr = ts_thread_start(_ts_search_thread, new_result); + ts_thread_detach(&thr); } current_search_result = new_result; diff --git a/src/search.h b/src/search.h index 51ae649..3518b33 100644 --- a/src/search.h +++ b/src/search.h @@ -10,22 +10,22 @@ #include "memory_bucket.h" #include "../utf8.h" -typedef struct t_found_file +typedef struct t_ts_found_file { utf8_int8_t *path; int match_count; -} found_file; +} ts_found_file; -typedef struct t_search_result +typedef struct t_ts_search_result { // data - array files; - array matches; + ts_array files; + ts_array matches; int match_count; int file_count; // thread syncing - mutex mutex; + ts_mutex mutex; int completed_match_threads; int done_finding_files; int file_list_read_cursor; @@ -34,36 +34,35 @@ typedef struct t_search_result // search query utf8_int8_t *directory_to_search; utf8_int8_t *search_text; - int max_thread_count; + int max_ts_thread_count; int max_file_size; -} search_result; +} ts_search_result; -typedef struct t_file_match +typedef struct t_ts_file_match { - found_file* file; + ts_found_file* file; int line_nr; int word_match_offset; int word_match_length; - utf8_int8_t *line_info; // will be null when no match is found -} file_match; + utf8_int8_t *line_info; +} ts_file_match; -typedef struct t_text_match +typedef struct t_ts_text_match { int line_nr; int word_offset; int word_match_len; char *line_start; char *line_info; -} text_match; +} ts_text_match; -extern search_result* current_search_result; +extern ts_search_result* current_search_result; -array get_filters(char *pattern); -int filter_matches(array *filters, char *string, char **matched_filter); -int string_match(char *first, char *second); -search_result *create_empty_search_result(); -bool string_contains_ex(char *text_to_search, char *text_to_find, array *text_matches); - -void ts_start_search(utf8_int8_t* path, utf8_int8_t* filter, utf8_int8_t* query); +ts_array ts_get_filters(char *pattern); +int ts_filter_matches(ts_array *filters, char *string, char **matched_filter); +int ts_string_match(char *first, char *second); +ts_search_result* ts_create_empty_search_result(); +bool ts_string_contains(char *text_to_search, char *text_to_find, ts_array *text_matches); +void ts_start_search(utf8_int8_t* path, utf8_int8_t* filter, utf8_int8_t* query); #endif
\ No newline at end of file |
