| 1 | # OpenCC Source Layout |
| 2 | |
| 3 | This directory contains the C++ core library, the public C API, command-line |
| 4 | tools, and native extension entry points. The code is organized around a simple |
| 5 | conversion pipeline: |
| 6 | |
| 7 | 1. Load a JSON configuration. |
| 8 | 2. Load segmenters and dictionaries referenced by that configuration. |
| 9 | 3. Segment input text. |
| 10 | 4. Apply one or more dictionary-based conversion stages. |
| 11 | 5. Return converted text or inspection data. |
| 12 | |
| 13 | The data files that drive this pipeline live outside this directory: |
| 14 | |
| 15 | - `data/config/*.json`: conversion schemes. |
| 16 | - `data/dictionary/*.txt`: source dictionaries. |
| 17 | - generated `.ocd2` files: marisa-trie dictionary binaries. |
| 18 | |
| 19 | ## Main Pipeline |
| 20 | |
| 21 | ### Configuration |
| 22 | |
| 23 | - `Config.hpp`, `Config.cpp` |
| 24 | - Finds and parses JSON configuration files. |
| 25 | - Builds segmenters, dictionaries, dictionary groups, conversions, and |
| 26 | converters. |
| 27 | - Resolves dictionary/resource files through `ResourceProvider`. |
| 28 | - Keeps config loading separate from dictionary resource lookup. |
| 29 | - Uses UTF-16-capable file checks on Windows for internal config and |
| 30 | resource paths. |
| 31 | - `ResourceProvider.hpp`, `ResourceProvider.cpp` |
| 32 | - Provides `ResourceProvider` and `FilesystemResourceProvider`. |
| 33 | - `FilesystemResourceProvider` searches configured resource directories in |
| 34 | order and returns a resolved path for lower-level dictionary loaders. |
| 35 | - Embedded hosts can combine application, plugin, and system OpenCC data |
| 36 | directories without changing config JSON. |
| 37 | |
| 38 | ### Segmentation |
| 39 | |
| 40 | - `Segmentation.hpp`, `Segmentation.cpp` |
| 41 | - Base interface for segmenters. |
| 42 | - `MaxMatchSegmentation.hpp`, `MaxMatchSegmentation.cpp` |
| 43 | - Maximum forward matching segmenter used by normal OpenCC configs. |
| 44 | - Uses dictionary prefix matches to keep phrases intact. |
| 45 | - `PluginSegmentation.hpp`, `PluginSegmentation.cpp` |
| 46 | - Runtime-loaded segmentation plugin support. |
| 47 | - Used by the Jieba plugin. |
| 48 | - `Segments.hpp` |
| 49 | - Segment container used between segmenters, conversions, and inspection |
| 50 | output. |
| 51 | |
| 52 | ### Conversion |
| 53 | |
| 54 | - `Converter.hpp`, `Converter.cpp` |
| 55 | - Owns a segmenter and a conversion chain. |
| 56 | - Provides high-level string and buffer conversion. |
| 57 | - `ConversionChain.hpp`, `ConversionChain.cpp` |
| 58 | - Ordered list of conversion stages. |
| 59 | - `Conversion.hpp`, `Conversion.cpp` |
| 60 | - One dictionary-backed conversion stage. |
| 61 | - `ConversionInspection.hpp` |
| 62 | - Data structures returned by inspection mode. |
| 63 | |
| 64 | The core conversion path depends on segmentation and longest-prefix dictionary |
| 65 | matching. Character-by-character replacement is not equivalent to OpenCC |
| 66 | behavior because phrase priority and multi-stage conversion order matter. |
| 67 | |
| 68 | ## Dictionaries |
| 69 | |
| 70 | ### Interfaces and Shared Types |
| 71 | |
| 72 | - `Dict.hpp`, `Dict.cpp` |
| 73 | - Abstract dictionary interface. |
| 74 | - Supports exact match, prefix match, all-prefix match, and enumeration. |
| 75 | - `DictEntry.hpp`, `DictEntry.cpp` |
| 76 | - Key/value entry representation. |
| 77 | - `PrefixMatch.hpp`, `PrefixMatch.cpp` |
| 78 | - Prefix match result. |
| 79 | - `Lexicon.hpp`, `Lexicon.cpp` |
| 80 | - In-memory collection of dictionary entries. |
| 81 | - `SerializableDict.hpp` |
| 82 | - Template helpers for loading serialized dictionaries from files. |
| 83 | - `SerializedValues.hpp`, `SerializedValues.cpp` |
| 84 | - Compact storage for candidate value lists used by `.ocd2`. |
| 85 | |
| 86 | ### Implementations |
| 87 | |
| 88 | - `TextDict.hpp`, `TextDict.cpp` |
| 89 | - Tab-delimited text dictionary. |
| 90 | - Useful for source data and tests. |
| 91 | - `MarisaDict.hpp`, `MarisaDict.cpp` |
| 92 | - Default `.ocd2` dictionary format. |
| 93 | - Uses marisa-trie for compact prefix lookup. |
| 94 | - `DartsDict.hpp`, `DartsDict.cpp` |
| 95 | - Legacy `.ocd` dictionary format. |
| 96 | - Requires Darts support. |
| 97 | - `BinaryDict.hpp`, `BinaryDict.cpp` |
| 98 | - Legacy binary payload support used with Darts serialization. |
| 99 | - `DictGroup.hpp`, `DictGroup.cpp` |
| 100 | - Ordered group of dictionaries. |
| 101 | - Tries dictionaries in sequence and returns the first usable match. |
| 102 | - `DictConverter.hpp`, `DictConverter.cpp` |
| 103 | - Converts dictionary files between supported formats. |
| 104 | |
| 105 | ## Public APIs |
| 106 | |
| 107 | ### C++ API |
| 108 | |
| 109 | - `SimpleConverter.hpp`, `SimpleConverter.cpp` |
| 110 | - High-level C++ wrapper around `Config` and `Converter`. |
| 111 | - Accepts a config name/path, optional search paths, or an explicit |
| 112 | `ResourceProvider`. |
| 113 | - Throws C++ exceptions on failures. |
| 114 | |
| 115 | ### C API |
| 116 | |
| 117 | - `opencc.h` |
| 118 | - Stable C ABI for downstream users and bindings. |
| 119 | - Exposes `opencc_open`, `opencc_open_w`, `opencc_convert_utf8`, |
| 120 | `opencc_convert_utf8_free`, `opencc_close`, and error helpers. |
| 121 | |
| 122 | Windows path semantics need care: |
| 123 | |
| 124 | - `opencc_open_w(const wchar_t*)` is the explicit UTF-16 Windows API. |
| 125 | - `opencc_open(const char*)` keeps the historical Windows/MSVC narrow-string |
| 126 | behavior. Do not silently change its encoding contract without a migration |
| 127 | plan. |
| 128 | - New Windows path-taking C APIs should use explicit names such as |
| 129 | `*_utf8` or `*_w` rather than relying on ambiguous `char*` semantics. |
| 130 | |
| 131 | ### Python Extension |
| 132 | |
| 133 | - `py_opencc.cpp` |
| 134 | - Python module binding built on top of the C++ core. |
| 135 | |
| 136 | ## Command-Line Tools |
| 137 | |
| 138 | The native CLI tools live under `src/tools`. |
| 139 | |
| 140 | - `CommandLine.cpp` |
| 141 | - Small executable entry point. |
| 142 | - On Windows/MSVC, obtains command-line arguments through wide Windows APIs |
| 143 | and converts them to UTF-8 before dispatching to the CLI core. |
| 144 | - `CommandLineMain.hpp`, `CommandLineMain.cpp` |
| 145 | - Main `opencc` command implementation. |
| 146 | - Handles conversion, segmentation/inspection modes, measurement output, |
| 147 | stream conversion, and explicit `--in-place` conversion. |
| 148 | - `PlatformIO.hpp`, `PlatformIO.cpp` |
| 149 | - CLI platform boundary for path-sensitive operations. |
| 150 | - Handles UTF-8 file open/remove/replace, temporary output files, same-file |
| 151 | detection, real path resolution, and Windows argv collection. |
| 152 | - `DictConverter.cpp` |
| 153 | - `opencc_dict` implementation. |
| 154 | - `PhraseExtract.cpp` |
| 155 | - `opencc_phrase_extract` implementation. |
| 156 | - `CmdLineOutput.hpp` |
| 157 | - Shared command-line help formatting. |
| 158 | |
| 159 | CLI file conversion must remain streaming. Do not replace stream processing |
| 160 | with "read whole file into memory" logic. In-place conversion is intentionally |
| 161 | opt-in: |
| 162 | |
| 163 | - Without `--in-place`, `-i` and `-o` referring to the same actual file is |
| 164 | rejected. |
| 165 | - With `--in-place`, output is written to a temporary file next to the target, |
| 166 | then the target is replaced after conversion succeeds. |
| 167 | |
| 168 | ## Platform and Path Handling |
| 169 | |
| 170 | OpenCC uses UTF-8 internally for paths unless an API explicitly documents a |
| 171 | different contract. Windows code should convert to UTF-16 at the platform |
| 172 | boundary. |
| 173 | |
| 174 | Important files: |
| 175 | |
| 176 | - `UTF8Util.hpp`, `UTF8Util.cpp` |
| 177 | - UTF-8 helpers and platform string conversion helpers. |
| 178 | - `WinUtil.hpp` |
| 179 | - Small Windows-only UTF-8/UTF-16 conversion helpers. |
| 180 | - `tools/PlatformIO.*` |
| 181 | - CLI path boundary. |
| 182 | |
| 183 | Maintenance rules: |
| 184 | |
| 185 | - Do not add raw `fopen`, `std::fstream`, `stat`, `std::tmpnam`, or narrow |
| 186 | Win32 path calls to new Windows-sensitive paths. |
| 187 | - Prefer existing platform helpers or add a clearly named boundary helper. |
| 188 | - Keep public API encoding contracts explicit. |
| 189 | - Validate Windows path changes with real Windows CI. Zig cross-compilation is |
| 190 | useful for compile/link coverage, but it does not execute Windows runtime |
| 191 | path behavior. |
| 192 | |
| 193 | ## Plugins |
| 194 | |
| 195 | - `plugin/OpenCCPlugin.h` |
| 196 | - C plugin ABI used by loadable segmentation plugins. |
| 197 | - `PluginSegmentation.*` |
| 198 | - Host-side plugin loader and adapter. |
| 199 | |
| 200 | The Jieba plugin lives outside `src` under `plugins/jieba`. |
| 201 | |
| 202 | ## Tests |
| 203 | |
| 204 | Most core modules have adjacent `*Test.cpp` files in this directory. Important |
| 205 | test groups include: |
| 206 | |
| 207 | - `ConfigTest.cpp` |
| 208 | - Config search paths and Unicode config path handling. |
| 209 | - `SimpleConverterTest.cpp` |
| 210 | - C++ wrapper behavior and C API basics. |
| 211 | - `Conversion*Test.cpp` |
| 212 | - Conversion and inspection behavior. |
| 213 | - `MaxMatchSegmentationTest.cpp` |
| 214 | - Segmenter behavior. |
| 215 | - `MarisaDictTest.cpp`, `TextDictTest.cpp`, `DictGroupTest.cpp` |
| 216 | - Dictionary implementations. |
| 217 | - `UTF8*Test.cpp` |
| 218 | - UTF-8 slicing and utility behavior. |
| 219 | |
| 220 | CLI tests live in `test/CommandLineConvertTest.cpp` because they execute the |
| 221 | built command-line binary. They cover streaming behavior, Unicode paths, |
| 222 | measurement output, inspection modes, and in-place conversion safety. |
| 223 | |
| 224 | ## Build Integration |
| 225 | |
| 226 | The source tree is built through both CMake and Bazel: |
| 227 | |
| 228 | - `src/CMakeLists.txt` |
| 229 | - `src/BUILD.bazel` |
| 230 | - `src/tools/CMakeLists.txt` |
| 231 | - `src/tools/BUILD.bazel` |
| 232 | |
| 233 | When adding or renaming source files, update both build systems and any direct |
| 234 | cross-build scripts that carry explicit source lists. |