Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSearch / raima / mapchar.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: mapchar.c /main/3 1996/10/01 16:39:53 drk $ */
24 /*
25  *   COMPONENT_NAME: austext
26  *
27  *   FUNCTIONS: bgets
28  *              ctb_init
29  *              ctbl_alloc
30  *              ctbl_free
31  *              d_mapchar
32  *              nextc
33  *
34  *   ORIGINS: 157
35  *
36  *   OBJECT CODE ONLY SOURCE MATERIALS
37  */
38 #ifndef NO_COUNTRY
39 /*-----------------------------------------------------------------------
40
41    mapchar.c -- db_VISTA character map module.
42
43    detailed description
44
45    AUTHOR: Guido Weischedel
46    DATE:   August, 1988
47    PROJECT: International Character sets
48
49    Copyright (C) 1988 by Raima Corporation
50
51 -----------------------------------------------------------------------*/
52
53 /* ********************** EDIT HISTORY *******************************
54
55  SCR    DATE    INI                   DESCRIPTION
56 ----- --------- --- -----------------------------------------------------
57       20-Sep-88 WLW MULTI_TASK changes
58
59 */
60
61
62 /* ********************** INCLUDE FILES ****************************** */
63 #include <stdio.h>
64 #include <fcntl.h>
65 #include "vista.h"
66 #include "dbtype.h"
67
68 /* ********************** LOCAL VARIABLE DECLARATIONS **************** */
69 static char buf[30];
70 static int cptr = -1;
71 static int buflen;
72
73 /* ********************** LOCAL FUNCTION DECLARATIONS **************** */
74 static void bgets(P1(char *) Pi(int) Pi(int));
75 static int nextc(P1(int));
76
77 /* Map ASCII-Characters for output and sorting
78 */
79 int d_mapchar(inchar,outchar,sort_str,subsort TASK_PARM)
80 unsigned char   inchar;     /* value of character to be mapped */
81 unsigned char   outchar;    /* output character as    ... */
82 CONST char FAR *sort_str;  /* sort string (max. len = 2) */
83 unsigned char   subsort;    /* subsort value, to distinguish between two */
84                             /* equal values (e.g. 'a' and 'A', if necessary) */
85 TASK_DECL
86 {
87    int indx;
88
89    DB_ENTER(NO_DB_ID TASK_ID LOCK_SET(RECORD_IO));
90
91    if ( strlen(sort_str) > 2 )
92       RETURN( dberr(S_INVSORT) );
93
94    /* Is character-set table already installed? */
95    if ( !db_global.ctbl_activ ) {
96       if ( ctbl_alloc() != S_OKAY )
97          RETURN( db_status );
98       db_global.ctbl_activ = TRUE;
99    }
100
101    /* Modify table for inchar specifications */
102    indx = inchar;
103    db_global.country_tbl.ptr[indx].out_chr = outchar;
104    db_global.country_tbl.ptr[indx].sort_as1 = sort_str[0];
105    db_global.country_tbl.ptr[indx].sort_as2 = sort_str[1];
106    db_global.country_tbl.ptr[indx].sub_sort = subsort;
107
108    RETURN( db_status=S_OKAY );
109 }
110
111 /* read MAP_FILE and make appropriate d_mapchar-calls
112 */
113 int ctb_init()
114 {
115    int map_fd;
116    unsigned char inchar, outchar, subsort;
117    char loc_buf[21], sortas[3];
118    short subs_i;
119    char ctb_name[FILENMLEN*2];
120
121    strcpy( ctb_name, db_global.ctbpath );
122    strcat( ctb_name, CTBNAME );
123
124    if ( (map_fd = open_b(ctb_name,O_RDONLY)) != -1 ) {
125       while(bgets(loc_buf,20,map_fd), *loc_buf) {
126          if ( strcmp( loc_buf, "ignorecase" ) == 0 ) {
127             if ( d_on_opt( IGNORECASE CURRTASK_PARM ) != S_OKAY )
128                break;
129          }
130          else {
131             sscanf(loc_buf,"%c,%c,%hd,%2s",&inchar,&outchar,&subs_i,&sortas[0]);
132             subsort = (unsigned char) subs_i;
133             if (d_mapchar(inchar,outchar,sortas,subsort CURRTASK_PARM) != S_OKAY )
134                break;
135             }
136       }
137       close(map_fd);
138       return( db_status );
139    }
140    return(db_status = S_OKAY);
141 }
142
143 /* do an fgets from a binary file */
144 static void bgets( s, len, fd )
145 char *s;
146 int len, fd;
147 {
148     int c;
149
150     len--;
151     while ( len-- ) {
152         do {
153             c = nextc( fd );
154             if ( c == -1 ) goto eof;
155             c &= 0xff;
156         } while ( c == '\r' );
157         if ( c == '\n' ) break;
158         *s++ = (char)c;
159     }
160 eof:
161     *s = '\0';
162 }
163
164 /* get one character from the file */
165 static int nextc( fd )
166 int fd;
167 {
168     int n;
169
170     if ( cptr < 0 || cptr >= buflen ) {
171         n = read( fd, buf, 30 );
172         if ( n == 0 ) {
173             cptr = -1;
174             return( -1 );
175         }
176         cptr = 0;
177         buflen = n;
178     }
179     return( (int)buf[cptr++] );
180 }
181
182
183 /* Allocate and initialize country_table
184 */
185 int ctbl_alloc()
186 {
187    if ((db_global.country_tbl.ptr = (CNTRY_TBL FAR *)
188            ALLOC(&db_global.country_tbl,256*sizeof(CNTRY_TBL)+1,"country_tbl")) 
189            == NULL ) return( dberr(S_NOMEMORY) );
190
191    /* fill table with standard values */
192    byteset(db_global.country_tbl.ptr, '\0', 256*sizeof(CNTRY_TBL)+1);
193
194    return( db_status = S_OKAY );
195 }
196
197 /* Free country table
198 */
199 void ctbl_free()
200 {
201    MEM_UNLOCK( &db_global.country_tbl );
202    FREE( &db_global.country_tbl );
203    db_global.ctbl_activ = FALSE;
204 }
205
206 #endif /* NO_COUNTRY */
207 /* vpp -nOS2 -dUNIX -nBSD -nVANILLA_BSD -nVMS -nMEMLOCK -nWINDOWS -nFAR_ALLOC -f/usr/users/master/config/nonwin mapchar.c */