orphan
[debian/elilo] / strops.c
1 /*
2  *  Copyright (C) 1999-2003 Hewlett-Packard Co.
3  *      Contributed by Stephane Eranian <eranian@hpl.hp.com>
4  *
5  * This file is part of the ELILO, the EFI Linux boot loader.
6  *
7  *  ELILO is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2, or (at your option)
10  *  any later version.
11  *
12  *  ELILO is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with ELILO; see the file COPYING.  If not, write to the Free
19  *  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20  *  02111-1307, USA.
21  *
22  * Please check out the elilo.txt for complete documentation on how
23  * to use this program.
24  */
25
26 #include <efi.h>
27 #include <efilib.h>
28
29 //#define CHAR_NULL     (CHAR16)'\0'
30
31 CHAR16 *
32 StrChr(IN const CHAR16 *s, IN const CHAR16 c)
33 {
34         for(; *s != c; ++s)  if (*s == CHAR_NULL) return NULL;
35
36         return (CHAR16 *)s;
37 }
38
39 CHAR16 *
40 StrnCpy(OUT CHAR16 *dst, IN const CHAR16 *src, IN UINTN size)
41 {
42         CHAR16 *res = dst;
43
44         while (size && size-- && (*dst++ = *src++) != CHAR_NULL);
45         /*
46          * does the null padding
47          */
48         while (size && size-- > 0) *dst++ = CHAR_NULL;
49
50         return res;
51 }
52
53 CHAR8 *
54 StrnXCpy(OUT CHAR8 *dst, IN const CHAR16 *src, IN UINTN size)
55 {
56         CHAR8 *res = dst;
57
58         while (size && size-- && (*dst++ = (CHAR8)*src++) != '\0');
59         /*
60          * does the null padding
61          */
62         while (size && size-- > 0) *dst++ = '\0';
63
64         return res;
65 }
66         
67 VOID
68 U2ascii(CHAR16 *in, CHAR8 *out, UINTN maxlen)
69 {
70         while(maxlen-- > 1 && (*out++ = *in++));
71         *out = '\0';
72 }
73         
74 CHAR8 *
75 strncpya(OUT CHAR8 *dst, IN const CHAR8 *src, IN UINTN size)
76 {
77         CHAR8 *res = dst;
78
79         while (size && size-- && (*dst++ = *src++) != '\0');
80         /*
81          * does the null padding
82          */
83         while (size && size-- > 0) *dst++ = '\0';
84
85         return res;
86 }
87
88 CHAR8 *
89 strcpya(CHAR8 *dst, const CHAR8 *src)
90 {
91         CHAR8 *tmp = dst;
92
93     while (*src) {
94         *(dst++) = *(src++);
95     }
96     *dst = 0;
97
98     return tmp;
99
100 }
101
102 CHAR8 *
103 strchra(IN const CHAR8 *s, IN const CHAR8 c)
104 {
105         for(; *s != c; ++s)   
106                 if (*s == 0) return NULL;
107         return (CHAR8 *)s;
108 }
109
110 CHAR8 *
111 strcata(IN CHAR8 *dst,IN CHAR8 *src)
112 {   
113     return strcpya(dst+strlena(dst), src);
114 }
115
116 CHAR8 *
117 strrchra(IN const CHAR8 *s, const INTN c)
118 {
119   CHAR8 *found, *p, ch = (CHAR8)c;
120
121   /* Since strchr is fast, we use it rather than the obvious loop.  */
122
123   if (ch == '\0') return strchra(s, '\0');
124
125   found = NULL;
126   while ((p = strchra(s, ch)) != NULL)
127     {
128       found = p;
129       s = p + 1;
130     }
131
132   return (CHAR8 *) found;
133 }
134
135 CHAR8 *
136 strtok_simple(CHAR8 *in, CHAR8 c)
137 {
138         static CHAR8 *last;
139         CHAR8 *tmp;
140
141         if (in == NULL) in = last;
142
143         if (in == NULL) return NULL;
144
145         if (*in == c) in++;
146
147         tmp = strchra(in, c);
148         if (tmp) {
149                 *tmp = '\0';
150                 last = tmp+1;
151         } else {
152                 last = NULL;
153         }
154         return in;
155 }
156         
157 VOID
158 ascii2U(CHAR8 *in, CHAR16 *out, UINTN maxlen)
159 {
160         while(maxlen-- > 1 && (*out++ = *in++));
161
162         *out = (CHAR16)0;
163 }
164
165