4 * Copyright (C) 1989 Free Software Foundation, Inc.
5 * written by Douglas C. Schmidt (d.schmidt@vanderbilt.edu)
7 * This file is part of GNU GPERF.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "List_Node.h"
26 #include "ace/OS_NS_ctype.h"
27 #include "ace/Truncate.h"
29 /// Sorts the key set alphabetically to speed up subsequent operation
30 /// Uses insertion sort since the set is probably quite small.
32 List_Node::sort (char *base
, int len
)
36 for (i
= 0, j
= len
- 1; i
< j
; ++i
)
41 for (curr
= i
+ 1, tmp
= base
[curr
];
42 curr
> 0 && tmp
< base
[curr
- 1];
45 base
[curr
] = base
[curr
- 1];
52 /// Initializes a List_Node. This requires obtaining memory for the
53 /// CHAR_SET initializing them using the information stored in the
54 /// KEY_POSITIONS array in Options, and checking for simple errors.
55 /// It's important to note that KEY and REST are both pointers to the
56 /// different offsets into the same block of dynamic memory pointed to
57 /// by parameter K. The data member REST is used to store any
58 /// additional fields of the input file (it is set to the "" string if
59 /// Option[TYPE] is not enabled). This is useful if the user wishes to
60 /// incorporate a lookup structure, rather than just an array of keys.
61 /// Finally, KEY_NUMBER contains a count of the total number of keys
62 /// seen so far. This is used to initialize the INDEX field to some
64 List_Node::List_Node (char *k
, int len
)
68 rest (option
[TYPE
] ? k
+ len
+ 1 : const_cast<char*> ("")),
72 char *ptr
= new char[(option
[ALLCHARS
] ?
73 static_cast<u_int
>(len
) :
74 option
.max_keysig_size ()) + 1];
76 k
[len
] = '\0'; // Null terminate KEY to separate it from REST.
78 // Lower case if STRCASECMP option is enabled.
79 if (option
[STRCASECMP
])
80 for (char *p
= k
; *p
; p
++)
81 if (ACE_OS::ace_isupper (*p
))
82 *p
= static_cast<char> (ACE_OS::ace_tolower (*p
));
84 if (option
[ALLCHARS
]) // Use all the character position in the KEY.
85 for (; *k
; k
++, ptr
++)
89 ++Vectors::occurrences
[i
];
93 // Only use those character positions specified by the user.
96 // Iterate thru the list of key_positions, initializing
97 // occurrences table and keysig (via char * pointer ptr).
98 for (int i
; (i
= option
.get ()) != EOS
; )
100 if (i
== WORD_END
) // Special notation for last KEY position, i.e. '$'.
102 else if (i
<= len
) // Within range of KEY length, so we'll keep it.
104 else // Out of range of KEY length, so we'll just skip it.
106 ++Vectors::occurrences
[(int) *ptr
++];
109 // Didn't get any hits and user doesn't want to consider the
110 // keylength, so there are essentially no usable hash positions!
111 if (ptr
== keysig
&& option
[NOLENGTH
])
112 ACE_ERROR ((LM_ERROR
,
113 "Can't hash keyword %s with chosen key positions.\n%a",
117 // Terminate this string.
120 // Sort the KEYSIG items alphabetically.
121 sort (keysig
, ACE_Utils::truncate_cast
<int> (ptr
- keysig
));
124 List_Node::~List_Node ()
127 delete [] this->keysig
;