| 1 |
// Copyright (c) 1998, 1999 Matthias Clasen |
| 2 |
// See the file copying.txt for copying permission. |
| 3 |
|
| 4 |
#include "stylelib.h" |
| 5 |
#include "LangObj.h" |
| 6 |
#include <OpenSP/HashTable.h> |
| 7 |
#include <OpenSP/CharMap.h> |
| 8 |
#include <OpenSP/constant.h> |
| 9 |
#include <math.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#include <ctype.h> |
| 14 |
#include <string.h> |
| 15 |
#include <wchar.h> |
| 16 |
#include <wctype.h> |
| 17 |
|
| 18 |
#ifdef SP_HAVE_LOCALE |
| 19 |
#include <locale.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
#ifdef DSSSL_NAMESPACE |
| 23 |
namespace DSSSL_NAMESPACE { |
| 24 |
#endif |
| 25 |
|
| 26 |
#ifdef SP_HAVE_LOCALE |
| 27 |
static char *strdup(const char *s) |
| 28 |
{ |
| 29 |
size_t l = strlen(s) + 1; |
| 30 |
return (char *) memcpy ((char *) malloc (sizeof (char) * l), s, l); |
| 31 |
} |
| 32 |
|
| 33 |
static char *stringify(const StringC &s) |
| 34 |
{ |
| 35 |
char *r = (char *) malloc (sizeof(char)*(s.size() + 1)); |
| 36 |
for (size_t i = 0; i < s.size(); i++) |
| 37 |
r[i] = char(s[i]); |
| 38 |
r[s.size()] = 0; |
| 39 |
return r; |
| 40 |
} |
| 41 |
|
| 42 |
// FIXME this is unneeded if SP_WCHAR_IS_USHORT |
| 43 |
static wchar_t *wchartify(const StringC &s) |
| 44 |
{ |
| 45 |
wchar_t *r = (wchar_t *) malloc (sizeof(wchar_t)*(s.size() + 1)); |
| 46 |
for (size_t i = 0; i < s.size(); i++) |
| 47 |
r[i] = wchar_t(s[i]); |
| 48 |
r[s.size()] = 0; |
| 49 |
return r; |
| 50 |
} |
| 51 |
|
| 52 |
char *RefLangObj::localeName(const StringC &lang, const StringC &country) |
| 53 |
{ |
| 54 |
char *p; |
| 55 |
|
| 56 |
p = (char *) malloc (sizeof(char)*(lang.size() + country.size() + 2)); |
| 57 |
size_t i; |
| 58 |
for (i = 0; i < lang.size(); i++) |
| 59 |
p[i] = tolower(char(lang[i])); |
| 60 |
p[i++] = '_'; |
| 61 |
for (size_t j = 0; j < country.size(); j++, i++) |
| 62 |
p[i] = toupper(char(country[j])); |
| 63 |
p[i] = 0; |
| 64 |
return p; |
| 65 |
} |
| 66 |
|
| 67 |
bool RefLangObj::supportedLanguage(const StringC &lang, const StringC &country) |
| 68 |
{ |
| 69 |
char *p = RefLangObj::localeName(lang, country); |
| 70 |
char *old = strdup(setlocale(LC_ALL, 0)); |
| 71 |
bool res = (setlocale(LC_ALL, p) != 0); |
| 72 |
setlocale(LC_ALL, old); |
| 73 |
free (p); free (old); |
| 74 |
return res; |
| 75 |
} |
| 76 |
|
| 77 |
RefLangObj::RefLangObj(const StringC &lang, const StringC &country) |
| 78 |
{ |
| 79 |
char *p = localeName(lang, country); |
| 80 |
oldLocale_ = strdup(setlocale(LC_ALL, 0)); |
| 81 |
newLocale_ = strdup(setlocale(LC_ALL, p)); |
| 82 |
free (p); |
| 83 |
} |
| 84 |
|
| 85 |
RefLangObj::~RefLangObj() |
| 86 |
{ |
| 87 |
free (oldLocale_); |
| 88 |
free (newLocale_); |
| 89 |
} |
| 90 |
|
| 91 |
LanguageObj *RefLangObj::asLanguage() |
| 92 |
{ |
| 93 |
return this; |
| 94 |
} |
| 95 |
|
| 96 |
Char RefLangObj::toUpper(const Char c) const |
| 97 |
{ |
| 98 |
setlocale(LC_ALL, newLocale_); |
| 99 |
Char uc = towupper(c); |
| 100 |
setlocale(LC_ALL, oldLocale_); |
| 101 |
return uc; |
| 102 |
} |
| 103 |
|
| 104 |
Char RefLangObj::toLower(const Char c) const |
| 105 |
{ |
| 106 |
setlocale(LC_ALL, newLocale_); |
| 107 |
Char lc = towlower(c); |
| 108 |
setlocale(LC_ALL, oldLocale_); |
| 109 |
return lc; |
| 110 |
} |
| 111 |
|
| 112 |
bool RefLangObj::areEquivalent(const StringC &r, const StringC &s, |
| 113 |
const Char l) const |
| 114 |
{ |
| 115 |
setlocale(LC_ALL, newLocale_); |
| 116 |
wchar_t *rr = wchartify(r); |
| 117 |
unsigned rn = wcsxfrm (0, rr, 0); |
| 118 |
wchar_t *rx = (wchar_t *) malloc (sizeof(wchar_t)*rn); |
| 119 |
wcsxfrm(rx, rr, rn); |
| 120 |
wchar_t *ss = wchartify(s); |
| 121 |
unsigned sn = wcsxfrm (0, ss, 0); |
| 122 |
wchar_t *sx = (wchar_t *) malloc (sizeof(wchar_t)*sn); |
| 123 |
wcsxfrm(sx, ss, sn); |
| 124 |
bool res; |
| 125 |
unsigned k = 0; |
| 126 |
for (unsigned i = 0; ; i++) { |
| 127 |
if (rx[i] != sx[i]) { res = 0; break; } |
| 128 |
if (rx[i] == 1) k++; |
| 129 |
if (k == l || rx[i] == 0) { res = 1; break; } |
| 130 |
} |
| 131 |
free (rr); free (ss); free (rx); free (sx); |
| 132 |
setlocale(LC_ALL, oldLocale_); |
| 133 |
return res; |
| 134 |
} |
| 135 |
|
| 136 |
bool RefLangObj::isLess(const StringC &r, const StringC &s) const |
| 137 |
{ |
| 138 |
setlocale(LC_ALL, newLocale_); |
| 139 |
wchar_t *rr = wchartify(r); |
| 140 |
wchar_t *ss = wchartify(s); |
| 141 |
int res = wcscoll(rr, ss); |
| 142 |
free (rr); free (ss); |
| 143 |
setlocale(LC_ALL, oldLocale_); |
| 144 |
return (res < 0); |
| 145 |
} |
| 146 |
|
| 147 |
bool RefLangObj::isLessOrEqual(const StringC &r, const StringC &s) const |
| 148 |
{ |
| 149 |
setlocale(LC_ALL, newLocale_); |
| 150 |
wchar_t *rr = wchartify(r); |
| 151 |
wchar_t *ss = wchartify(s); |
| 152 |
int res = wcscoll(rr, ss); |
| 153 |
free (rr); free (ss); |
| 154 |
setlocale(LC_ALL, oldLocale_); |
| 155 |
return (res <= 0); |
| 156 |
} |
| 157 |
#endif /* SP_HAVE_LOCALE */ |
| 158 |
|
| 159 |
class LangBuildData { |
| 160 |
public: |
| 161 |
LangBuildData() : currentpos(0) {}; |
| 162 |
HashTable<StringC, StringC> order; |
| 163 |
Char currentpos; |
| 164 |
HashTable<StringC, StringC> ce; |
| 165 |
HashTable<StringC, Char> syms; |
| 166 |
}; |
| 167 |
|
| 168 |
class LangData { |
| 169 |
public: |
| 170 |
LangData(); |
| 171 |
LangObj::LevelSort level[20]; // FIXME |
| 172 |
Char levels; |
| 173 |
HashTable<StringC, StringC> weights; |
| 174 |
HashTable<StringC, Char> collate; |
| 175 |
CharMap<Char> toupper; |
| 176 |
CharMap<Char> tolower; |
| 177 |
}; |
| 178 |
|
| 179 |
LangData::LangData() |
| 180 |
{ |
| 181 |
toupper.setAll(charMax); |
| 182 |
tolower.setAll(charMax); |
| 183 |
levels = 0; |
| 184 |
} |
| 185 |
|
| 186 |
void LangObj::addMultiCollatingElement(const StringC &sym, |
| 187 |
const StringC &str) |
| 188 |
{ |
| 189 |
buildData_->ce.insert(sym, str); |
| 190 |
} |
| 191 |
|
| 192 |
void LangObj::addCollatingSymbol(const StringC &sym) |
| 193 |
{ |
| 194 |
buildData_->syms.insert(sym, charMax); |
| 195 |
} |
| 196 |
|
| 197 |
void LangObj::addLevel(const LevelSort &sort) |
| 198 |
{ |
| 199 |
data_->level[data_->levels++] = sort; |
| 200 |
} |
| 201 |
|
| 202 |
void LangObj::addDefaultPos() |
| 203 |
{ |
| 204 |
StringC empty; |
| 205 |
addCollatingPos(empty); |
| 206 |
} |
| 207 |
|
| 208 |
bool LangObj::addCollatingPos(const StringC &sym) |
| 209 |
{ |
| 210 |
if (!buildData_->ce.lookup(sym) && !buildData_->syms.lookup(sym)) |
| 211 |
if (sym.size() <= 1) |
| 212 |
buildData_->ce.insert(sym, sym); |
| 213 |
else |
| 214 |
return 0; |
| 215 |
buildData_->order.insert(StringC(&buildData_->currentpos, 1), sym); |
| 216 |
buildData_->currentpos++; |
| 217 |
return 1; |
| 218 |
} |
| 219 |
|
| 220 |
bool LangObj::addLevelWeight(const Char l, const StringC &w) |
| 221 |
{ |
| 222 |
if (!buildData_->ce.lookup(w) && !buildData_->syms.lookup(w)) |
| 223 |
if (w.size() <= 1) |
| 224 |
buildData_->ce.insert(w, w); |
| 225 |
else |
| 226 |
return 0; |
| 227 |
StringC key; |
| 228 |
key.resize(3); |
| 229 |
key[0] = buildData_->currentpos - 1; |
| 230 |
key[1] = l; |
| 231 |
for (key[2] = 0; buildData_->order.lookup(key); key[2]++) ; |
| 232 |
buildData_->order.insert(key, w); |
| 233 |
return 1; |
| 234 |
} |
| 235 |
|
| 236 |
void LangObj::addToupper(const Char lc, const Char uc) |
| 237 |
{ |
| 238 |
data_->toupper.setChar(lc, uc); |
| 239 |
} |
| 240 |
|
| 241 |
void LangObj::addTolower(const Char uc, const Char lc) |
| 242 |
{ |
| 243 |
data_->tolower.setChar(uc, lc); |
| 244 |
} |
| 245 |
|
| 246 |
LangObj::LangObj() |
| 247 |
{ |
| 248 |
buildData_ = new LangBuildData; |
| 249 |
data_ = new LangData; |
| 250 |
} |
| 251 |
|
| 252 |
LangObj::~LangObj() |
| 253 |
{ |
| 254 |
if (buildData_) delete buildData_; |
| 255 |
if (data_) delete data_; |
| 256 |
} |
| 257 |
|
| 258 |
bool LangObj::compile() |
| 259 |
{ |
| 260 |
const Char *col; |
| 261 |
StringC key, val, data; |
| 262 |
StringC empty; |
| 263 |
const StringC *match, *match2; |
| 264 |
|
| 265 |
data_->collate.insert(empty, buildData_->currentpos); |
| 266 |
key.resize(1); |
| 267 |
for (key[0] = 0; key[0] < buildData_->currentpos; key[0]++) { |
| 268 |
match = buildData_->order.lookup(key); |
| 269 |
if (match == 0) |
| 270 |
return 0; |
| 271 |
match2 = buildData_->ce.lookup(*match); |
| 272 |
if (match2 == 0) |
| 273 |
buildData_->syms.insert(*match, key[0]); |
| 274 |
else |
| 275 |
data_->collate.insert(*match2, key[0]); |
| 276 |
} |
| 277 |
key.resize(2); |
| 278 |
data.resize(3); |
| 279 |
for (data[0] = 0; data[0] < buildData_->currentpos; data[0]++) { |
| 280 |
key[0] = data[0]; |
| 281 |
for (data[1] = 0; data[1] < levels(); data[1]++) { |
| 282 |
key[1] = data[1]; |
| 283 |
val.resize(0); |
| 284 |
for (data[2] = 0; buildData_->order.lookup(data); data[2]++) { |
| 285 |
match = buildData_->order.lookup(data); |
| 286 |
if (match == 0) |
| 287 |
return 0; |
| 288 |
match2 = buildData_->ce.lookup(*match); |
| 289 |
if (match2 == 0) |
| 290 |
col = buildData_->syms.lookup(*match); |
| 291 |
else |
| 292 |
col = data_->collate.lookup(*match2); |
| 293 |
if (col == 0) |
| 294 |
return 0; |
| 295 |
val += *col; |
| 296 |
} |
| 297 |
data_->weights.insert(key, val); |
| 298 |
} |
| 299 |
} |
| 300 |
delete buildData_; |
| 301 |
buildData_ = 0; |
| 302 |
return 1; |
| 303 |
} |
| 304 |
|
| 305 |
LanguageObj *LangObj::asLanguage() |
| 306 |
{ |
| 307 |
return this; |
| 308 |
} |
| 309 |
|
| 310 |
bool LangObj::areEquivalent(const StringC &r, const StringC &s, |
| 311 |
const Char k) const |
| 312 |
{ |
| 313 |
return (compare(r, s, k) == 0); |
| 314 |
} |
| 315 |
|
| 316 |
int LangObj::compare(const StringC &rr, const StringC &ss, |
| 317 |
const Char k) const |
| 318 |
{ |
| 319 |
StringC rc = asCollatingElts(rr); |
| 320 |
StringC sc = asCollatingElts(ss); |
| 321 |
|
| 322 |
for (Char l = 0; (l < k) && (l < levels()); l++) { |
| 323 |
StringC r = atLevel(rc, l); |
| 324 |
StringC s = atLevel(sc, l); |
| 325 |
for (size_t i = 0; (i < r.size()) || (i < s.size()); i++) { |
| 326 |
if (i == r.size()) return -1; |
| 327 |
if (i == s.size()) return 1; |
| 328 |
if (r[i] < s[i]) return -1; |
| 329 |
if (r[i] > s[i]) return 1; |
| 330 |
} |
| 331 |
} |
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
StringC LangObj::asCollatingElts(const StringC &s) const |
| 336 |
{ |
| 337 |
StringC res, key, empty; |
| 338 |
unsigned i, j; |
| 339 |
int col; |
| 340 |
const Char *c, *def; |
| 341 |
Char def_val; |
| 342 |
|
| 343 |
def = data_->collate.lookup(empty); |
| 344 |
def_val = (def == 0) ? charMax : *def; |
| 345 |
for (i = 0; i < s.size(); i = j) { |
| 346 |
col = def_val; |
| 347 |
key = empty; |
| 348 |
for (j = i; j < s.size(); j++) { |
| 349 |
key += s[j]; |
| 350 |
c = data_->collate.lookup(key); |
| 351 |
if (c == 0) |
| 352 |
break; |
| 353 |
col = *c; |
| 354 |
} |
| 355 |
if (i == j) |
| 356 |
// if we get here, s[j] is a single `unknown' char and |
| 357 |
// we better not reconsider it to avoid an infinite loop. |
| 358 |
j++; |
| 359 |
res += col; |
| 360 |
} |
| 361 |
return res; |
| 362 |
} |
| 363 |
|
| 364 |
StringC LangObj::atLevel(const StringC &s, const Char l) const |
| 365 |
{ |
| 366 |
StringC cols, res, key; |
| 367 |
const StringC *w; |
| 368 |
|
| 369 |
if (data_->level[l].backward) |
| 370 |
for (int i = s.size() - 1; i >= 0; i--) |
| 371 |
cols += s[i]; |
| 372 |
else |
| 373 |
cols = s; |
| 374 |
key.resize(2); |
| 375 |
key[1] = l; |
| 376 |
for (size_t i = 0; i < cols.size(); i++) { |
| 377 |
key[0] = cols[i]; |
| 378 |
w = data_->weights.lookup(key); |
| 379 |
if (w == 0) |
| 380 |
return res; |
| 381 |
if (data_->level[l].backward) |
| 382 |
for (int j = w->size() - 1; j >= 0; j--) { |
| 383 |
if (data_->level[l].position) |
| 384 |
res += i; |
| 385 |
res += (*w)[j]; |
| 386 |
} |
| 387 |
else // forward |
| 388 |
for (int j = 0; j < w->size(); j++) { |
| 389 |
if (data_->level[l].position) |
| 390 |
res += i; |
| 391 |
res += (*w)[j]; |
| 392 |
} |
| 393 |
} |
| 394 |
return res; |
| 395 |
} |
| 396 |
|
| 397 |
Char LangObj::toUpper(const Char c) const |
| 398 |
{ |
| 399 |
Char uc = data_->toupper[c]; |
| 400 |
return (uc == charMax) ? c : uc; |
| 401 |
} |
| 402 |
|
| 403 |
Char LangObj::toLower(const Char c) const |
| 404 |
{ |
| 405 |
Char lc = data_->tolower[c]; |
| 406 |
return (lc == charMax) ? c : lc; |
| 407 |
} |
| 408 |
|
| 409 |
unsigned LangObj::levels() const |
| 410 |
{ |
| 411 |
return data_->levels; |
| 412 |
} |
| 413 |
|
| 414 |
bool LangObj::isLess(const StringC &r, const StringC &s) const |
| 415 |
{ |
| 416 |
return (compare(r, s, levels()) < 0); |
| 417 |
} |
| 418 |
|
| 419 |
bool LangObj::isLessOrEqual(const StringC &r, const StringC &s) const |
| 420 |
{ |
| 421 |
return (compare(r, s, levels()) <= 0); |
| 422 |
} |
| 423 |
|
| 424 |
#ifdef DSSSL_NAMESPACE |
| 425 |
} |
| 426 |
#endif |
| 427 |
|
| 428 |
/* |
| 429 |
|
| 430 |
Notes on the LangObj class: |
| 431 |
|
| 432 |
All data is in the classes LanguageData and |
| 433 |
LanguageBuildData. The LanguageBuildData is only |
| 434 |
needed until we call LanguageObj::compile() and |
| 435 |
is freed afterwards. |
| 436 |
|
| 437 |
Once a LangObj is built, we use the functions isLess(), |
| 438 |
isLessOrEqual(), toUpper(), toLower() and areEquivalent() |
| 439 |
to implement the language-dependent functions of the |
| 440 |
expression language. |
| 441 |
|
| 442 |
The LangBuildData stores a map symbols --> strings |
| 443 |
for multi-collating-elements (ce) and a second map |
| 444 |
positions --> symbols (order). These are incrementally |
| 445 |
filled up while parsing a define-language expression. |
| 446 |
order also stores a map |
| 447 |
|
| 448 |
positions x levels x weight-numbers --> weights |
| 449 |
|
| 450 |
(there may be more than one weight for a given position |
| 451 |
and level). The empty string is used as key for the |
| 452 |
default entry in syms. |
| 453 |
|
| 454 |
Compilation is done in two phases: In the first phase, |
| 455 |
we build the syms and collate maps (syms maps |
| 456 |
symbols --> positions and collate maps |
| 457 |
multi-collating-elements --> positions). This is achived |
| 458 |
by inverting the corresponding part of order. |
| 459 |
|
| 460 |
In the next phase, the weights map is built. It maps |
| 461 |
positions x levels x weight-numbers --> positions. |
| 462 |
|
| 463 |
Comparing two string works in three phases: First, |
| 464 |
the strings are converted to positions (done by |
| 465 |
asCollatingElts()), then we are comparing one level |
| 466 |
at a time. For this, the strings of positions are |
| 467 |
converted into strings of weights. How this is done |
| 468 |
depends on the level. |
| 469 |
Finally the strings of weights are lexicographically |
| 470 |
compared. |
| 471 |
|
| 472 |
Notes on the RefLangObj class: |
| 473 |
|
| 474 |
It uses the underlying POSIX locale system and wchar_t. At least |
| 475 |
with GNU libc, wchar_t is always encoded as ISO-10646 UCS4, thus |
| 476 |
the Char and wchar_t codes of a character have the same value. But |
| 477 |
since sizeof(wchar_t) = 4 != 2 = sizeof(Char), we have to do ugly |
| 478 |
conversions (at least on i386-linux-glibc2) to be able to use the |
| 479 |
libc wide character routines wcscoll() and wcsxfrm(). |
| 480 |
|
| 481 |
To create a language by reference to a locale, use the external |
| 482 |
procedure with pubid UNREGISTERED::OpenJade//Procedure::language |
| 483 |
|
| 484 |
(language lang country) |
| 485 |
|
| 486 |
where lang and country are symbols or strings like the ones used |
| 487 |
for the language: and country: characteristics of the paragraph FO. |
| 488 |
If the locale is not supported, language returns #f. If Jade is |
| 489 |
compiled without locale support (!SP_HAVE_LOCALE) language *always* |
| 490 |
returns #f. |
| 491 |
|
| 492 |
The implementation of string-equiv? depends on the fact that |
| 493 |
wcsxfrm() returns a 0-terminated string of integers where the |
| 494 |
substrings for each level are separated by 1. This is true for |
| 495 |
glibc, but I don't know if it is universally true. |
| 496 |
|
| 497 |
*/ |