xref: /haiku/src/apps/mail/Utilities.cpp (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2001, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 BeMail(TM), Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 
35 //--------------------------------------------------------------------
36 //
37 //	Utilities.cpp
38 //
39 //--------------------------------------------------------------------
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include <Alert.h>
46 #include <String.h>
47 #include <Node.h>
48 #include <TypeConstants.h>
49 #include <fs_attr.h>
50 
51 #include <MailMessage.h>
52 
53 #include "Utilities.h"
54 
55 
56 status_t
57 WriteAttrString(BNode *node, const char *attr, const char *value)
58 {
59 	if (!value)
60 		value = B_EMPTY_STRING;
61 
62 	ssize_t size = node->WriteAttr(attr, B_STRING_TYPE, 0, value, strlen(value) + 1);
63 
64 	return size >= 0 ? B_OK : size;
65 }
66 
67 
68 status_t
69 ReadAttrString(BNode *node, const char *attr, BString *value)
70 {
71 	attr_info attrInfo;
72 
73 	value->SetTo("");
74 	status_t status = node->GetAttrInfo(attr, &attrInfo);
75 	if (status < B_OK)
76 		return status;
77 
78 	ssize_t size = node->ReadAttr(attr, B_STRING_TYPE, 0, value->LockBuffer(attrInfo.size + 1), attrInfo.size);
79 	value->UnlockBuffer();
80 
81 	return size >= 0 ? B_OK : size;
82 }
83 
84 
85 //====================================================================
86 // case-insensitive version of strcmp
87 //
88 
89 int32
90 cistrcmp(const char *str1, const char *str2)
91 {
92 	char	c1;
93 	char	c2;
94 	int32	len;
95 	int32	loop;
96 
97 	len = strlen(str1) + 1;
98 	for (loop = 0; loop < len; loop++)
99 	{
100 		c1 = str1[loop];
101 		if (c1 >= 'A' && c1 <= 'Z')
102 			c1 += 'a' - 'A';
103 		c2 = str2[loop];
104 		if (c2 >= 'A' && c2 <= 'Z')
105 			c2 += 'a' - 'A';
106 		if (c1 == c2)
107 		{
108 		}
109 		else if (c1 < c2)
110 			return -1;
111 		else if (c1 > c2 || !c2)
112 			return 1;
113 	}
114 	return 0;
115 }
116 
117 
118 //====================================================================
119 // case-insensitive version of strncmp
120 //
121 
122 int32
123 cistrncmp(const char *str1, const char *str2, int32 max)
124 {
125 	char		c1;
126 	char		c2;
127 	int32		loop;
128 
129 	for (loop = 0; loop < max; loop++)
130 	{
131 		c1 = *str1++;
132 		if (c1 >= 'A' && c1 <= 'Z')
133 			c1 += 'a' - 'A';
134 		c2 = *str2++;
135 		if (c2 >= 'A' && c2 <= 'Z')
136 			c2 += 'a' - 'A';
137 		if (c1 == c2)
138 		{
139 		}
140 		else if (c1 < c2)
141 			return -1;
142 		else if (c1 > c2 || !c2)
143 			return 1;
144 	}
145 	return 0;
146 }
147 
148 
149 //--------------------------------------------------------------------
150 // case-insensitive version of strstr
151 //
152 
153 char *
154 cistrstr(const char *cs, const char *ct)
155 {
156 	char		c1;
157 	char		c2;
158 	int32		cs_len;
159 	int32		ct_len;
160 	int32		loop1;
161 	int32		loop2;
162 
163 	cs_len = strlen(cs);
164 	ct_len = strlen(ct);
165 	for (loop1 = 0; loop1 < cs_len; loop1++)
166 	{
167 		if (cs_len - loop1 < ct_len)
168 			return NULL;
169 
170 		for (loop2 = 0; loop2 < ct_len; loop2++)
171 		{
172 			c1 = cs[loop1 + loop2];
173 			if ((c1 >= 'A') && (c1 <= 'Z'))
174 				c1 += ('a' - 'A');
175 			c2 = ct[loop2];
176 			if ((c2 >= 'A') && (c2 <= 'Z'))
177 				c2 += ('a' - 'A');
178 			if (c1 != c2)
179 				goto next;
180 		}
181 		return const_cast<char *>(&cs[loop1]);
182 next:
183 		// label must be followed by a statement
184 		;
185 	}
186 	return NULL;
187 }
188 
189 
190 //--------------------------------------------------------------------
191 // return length of \n terminated line
192 //
193 
194 int32
195 linelen(char *str, int32 len, bool header)
196 {
197 	int32		loop;
198 
199 	for (loop = 0; loop < len; loop++)
200 	{
201 		if (str[loop] == '\n')
202 		{
203 			if (!header || loop < 2
204 				|| (header && str[loop + 1] != ' ' && str[loop + 1] != '\t'))
205 				return loop + 1;
206 		}
207 	}
208 	return len;
209 }
210 
211