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
14static const char kIndexSeparator = ';';
15
16
17ArrayIndexPath::ArrayIndexPath()
18{
19}
20
21
22ArrayIndexPath::ArrayIndexPath(const ArrayIndexPath& other)
23	:
24	fIndices(other.fIndices)
25{
26}
27
28
29ArrayIndexPath::~ArrayIndexPath()
30{
31}
32
33
34status_t
35ArrayIndexPath::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
64void
65ArrayIndexPath::Clear()
66{
67	fIndices.Clear();
68}
69
70
71bool
72ArrayIndexPath::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
95ArrayIndexPath&
96ArrayIndexPath::operator=(const ArrayIndexPath& other)
97{
98	fIndices = other.fIndices;
99	return *this;
100}
101