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