latin1.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. /*
  11. * The code makes two assumptions: strlen(ld) is 1 or 2; latintab[i].ld can be a
  12. * prefix of latintab[j].ld only when j<i.
  13. */
  14. struct cvlist
  15. {
  16. char *ld; /* must be seen before using this conversion */
  17. char *si; /* options for last input characters */
  18. int32_t *so; /* the corresponding Rune for each si entry */
  19. } latintab[] = {
  20. #include "../port/latin1.h"
  21. {nil, nil, nil}
  22. };
  23. /*
  24. * Given 5 characters k[0]..k[4], find the rune or return -1 for failure.
  25. */
  26. int32_t
  27. unicode(Rune *k)
  28. {
  29. int32_t i, c;
  30. k++; /* skip 'X' */
  31. c = 0;
  32. for(i=0; i<4; i++,k++){
  33. c <<= 4;
  34. if('0'<=*k && *k<='9')
  35. c += *k-'0';
  36. else if('a'<=*k && *k<='f')
  37. c += 10 + *k-'a';
  38. else if('A'<=*k && *k<='F')
  39. c += 10 + *k-'A';
  40. else
  41. return -1;
  42. }
  43. return c;
  44. }
  45. /*
  46. * Given n characters k[0]..k[n-1], find the corresponding rune or return -1 for
  47. * failure, or something < -1 if n is too small. In the latter case, the result
  48. * is minus the required n.
  49. */
  50. int32_t
  51. latin1(Rune *k, int n)
  52. {
  53. struct cvlist *l;
  54. int c;
  55. char* p;
  56. if(k[0] == 'X'){
  57. if(n>=5)
  58. return unicode(k);
  59. else
  60. return -5;
  61. }
  62. for(l=latintab; l->ld!=0; l++)
  63. if(k[0] == l->ld[0]){
  64. if(n == 1)
  65. return -2;
  66. if(l->ld[1] == 0)
  67. c = k[1];
  68. else if(l->ld[1] != k[1])
  69. continue;
  70. else if(n == 2)
  71. return -3;
  72. else
  73. c = k[2];
  74. for(p=l->si; *p!=0; p++)
  75. if(*p == c)
  76. return l->so[p - l->si];
  77. return -1;
  78. }
  79. return -1;
  80. }