2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
24 //%% (c) Copyright 1993, 1994 International Business Machines Corp.
25 //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
26 //%% (c) Copyright 1993, 1994 Novell, Inc.
27 //%% $TOG: tt_string_map.C /main/10 1999/10/14 18:42:44 mgreess $
29 /* @(#)tt_string_map.C 1.10 95/01/06
33 * Copyright (c) 1990 by Sun Microsystems, Inc.
35 * Defines a class for holding string maps. This
36 * class is used for 2 different things at this time:
38 * _Tt_db_hostname_redirection_map
39 * _Tt_db_partition_redirection_map
42 #include "util/tt_string_map.h"
43 #include "util/tt_map_entry.h"
45 /* Included after "util/tt_string.h" to avoid index/strchr conflicts. */
46 #define X_INCLUDE_STRING_H
47 #define XOS_USE_NO_LOCKING
52 #include <X11/Xos_r.h>
59 _Tt_string_map (_Tt_object_table_keyfn key_function)
61 mapEntries = new _Tt_map_entry_table(key_function);
67 mapEntries = new _Tt_map_entry_table((_Tt_object_table_keyfn)
68 &_Tt_map_entry::getAddress);
77 loadFile (const _Tt_string &file)
79 const int MAX_BUFFER_LEN = 1023;
80 const char *SEPARATORS = " \t";
84 FILE *fp = fopen(file, "r");
88 _Tt_string buffer(MAX_BUFFER_LEN);
90 bool_t throw_away = FALSE;
92 fcntl(fileno(fp), F_SETFD, 1); /* Close on exec */
94 while (fgets(buffer, MAX_BUFFER_LEN+1, fp)) {
95 int new_line_index = buffer.index('\n');
97 // Throw away the characters read because no
98 // new-line was found in the last buffer and
99 // we only handle the first 1023 characters in a line
101 // If there is a new-line, then keep the
103 if (new_line_index > -1) {
109 // If the current buffer has no new-line in it,
110 // remember to throw away the next buffer
111 if (new_line_index == -1) {
115 buffer [new_line_index] = '\0';
118 _Xstrtokparams strtok_buf;
119 memset((char*) &strtok_buf, 0, sizeof(_Xstrtokparams));
121 char *address_token =
122 _XStrtok(buffer, SEPARATORS, strtok_buf);
124 // Make sure this is not a comment line
125 if (*address_token == '#') {
130 _XStrtok(NULL, SEPARATORS, strtok_buf);
132 _Tt_map_entry_ptr entry = new _Tt_map_entry;
133 entry->address = address_token;
134 entry->data = data_token;
135 mapEntries->insert(entry);
145 _Tt_string _Tt_string_map::findEntry(const _Tt_string &address)
147 _Tt_map_entry_table_ptr repeat_reference_map =
148 new _Tt_map_entry_table((_Tt_object_table_keyfn)
149 &_Tt_map_entry::getAddress);
151 _Tt_map_entry_ptr repeat_entry;
152 _Tt_map_entry_ptr current_entry = new _Tt_map_entry;
153 _Tt_map_entry_ptr next_entry = mapEntries->lookup(address);
155 // Chain the references to the entries in the map
156 while (!next_entry.is_null()) {
157 repeat_reference_map->insert(next_entry);
159 current_entry = next_entry;
160 next_entry = mapEntries->lookup(current_entry->data);
162 if (!next_entry.is_null()) {
163 // Check if we have seen this entry before. If we
164 // have, then we have a reference loop in the table,
165 // so return a NULL string...
167 = repeat_reference_map->lookup(next_entry->address);
168 if (!repeat_entry.is_null()) {
169 return _Tt_string((char *)NULL);
173 return current_entry->data;