xref: /haiku/src/apps/mail/Utilities.cpp (revision 1ea9f437ea2a94167c336d78136c1f043671ff72)
10995b563SAxel Dörfler /*
20995b563SAxel Dörfler Open Tracker License
30995b563SAxel Dörfler 
40995b563SAxel Dörfler Terms and Conditions
50995b563SAxel Dörfler 
60995b563SAxel Dörfler Copyright (c) 1991-2001, Be Incorporated. All rights reserved.
70995b563SAxel Dörfler 
80995b563SAxel Dörfler Permission is hereby granted, free of charge, to any person obtaining a copy of
90995b563SAxel Dörfler this software and associated documentation files (the "Software"), to deal in
100995b563SAxel Dörfler the Software without restriction, including without limitation the rights to
110995b563SAxel Dörfler use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
120995b563SAxel Dörfler of the Software, and to permit persons to whom the Software is furnished to do
130995b563SAxel Dörfler so, subject to the following conditions:
140995b563SAxel Dörfler 
150995b563SAxel Dörfler The above copyright notice and this permission notice applies to all licensees
160995b563SAxel Dörfler and shall be included in all copies or substantial portions of the Software.
170995b563SAxel Dörfler 
180995b563SAxel Dörfler THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
190995b563SAxel Dörfler IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
200995b563SAxel Dörfler FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
210995b563SAxel Dörfler BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
220995b563SAxel Dörfler AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
230995b563SAxel Dörfler WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
240995b563SAxel Dörfler 
250995b563SAxel Dörfler Except as contained in this notice, the name of Be Incorporated shall not be
260995b563SAxel Dörfler used in advertising or otherwise to promote the sale, use or other dealings in
270995b563SAxel Dörfler this Software without prior written authorization from Be Incorporated.
280995b563SAxel Dörfler 
290995b563SAxel Dörfler BeMail(TM), Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
300995b563SAxel Dörfler of Be Incorporated in the United States and other countries. Other brand product
310995b563SAxel Dörfler names are registered trademarks or trademarks of their respective holders.
320995b563SAxel Dörfler All rights reserved.
330995b563SAxel Dörfler */
340995b563SAxel Dörfler 
35*b06785e8SJonas Sundström 
36*b06785e8SJonas Sundström #include "Utilities.h"
37*b06785e8SJonas Sundström 
38*b06785e8SJonas Sundström #include <fs_attr.h>
39*b06785e8SJonas Sundström #include <Node.h>
40*b06785e8SJonas Sundström #include <String.h>
41*b06785e8SJonas Sundström #include <TypeConstants.h>
420995b563SAxel Dörfler 
430995b563SAxel Dörfler #include <stdio.h>
440995b563SAxel Dörfler #include <stdlib.h>
450995b563SAxel Dörfler #include <string.h>
460995b563SAxel Dörfler 
470995b563SAxel Dörfler 
480995b563SAxel Dörfler status_t
WriteAttrString(BNode * node,const char * attr,const char * value)490995b563SAxel Dörfler WriteAttrString(BNode* node, const char* attr, const char* value)
500995b563SAxel Dörfler {
510995b563SAxel Dörfler 	if (!value)
520995b563SAxel Dörfler 		value = B_EMPTY_STRING;
530995b563SAxel Dörfler 
54*b06785e8SJonas Sundström 	ssize_t size = node->WriteAttr(attr, B_STRING_TYPE, 0, value,
55*b06785e8SJonas Sundström 		strlen(value) + 1);
560995b563SAxel Dörfler 
570995b563SAxel Dörfler 	return size >= 0 ? B_OK : size;
580995b563SAxel Dörfler }
590995b563SAxel Dörfler 
600995b563SAxel Dörfler 
610995b563SAxel Dörfler //====================================================================
620995b563SAxel Dörfler // case-insensitive version of strcmp
630995b563SAxel Dörfler //
640995b563SAxel Dörfler 
650995b563SAxel Dörfler int32
cistrcmp(const char * str1,const char * str2)660995b563SAxel Dörfler cistrcmp(const char* str1, const char* str2)
670995b563SAxel Dörfler {
680995b563SAxel Dörfler 	char	c1;
690995b563SAxel Dörfler 	char	c2;
700995b563SAxel Dörfler 	int32	len;
710995b563SAxel Dörfler 	int32	loop;
720995b563SAxel Dörfler 
730995b563SAxel Dörfler 	len = strlen(str1) + 1;
74*b06785e8SJonas Sundström 	for (loop = 0; loop < len; loop++) {
750995b563SAxel Dörfler 		c1 = str1[loop];
760995b563SAxel Dörfler 		if (c1 >= 'A' && c1 <= 'Z')
770995b563SAxel Dörfler 			c1 += 'a' - 'A';
780995b563SAxel Dörfler 		c2 = str2[loop];
790995b563SAxel Dörfler 		if (c2 >= 'A' && c2 <= 'Z')
800995b563SAxel Dörfler 			c2 += 'a' - 'A';
81*b06785e8SJonas Sundström 		if (c1 == c2) {
82*b06785e8SJonas Sundström 		} else if (c1 < c2) {
830995b563SAxel Dörfler 			return -1;
84*b06785e8SJonas Sundström 		} else if (c1 > c2 || !c2) {
850995b563SAxel Dörfler 			return 1;
860995b563SAxel Dörfler 		}
87*b06785e8SJonas Sundström 	}
880995b563SAxel Dörfler 	return 0;
890995b563SAxel Dörfler }
900995b563SAxel Dörfler 
910995b563SAxel Dörfler 
920995b563SAxel Dörfler //====================================================================
930995b563SAxel Dörfler // case-insensitive version of strncmp
940995b563SAxel Dörfler //
950995b563SAxel Dörfler 
960995b563SAxel Dörfler int32
cistrncmp(const char * str1,const char * str2,int32 max)970995b563SAxel Dörfler cistrncmp(const char* str1, const char* str2, int32 max)
980995b563SAxel Dörfler {
990995b563SAxel Dörfler 	char		c1;
1000995b563SAxel Dörfler 	char		c2;
1010995b563SAxel Dörfler 	int32		loop;
1020995b563SAxel Dörfler 
103*b06785e8SJonas Sundström 	for (loop = 0; loop < max; loop++) {
1040995b563SAxel Dörfler 		c1 = *str1++;
1050995b563SAxel Dörfler 		if (c1 >= 'A' && c1 <= 'Z')
1060995b563SAxel Dörfler 			c1 += 'a' - 'A';
1070995b563SAxel Dörfler 		c2 = *str2++;
1080995b563SAxel Dörfler 		if (c2 >= 'A' && c2 <= 'Z')
1090995b563SAxel Dörfler 			c2 += 'a' - 'A';
110*b06785e8SJonas Sundström 		if (c1 == c2) {
111*b06785e8SJonas Sundström 		} else if (c1 < c2) {
1120995b563SAxel Dörfler 			return -1;
113*b06785e8SJonas Sundström 		} else if (c1 > c2 || !c2) {
1140995b563SAxel Dörfler 			return 1;
1150995b563SAxel Dörfler 		}
116*b06785e8SJonas Sundström 	}
1170995b563SAxel Dörfler 	return 0;
1180995b563SAxel Dörfler }
1190995b563SAxel Dörfler 
1200995b563SAxel Dörfler 
1210995b563SAxel Dörfler //--------------------------------------------------------------------
1220995b563SAxel Dörfler // case-insensitive version of strstr
1230995b563SAxel Dörfler //
1240995b563SAxel Dörfler 
1250995b563SAxel Dörfler char*
cistrstr(const char * cs,const char * ct)1260995b563SAxel Dörfler cistrstr(const char* cs, const char* ct)
1270995b563SAxel Dörfler {
1280995b563SAxel Dörfler 	char		c1;
1290995b563SAxel Dörfler 	char		c2;
1300995b563SAxel Dörfler 	int32		cs_len;
1310995b563SAxel Dörfler 	int32		ct_len;
1320995b563SAxel Dörfler 	int32		loop1;
1330995b563SAxel Dörfler 	int32		loop2;
1340995b563SAxel Dörfler 
1350995b563SAxel Dörfler 	cs_len = strlen(cs);
1360995b563SAxel Dörfler 	ct_len = strlen(ct);
137*b06785e8SJonas Sundström 	for (loop1 = 0; loop1 < cs_len; loop1++) {
1380995b563SAxel Dörfler 		if (cs_len - loop1 < ct_len)
1390995b563SAxel Dörfler 			return NULL;
1400995b563SAxel Dörfler 
141*b06785e8SJonas Sundström 		for (loop2 = 0; loop2 < ct_len; loop2++) {
1420995b563SAxel Dörfler 			c1 = cs[loop1 + loop2];
1430995b563SAxel Dörfler 			if ((c1 >= 'A') && (c1 <= 'Z'))
1440995b563SAxel Dörfler 				c1 += ('a' - 'A');
1450995b563SAxel Dörfler 			c2 = ct[loop2];
1460995b563SAxel Dörfler 			if ((c2 >= 'A') && (c2 <= 'Z'))
1470995b563SAxel Dörfler 				c2 += ('a' - 'A');
1480995b563SAxel Dörfler 			if (c1 != c2)
1490995b563SAxel Dörfler 				goto next;
1500995b563SAxel Dörfler 		}
1510995b563SAxel Dörfler 		return const_cast<char*>(&cs[loop1]);
1520995b563SAxel Dörfler next:
1530995b563SAxel Dörfler 		// label must be followed by a statement
1540995b563SAxel Dörfler 		;
1550995b563SAxel Dörfler 	}
1560995b563SAxel Dörfler 	return NULL;
1570995b563SAxel Dörfler }
1580995b563SAxel Dörfler 
1590995b563SAxel Dörfler 
1600995b563SAxel Dörfler //--------------------------------------------------------------------
1610995b563SAxel Dörfler // return length of \n terminated line
1620995b563SAxel Dörfler //
1630995b563SAxel Dörfler 
1640995b563SAxel Dörfler int32
linelen(char * str,int32 len,bool header)1650995b563SAxel Dörfler linelen(char* str, int32 len, bool header)
1660995b563SAxel Dörfler {
1670995b563SAxel Dörfler 	int32		loop;
1680995b563SAxel Dörfler 
169*b06785e8SJonas Sundström 	for (loop = 0; loop < len; loop++) {
170*b06785e8SJonas Sundström 		if (str[loop] == '\n') {
1710995b563SAxel Dörfler 			if (!header || loop < 2
1720995b563SAxel Dörfler 				|| (header && str[loop + 1] != ' ' && str[loop + 1] != '\t'))
1730995b563SAxel Dörfler 				return loop + 1;
1740995b563SAxel Dörfler 		}
1750995b563SAxel Dörfler 	}
1760995b563SAxel Dörfler 	return len;
1770995b563SAxel Dörfler }
1780995b563SAxel Dörfler 
179