From 4fc4757d3d24091a13990cc093318e8859645e2f Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Thu, 1 Oct 2009 09:37:24 +0000 Subject: [PATCH] Added ArrayIndexPath, a small class storing a index list for identifying an element in a multi-dimensional array, featuring conversion from and to string. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33393 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/debugger/Jamfile | 1 + src/apps/debugger/types/ArrayIndexPath.cpp | 100 +++++++++++++++++++++ src/apps/debugger/types/ArrayIndexPath.h | 64 +++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 src/apps/debugger/types/ArrayIndexPath.cpp create mode 100644 src/apps/debugger/types/ArrayIndexPath.h diff --git a/src/apps/debugger/Jamfile b/src/apps/debugger/Jamfile index 770ddf09aa..710efe1450 100644 --- a/src/apps/debugger/Jamfile +++ b/src/apps/debugger/Jamfile @@ -140,6 +140,7 @@ Application Debugger : X86AssemblyLanguage.cpp # types + ArrayIndexPath.cpp TargetAddressRangeList.cpp ValueLocation.cpp diff --git a/src/apps/debugger/types/ArrayIndexPath.cpp b/src/apps/debugger/types/ArrayIndexPath.cpp new file mode 100644 index 0000000000..1eba023b44 --- /dev/null +++ b/src/apps/debugger/types/ArrayIndexPath.cpp @@ -0,0 +1,100 @@ +/* + * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ + + +#include "ArrayIndexPath.h" + +#include + +#include + + +static const char kIndexSeparator = ';'; + + +ArrayIndexPath::ArrayIndexPath() +{ +} + + +ArrayIndexPath::ArrayIndexPath(const ArrayIndexPath& other) + : + fIndices(other.fIndices) +{ +} + + +ArrayIndexPath::~ArrayIndexPath() +{ +} + + +bool +ArrayIndexPath::SetTo(const char* path) +{ + fIndices.Clear(); + + if (path == NULL) + return true; + + while (*path != '\0') { + char* numberEnd; + int64 index = strtoll(path, &numberEnd, 0); + if (numberEnd == path) + return false; + path = numberEnd; + + if (!fIndices.Add(index)) + return false; + + if (*path == '\0') + break; + + if (*path != kIndexSeparator) + return false; + path++; + } + + return true; +} + + +void +ArrayIndexPath::Clear() +{ + fIndices.Clear(); +} + + +bool +ArrayIndexPath::GetPathString(BString& path) const +{ + path.Truncate(0); + + int32 count = CountIndices(); + for (int32 i = 0; i < count; i++) { + // append separator for all but the first index + if (i > 0) { + int32 oldLength = path.Length(); + if (path.Append(kIndexSeparator, 1).Length() != oldLength + 1) + return false; + } + + // append index + int32 oldLength = path.Length(); + if ((path << IndexAt(i)).Length() == oldLength) + return false; + } + + return true; +} + + +ArrayIndexPath& +ArrayIndexPath::operator=(const ArrayIndexPath& other) +{ + fIndices = other.fIndices; + return *this; +} diff --git a/src/apps/debugger/types/ArrayIndexPath.h b/src/apps/debugger/types/ArrayIndexPath.h new file mode 100644 index 0000000000..4b8548544f --- /dev/null +++ b/src/apps/debugger/types/ArrayIndexPath.h @@ -0,0 +1,64 @@ +/* + * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ +#ifndef ARRAY_INDEX_PATH_H +#define ARRAY_INDEX_PATH_H + + +#include "Array.h" +#include "Types.h" + + +class BString; + + +class ArrayIndexPath { +public: + ArrayIndexPath(); + ArrayIndexPath(const ArrayIndexPath& other); + ~ArrayIndexPath(); + + bool SetTo(const char* path); + void Clear(); + + bool GetPathString(BString& path) const; + + inline int32 CountIndices() const; + inline int64 IndexAt(int32 index) const; + inline bool AddIndex(int64 index); + + + ArrayIndexPath& operator=(const ArrayIndexPath& other); + +private: + typedef Array IndexArray; + +private: + IndexArray fIndices; +}; + + +int32 +ArrayIndexPath::CountIndices() const +{ + return fIndices.Count(); +} + + +int64 +ArrayIndexPath::IndexAt(int32 index) const +{ + return index >= 0 && index < fIndices.Count() + ? fIndices.ElementAt(index) : -1; +} + + +bool +ArrayIndexPath::AddIndex(int64 index) +{ + return fIndices.Add(index); +} + + +#endif // ARRAY_INDEX_PATH_H