sbcs.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * sbcs.c - routines to handle single-byte character sets.
  3. */
  4. #include "charset.h"
  5. #include "internal.h"
  6. /*
  7. * The charset_spec for any single-byte character set should
  8. * provide read_sbcs() as its read function, and its `data' field
  9. * should be a wchar_t string constant containing the 256 entries
  10. * of the translation table.
  11. */
  12. void read_sbcs(charset_spec const *charset, long int input_chr,
  13. charset_state *state,
  14. void (*emit)(void *ctx, long int output), void *emitctx)
  15. {
  16. const struct sbcs_data *sd = charset->data;
  17. UNUSEDARG(state);
  18. emit(emitctx, sd->sbcs2ucs[input_chr]);
  19. }
  20. void write_sbcs(charset_spec const *charset, long int input_chr,
  21. charset_state *state,
  22. void (*emit)(void *ctx, long int output), void *emitctx)
  23. {
  24. const struct sbcs_data *sd = charset->data;
  25. int i, j, k, c;
  26. UNUSEDARG(state);
  27. /*
  28. * Binary-search in the ucs2sbcs table.
  29. */
  30. i = -1;
  31. j = sd->nvalid;
  32. while (i+1 < j) {
  33. k = (i+j)/2;
  34. c = sd->ucs2sbcs[k];
  35. if (input_chr < sd->sbcs2ucs[c])
  36. j = k;
  37. else if (input_chr > sd->sbcs2ucs[c])
  38. i = k;
  39. else {
  40. emit(emitctx, c);
  41. return;
  42. }
  43. }
  44. emit(emitctx, ERROR);
  45. }