summaryrefslogtreecommitdiffstats
path: root/src/lng.c
blob: df9b2e952913a2acf3a3e1c697bc05779172cc22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
  LibRCC - base module for language manipulations

  Copyright (C) 2005-2008 Suren A. Chilingaryan <csa@dside.dyndns.org>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License version 2.1 or later
  as published by the Free Software Foundation.

  This library is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License 
  for more details.

  You should have received a copy of the GNU Lesser General Public License 
  along with this program; if not, write to the Free Software Foundation, Inc.,
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

#include <stdio.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif /* HAVE_STRINGS_H */

#include "internal.h"
#include "rccconfig.h"
#include "rcclocale.h"

int rccGetLanguageNumber(rcc_context ctx) {
    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return 0;
    }

    return ctx->n_languages;
}

int rccGetClassNumber(rcc_context ctx) {
    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return 0;
    }

    return ctx->n_classes;
}

rcc_language_ptr rccGetLanguagePointer(rcc_context ctx, rcc_language_id language_id) {
    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return NULL;
    }
    if (language_id>=ctx->n_languages) return NULL;
    return ctx->languages[language_id];
}

const char *rccGetLanguageName(rcc_context ctx, rcc_language_id language_id) {
    rcc_language_ptr language;
    language = rccGetLanguagePointer(ctx, language_id);
    if (!language) return NULL;
    return language->sn;
}

rcc_language_id rccGetLanguageByName(rcc_context ctx, const char *name) {
    unsigned int i;

    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return (rcc_language_id)-1;
    }
    if (!name) return (rcc_language_id)-1;

    for (i=0;ctx->languages[i];i++)
	if (!strcasecmp(ctx->languages[i]->sn, name)) return (rcc_language_id)i;

    return (rcc_language_id)-1;
}

int rccCheckLanguageUsability(rcc_context ctx, rcc_language_id language_id) {
    rcc_language_config config;
    rcc_option_value clo;
    rcc_engine_ptr *engines;
    rcc_charset *charsets;

    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return 0;
    }
    if (language_id>=ctx->n_languages) return 0;

    language_id = rccGetRealLanguage(ctx, language_id);
    
    clo = rccGetOption(ctx, RCC_OPTION_CONFIGURED_LANGUAGES_ONLY);
    if (clo) {
	config = rccCheckConfig(ctx, (rcc_language_id)language_id);
	if ((!config)||(!config->configured)) {
	    charsets = ctx->languages[language_id]->charsets;
	    if ((charsets[0])&&(charsets[1])&&(charsets[2])) {
		if (clo == 1) {
		    engines = ctx->languages[language_id]->engines;
		    if ((!engines[0])||(!engines[1])) return 0;
		} else return 0;
	    }
	}
    }
    return 1;
}


static rcc_language_id rccGetDefaultLanguage(rcc_context ctx) {
    unsigned int i;
    char stmp[RCC_MAX_LANGUAGE_CHARS+1];

    if (ctx->default_language) return ctx->default_language;
    
    if (!rccLocaleGetLanguage(stmp, ctx->locale_variable, RCC_MAX_LANGUAGE_CHARS)) {
    	for (i=0;ctx->languages[i];i++) {
	    if (!strcmp(ctx->languages[i]->sn, stmp)) {
		if (!rccCheckLanguageUsability(ctx, (rcc_language_id)i)) break;
		ctx->default_language = (rcc_language_id)i;
		return (rcc_language_id)i;
	    }
	}
    }
    
    if (ctx->n_languages>1) return (rcc_language_id)1;
    return (rcc_language_id)-1;
}

rcc_language_id rccGetRealLanguage(rcc_context ctx, rcc_language_id language_id) {
    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return (rcc_language_id)-1;
    }
    if (language_id>=ctx->n_languages) return (rcc_language_id)-1;

    if (language_id) return language_id;
    return rccGetDefaultLanguage(ctx);    
}

const char *rccGetRealLanguageName(rcc_context ctx, rcc_language_id language_id) {
    language_id = rccGetRealLanguage(ctx, language_id);
    if (language_id == (rcc_language_id)-1) return NULL;
    
    return rccGetLanguageName(ctx, language_id);
}

rcc_language_id rccGetSelectedLanguage(rcc_context ctx) {
    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return (rcc_language_id)-1;
    }

    return ctx->current_language;
}

const char *rccGetSelectedLanguageName(rcc_context ctx) {
    rcc_language_id language_id;
    
    language_id = rccGetSelectedLanguage(ctx);
    if (language_id == (rcc_language_id)-1) return NULL;
    
    return rccGetLanguageName(ctx, language_id);
}

rcc_language_id rccGetCurrentLanguage(rcc_context ctx) {
    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return (rcc_language_id)-1;
    }
    
    return rccGetRealLanguage(ctx, ctx->current_language);    
}

const char *rccGetCurrentLanguageName(rcc_context ctx) {
    rcc_language_id language_id;
    
    language_id = rccGetCurrentLanguage(ctx);
    if (language_id == (rcc_language_id)-1) return NULL;
    
    return rccGetLanguageName(ctx, language_id);
}


int rccSetLanguage(rcc_context ctx, rcc_language_id language_id) {
    rcc_language_config config;

    if (!ctx) {
	if (rcc_default_ctx) ctx = rcc_default_ctx;
	else return -1;
    }
    
    if (language_id >= ctx->n_languages) return -1;
    if ((!ctx->languages[language_id]->engines[0])||(!ctx->languages[language_id]->charsets[0])) return -2;

    if (ctx->current_language != language_id) {
	config = rccGetConfig(ctx, language_id);
	// NULL is Okey (Off), if (!config) return -1;
	
	rccMutexLock(ctx->mutex);
	ctx->configure = 1;
	ctx->current_language = language_id;
	ctx->current_config = config; 
	rccMutexUnLock(ctx->mutex);
    }

    return 0;
}

int rccSetLanguageByName(rcc_context ctx, const char *name) {
    rcc_language_id language_id;
    
    language_id = rccGetLanguageByName(ctx, name);
    if (language_id == (rcc_language_id)-1) return -1;
    
    return rccSetLanguage(ctx, language_id);
}