diff --git a/Makefile b/Makefile index 7153dea..38173d5 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,3 @@ -# Compiler and flags CXX = g++ CC = gcc CXXFLAGS = -shared -fPIC @@ -7,7 +6,7 @@ LDFLAGS = -L. -ltrie_loader -Wl,-rpath=. # Paths and files LIBRARY_NAME = libtrie_loader.so EXECUTABLE_NAME = wrapper_executable -SRC_CPP = libtrie_loader.hpp +SRC_CPP = libtrie_loader.cpp SRC_C = main.c # Targets diff --git a/haskell.hs b/haskell.hs new file mode 100644 index 0000000..cfdd578 --- /dev/null +++ b/haskell.hs @@ -0,0 +1,47 @@ +{-# LANGUAGE ForeignFunctionInterface #-} + +{- + $ ghc -o my_program Haskell.hs -L. -ltrie_loader +-} + +import Foreign +import Foreign.C +import Control.Monad (when) + +-- Declare the C functions +foreign import ccall "load_trie" loadTrie :: CString -> IO (Ptr ()) +foreign import ccall "perform_search" performSearch :: Ptr () -> CString -> Ptr CSize -> IO (Ptr (Ptr CChar)) +foreign import ccall "free_trie" freeTrie :: Ptr () -> IO () +foreign import ccall "free_search_results" freeSearchResults :: Ptr (Ptr CChar) -> IO () + +main :: IO () +main = do + -- Just call the functions, do nothing with the result + + putStrLn "Calling load_trie..." + -- Load the trie (don't care about the result) + dictFile <- newCString "/home/hashirama/.local/share/hakurei/dict.bin" + trieHandle <- loadTrie dictFile + if trieHandle == nullPtr + then putStrLn "Failed to load trie" + else putStrLn "Trie loaded successfully" + + putStrLn "Calling perform_search..." + -- Perform search (we care about the result here) + searchStr <- newCString "電気通信業務取扱規程" + resultCount <- malloc :: IO (Ptr CSize) + resultsPtr <- performSearch trieHandle searchStr resultCount + when (resultsPtr /= nullPtr) $ do + -- If results are returned, process them + -- Dereference the first result in the results pointer + firstResult <- peek resultsPtr >>= peekCString + putStrLn $ "First search result: " ++ firstResult + + -- Free the search results (assuming we have a way to handle multiple results) + freeSearchResults resultsPtr + + putStrLn "Calling free_trie..." + -- Free the trie (again, no checks, just call) + freeTrie trieHandle + + putStrLn "Functions were called" diff --git a/libtrie_loader.hpp b/libtrie_loader.cpp similarity index 86% rename from libtrie_loader.hpp rename to libtrie_loader.cpp index be6d237..cdf59a8 100644 --- a/libtrie_loader.hpp +++ b/libtrie_loader.cpp @@ -2,6 +2,7 @@ #include #include #include +#include // for strdup typedef void* TrieHandle; // Opaque pointer to represent the trie in C @@ -31,7 +32,6 @@ TrieHandle load_trie_from_cstring(const char* cstr) { } -// C++ function to perform prefix search and return results as a dynamically allocated array of strings (C-compatible) const char** search_prefix(TrieHandle handle, const char* search_str, size_t* result_count) { xcdat::trie_15_type* trie = reinterpret_cast(handle); xcdat::trie_15_type::prefix_iterator itr = trie->make_prefix_iterator(search_str); @@ -42,18 +42,23 @@ const char** search_prefix(TrieHandle handle, const char* search_str, size_t* re results.push_back(std::string(itr.decoded_view())); } - // Allocate memory to store the results + // Allocate memory to store the results + 1 for the null terminator *result_count = results.size(); - const char** result_array = new const char*[*result_count]; + const char** result_array = new const char*[*result_count + 1]; // +1 for null pointer // Copy the results from the vector to the array for (size_t i = 0; i < *result_count; ++i) { - result_array[i] = results[i].c_str(); // Store the raw pointer to the string data + result_array[i] = strdup(results[i].c_str()); // Dynamically allocate strings } + + // Add a null terminator + result_array[*result_count] = nullptr; + return result_array; } + // C territory // Wrapper function to load the trie, for C usage diff --git a/main.c b/main.c index ebd35df..7d48cdf 100644 --- a/main.c +++ b/main.c @@ -23,7 +23,7 @@ int main() { size_t result_count = 0; // Perform the search using the wrapper function - const char** results = perform_search(trie_handle, "真珠湾降", &result_count); + const char** results = perform_search(trie_handle, "よくない影響", &result_count); // Print all the results printf("Results found:\n");