xref: /haiku/src/apps/aboutsystem/Utilities.cpp (revision cc6e7cb3477cdb34c23be8ce246203d2b7f002de)
1 /*
2  * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT license.
4  */
5 
6 #include "Utilities.h"
7 
8 #include <ctype.h>
9 #include <stdarg.h>
10 
11 #include <Message.h>
12 
13 
14 BString
15 trim_string(const char* string, size_t len)
16 {
17 	BString trimmed;
18 	size_t i = 0;
19 	bool addSpace = false;
20 
21 	while (i < len) {
22 		// skip space block
23 		while (i < len && isspace(string[i]))
24 			i++;
25 
26 		// write non-spaced (if any)
27 		if (i < len && !isspace(string[i])) {
28 			// pad with a single space, for all but the first block
29 			if (addSpace)
30 				trimmed << ' ';
31 			else
32 				addSpace = true;
33 
34 			// append chars
35 			while (i < len && !isspace(string[i]))
36 				trimmed << string[i++];
37 		}
38 	}
39 
40 	return trimmed;
41 }
42 
43 
44 // #pragma mark - Licenses
45 
46 
47 Licenses::Licenses(const char* license,...)
48 	:
49 	fLicenses(NULL),
50 	fCount(0)
51 {
52 	if (license == NULL)
53 		return;
54 
55 	va_list list;
56 
57 	// count licenses
58 	va_start(list, license);
59 	fCount = 1;
60 	while (va_arg(list, const char*) != NULL)
61 		fCount++;
62 	va_end(list);
63 
64 	// create array and copy them
65 	fLicenses = new BString[fCount];
66 	fLicenses[0] = license;
67 
68 	va_start(list, license);
69 	for (int32 i = 1; i < fCount; i++)
70 		fLicenses[i] = va_arg(list, const char*);
71 	va_end(list);
72 }
73 
74 
75 Licenses::Licenses(const BMessage& licenses)
76 	:
77 	fLicenses(NULL),
78 	fCount(0)
79 {
80 	type_code type;
81 	int32 count;
82 	if (licenses.GetInfo("License", &type, &count) != B_OK
83 		|| type != B_STRING_TYPE) {
84 		return;
85 	}
86 
87 	fLicenses = new BString[count];
88 
89 	for (int32 i = 0; i < count; i++) {
90 		if (licenses.FindString("License", i, &fLicenses[i]) != B_OK)
91 			return;
92 		fCount++;
93 	}
94 }
95 
96 
97 Licenses::~Licenses()
98 {
99 	delete[] fLicenses;
100 }
101 
102 
103 const char*
104 Licenses::LicenseAt(int32 index) const
105 {
106 	return (index >= 0 && index < fCount ? fLicenses[index].String() : NULL);
107 }
108