19 #include "config_reader.h"
20 #include "dict_group.h"
21 #include "dict_chain.h"
23 static dictionary_error errnum = DICTIONARY_ERROR_VOID;
28 dict_group->count = 0;
29 dict_group->dict_chain = dict_chain;
33 void dict_group_delete(
DictGroup* dict_group) {
35 for (i = 0; i < dict_group->count; i++) {
36 dict_delete(dict_group->dicts[i]);
41 static char* try_find_dictionary_with_config(
43 const char* filename) {
44 if (is_absolute_path(filename)) {
48 if (dict_group->dict_chain == NULL) {
51 Config* config = dict_group->dict_chain->config;
55 const char* config_path = config->file_path;
56 if (config_path == NULL) {
59 char* config_path_filename = (
char*)malloc(strlen(config_path) + strlen(
61 sprintf(config_path_filename,
"%s/%s", config_path, filename);
62 FILE* fp = fopen(config_path_filename,
"r");
65 return config_path_filename;
70 int dict_group_load(
DictGroup* dict_group,
72 opencc_dictionary_type type) {
74 char* path = try_open_file(filename);
76 path = try_find_dictionary_with_config(dict_group, filename);
78 errnum = DICTIONARY_ERROR_CANNOT_ACCESS_DICTFILE;
82 dictionary = dict_new(path, type);
84 if (dictionary == (
Dict*)-1) {
85 errnum = DICTIONARY_ERROR_INVALID_DICT;
88 dict_group->dicts[dict_group->count++] = dictionary;
92 Dict* dict_group_get_dict(
DictGroup* dict_group,
size_t index) {
93 if (index >= dict_group->count) {
94 errnum = DICTIONARY_ERROR_INVALID_INDEX;
97 return dict_group->dicts[index];
100 const ucs4_t*
const* dict_group_match_longest(
104 size_t* match_length) {
105 if (dict_group->count == 0) {
106 errnum = DICTIONARY_ERROR_NODICT;
107 return (
const ucs4_t*
const*)-1;
109 const ucs4_t*
const* retval = NULL;
110 size_t t_match_length, max_length = 0;
112 for (i = 0; i < dict_group->count; i++) {
114 const ucs4_t*
const* t_retval = dict_match_longest(
115 dict_group->dicts[i],
119 if (t_retval != NULL) {
120 if (t_match_length > max_length) {
121 max_length = t_match_length;
126 if (match_length != NULL) {
127 *match_length = max_length;
132 size_t dict_group_get_all_match_lengths(
DictGroup* dict_group,
134 size_t* match_length) {
135 if (dict_group->count == 0) {
136 errnum = DICTIONARY_ERROR_NODICT;
141 for (i = 0; i < dict_group->count; i++) {
143 retval = dict_get_all_match_lengths(
144 dict_group->dicts[i],
150 if ((i > 0) && (rscnt > 1)) {
151 qsort(match_length, rscnt,
sizeof(match_length[0]), qsort_int_cmp);
153 for (j = 0, k = 1; k < rscnt; k++) {
154 if (match_length[k] != match_length[j]) {
155 match_length[++j] = match_length[k];
164 dictionary_error dictionary_errno(
void) {
168 void dictionary_perror(
const char* spec) {
172 case DICTIONARY_ERROR_VOID:
174 case DICTIONARY_ERROR_NODICT:
175 perr(_(
"No dictionary loaded"));
177 case DICTIONARY_ERROR_CANNOT_ACCESS_DICTFILE:
178 perror(_(
"Can not open dictionary file"));
180 case DICTIONARY_ERROR_INVALID_DICT:
181 perror(_(
"Invalid dictionary file"));
183 case DICTIONARY_ERROR_INVALID_INDEX:
184 perror(_(
"Invalid dictionary index"));