home *** CD-ROM | disk | FTP | other *** search
/ Phoenix Heaven Sunny 2 / APPARE2.BIN / oh_towns / dic / src / lib.c < prev    next >
C/C++ Source or Header  |  1995-06-20  |  11KB  |  445 lines

  1. /********************************************
  2.  
  3.     Low Lib Func
  4.  
  5. *********************************************/
  6. #include    "defs.h"
  7.  
  8. static ushort ank_tbl[]={
  9.         0x8140,0x8149,0x8168,0x8194,0x8190,0x8193,0x8195,0x8166,
  10.         0x8169,0x816A,0x8196,0x817B,0x8143,0x817C,0x8144,0x815E,
  11.         0x824F,0x8250,0x8251,0x8252,0x8253,0x8254,0x8255,0x8256,
  12.         0x8257,0x8258,0x8146,0x8147,0x8183,0x8181,0x8184,0x8148,
  13.         0x8197,0x8260,0x8261,0x8262,0x8263,0x8264,0x8265,0x8266,
  14.     0x8267,0x8268,0x8269,0x826A,0x826B,0x826C,0x826D,0x826E,
  15.         0x826F,0x8270,0x8271,0x8272,0x8273,0x8274,0x8275,0x8276,
  16.         0x8277,0x8278,0x8279,0x816D,0x818F,0x816E,0x814F,0x8151,
  17.         0x8166,0x8281,0x8282,0x8283,0x8284,0x8285,0x8286,0x8287,
  18.         0x8288,0x8289,0x828A,0x828B,0x828C,0x828D,0x828E,0x828F,
  19.         0x8290,0x8291,0x8292,0x8293,0x8294,0x8295,0x8296,0x8297,
  20.         0x8298,0x8299,0x829A,0x816F,0x8162,0x8170,0x8150,0x85A1 };
  21. #ifdef    SJIS
  22. static ushort kana_tbl[]={
  23.         0x8140,0x8142,0x8175,0x8176,0x8141,0x8145,0x8392,0x8340,
  24.         0x8342,0x8344,0x8346,0x8348,0x8383,0x8385,0x8387,0x8362,
  25.         0x815B,0x8341,0x8343,0x8345,0x8347,0x8349,0x834A,0x834C,
  26.         0x834E,0x8350,0x8352,0x8354,0x8356,0x8358,0x835A,0x835C,
  27.         0x835E,0x8360,0x8363,0x8365,0x8367,0x8369,0x836A,0x836B,
  28.         0x836C,0x836D,0x836E,0x8371,0x8374,0x8377,0x837A,0x837D,
  29.         0x837E,0x8380,0x8381,0x8382,0x8384,0x8386,0x8388,0x8389,
  30.         0x838A,0x838B,0x838C,0x838D,0x838F,0x8393,0x814A,0x814B };
  31. #endif    /* SJIS */
  32.  
  33. /********************************************
  34.  
  35.     short & long Byte Swaping
  36.  
  37. *********************************************/
  38. int    toshort(uchar *p)
  39. {
  40.     int     n;
  41.  
  42.     n = *(p++) << 8;
  43.     n |= *p;
  44.     return n;
  45. }
  46. long    tolong(uchar *p)
  47. {
  48.     long    l;
  49.  
  50.     l  = ((long)(*(p++)) << 24);
  51.     l |= ((long)(*(p++)) << 16);
  52.     l |= ((long)(*(p++)) << 8);
  53.     l |= *p;
  54.     return l;
  55. }
  56. int    iszero(uchar *p, int len)
  57. {
  58.     while ( len-- > 0 && *(p++) == '\0' )
  59.     ;
  60.     return (len <= 0 ? TRUE:FALSE);
  61. }
  62. int    htoi(char *p)
  63. {
  64.     int     n = 0;
  65.     int     ch;
  66.  
  67.     while ( (ch = *(p++)) != '\0' ) {
  68.     ch = toupper(ch);
  69.     if ( ch >= '0' && ch <= '9' )
  70.         n = n * 16 + (ch - '0');
  71.     else if ( ch >= 'A' && ch <= 'F' )
  72.         n = n * 16 + (ch - 'A') + 10;
  73.     else
  74.         break;
  75.     }
  76.     return n;
  77. }
  78. /********************************************
  79.  
  80.     Kanji Code Conv Func
  81.  
  82. *********************************************/
  83. int    sjistojis(int cd)
  84. {
  85.     int    hi,lo;
  86.  
  87.     if ( (cd & 0x8000) == 0 )
  88.     return cd;
  89.  
  90.     hi = (cd >> 8) & 0xff;
  91.     lo = cd & 0xff;
  92.     hi -= (hi <= 0x9f ? 0x71 : 0xb1);
  93.     hi = hi * 2 +1;
  94.     if ( lo > 0x7f )
  95.         lo--;
  96.     if ( lo >= 0x9e ) {
  97.         lo -= 0x7d;
  98.         hi++;
  99.     }
  100.     else
  101.         lo -= 0x1f;
  102.     return (hi << 8 | lo);
  103. }
  104. int     jistosjis(int cd)
  105. {
  106.     int    hi,lo;
  107.  
  108.     hi = (cd >> 8) & 0xff;
  109.     lo = cd & 0xff;
  110.     if ( (hi & 1) != 0 )
  111.         lo += 0x1F;
  112.     else
  113.         lo += 0x7D;
  114.     if ( lo >= 0x7F )
  115.         lo++;
  116.     hi = (hi - 0x21 >> 1) + 0x81;
  117.     if ( hi > 0x9F )
  118.         hi += 0x40;
  119.     return (hi << 8 | lo);
  120. }
  121. #ifdef    EUC
  122. int    jistoeuc(int ch)
  123. {
  124.     return (ch | 0x8080);
  125. }
  126. #endif
  127. int    hantozen(int code)
  128. {
  129.     if ( 0x20 <= code && code <= 0x7F )
  130.         return ank_tbl[code - 0x20];
  131. #ifdef    SJIS
  132.     else if ( 0xA0 <= code && code <= 0xDF )
  133.     return kana_tbl[code - 0xA0];
  134. #endif    /* SJIS */
  135.     else
  136.         return 0x8140;
  137. }
  138. int    zentohan(int ch)
  139. {
  140.     int     n;
  141. #ifdef    SJIS
  142.     static ushort hira_tab[] = {
  143.     0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x82F0,0x829F,
  144.     0x82A1,0x82A3,0x82A5,0x82A7,0x82E1,0x82E3,0x82E5,0x82C1,
  145.     0x0000,0x82A0,0x82A2,0x82A4,0x82A6,0x82A8,0x82A9,0x82AB,
  146.     0x82AD,0x82AF,0x82B1,0x82B3,0x82B5,0x82B7,0x82B9,0x82BB,
  147.     0x82BD,0x82BF,0x82C2,0x82C4,0x82C6,0x82C8,0x82C9,0x82CA,
  148.     0x82CB,0x82CC,0x82CD,0x82D0,0x82D3,0x82D6,0x82D9,0x82DC,
  149.     0x82DD,0x82DE,0x82DF,0x82E0,0x82E2,0x82E4,0x82E6,0x82E7,
  150.     0x82E8,0x82E9,0x82EA,0x82EB,0x82ED,0x82F1,0x0000,0x0000
  151.     };
  152. #endif    /* SJIS */
  153.     static struct {
  154.     ushort        kanji, ank;
  155.     } *cp, cnv_tab[] = {
  156. #ifdef    SJIS
  157.     { 0x82AA, 0xB6DE },    { 0x82AC, 0xB7DE },    { 0x82AE, 0xB8DE },
  158.     { 0x82B0, 0xB9DE },    { 0x82B2, 0xBADE },    { 0x82B4, 0xBBDE },
  159.     { 0x82B6, 0xBCDE },    { 0x82B8, 0xBDDE },    { 0x82BA, 0xBEDE },
  160.     { 0x82BC, 0xBFDE },    { 0x82BE, 0xC0DE },    { 0x82C0, 0xC1DE },
  161.     { 0x82C3, 0xC2DE },    { 0x82C5, 0xC3DE },    { 0x82C7, 0xC4DE },
  162.     { 0x82CE, 0xCADE },    { 0x82D1, 0xCBDE },    { 0x82D4, 0xCCDE },
  163.     { 0x82D7, 0xCCDE },    { 0x82DA, 0xCEDE },    { 0x82F2, 0xDDDE },
  164.     { 0x834B, 0xB6DE },    { 0x834D, 0xB7DE },    { 0x834F, 0xB8DE },
  165.     { 0x8351, 0xB9DE },    { 0x8353, 0xBADE },    { 0x8355, 0xBBDE },
  166.     { 0x8357, 0xBCDE },    { 0x8359, 0xBDDE },    { 0x835B, 0xBEDE },
  167.     { 0x835D, 0xBFDE },    { 0x835F, 0xC0DE },    { 0x8361, 0xC1DE },
  168.     { 0x8364, 0xC2DE },    { 0x8366, 0xC3DE },    { 0x8368, 0xC4DE },
  169.     { 0x836F, 0xCADE },    { 0x8372, 0xCBDE },    { 0x8375, 0xCCDE },
  170.     { 0x8378, 0xCDDE },    { 0x837B, 0xCEDE },    { 0x8394, 0xDDDE },
  171.     { 0x82CF, 0xCADF },    { 0x82D2, 0xCBDF },    { 0x82D5, 0xCCDF },
  172.     { 0x82D8, 0xCDDF },    { 0x82DB, 0xCEDF },    { 0x8370, 0xCADF },
  173.     { 0x8373, 0xCBDF },    { 0x8376, 0xCCDF },    { 0x8379, 0xCDDF },
  174.     { 0x837C, 0xCEDF },
  175. #endif    /* SJIS */
  176.     { 0x8160, 0x002D },    { 0x815C, 0x002D },    { 0x815D, 0x002D },
  177.     { 0x0000, 0x0000 }
  178.     };
  179.  
  180.     for ( n = 0x20 ; n <= 0x7F ; n++ ) {
  181.     if ( ank_tbl[n - 0x20] == ch )
  182.         return n;
  183.     }
  184. #ifdef    SJIS
  185.     for ( n = 0xA0 ; n <= 0xDF ; n++ ) {
  186.     if ( kana_tbl[n - 0xA0] == ch )
  187.         return n;
  188.     }
  189.     for ( n = 0xA0 ; n <= 0xDF ; n++ ) {
  190.     if ( hira_tab[n - 0xA0] == ch )
  191.         return n;
  192.     }
  193. #endif    /* SJIS */
  194.     cp = cnv_tab;
  195.     while ( cp->kanji != 0 ) {
  196.     if ( cp->kanji == ch )
  197.         return cp->ank;
  198.     cp++;
  199.     }
  200.  
  201.     return ch;
  202. }
  203. void    str_cnv(uchar *buf, uchar *str, int len)
  204. {
  205.     int     ch;
  206.  
  207.     while ( len > 0 ) {
  208.     ch  = *(str++) << 8;
  209.     ch |= *(str++);
  210.     if ( ch == 0 || ch < 0x2121 || ch > 0x7F7F )
  211.         break;
  212. #ifdef    SJIS
  213.     ch = jistosjis(ch);
  214. #endif    /* SJIS */
  215.  
  216. #ifdef    EUC
  217.     ch = jistoeuc(ch);
  218. #endif
  219.     *(buf++) = ch >> 8;
  220.     *(buf++) = ch & 0xff;
  221.     len -= 2;
  222.     }
  223.     *(buf++) = '\0';
  224.     *(buf++) = '\0';
  225. }
  226. void    put_jis(uchar *str, int len)
  227. {
  228.     int     ch;
  229.  
  230.     while ( len > 0 ) {
  231.     ch = *(str++) << 8;
  232.     ch |= *(str++);
  233.     if ( ch == 0 || ch < 0x2121 || ch > 0x7F7F )
  234.         break;
  235. #ifdef    SJIS
  236.     ch = jistosjis(ch);
  237. #endif    /* SJIS */
  238.  
  239. #ifdef    EUC
  240.     ch = jistoeuc(ch);
  241. #endif
  242.     putchar(ch >> 8);
  243.     putchar(ch & 0xff);
  244.     len -= 2;
  245.     }
  246. }
  247. uchar    *str_jis(uchar *buf, uchar *str)
  248. {
  249.     int     ch;
  250.     int     len = 0;
  251.  
  252.     while ( len < COLS_MAX && (ch = *(str++)) != '\0' ) {
  253. #ifdef    SJIS
  254.     if ( iskanji(ch) && iskanji2(*str) )
  255.         ch = (ch << 8) | *(str++);
  256.     else
  257.         ch = hantozen(ch);
  258.  
  259.     ch = sjistojis(ch);
  260. #endif    /* SJIS */
  261.  
  262. #ifdef    EUC
  263.     if ( (ch & 0x80) && (*str & 0x80) )
  264.         ch = ((ch << 8) | *(str++)) & 0x7F7F;
  265.     else
  266.         ch = sjistojis(hantozen(ch));
  267. #endif    /* EUC */
  268.  
  269. /****************
  270.     if ( ch == 0x2121 )
  271.         continue;
  272. *****************/
  273.  
  274.     buf[len++] = (uchar)(ch >> 8);
  275.     buf[len++] = (uchar)(ch & 0xFF);
  276.     }
  277.  
  278.     buf[len++] = '\0';
  279.     buf[len++] = '\0';
  280.  
  281.     return buf;
  282. }
  283. uchar    *str_word(uchar *buf, uchar *str)
  284. {
  285.     int     ch;
  286.  
  287.     for ( ; ; ) {
  288.     ch = *str << 8;
  289.     ch |= *(str + 1);
  290.     if ( ch != 0x2121 && ch != 0x2161 )    /* = */
  291.         break;
  292.     str += 2;
  293.     }
  294.  
  295.     switch(ch) {
  296.     case 0x215C:        /* + */
  297.     case 0x2177:        /* @ */
  298.     *(buf++) = *(str++);
  299.     *(buf++) = *(str++);
  300.     break;
  301.     }
  302.  
  303.     for ( ; ; ) {
  304.     ch = *str << 8;
  305.     ch |= *(str + 1);
  306.     if ( ch == 0x0000 || ch == 0x2121 || ch == 0x2161 ||
  307.          ch == 0x215C || ch == 0x2177 )
  308.         break;
  309.     *(buf++) = *(str++);
  310.     *(buf++) = *(str++);
  311.     }
  312.  
  313.     *(buf++) = '\0';
  314.     *(buf++) = '\0';
  315.  
  316.     for ( ; ; ) {
  317.     ch = *str << 8;
  318.     ch |= *(str + 1);
  319.     if ( ch != 0x2121 && ch != 0x2161 )
  320.         break;
  321.     str += 2;
  322.     }
  323.  
  324.     return str;
  325. }
  326. char    *strlow(char *str)
  327. {
  328.     register char *p;
  329.  
  330.     for ( p = str ; *p != '\0' ; p++ )
  331.     *p = tolower(*p);
  332.     return str;
  333. }
  334. char    *skipspc(char *str)
  335. {
  336.     while ( isspace((uchar)*str) || *str == ',' )
  337.     str++;
  338.     return str;
  339. }
  340. char    *skipword(char *str)
  341. {
  342.     while ( !isspace((uchar)*str) && 
  343.         *str != ',' && *str != '\n' && *str != '\0' )
  344.     str++;
  345.     return str;
  346. }
  347. int    strcut(int max, char *av[], char *str)
  348. {
  349.     int    ac = 0;
  350.  
  351.     while ( ac < max && *str != '\0' ) {
  352.     str = skipspc(str);
  353.     if ( *str == '#' )
  354.         break;
  355.     else if ( *str == '\"' ) {
  356.         av[ac++] = ++str;
  357.         while ( *str != '\"' && *str != '\0' )
  358.         str++;
  359.         if ( *str != '\"' )
  360.         break;
  361.         *(str++) = '\0';
  362.     } else if ( *str != '\n' && *str != '\0' ) {
  363.         av[ac++] = str;
  364.         str = skipword(str);
  365.         if ( *str != '\0' )
  366.         *(str++) = '\0';
  367.     }
  368.     }
  369.     return ac;
  370. }
  371. #ifdef    LSIC
  372. char    *strdup(char *str)
  373. {
  374.     register char *p;
  375.  
  376.     if ( (p = (char *)malloc(strlen(str) + 1)) != NULL )
  377.     strcpy(p, str);
  378.     return p;
  379. }
  380. #endif
  381.  
  382. static    struct    {
  383.     char    len;
  384.     char    *base;
  385.     char    *conv[4];
  386.     } gobi_tab[] = {
  387.         {    3,  "ies",    "ie",    "i",    "y",    NULL    },
  388.         {    2,  "es",    "e",    "",    NULL,    NULL    },
  389.         {    1,  "s",    "",    NULL,    NULL,    NULL    },
  390.         {    3,  "ied",    "ie",    "i",    "y",    NULL    },
  391.         {    2,  "ed",    "e",    "",    NULL,    NULL    },
  392.         {    4,  "iest",    "ie",    "i",    "y",    NULL    },
  393.         {    2,  "st",    "",    NULL,    NULL,    NULL    },
  394.         {    3,  "ier",    "ie",    "i",    "y",    NULL    },
  395.         {    2,  "er",    "e",    "",    NULL,    NULL    },
  396.         {    1,  "r",    "",    NULL,    NULL,    NULL    },
  397.         {    3,  "ing",    "",    "e",    NULL,    NULL    },
  398.         {    2,  "ly",    "",    "le",    NULL,    NULL    },
  399.         {    0,  NULL,    NULL,    NULL,    NULL,    NULL    }
  400.     };
  401. static    char    gobi_tmp[COLS_MAX + 2];
  402. static    char    gobi_buf[COLS_MAX + 2];
  403. static    char    *gobi_base = NULL;
  404. static    char    **gobi_pos = NULL;
  405.  
  406. char    *gobi_conv()
  407. {
  408.     if ( gobi_pos == NULL || *gobi_pos == NULL )
  409.     return NULL;
  410.     strcpy(gobi_base, *(gobi_pos++));
  411.     return str_jis(gobi_buf, gobi_tmp);
  412. }
  413. int    gobi_init(uchar *str)
  414. {
  415.     int ch, len, n;
  416.     char *p;
  417.  
  418.     for ( len = 0 ; *str != '\0' && len < COLS_MAX ; ) {
  419.     ch = (*(str++) << 8);
  420.     ch |= *(str++);
  421.  
  422.     if ( ch >= 0x2341 && ch <= 0x235A )
  423.         gobi_tmp[len++] = ch - 0x2341 + 'a';
  424.     else if ( ch >= 0x2361 && ch <= 0x237A )
  425.         gobi_tmp[len++] = ch - 0x2361 + 'a';
  426.     else {
  427.         gobi_tmp[len++] = ch >> 8;
  428.         gobi_tmp[len++] = (char)ch;
  429.     }
  430.     }
  431.     gobi_tmp[len] = '\0';
  432.     p = &(gobi_tmp[len]);
  433.  
  434.     for ( n = 0 ; gobi_tab[n].len > 0 ; n++ ) {
  435.     if ( len > gobi_tab[n].len &&
  436.         strcmp(gobi_tab[n].base, p - gobi_tab[n].len) == 0 ) {
  437.         gobi_pos = gobi_tab[n].conv;
  438.         gobi_base = p - gobi_tab[n].len;
  439.         return TRUE;
  440.     }
  441.     }
  442.  
  443.     return FALSE;
  444. }
  445.