* Cleanup.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31710 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2009-07-23 09:45:54 +00:00
parent 664683a7d2
commit 8315ac2417
2 changed files with 141 additions and 147 deletions

View File

@ -1,23 +1,19 @@
//----------------------------------------------------------------------
// This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license.
//---------------------------------------------------------------------
/*!
\file Query.h
BQuery interface declaration.
*/
/*
* Copyright 2002-2009, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _QUERY_H
#define _QUERY_H
#include <EntryList.h>
#include <Messenger.h>
#include <OS.h>
#include <SupportDefs.h>
class BVolume;
struct entry_ref;
namespace BPrivate {
namespace Storage {
class QueryNode;
@ -43,6 +39,7 @@ typedef enum {
_B_RESERVED_OP_ = 0x100000
} query_op;
/*!
\class BQuery
\brief Represents a live or non-live file system query
@ -61,7 +58,7 @@ public:
status_t Clear();
status_t PushAttr(const char *attrName);
status_t PushAttr(const char* attrName);
status_t PushOp(query_op op);
status_t PushUInt32(uint32 value);
@ -70,17 +67,18 @@ public:
status_t PushInt64(int64 value);
status_t PushFloat(float value);
status_t PushDouble(double value);
status_t PushString(const char *value, bool caseInsensitive = false);
status_t PushDate(const char *date);
status_t PushString(const char* value,
bool caseInsensitive = false);
status_t PushDate(const char* date);
status_t SetVolume(const BVolume *volume);
status_t SetPredicate(const char *expression);
status_t SetVolume(const BVolume* volume);
status_t SetPredicate(const char* expression);
status_t SetTarget(BMessenger messenger);
bool IsLive() const;
status_t GetPredicate(char *buffer, size_t length);
status_t GetPredicate(BString *predicate);
status_t GetPredicate(char* buffer, size_t length);
status_t GetPredicate(BString* predicate);
size_t PredicateLength();
dev_t TargetDevice() const;
@ -88,17 +86,18 @@ public:
status_t Fetch();
// BEntryList interface
virtual status_t GetNextEntry(BEntry *entry, bool traverse = false);
virtual status_t GetNextRef(entry_ref *ref);
virtual int32 GetNextDirents(struct dirent *buf, size_t length,
virtual status_t GetNextEntry(BEntry* entry, bool traverse = false);
virtual status_t GetNextRef(entry_ref* ref);
virtual int32 GetNextDirents(struct dirent* buffer, size_t length,
int32 count = INT_MAX);
virtual status_t Rewind();
virtual int32 CountEntries();
private:
bool _HasFetched() const;
status_t _PushNode(BPrivate::Storage::QueryNode *node, bool deleteOnError);
status_t _SetPredicate(const char *expression);
status_t _PushNode(BPrivate::Storage::QueryNode* node,
bool deleteOnError);
status_t _SetPredicate(const char* expression);
status_t _EvaluateStack();
// FBC
@ -110,14 +109,14 @@ private:
virtual void _QwertyQuery6();
private:
int32 _reservedData[4]; // FBC
BPrivate::Storage::QueryStack *fStack;
char *fPredicate;
BPrivate::Storage::QueryStack* fStack;
char* fPredicate;
dev_t fDevice;
bool fLive;
port_id fPort;
long fToken;
int fQueryFd;
int32 _reservedData[4];
};
#endif // _QUERY_H

View File

@ -1,20 +1,23 @@
//----------------------------------------------------------------------
// This software is part of the Haiku distribution and is covered
// by the MIT license.
//---------------------------------------------------------------------
/*!
\file Query.cpp
BQuery implementation.
*/
/*
* Copyright 2002-2009, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Tyler Dauwalder
* Ingo Weinhold
* Axel Dörfler, axeld@pinc-software.de.
*/
#include <Query.h>
#include <fcntl.h>
#include <new>
#include <parsedate.h>
#include <time.h>
#include <Entry.h>
#include <fs_query.h>
#include <Query.h>
#include <parsedate.h>
#include <Volume.h>
#include <MessengerPrivate.h>
@ -26,13 +29,12 @@
using namespace std;
using namespace BPrivate::Storage;
// BQuery
// constructor
/*! \brief Creates an uninitialized BQuery.
*/
BQuery::BQuery()
: BEntryList(),
:
BEntryList(),
fStack(NULL),
fPredicate(NULL),
fDevice((dev_t)B_ERROR),
@ -43,7 +45,7 @@ BQuery::BQuery()
{
}
// destructor
/*! \brief Frees all resources associated with the object.
*/
BQuery::~BQuery()
@ -51,7 +53,7 @@ BQuery::~BQuery()
Clear();
}
// Clear
/*! \brief Resets the object to a uninitialized state.
\return \c B_OK
*/
@ -77,7 +79,7 @@ BQuery::Clear()
return error;
}
// PushAttr
/*! \brief Pushes an attribute name onto the BQuery's predicate stack.
\param attrName the attribute name
\return
@ -93,12 +95,12 @@ BQuery::Clear()
to be called and Clear() also deletes the predicate.
*/
status_t
BQuery::PushAttr(const char *attrName)
BQuery::PushAttr(const char* attrName)
{
return _PushNode(new(nothrow) AttributeNode(attrName), true);
}
// PushOp
/*! \brief Pushes an operator onto the BQuery's predicate stack.
\param op the code representing the operator
\return
@ -141,7 +143,7 @@ BQuery::PushOp(query_op op)
return error;
}
// PushUInt32
/*! \brief Pushes a uint32 value onto the BQuery's predicate stack.
\param value the value
\return
@ -162,7 +164,7 @@ BQuery::PushUInt32(uint32 value)
return _PushNode(new(nothrow) UInt32ValueNode(value), true);
}
// PushInt32
/*! \brief Pushes an int32 value onto the BQuery's predicate stack.
\param value the value
\return
@ -183,7 +185,7 @@ BQuery::PushInt32(int32 value)
return _PushNode(new(nothrow) Int32ValueNode(value), true);
}
// PushUInt64
/*! \brief Pushes a uint64 value onto the BQuery's predicate stack.
\param value the value
\return
@ -204,7 +206,7 @@ BQuery::PushUInt64(uint64 value)
return _PushNode(new(nothrow) UInt64ValueNode(value), true);
}
// PushInt64
/*! \brief Pushes an int64 value onto the BQuery's predicate stack.
\param value the value
\return
@ -225,7 +227,7 @@ BQuery::PushInt64(int64 value)
return _PushNode(new(nothrow) Int64ValueNode(value), true);
}
// PushFloat
/*! \brief Pushes a float value onto the BQuery's predicate stack.
\param value the value
\return
@ -246,7 +248,7 @@ BQuery::PushFloat(float value)
return _PushNode(new(nothrow) FloatValueNode(value), true);
}
// PushDouble
/*! \brief Pushes a double value onto the BQuery's predicate stack.
\param value the value
\return
@ -267,7 +269,7 @@ BQuery::PushDouble(double value)
return _PushNode(new(nothrow) DoubleValueNode(value), true);
}
// PushString
/*! \brief Pushes a string value onto the BQuery's predicate stack.
\param value the value
\param caseInsensitive \c true, if the case of the string should be
@ -285,12 +287,12 @@ BQuery::PushDouble(double value)
to be called and Clear() also deletes the predicate.
*/
status_t
BQuery::PushString(const char *value, bool caseInsensitive)
BQuery::PushString(const char* value, bool caseInsensitive)
{
return _PushNode(new(nothrow) StringNode(value, caseInsensitive), true);
}
// PushDate
/*! \brief Pushes a date value onto the BQuery's predicate stack.
The supplied date can be any string understood by the POSIX function
parsedate().
@ -306,22 +308,15 @@ BQuery::PushString(const char *value, bool caseInsensitive)
to be called and Clear() also deletes the predicate.
*/
status_t
BQuery::PushDate(const char *date)
BQuery::PushDate(const char* date)
{
status_t error = (date ? B_OK : B_BAD_VALUE);
if (error == B_OK) {
time_t t;
time(&t);
t = parsedate(date, t);
if (t < 0)
error = B_BAD_VALUE;
}
if (error == B_OK)
error = _PushNode(new(nothrow) DateNode(date), true);
return error;
if (date == NULL || !date[0] || parsedate(date, time(NULL)) < 0)
return B_BAD_VALUE;
return _PushNode(new(nothrow) DateNode(date), true);
}
// SetVolume
/*! \brief Sets the BQuery's volume.
A query is restricted to one volume. This method sets this volume. It
fails, if called after Fetch(). To reuse a BQuery object it has to be
@ -332,21 +327,22 @@ BQuery::PushDate(const char *date)
- \c B_NOT_ALLOWED: SetVolume() was called after Fetch().
*/
status_t
BQuery::SetVolume(const BVolume *volume)
BQuery::SetVolume(const BVolume* volume)
{
status_t error = (volume ? B_OK : B_BAD_VALUE);
if (error == B_OK && _HasFetched())
error = B_NOT_ALLOWED;
if (error == B_OK) {
if (volume == NULL)
return B_BAD_VALUE;
if (_HasFetched())
return B_NOT_ALLOWED;
if (volume->InitCheck() == B_OK)
fDevice = volume->Device();
else
fDevice = (dev_t)B_ERROR;
}
return error;
return B_OK;
}
// SetPredicate
/*! \brief Sets the BQuery's predicate.
A predicate can be set either using this method or constructing one on
the predicate stack. The two methods can not be mixed. The letter one
@ -360,7 +356,7 @@ BQuery::SetVolume(const BVolume *volume)
- \c B_NO_MEMORY: Insufficient memory to store the predicate.
*/
status_t
BQuery::SetPredicate(const char *expression)
BQuery::SetPredicate(const char* expression)
{
status_t error = (expression ? B_OK : B_BAD_VALUE);
if (error == B_OK && _HasFetched())
@ -370,7 +366,7 @@ BQuery::SetPredicate(const char *expression)
return error;
}
// SetTarget
/*! \brief Sets the BQuery's target and makes the query live.
The query update messages are sent to the specified target. They might
roll in immediately after calling Fetch().
@ -397,7 +393,7 @@ BQuery::SetTarget(BMessenger messenger)
return error;
}
// IsLive
/*! \brief Returns whether the query associated with this object is live.
\return \c true, if the query is live, \c false otherwise
*/
@ -407,7 +403,7 @@ BQuery::IsLive() const
return fLive;
}
// GetPredicate
/*! \brief Returns the BQuery's predicate.
Regardless of whether the predicate has been constructed using the
predicate stack or set via SetPredicate(), this method returns a
@ -423,7 +419,7 @@ BQuery::IsLive() const
You can't interleave Push*() and GetPredicate() calls.
*/
status_t
BQuery::GetPredicate(char *buffer, size_t length)
BQuery::GetPredicate(char* buffer, size_t length)
{
status_t error = (buffer ? B_OK : B_BAD_VALUE);
if (error == B_OK)
@ -437,7 +433,7 @@ BQuery::GetPredicate(char *buffer, size_t length)
return error;
}
// GetPredicate
/*! \brief Returns the BQuery's predicate.
Regardless of whether the predicate has been constructed using the
predicate stack or set via SetPredicate(), this method returns a
@ -452,7 +448,7 @@ BQuery::GetPredicate(char *buffer, size_t length)
You can't interleave Push*() and GetPredicate() calls.
*/
status_t
BQuery::GetPredicate(BString *predicate)
BQuery::GetPredicate(BString* predicate)
{
status_t error = (predicate ? B_OK : B_BAD_VALUE);
if (error == B_OK)
@ -464,7 +460,7 @@ BQuery::GetPredicate(BString *predicate)
return error;
}
// PredicateLength
/*! \brief Returns the length of the BQuery's predicate string.
Regardless of whether the predicate has been constructed using the
predicate stack or set via SetPredicate(), this method returns the length
@ -487,11 +483,10 @@ BQuery::PredicateLength()
return size;
}
// TargetDevice
/*! \brief Returns the device ID identifying the BQuery's volume.
\return the device ID of the BQuery's volume or \c B_NO_INIT, if the
volume isn't set.
*/
dev_t
BQuery::TargetDevice() const
@ -499,7 +494,7 @@ BQuery::TargetDevice() const
return fDevice;
}
// Fetch
/*! \brief Tells the BQuery to start fetching entries satisfying the predicate.
After Fetch() has been called GetNextEntry(), GetNextRef() and
GetNextDirents() can be used to retrieve the enties. Live query updates
@ -535,9 +530,9 @@ BQuery::Fetch()
}
// BEntryList interface
// #pragma mark - BEntryList interface
// GetNextEntry
/*! \brief Returns the BQuery's next entry as a BEntry.
Places the next entry in the list in \a entry, traversing symlinks if
\a traverse is \c true.
@ -553,7 +548,7 @@ BQuery::Fetch()
- \c B_FILE_ERROR: Fetch() has not been called before.
*/
status_t
BQuery::GetNextEntry(BEntry *entry, bool traverse)
BQuery::GetNextEntry(BEntry* entry, bool traverse)
{
status_t error = (entry ? B_OK : B_BAD_VALUE);
if (error == B_OK) {
@ -565,7 +560,7 @@ BQuery::GetNextEntry(BEntry *entry, bool traverse)
return error;
}
// GetNextRef
/*! \brief Returns the BQuery's next entry as an entry_ref.
Places an entry_ref to the next entry in the list into \a ref.
\param ref a pointer to an entry_ref to be filled in with the data of the
@ -579,7 +574,7 @@ BQuery::GetNextEntry(BEntry *entry, bool traverse)
- \c B_FILE_ERROR: Fetch() has not been called before.
*/
status_t
BQuery::GetNextRef(entry_ref *ref)
BQuery::GetNextRef(entry_ref* ref)
{
status_t error = (ref ? B_OK : B_BAD_VALUE);
if (error == B_OK && !_HasFetched())
@ -604,7 +599,7 @@ BQuery::GetNextRef(entry_ref *ref)
return error;
}
// GetNextDirents
/*! \brief Returns the BQuery's next entries as dirent structures.
Reads a number of entries into the array of dirent structures pointed to by
\a buf. Reads as many but no more than \a count entries, as many entries as
@ -622,16 +617,16 @@ BQuery::GetNextRef(entry_ref *ref)
- \c B_FILE_ERROR: Fetch() has not been called before.
*/
int32
BQuery::GetNextDirents(struct dirent *buf, size_t length, int32 count)
BQuery::GetNextDirents(struct dirent* buffer, size_t length, int32 count)
{
if (!buf)
if (!buffer)
return B_BAD_VALUE;
if (!_HasFetched())
return B_FILE_ERROR;
return _kern_read_dir(fQueryFd, buf, length, count);
return _kern_read_dir(fQueryFd, buffer, length, count);
}
// Rewind
/*! \brief Rewinds the entry list back to the first entry.
Unlike R5 Haiku implements this method for BQuery.
@ -648,7 +643,7 @@ BQuery::Rewind()
return _kern_rewind_dir(fQueryFd);
}
// CountEntries
/*! \brief Unimplemented method of the BEntryList interface.
\return 0.
*/
@ -658,7 +653,7 @@ BQuery::CountEntries()
return B_ERROR;
}
// _HasFetched
/*! Returns whether Fetch() has already been called on this object.
\return \c true, if Fetch() has successfully been invoked, \c false
otherwise.
@ -666,10 +661,10 @@ BQuery::CountEntries()
bool
BQuery::_HasFetched() const
{
return (fQueryFd >= 0);
return fQueryFd >= 0;
}
// _PushNode
/*! \brief Pushs a node onto the predicate stack.
If the stack has not been allocate until this time, this method does
allocate it.
@ -687,7 +682,7 @@ BQuery::_HasFetched() const
- \c B_NOT_ALLOWED: _PushNode() was called after Fetch().
*/
status_t
BQuery::_PushNode(QueryNode *node, bool deleteOnError)
BQuery::_PushNode(QueryNode* node, bool deleteOnError)
{
status_t error = (node ? B_OK : B_NO_MEMORY);
if (error == B_OK && _HasFetched())
@ -705,7 +700,7 @@ BQuery::_PushNode(QueryNode *node, bool deleteOnError)
return error;
}
// _SetPredicate
/*! \brief Helper method to set the BQuery's predicate.
It is not checked whether Fetch() has already been invoked.
\param predicate the predicate string
@ -714,7 +709,7 @@ BQuery::_PushNode(QueryNode *node, bool deleteOnError)
- \c B_NO_MEMORY: Insufficient memory to store the predicate.
*/
status_t
BQuery::_SetPredicate(const char *expression)
BQuery::_SetPredicate(const char* expression)
{
status_t error = B_OK;
// unset the old predicate
@ -731,7 +726,7 @@ BQuery::_SetPredicate(const char *expression)
return error;
}
// _EvaluateStack
/*! Evaluates the query's predicate stack.
The method does nothing (and returns \c B_OK), if the stack is \c NULL.
If the stack is non-null and Fetch() has already been called, the method
@ -751,7 +746,7 @@ BQuery::_EvaluateStack()
if (_HasFetched())
error = B_NOT_ALLOWED;
// convert the stack to a tree and evaluate it
QueryNode *node = NULL;
QueryNode* node = NULL;
if (error == B_OK)
error = fStack->ConvertToTree(node);
BString predicate;