2010-01-23 06:36:03 +00:00
|
|
|
// Copyright (C) 2009-2010, International Business Machines
|
2009-03-06 19:19:00 +00:00
|
|
|
// Corporation and others. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Copyright 2007 Google Inc. All Rights Reserved.
|
|
|
|
// Author: sanjay@google.com (Sanjay Ghemawat)
|
|
|
|
//
|
|
|
|
// Abstract interface that consumes a sequence of bytes (ByteSink).
|
|
|
|
//
|
|
|
|
// Used so that we can write a single piece of code that can operate
|
|
|
|
// on a variety of output string types.
|
|
|
|
//
|
|
|
|
// Various implementations of this interface are provided:
|
|
|
|
// ByteSink:
|
|
|
|
// CheckedArrayByteSink Write to a flat array, with bounds checking
|
|
|
|
// StringByteSink Write to an STL string
|
|
|
|
|
2009-04-15 04:21:57 +00:00
|
|
|
// This code is a contribution of Google code, and the style used here is
|
|
|
|
// a compromise between the original Google code and the ICU coding guidelines.
|
|
|
|
// For example, data types are ICU-ified (size_t,int->int32_t),
|
|
|
|
// and API comments doxygen-ified, but function names and behavior are
|
|
|
|
// as in the original, if possible.
|
|
|
|
// Assertion-style error handling, not available in ICU, was changed to
|
|
|
|
// parameter "pinning" similar to UnicodeString.
|
|
|
|
//
|
|
|
|
// In addition, this is only a partial port of the original Google code,
|
|
|
|
// limited to what was needed so far. The (nearly) complete original code
|
|
|
|
// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
|
|
|
|
// (see ICU ticket 6765, r25517).
|
|
|
|
|
2009-03-06 19:19:00 +00:00
|
|
|
#ifndef __BYTESTREAM_H__
|
|
|
|
#define __BYTESTREAM_H__
|
|
|
|
|
|
|
|
/**
|
2010-05-15 23:00:26 +00:00
|
|
|
* \file
|
2009-03-06 19:19:00 +00:00
|
|
|
* \brief C++ API: Interface for writing bytes, and implementation classes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/uobject.h"
|
|
|
|
#include "unicode/std_string.h"
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2010-05-15 23:00:26 +00:00
|
|
|
/**
|
2009-03-18 20:35:19 +00:00
|
|
|
* A ByteSink can be filled with bytes.
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-17 01:47:25 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
class U_COMMON_API ByteSink : public UMemory {
|
|
|
|
public:
|
2009-03-17 01:47:25 +00:00
|
|
|
/**
|
2009-03-18 20:35:19 +00:00
|
|
|
* Default constructor.
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-17 01:47:25 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
ByteSink() { }
|
2009-03-17 01:47:25 +00:00
|
|
|
/**
|
2009-03-18 20:35:19 +00:00
|
|
|
* Virtual destructor.
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2010-05-15 23:00:26 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
virtual ~ByteSink() { }
|
|
|
|
|
2009-03-17 01:47:25 +00:00
|
|
|
/**
|
|
|
|
* Append "bytes[0,n-1]" to this.
|
2009-03-18 20:35:19 +00:00
|
|
|
* @param bytes the pointer to the bytes
|
|
|
|
* @param n the number of bytes; must be non-negative
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-17 01:47:25 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
virtual void Append(const char* bytes, int32_t n) = 0;
|
2010-05-15 23:00:26 +00:00
|
|
|
|
2009-03-18 20:35:19 +00:00
|
|
|
/**
|
|
|
|
* Returns a writable buffer for appending and writes the buffer's capacity to
|
|
|
|
* *result_capacity. Guarantees *result_capacity>=min_capacity.
|
|
|
|
* May return a pointer to the caller-owned scratch buffer which must have
|
|
|
|
* scratch_capacity>=min_capacity.
|
|
|
|
* The returned buffer is only valid until the next operation
|
|
|
|
* on this ByteSink.
|
|
|
|
*
|
|
|
|
* After writing at most *result_capacity bytes, call Append() with the
|
|
|
|
* pointer returned from this function and the number of bytes written.
|
|
|
|
* Many Append() implementations will avoid copying bytes if this function
|
|
|
|
* returned an internal buffer.
|
|
|
|
*
|
|
|
|
* Partial usage example:
|
|
|
|
* int32_t capacity;
|
|
|
|
* char* buffer = sink->GetAppendBuffer(..., &capacity);
|
|
|
|
* ... Write n bytes into buffer, with n <= capacity.
|
|
|
|
* sink->Append(buffer, n);
|
|
|
|
* In many implementations, that call to Append will avoid copying bytes.
|
|
|
|
*
|
|
|
|
* If the ByteSink allocates or reallocates an internal buffer, it should use
|
|
|
|
* the desired_capacity_hint if appropriate.
|
|
|
|
* If a caller cannot provide a reasonable guess at the desired capacity,
|
|
|
|
* it should pass desired_capacity_hint=0.
|
|
|
|
*
|
|
|
|
* If a non-scratch buffer is returned, the caller may only pass
|
|
|
|
* a prefix to it to Append().
|
|
|
|
* That is, it is not correct to pass an interior pointer to Append().
|
|
|
|
*
|
|
|
|
* The default implementation always returns the scratch buffer.
|
|
|
|
*
|
|
|
|
* @param min_capacity required minimum capacity of the returned buffer;
|
|
|
|
* must be non-negative
|
|
|
|
* @param desired_capacity_hint desired capacity of the returned buffer;
|
|
|
|
* must be non-negative
|
|
|
|
* @param scratch default caller-owned buffer
|
|
|
|
* @param scratch_capacity capacity of the scratch buffer
|
|
|
|
* @param result_capacity pointer to an integer which will be set to the
|
|
|
|
* capacity of the returned buffer
|
|
|
|
* @return a buffer with *result_capacity>=min_capacity
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-18 20:35:19 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
virtual char* GetAppendBuffer(int32_t min_capacity,
|
|
|
|
int32_t desired_capacity_hint,
|
|
|
|
char* scratch, int32_t scratch_capacity,
|
|
|
|
int32_t* result_capacity);
|
|
|
|
|
2009-03-18 20:35:19 +00:00
|
|
|
/**
|
|
|
|
* Flush internal buffers.
|
|
|
|
* Some byte sinks use internal buffers or provide buffering
|
|
|
|
* and require calling Flush() at the end of the stream.
|
|
|
|
* The default implementation of Flush() does nothing.
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-18 20:35:19 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
virtual void Flush();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ByteSink(const ByteSink &); // copy constructor not implemented
|
|
|
|
ByteSink &operator=(const ByteSink &); // assignment operator not implemented
|
|
|
|
};
|
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// Some standard implementations
|
|
|
|
|
2009-03-17 01:47:25 +00:00
|
|
|
/**
|
|
|
|
* Implementation of ByteSink that writes to a flat byte array,
|
|
|
|
* with bounds-checking:
|
|
|
|
* This sink will not write more than capacity bytes to outbuf.
|
|
|
|
* If more than capacity bytes are Append()ed, then excess bytes are ignored,
|
|
|
|
* and Overflowed() will return true.
|
|
|
|
* Overflow does not cause a runtime error.
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-17 01:47:25 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
class U_COMMON_API CheckedArrayByteSink : public ByteSink {
|
|
|
|
public:
|
2009-03-17 01:47:25 +00:00
|
|
|
/**
|
2009-03-18 20:35:19 +00:00
|
|
|
* Constructs a ByteSink that will write to outbuf[0..capacity-1].
|
|
|
|
* @param outbuf buffer to write to
|
|
|
|
* @param capacity size of the buffer
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-17 01:47:25 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
CheckedArrayByteSink(char* outbuf, int32_t capacity);
|
2010-05-15 23:18:12 +00:00
|
|
|
/**
|
|
|
|
* Returns the sink to its original state, without modifying the buffer.
|
|
|
|
* Useful for reusing both the buffer and the sink for multiple streams.
|
|
|
|
* Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
|
|
|
|
* and Overflowed()=FALSE.
|
|
|
|
* @draft ICU 4.6
|
|
|
|
*/
|
|
|
|
CheckedArrayByteSink& Reset();
|
2009-03-17 01:47:25 +00:00
|
|
|
/**
|
2009-03-18 20:35:19 +00:00
|
|
|
* Append "bytes[0,n-1]" to this.
|
|
|
|
* @param bytes the pointer to the bytes
|
|
|
|
* @param n the number of bytes; must be non-negative
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-17 01:47:25 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
virtual void Append(const char* bytes, int32_t n);
|
2009-03-17 01:47:25 +00:00
|
|
|
/**
|
2009-03-18 20:35:19 +00:00
|
|
|
* Returns a writable buffer for appending and writes the buffer's capacity to
|
|
|
|
* *result_capacity. For details see the base class documentation.
|
|
|
|
* @param min_capacity required minimum capacity of the returned buffer;
|
|
|
|
* must be non-negative
|
|
|
|
* @param desired_capacity_hint desired capacity of the returned buffer;
|
|
|
|
* must be non-negative
|
|
|
|
* @param scratch default caller-owned buffer
|
|
|
|
* @param scratch_capacity capacity of the scratch buffer
|
|
|
|
* @param result_capacity pointer to an integer which will be set to the
|
|
|
|
* capacity of the returned buffer
|
|
|
|
* @return a buffer with *result_capacity>=min_capacity
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-17 01:47:25 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
virtual char* GetAppendBuffer(int32_t min_capacity,
|
|
|
|
int32_t desired_capacity_hint,
|
|
|
|
char* scratch, int32_t scratch_capacity,
|
|
|
|
int32_t* result_capacity);
|
2009-03-17 01:47:25 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of bytes actually written to the sink.
|
2009-03-18 20:35:19 +00:00
|
|
|
* @return number of bytes written to the buffer
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-17 01:47:25 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
int32_t NumberOfBytesWritten() const { return size_; }
|
2009-03-17 01:47:25 +00:00
|
|
|
/**
|
|
|
|
* Returns true if any bytes were discarded, i.e., if there was an
|
|
|
|
* attempt to write more than 'capacity' bytes.
|
2009-03-18 20:35:19 +00:00
|
|
|
* @return TRUE if more than 'capacity' bytes were Append()ed
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-17 01:47:25 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
UBool Overflowed() const { return overflowed_; }
|
2010-05-15 23:00:26 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of bytes appended to the sink.
|
|
|
|
* If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()
|
|
|
|
* else they return the same number.
|
|
|
|
* @return number of bytes written to the buffer
|
|
|
|
* @draft ICU 4.6
|
|
|
|
*/
|
|
|
|
int32_t NumberOfBytesAppended() const { return appended_; }
|
2009-03-06 19:19:00 +00:00
|
|
|
private:
|
|
|
|
char* outbuf_;
|
|
|
|
const int32_t capacity_;
|
|
|
|
int32_t size_;
|
2010-05-15 23:00:26 +00:00
|
|
|
int32_t appended_;
|
2010-05-15 23:18:12 +00:00
|
|
|
UBool overflowed_;
|
2009-03-17 01:47:25 +00:00
|
|
|
CheckedArrayByteSink(); ///< default constructor not implemented
|
|
|
|
CheckedArrayByteSink(const CheckedArrayByteSink &); ///< copy constructor not implemented
|
|
|
|
CheckedArrayByteSink &operator=(const CheckedArrayByteSink &); ///< assignment operator not implemented
|
2009-03-06 19:19:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#if U_HAVE_STD_STRING
|
|
|
|
|
2009-03-17 01:47:25 +00:00
|
|
|
/**
|
|
|
|
* Implementation of ByteSink that writes to a "string".
|
2009-03-18 20:35:19 +00:00
|
|
|
* The StringClass is usually instantiated with a std::string.
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-17 01:47:25 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
template<typename StringClass>
|
|
|
|
class StringByteSink : public ByteSink {
|
|
|
|
public:
|
2009-03-17 01:47:25 +00:00
|
|
|
/**
|
2009-03-18 20:35:19 +00:00
|
|
|
* Constructs a ByteSink that will append bytes to the dest string.
|
|
|
|
* @param dest pointer to string object to append to
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-17 01:47:25 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
StringByteSink(StringClass* dest) : dest_(dest) { }
|
2009-03-17 01:47:25 +00:00
|
|
|
/**
|
2009-03-18 20:35:19 +00:00
|
|
|
* Append "bytes[0,n-1]" to this.
|
2010-02-11 00:53:57 +00:00
|
|
|
* @param data the pointer to the bytes
|
2009-03-18 20:35:19 +00:00
|
|
|
* @param n the number of bytes; must be non-negative
|
2010-01-28 21:09:53 +00:00
|
|
|
* @stable ICU 4.2
|
2009-03-17 01:47:25 +00:00
|
|
|
*/
|
2009-03-06 19:19:00 +00:00
|
|
|
virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
|
|
|
|
private:
|
|
|
|
StringClass* dest_;
|
2009-03-17 01:47:25 +00:00
|
|
|
StringByteSink(); ///< default constructor not implemented
|
|
|
|
StringByteSink(const StringByteSink &); ///< copy constructor not implemented
|
|
|
|
StringByteSink &operator=(const StringByteSink &); ///< assignment operator not implemented
|
2009-03-06 19:19:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif // __BYTESTREAM_H__
|