xref: /haiku/src/kits/debugger/types/ArrayIndexPath.cpp (revision fce4895d1884da5ae6fb299d23c735c598e690b1)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "ArrayIndexPath.h"
8 
9 #include <stdlib.h>
10 
11 #include <String.h>
12 
13 
14 static const char kIndexSeparator = ';';
15 
16 
ArrayIndexPath()17 ArrayIndexPath::ArrayIndexPath()
18 {
19 }
20 
21 
ArrayIndexPath(const ArrayIndexPath & other)22 ArrayIndexPath::ArrayIndexPath(const ArrayIndexPath& other)
23 	:
24 	fIndices(other.fIndices)
25 {
26 }
27 
28 
~ArrayIndexPath()29 ArrayIndexPath::~ArrayIndexPath()
30 {
31 }
32 
33 
34 status_t
SetTo(const char * path)35 ArrayIndexPath::SetTo(const char* path)
36 {
37 	fIndices.Clear();
38 
39 	if (path == NULL)
40 		return B_OK;
41 
42 	while (*path != '\0') {
43 		char* numberEnd;
44 		int64 index = strtoll(path, &numberEnd, 0);
45 		if (numberEnd == path)
46 			return B_BAD_VALUE;
47 		path = numberEnd;
48 
49 		if (!fIndices.Add(index))
50 			return B_NO_MEMORY;
51 
52 		if (*path == '\0')
53 			break;
54 
55 		if (*path != kIndexSeparator)
56 			return B_BAD_VALUE;
57 		path++;
58 	}
59 
60 	return B_OK;
61 }
62 
63 
64 void
Clear()65 ArrayIndexPath::Clear()
66 {
67 	fIndices.Clear();
68 }
69 
70 
71 bool
GetPathString(BString & path) const72 ArrayIndexPath::GetPathString(BString& path) const
73 {
74 	path.Truncate(0);
75 
76 	int32 count = CountIndices();
77 	for (int32 i = 0; i < count; i++) {
78 		// append separator for all but the first index
79 		if (i > 0) {
80 			int32 oldLength = path.Length();
81 			if (path.Append(kIndexSeparator, 1).Length() != oldLength + 1)
82 				return false;
83 		}
84 
85 		// append index
86 		int32 oldLength = path.Length();
87 		if ((path << IndexAt(i)).Length() == oldLength)
88 			return false;
89 	}
90 
91 	return true;
92 }
93 
94 
95 ArrayIndexPath&
operator =(const ArrayIndexPath & other)96 ArrayIndexPath::operator=(const ArrayIndexPath& other)
97 {
98 	fIndices = other.fIndices;
99 	return *this;
100 }
101