Minor style cleanup.

This commit is contained in:
Axel Dörfler
2017-04-29 14:37:56 +02:00
parent ec39b68443
commit 3582d4fe85
2 changed files with 62 additions and 53 deletions
+18 -11
View File
@@ -13,36 +13,40 @@ namespace BPrivate {
namespace DiskDevice { namespace DiskDevice {
class KPath { class KPath {
public: public:
KPath(size_t bufferSize = B_PATH_NAME_LENGTH); KPath(size_t bufferSize = B_PATH_NAME_LENGTH);
KPath(const char* path, bool normalize = false, KPath(const char* path, bool normalize = false,
size_t bufferSize = B_PATH_NAME_LENGTH); size_t bufferSize = B_PATH_NAME_LENGTH);
KPath(const KPath& other); KPath(const KPath& other);
~KPath(); ~KPath();
status_t SetTo(const char *path, bool normalize = false, status_t SetTo(const char* path, bool normalize = false,
size_t bufferSize = B_PATH_NAME_LENGTH, size_t bufferSize = B_PATH_NAME_LENGTH,
bool traverseLeafLink = false); bool traverseLeafLink = false);
void Adopt(KPath& other); void Adopt(KPath& other);
status_t InitCheck() const; status_t InitCheck() const;
status_t SetPath(const char *path, bool normalize = false, status_t SetPath(const char* path,
bool normalize = false,
bool traverseLeafLink = false); bool traverseLeafLink = false);
const char *Path() const; const char* Path() const;
size_t Length() const { return fPathLength; } size_t Length() const
{ return fPathLength; }
size_t BufferSize() const { return fBufferSize; } size_t BufferSize() const
char *LockBuffer(); { return fBufferSize; }
char* LockBuffer();
void UnlockBuffer(); void UnlockBuffer();
char* DetachBuffer(); char* DetachBuffer();
const char *Leaf() const; const char* Leaf() const;
status_t ReplaceLeaf(const char *newLeaf); status_t ReplaceLeaf(const char* newLeaf);
bool RemoveLeaf(); bool RemoveLeaf();
// returns false, if nothing could be removed anymore // returns false, if nothing could be removed anymore
status_t Append(const char *toAppend, bool isComponent = true); status_t Append(const char* toAppend,
bool isComponent = true);
status_t Normalize(bool traverseLeafLink); status_t Normalize(bool traverseLeafLink);
@@ -54,7 +58,7 @@ class KPath {
bool operator!=(const KPath& other) const; bool operator!=(const KPath& other) const;
bool operator!=(const char* path) const; bool operator!=(const char* path) const;
private: private:
void _ChopTrailingSlashes(); void _ChopTrailingSlashes();
char* fBuffer; char* fBuffer;
@@ -63,9 +67,12 @@ class KPath {
bool fLocked; bool fLocked;
}; };
} // namespace DiskDevice } // namespace DiskDevice
} // namespace BPrivate } // namespace BPrivate
using BPrivate::DiskDevice::KPath; using BPrivate::DiskDevice::KPath;
#endif /* _K_PATH_H */ #endif /* _K_PATH_H */
+16 -14
View File
@@ -3,7 +3,9 @@
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
/** A simple class wrapping a path. Has a fixed-sized buffer. */
/*! A simple class wrapping a path. Has a fixed-sized buffer. */
#include <fs/KPath.h> #include <fs/KPath.h>
@@ -66,7 +68,7 @@ KPath::SetTo(const char* path, bool normalize, size_t bufferSize,
bufferSize = B_PATH_NAME_LENGTH; bufferSize = B_PATH_NAME_LENGTH;
// free the previous buffer, if the buffer size differs // free the previous buffer, if the buffer size differs
if (fBuffer && fBufferSize != bufferSize) { if (fBuffer != NULL && fBufferSize != bufferSize) {
free(fBuffer); free(fBuffer);
fBuffer = NULL; fBuffer = NULL;
fBufferSize = 0; fBufferSize = 0;
@@ -75,14 +77,14 @@ KPath::SetTo(const char* path, bool normalize, size_t bufferSize,
fLocked = false; fLocked = false;
// allocate buffer // allocate buffer
if (!fBuffer) if (fBuffer == NULL)
fBuffer = (char*)malloc(bufferSize); fBuffer = (char*)malloc(bufferSize);
if (!fBuffer) if (fBuffer == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
if (fBuffer) {
fBufferSize = bufferSize; fBufferSize = bufferSize;
fBuffer[0] = '\0'; fBuffer[0] = '\0';
}
return SetPath(path, normalize, traverseLeafLink); return SetPath(path, normalize, traverseLeafLink);
} }
@@ -196,7 +198,7 @@ KPath::DetachBuffer()
const char* const char*
KPath::Leaf() const KPath::Leaf() const
{ {
if (!fBuffer) if (fBuffer == NULL)
return NULL; return NULL;
// only "/" has trailing slashes -- then we have to return the complete // only "/" has trailing slashes -- then we have to return the complete
@@ -215,7 +217,7 @@ status_t
KPath::ReplaceLeaf(const char* newLeaf) KPath::ReplaceLeaf(const char* newLeaf)
{ {
const char* leaf = Leaf(); const char* leaf = Leaf();
if (!leaf) if (leaf == NULL)
return B_NO_INIT; return B_NO_INIT;
int32 leafIndex = leaf - fBuffer; int32 leafIndex = leaf - fBuffer;
@@ -227,7 +229,7 @@ KPath::ReplaceLeaf(const char* newLeaf)
} }
// if a leaf was given, append it // if a leaf was given, append it
if (newLeaf) if (newLeaf != NULL)
return Append(newLeaf); return Append(newLeaf);
return B_OK; return B_OK;
} }
@@ -238,7 +240,7 @@ KPath::RemoveLeaf()
{ {
// get the leaf -- bail out, if not initialized or only the "/" is left // get the leaf -- bail out, if not initialized or only the "/" is left
const char* leaf = Leaf(); const char* leaf = Leaf();
if (!leaf || leaf == fBuffer) if (leaf == NULL || leaf == fBuffer)
return false; return false;
// chop off the leaf // chop off the leaf
@@ -330,9 +332,9 @@ KPath::operator==(const KPath& other) const
if (!fBuffer) if (!fBuffer)
return !other.fBuffer; return !other.fBuffer;
return (other.fBuffer return other.fBuffer
&& fPathLength == other.fPathLength && fPathLength == other.fPathLength
&& strcmp(fBuffer, other.fBuffer) == 0); && strcmp(fBuffer, other.fBuffer) == 0;
} }
@@ -342,7 +344,7 @@ KPath::operator==(const char* path) const
if (!fBuffer) if (!fBuffer)
return (!path); return (!path);
return path && !strcmp(fBuffer, path); return path && strcmp(fBuffer, path) == 0;
} }
@@ -363,7 +365,7 @@ KPath::operator!=(const char* path) const
void void
KPath::_ChopTrailingSlashes() KPath::_ChopTrailingSlashes()
{ {
if (fBuffer) { if (fBuffer != NULL) {
while (fPathLength > 1 && fBuffer[fPathLength - 1] == '/') while (fPathLength > 1 && fBuffer[fPathLength - 1] == '/')
fBuffer[--fPathLength] = '\0'; fBuffer[--fPathLength] = '\0';
} }