Home | History | Annotate | Line # | Download | only in nls
catgets.c revision 1.8
      1  1.8  jtc /*	$NetBSD: catgets.c,v 1.8 1996/05/13 23:29:38 jtc Exp $	*/
      2  1.6  cgd 
      3  1.8  jtc /*-
      4  1.8  jtc  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  1.8  jtc  * All rights reserved.
      6  1.8  jtc  *
      7  1.8  jtc  * This code is derived from software contributed to The NetBSD Foundation
      8  1.8  jtc  * by J.T. Conklin.
      9  1.8  jtc  *
     10  1.8  jtc  * Redistribution and use in source and binary forms, with or without
     11  1.8  jtc  * modification, are permitted provided that the following conditions
     12  1.8  jtc  * are met:
     13  1.8  jtc  * 1. Redistributions of source code must retain the above copyright
     14  1.8  jtc  *    notice, this list of conditions and the following disclaimer.
     15  1.8  jtc  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.8  jtc  *    notice, this list of conditions and the following disclaimer in the
     17  1.8  jtc  *    documentation and/or other materials provided with the distribution.
     18  1.8  jtc  * 3. All advertising materials mentioning features or use of this software
     19  1.8  jtc  *    must display the following acknowledgement:
     20  1.8  jtc  *        This product includes software developed by the NetBSD
     21  1.8  jtc  *        Foundation, Inc. and its contributors.
     22  1.8  jtc  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.8  jtc  *    contributors may be used to endorse or promote products derived
     24  1.8  jtc  *    from this software without specific prior written permission.
     25  1.8  jtc  *
     26  1.8  jtc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.8  jtc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.8  jtc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.8  jtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
     30  1.8  jtc  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.8  jtc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.8  jtc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.8  jtc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.8  jtc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.8  jtc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.8  jtc  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  jtc  */
     38  1.1  jtc 
     39  1.8  jtc #define _NLS_PRIVATE
     40  1.1  jtc 
     41  1.8  jtc #include <stdlib.h>
     42  1.8  jtc #include <string.h>
     43  1.1  jtc #include <nl_types.h>
     44  1.1  jtc 
     45  1.4  cgd char *
     46  1.8  jtc _catgets(catd, set_id, msg_id, s)
     47  1.1  jtc 	nl_catd catd;
     48  1.1  jtc 	int set_id;
     49  1.1  jtc 	int msg_id;
     50  1.8  jtc 	const char *s;
     51  1.1  jtc {
     52  1.8  jtc 	struct _nls_cat_hdr *cat_hdr;
     53  1.8  jtc 	struct _nls_set_hdr *set_hdr;
     54  1.8  jtc 	struct _nls_msg_hdr *msg_hdr;
     55  1.8  jtc 	int l, u, i, r;
     56  1.8  jtc 
     57  1.8  jtc 	if (catd == (nl_catd) 0 || catd == (nl_catd) -1) {
     58  1.8  jtc 		return (char *) s;
     59  1.8  jtc 	}
     60  1.8  jtc 
     61  1.8  jtc 	cat_hdr = (struct _nls_cat_hdr *) catd->__data;
     62  1.8  jtc 	set_hdr = (struct _nls_set_hdr *) (catd->__data
     63  1.8  jtc 		+ sizeof(struct _nls_cat_hdr));
     64  1.8  jtc 
     65  1.8  jtc 	/* binary search, see knuth algorithm b */
     66  1.8  jtc 	l = 0;
     67  1.8  jtc 	u = ntohl(cat_hdr->__nsets) - 1;
     68  1.8  jtc 	while (l <= u) {
     69  1.8  jtc 		i = (l + u) / 2;
     70  1.8  jtc 		r = set_id - ntohl(set_hdr[i].__setno);
     71  1.8  jtc 
     72  1.8  jtc 		if (r == 0) {
     73  1.8  jtc 			msg_hdr = (struct _nls_msg_hdr *) (catd->__data
     74  1.8  jtc 				+ sizeof(struct _nls_cat_hdr)
     75  1.8  jtc 				+ ntohl(cat_hdr->__msg_hdr_offset));
     76  1.8  jtc 
     77  1.8  jtc 			l = ntohl(set_hdr[i].__index);
     78  1.8  jtc 			u = l + ntohl(set_hdr[i].__nmsgs) - 1;
     79  1.8  jtc 			while (l <= u) {
     80  1.8  jtc 				i = (l + u) / 2;
     81  1.8  jtc 				r = msg_id - ntohl(msg_hdr[i].__msgno);
     82  1.8  jtc 				if (r == 0) {
     83  1.8  jtc 					return (char *) (catd->__data
     84  1.8  jtc 					    + sizeof(struct _nls_cat_hdr)
     85  1.8  jtc 					    + ntohl(cat_hdr->__msg_txt_offset)
     86  1.8  jtc 					    + ntohl(msg_hdr[i].__offset));
     87  1.8  jtc 				} else if (r < 0) {
     88  1.8  jtc 					u = i - 1;
     89  1.8  jtc 				} else {
     90  1.8  jtc 					l = i + 1;
     91  1.8  jtc 				}
     92  1.8  jtc 			}
     93  1.8  jtc 
     94  1.8  jtc 			/* not found */
     95  1.8  jtc 			return (char *) s;
     96  1.8  jtc 
     97  1.8  jtc 		} else if (r < 0) {
     98  1.8  jtc 			u = i - 1;
     99  1.8  jtc 		} else {
    100  1.8  jtc 			l = i + 1;
    101  1.8  jtc 		}
    102  1.8  jtc 	}
    103  1.8  jtc 
    104  1.8  jtc 	/* not found */
    105  1.8  jtc 	return (char *) s;
    106  1.1  jtc }
    107