xref: /haiku/src/apps/mail/Utilities.cpp (revision 3be9edf8da228afd9fec0390f408c964766122aa)
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 <MDRLanguage.h>
54 
55 #include "Utilities.h"
56 
57 
58 status_t
59 WriteAttrString(BNode *node, const char *attr, const char *value)
60 {
61 	if (!value)
62 		value = B_EMPTY_STRING;
63 
64 	ssize_t size = node->WriteAttr(attr, B_STRING_TYPE, 0, value, strlen(value) + 1);
65 
66 	return size >= 0 ? B_OK : size;
67 }
68 
69 
70 status_t
71 ReadAttrString(BNode *node, const char *attr, BString *value)
72 {
73 	attr_info attrInfo;
74 
75 	value->SetTo("");
76 	status_t status = node->GetAttrInfo(attr, &attrInfo);
77 	if (status < B_OK)
78 		return status;
79 
80 	ssize_t size = node->ReadAttr(attr, B_STRING_TYPE, 0, value->LockBuffer(attrInfo.size + 1), attrInfo.size);
81 	value->UnlockBuffer();
82 
83 	return size >= 0 ? B_OK : size;
84 }
85 
86 
87 //====================================================================
88 // case-insensitive version of strcmp
89 //
90 
91 int32
92 cistrcmp(const char *str1, const char *str2)
93 {
94 	char	c1;
95 	char	c2;
96 	int32	len;
97 	int32	loop;
98 
99 	len = strlen(str1) + 1;
100 	for (loop = 0; loop < len; loop++)
101 	{
102 		c1 = str1[loop];
103 		if (c1 >= 'A' && c1 <= 'Z')
104 			c1 += 'a' - 'A';
105 		c2 = str2[loop];
106 		if (c2 >= 'A' && c2 <= 'Z')
107 			c2 += 'a' - 'A';
108 		if (c1 == c2)
109 		{
110 		}
111 		else if (c1 < c2)
112 			return -1;
113 		else if (c1 > c2 || !c2)
114 			return 1;
115 	}
116 	return 0;
117 }
118 
119 
120 //====================================================================
121 // case-insensitive version of strncmp
122 //
123 
124 int32
125 cistrncmp(const char *str1, const char *str2, int32 max)
126 {
127 	char		c1;
128 	char		c2;
129 	int32		loop;
130 
131 	for (loop = 0; loop < max; loop++)
132 	{
133 		c1 = *str1++;
134 		if (c1 >= 'A' && c1 <= 'Z')
135 			c1 += 'a' - 'A';
136 		c2 = *str2++;
137 		if (c2 >= 'A' && c2 <= 'Z')
138 			c2 += 'a' - 'A';
139 		if (c1 == c2)
140 		{
141 		}
142 		else if (c1 < c2)
143 			return -1;
144 		else if (c1 > c2 || !c2)
145 			return 1;
146 	}
147 	return 0;
148 }
149 
150 
151 //--------------------------------------------------------------------
152 // case-insensitive version of strstr
153 //
154 
155 char *
156 cistrstr(const char *cs, const char *ct)
157 {
158 	char		c1;
159 	char		c2;
160 	int32		cs_len;
161 	int32		ct_len;
162 	int32		loop1;
163 	int32		loop2;
164 
165 	cs_len = strlen(cs);
166 	ct_len = strlen(ct);
167 	for (loop1 = 0; loop1 < cs_len; loop1++)
168 	{
169 		if (cs_len - loop1 < ct_len)
170 			return NULL;
171 
172 		for (loop2 = 0; loop2 < ct_len; loop2++)
173 		{
174 			c1 = cs[loop1 + loop2];
175 			if ((c1 >= 'A') && (c1 <= 'Z'))
176 				c1 += ('a' - 'A');
177 			c2 = ct[loop2];
178 			if ((c2 >= 'A') && (c2 <= 'Z'))
179 				c2 += ('a' - 'A');
180 			if (c1 != c2)
181 				goto next;
182 		}
183 		return const_cast<char *>(&cs[loop1]);
184 next:
185 		// label must be followed by a statement
186 		;
187 	}
188 	return NULL;
189 }
190 
191 
192 //--------------------------------------------------------------------
193 // return length of \n terminated line
194 //
195 
196 int32
197 linelen(char *str, int32 len, bool header)
198 {
199 	int32		loop;
200 
201 	for (loop = 0; loop < len; loop++)
202 	{
203 		if (str[loop] == '\n')
204 		{
205 			if (!header || loop < 2
206 				|| (header && str[loop + 1] != ' ' && str[loop + 1] != '\t'))
207 				return loop + 1;
208 		}
209 	}
210 	return len;
211 }
212 
213