Back to index

im-sdk  12.3.91
converter.c
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2003 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 #include <stdio.h>
00044 
00045 #define MAX_CHAR_LEN        4
00046 
00047 int convert_to_hex_code(char high_code, char low_code)
00048 {
00049        unsigned char high, low;
00050 
00051        high = toupper(high_code);
00052        if (high >= '0' && high <= '9') {
00053               high = high - '0' + 0x8;
00054        } else if (high >= 'A' && high <= 'F') {
00055               high = high - 'A' + 0xa;
00056        }
00057        high <<= 4;
00058 
00059        low = toupper(low_code);
00060        if (low >= '0' && low <= '9') {
00061               low = low - '0' + 0x0;
00062        } else if (low >= 'A' && low <= 'F') {
00063               low = low - 'A' + 0xa;
00064        }
00065 
00066        return((high | low) & 0x00ff);
00067 }
00068 
00069 int convert_to_cns11643_code(unsigned char *qu_str, unsigned char *hex_str)
00070 {
00071        int qu_code;
00072        int cur_pos = 0;
00073 
00074        qu_code = toupper(qu_str[0]);
00075        if (qu_code >= '2' && qu_code <= '7') {
00076               hex_str[cur_pos++] = 0x8e;
00077               hex_str[cur_pos++] = 0xa1 + (qu_code - '1');
00078        } else if (qu_code >= 'A' && qu_code <= 'F') {
00079               hex_str[cur_pos++] = 0x8e;
00080               hex_str[cur_pos++] = 0xaa + (qu_code - 'A');
00081        }
00082        hex_str[cur_pos++] = convert_to_hex_code(qu_str[1], qu_str[2]);
00083        hex_str[cur_pos++] = convert_to_hex_code(qu_str[3], qu_str[4]);
00084        hex_str[cur_pos] = 0;
00085 
00086        return(0);
00087 }
00088 
00089 int  main(int argc, char **argv)
00090 {
00091        char            *file_name; 
00092        char          line[256];
00093        unsigned char value1[256], value2[256], hex_str[5];
00094     
00095        register char        *ptr;
00096        FILE          *fd;
00097        int           i;
00098 
00099        if (argc != 2) {
00100               printf("usage: %s file_name\n", argv[0]);
00101               exit(0);
00102        }
00103 
00104        /* read profile from file to memory buffer  */ 
00105        file_name = argv[1];
00106 
00107        fd = fopen(file_name, "r");
00108        if (! fd)
00109               return -1;
00110 
00111        while (fgets(line, 255, fd) != NULL) {
00112               ptr = line;
00113 
00114               /* skip space */
00115               while (*ptr == ' ' || *ptr == '\t') ptr++;
00116 
00117               /* check null line or comment line */
00118               if ((*ptr == '\0') || (*ptr == '\n') || (*ptr == '#'))
00119                      continue;
00120 
00121 /*
00122               sscanf(ptr, "%s %s", &value1, &value2);
00123 */
00124               memset(value1, 0, 256);
00125               memset(value2, 0, 256);
00126               for (i=0; i<5; i++) {
00127                      value1[i] = *ptr++;
00128               }
00129               value1[5] ='\0';
00130 
00131               /* skip space */
00132               while (*ptr==' ' || *ptr=='\t') ptr++;
00133 
00134               if (*ptr && *ptr != '\n') {
00135                      for (i=0; i<MAX_CHAR_LEN; i++) {
00136                             if (*ptr == 0 || *ptr==' ' || *ptr=='\t' || *ptr=='\n') {
00137                                    value2[i] = 0;
00138                                    break;
00139                             }
00140                             value2[i] = tolower(*ptr++);
00141                      }
00142 
00143                      convert_to_cns11643_code(value1, hex_str);
00144                      printf("%s\t%s\n", value2, hex_str);
00145               }
00146        }
00147        
00148        fclose(fd);
00149 
00150        return 0;
00151 }
00152