xref: /netbsd-src/sys/external/bsd/acpica/dist/tools/acpisrc/asutils.c (revision daf6c4152fcddc27c445489775ed1f66ab4ea9a9)
1 
2 /******************************************************************************
3  *
4  * Module Name: asutils - common utilities
5  *
6  *****************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2011, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44 
45 #include "acpisrc.h"
46 
47 
48 /******************************************************************************
49  *
50  * FUNCTION:    AsSkipUntilChar
51  *
52  * DESCRIPTION: Find the next instance of the input character
53  *
54  ******************************************************************************/
55 
56 char *
57 AsSkipUntilChar (
58     char                    *Buffer,
59     char                    Target)
60 {
61 
62     while (*Buffer != Target)
63     {
64         if (!*Buffer)
65         {
66             return NULL;
67         }
68 
69         Buffer++;
70     }
71 
72     return (Buffer);
73 }
74 
75 
76 /******************************************************************************
77  *
78  * FUNCTION:    AsSkipPastChar
79  *
80  * DESCRIPTION: Find the next instance of the input character, return a buffer
81  *              pointer to this character+1.
82  *
83  ******************************************************************************/
84 
85 char *
86 AsSkipPastChar (
87     char                    *Buffer,
88     char                    Target)
89 {
90 
91     while (*Buffer != Target)
92     {
93         if (!*Buffer)
94         {
95             return NULL;
96         }
97 
98         Buffer++;
99     }
100 
101     Buffer++;
102 
103     return (Buffer);
104 }
105 
106 
107 /******************************************************************************
108  *
109  * FUNCTION:    AsReplaceData
110  *
111  * DESCRIPTION: This function inserts and removes data from the file buffer.
112  *              if more data is inserted than is removed, the data in the buffer
113  *              is moved to make room.  If less data is inserted than is removed,
114  *              the remaining data is moved to close the hole.
115  *
116  ******************************************************************************/
117 
118 char *
119 AsReplaceData (
120     char                    *Buffer,
121     UINT32                  LengthToRemove,
122     char                    *BufferToAdd,
123     UINT32                  LengthToAdd)
124 {
125     UINT32                  BufferLength;
126 
127 
128     /*
129      * Buffer is a string, so the length must include the terminating zero
130      */
131     BufferLength = strlen (Buffer) + 1;
132 
133     if (LengthToRemove != LengthToAdd)
134     {
135         /*
136          * Move some of the existing data
137          * 1) If adding more bytes than removing, make room for the new data
138          * 2) if removing more bytes than adding, delete the extra space
139          */
140         if (LengthToRemove > 0)
141         {
142             Gbl_MadeChanges = TRUE;
143             memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove), (BufferLength - LengthToRemove));
144         }
145     }
146 
147     /*
148      * Now we can move in the new data
149      */
150     if (LengthToAdd > 0)
151     {
152         Gbl_MadeChanges = TRUE;
153         memmove (Buffer, BufferToAdd, LengthToAdd);
154     }
155 
156     return (Buffer + LengthToAdd);
157 }
158 
159 
160 /******************************************************************************
161  *
162  * FUNCTION:    AsInsertData
163  *
164  * DESCRIPTION: This function inserts and removes data from the file buffer.
165  *              if more data is inserted than is removed, the data in the buffer
166  *              is moved to make room.  If less data is inserted than is removed,
167  *              the remaining data is moved to close the hole.
168  *
169  ******************************************************************************/
170 
171 char *
172 AsInsertData (
173     char                    *Buffer,
174     char                    *BufferToAdd,
175     UINT32                  LengthToAdd)
176 {
177     UINT32                  BufferLength;
178 
179 
180     if (LengthToAdd > 0)
181     {
182         /*
183          * Buffer is a string, so the length must include the terminating zero
184          */
185         BufferLength = strlen (Buffer) + 1;
186 
187         /*
188          * Move some of the existing data
189          * 1) If adding more bytes than removing, make room for the new data
190          * 2) if removing more bytes than adding, delete the extra space
191          */
192         Gbl_MadeChanges = TRUE;
193         memmove ((Buffer + LengthToAdd), Buffer, BufferLength);
194 
195         /*
196          * Now we can move in the new data
197          */
198         memmove (Buffer, BufferToAdd, LengthToAdd);
199     }
200 
201     return (Buffer + LengthToAdd);
202 }
203 
204 
205 /******************************************************************************
206  *
207  * FUNCTION:    AsRemoveData
208  *
209  * DESCRIPTION: This function inserts and removes data from the file buffer.
210  *              if more data is inserted than is removed, the data in the buffer
211  *              is moved to make room.  If less data is inserted than is removed,
212  *              the remaining data is moved to close the hole.
213  *
214  ******************************************************************************/
215 
216 char *
217 AsRemoveData (
218     char                    *StartPointer,
219     char                    *EndPointer)
220 {
221     UINT32                  BufferLength;
222 
223 
224     /*
225      * Buffer is a string, so the length must include the terminating zero
226      */
227     BufferLength = strlen (EndPointer) + 1;
228 
229     Gbl_MadeChanges = TRUE;
230     memmove (StartPointer, EndPointer, BufferLength);
231 
232     return (StartPointer);
233 }
234 
235