diff options
Diffstat (limited to 'common')
45 files changed, 2600 insertions, 254 deletions
diff --git a/common/algorithm.h b/common/algorithm.h index 6453073ae5..13cdd9f991 100644 --- a/common/algorithm.h +++ b/common/algorithm.h @@ -177,7 +177,8 @@ T sortChoosePivot(T first, T last) { template<typename T, class StrictWeakOrdering> T sortPartition(T first, T last, T pivot, StrictWeakOrdering &comp) { --last; - SWAP(*pivot, *last); + if (pivot != last) + SWAP(*pivot, *last); T sorted; for (sorted = first; first != last; ++first) { @@ -188,7 +189,8 @@ T sortPartition(T first, T last, T pivot, StrictWeakOrdering &comp) { } } - SWAP(*last, *sorted); + if (last != sorted) + SWAP(*last, *sorted); return sorted; } @@ -268,5 +270,26 @@ T gcd(T a, T b) { #pragma warning(pop) #endif +/** + * Replacement algorithm for iterables. + * + * Replaces all occurrences of "original" in [begin, end) with occurrences of "replaced". + * + * @param[in, out] begin: First element to be examined. + * @param[in] end: Last element in the seubsection. Not examined. + * @param[in] original: Elements to be replaced. + * @param[in] replaced: Element to replace occurrences of "original". + * + * @note Usage examples and unit tests may be found in "test/common/algorithm.h" + */ +template<class It, class Dat> +void replace(It begin, It end, const Dat &original, const Dat &replaced) { + for (; begin != end; ++begin) { + if (*begin == original) { + *begin = replaced; + } + } +} + } // End of namespace Common #endif diff --git a/common/archive.cpp b/common/archive.cpp index 36d420561f..5a339900b6 100644 --- a/common/archive.cpp +++ b/common/archive.cpp @@ -48,7 +48,7 @@ int Archive::listMatchingMembers(ArchiveMemberList &list, const String &pattern) int matches = 0; ArchiveMemberList::const_iterator it = allNames.begin(); - for ( ; it != allNames.end(); ++it) { + for (; it != allNames.end(); ++it) { // TODO: We match case-insenstivie for now, our API does not define whether that's ok or not though... // For our use case case-insensitive is probably what we want to have though. if ((*it)->getName().matchString(pattern, true, true)) { @@ -64,7 +64,7 @@ int Archive::listMatchingMembers(ArchiveMemberList &list, const String &pattern) SearchSet::ArchiveNodeList::iterator SearchSet::find(const String &name) { ArchiveNodeList::iterator it = _list.begin(); - for ( ; it != _list.end(); ++it) { + for (; it != _list.end(); ++it) { if (it->_name == name) break; } @@ -73,7 +73,7 @@ SearchSet::ArchiveNodeList::iterator SearchSet::find(const String &name) { SearchSet::ArchiveNodeList::const_iterator SearchSet::find(const String &name) const { ArchiveNodeList::const_iterator it = _list.begin(); - for ( ; it != _list.end(); ++it) { + for (; it != _list.end(); ++it) { if (it->_name == name) break; } @@ -81,13 +81,13 @@ SearchSet::ArchiveNodeList::const_iterator SearchSet::find(const String &name) c } /* - Keep the nodes sorted according to descending priorities. - In case two or node nodes have the same priority, insertion - order prevails. + Keep the nodes sorted according to descending priorities. + In case two or node nodes have the same priority, insertion + order prevails. */ void SearchSet::insert(const Node &node) { ArchiveNodeList::iterator it = _list.begin(); - for ( ; it != _list.end(); ++it) { + for (; it != _list.end(); ++it) { if (it->_priority < node._priority) break; } @@ -131,8 +131,7 @@ void SearchSet::addSubDirectoriesMatching(const FSNode &directory, String origPa ++sep; if (sep != origPattern.end()) nextPattern = String(sep, origPattern.end()); - } - else { + } else { pattern = origPattern; } @@ -211,7 +210,7 @@ bool SearchSet::hasFile(const String &name) const { return false; ArchiveNodeList::const_iterator it = _list.begin(); - for ( ; it != _list.end(); ++it) { + for (; it != _list.end(); ++it) { if (it->_arc->hasFile(name)) return true; } @@ -223,7 +222,7 @@ int SearchSet::listMatchingMembers(ArchiveMemberList &list, const String &patter int matches = 0; ArchiveNodeList::const_iterator it = _list.begin(); - for ( ; it != _list.end(); ++it) + for (; it != _list.end(); ++it) matches += it->_arc->listMatchingMembers(list, pattern); return matches; @@ -233,7 +232,7 @@ int SearchSet::listMembers(ArchiveMemberList &list) const { int matches = 0; ArchiveNodeList::const_iterator it = _list.begin(); - for ( ; it != _list.end(); ++it) + for (; it != _list.end(); ++it) matches += it->_arc->listMembers(list); return matches; @@ -244,7 +243,7 @@ const ArchiveMemberPtr SearchSet::getMember(const String &name) const { return ArchiveMemberPtr(); ArchiveNodeList::const_iterator it = _list.begin(); - for ( ; it != _list.end(); ++it) { + for (; it != _list.end(); ++it) { if (it->_arc->hasFile(name)) return it->_arc->getMember(name); } @@ -257,7 +256,7 @@ SeekableReadStream *SearchSet::createReadStreamForMember(const String &name) con return 0; ArchiveNodeList::const_iterator it = _list.begin(); - for ( ; it != _list.end(); ++it) { + for (; it != _list.end(); ++it) { SeekableReadStream *stream = it->_arc->createReadStreamForMember(name); if (stream) return stream; @@ -268,7 +267,7 @@ SeekableReadStream *SearchSet::createReadStreamForMember(const String &name) con SearchManager::SearchManager() { - clear(); // Force a reset + clear(); // Force a reset } void SearchManager::clear() { diff --git a/common/array.h b/common/array.h index f240a9c2f5..04ec9f9ccb 100644 --- a/common/array.h +++ b/common/array.h @@ -141,6 +141,12 @@ public: insert_aux(_storage + idx, array.begin(), array.end()); } + /** + * Inserts element before pos. + */ + void insert(iterator pos, const T &element) { + insert_aux(pos, &element, &element + 1); + } T remove_at(size_type idx) { assert(idx < _size); @@ -187,6 +193,14 @@ public: _capacity = 0; } + iterator erase(iterator pos) { + copy(pos + 1, _storage + _size, pos); + _size--; + // We also need to destroy the last object properly here. + _storage[_size].~T(); + return pos; + } + bool empty() const { return (_size == 0); } @@ -347,6 +361,87 @@ protected: }; +/** + * Double linked list with sorted nodes. + */ +template<class T> +class SortedArray : public Array<T> { +public: + typedef T *iterator; + typedef uint size_type; + + SortedArray(int (*comparator)(const void *, const void *)) { + _comparator = comparator; + } + + /** + * Inserts element at the sorted position. + */ + void insert(const T &element) { + if (!this->_size) { + this->insert_aux(this->_storage, &element, &element + 1); + return; + } + + T *where = bsearchMin(element); + + if (where > this->_storage + this->_size) + Array<T>::push_back(element); + else + Array<T>::insert(where, element); + } + + T &operator[](size_type idx) { + error("Operation []= not allowed with SortedArray"); + } + + void insert_at(size_type idx, const T &element) { + error("Operation insert_at(idx, element) not allowed with SortedArray"); + } + + void insert_at(size_type idx, const Array<T> &array) { + error("Operation insert_at(idx, array) not allowed with SortedArray"); + } + + void insert(iterator pos, const T &element) { + error("Operation insert(pos, elemnet) not allowed with SortedArray"); + } + + void push_back(const T &element) { + error("Operation push_back(element) not allowed with SortedArray"); + } + + void push_back(const Array<T> &array) { + error("Operation push_back(array) not allowed with SortedArray"); + } + +private: + // Based on code Copyright (C) 2008-2009 Ksplice, Inc. + // Author: Tim Abbott <tabbott@ksplice.com> + // Licensed under GPLv2+ + T *bsearchMin(void *key) { + uint start_ = 0, end_ = this->_size; + int result; + + while (start_ < end_) { + uint mid = start_ + (end_ - start_) / 2; + + result = this->_comparator(key, this->_storage[mid]); + if (result < 0) + end_ = mid; + else if (result > 0) + start_ = mid + 1; + else + return &this->_storage[mid]; + } + + return &this->_storage[start_]; + } + +private: + int (*_comparator)(const void *, const void *); +}; + } // End of namespace Common #endif diff --git a/common/callback.h b/common/callback.h new file mode 100644 index 0000000000..c6c249a511 --- /dev/null +++ b/common/callback.h @@ -0,0 +1,138 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef COMMON_CALLBACK_H +#define COMMON_CALLBACK_H + +namespace Common { + +/** + * BaseCallback<S> is a simple base class for object-oriented callbacks. + * + * Object-oriented callbacks are such callbacks that know exact instance + * which method must be called. + * + * For backwards compatibility purposes, there is a GlobalFunctionCallback, + * which is BaseCallback<void *>, so it can be used with global C-like + * functions too. + * + * <S> is the type, which is passed to operator() of this callback. + * This allows you to specify that you accept a callback, which wants + * to receive an <S> object. + */ +template<typename S = void *> class BaseCallback { +public: + BaseCallback() {} + virtual ~BaseCallback() {} + virtual void operator()(S data) = 0; +}; + +/** + * GlobalFunctionCallback<T> is a simple wrapper for global C functions. + * + * If there is a method, which accepts BaseCallback<T>, you can + * easily pass your C function by passing + * new GlobalFunctionCallback<T>(yourFunction) + */ +template<typename T> class GlobalFunctionCallback: public BaseCallback<T> { + typedef void(*GlobalFunction)(T result); + GlobalFunction _callback; + +public: + GlobalFunctionCallback(GlobalFunction cb): _callback(cb) {} + virtual ~GlobalFunctionCallback() {} + virtual void operator()(T data) { + if (_callback) _callback(data); + } +}; + +/** + * Callback<T, S> implements an object-oriented callback. + * + * <T> stands for a class which method you want to call. + * <S>, again, is the type of an object passed to operator(). + * + * So, if you have void MyClass::myMethod(AnotherClass) method, + * the corresponding callback is Callback<MyClass, AnotherClass>. + * You create it similarly to this: + * new Callback<MyClass, AnotherClass>( + * pointerToMyClassObject, + * &MyClass::myMethod + * ) + */ +template<class T, typename S = void *> class Callback: public BaseCallback<S> { +protected: + typedef void(T::*TMethod)(S); + T *_object; + TMethod _method; +public: + Callback(T *object, TMethod method): _object(object), _method(method) {} + virtual ~Callback() {} + void operator()(S data) { (_object->*_method)(data); } +}; + +/** + * CallbackBridge<T, OS, S> helps you to chain callbacks. + * + * CallbackBridge keeps a pointer to BaseCallback<OS>. + * When its operator() is called, it passes this pointer + * along with the actual data (of type <S>) to the method + * of <T> class. + * + * This is needed when you have to call a callback only + * when your own callback is called. So, your callback + * is "inner" and the other one is "outer". + * + * CallbackBridge implements the "inner" one and calls + * the method you wanted. It passes the "outer", so you + * can call it from your method. You can ignore it, but + * probably there is no point in using CallbackBridge then. + * + * So, if you receive a BaseCallback<SomeClass> callback + * and you want to call it from your MyClass::myMethod method, + * you should create CallbackBridge<MyClass, SomeClass, S>, + * where <S> is data type you want to receive in MyClass::myMethod. + * + * You create it similarly to this: + * new Callback<MyClass, SomeClass, AnotherClass>( + * pointerToMyClassObject, + * &MyClass::myMethod, + * outerCallback + * ) + * where `outerCallback` is BaseCallback<SomeClass> and `myMethod` is: + * void MyClass::myMethod(BaseCallback<SomeClass> *, AnotherClass) + */ +template<class T, typename OS, typename S = void *> class CallbackBridge: public BaseCallback<S> { + typedef void(T::*TCallbackMethod)(BaseCallback<OS> *, S); + T *_object; + TCallbackMethod _method; + BaseCallback<OS> *_outerCallback; +public: + CallbackBridge(T *object, TCallbackMethod method, BaseCallback<OS> *outerCallback): + _object(object), _method(method), _outerCallback(outerCallback) {} + virtual ~CallbackBridge() {} + void operator()(S data) { (_object->*_method)(_outerCallback, data); } +}; + +} // End of namespace Common + +#endif diff --git a/common/config-manager.cpp b/common/config-manager.cpp index 24c877a4e9..fdd0c6f033 100644 --- a/common/config-manager.cpp +++ b/common/config-manager.cpp @@ -45,6 +45,10 @@ char const *const ConfigManager::kTransientDomain = "__TRANSIENT"; char const *const ConfigManager::kKeymapperDomain = "keymapper"; #endif +#ifdef USE_CLOUD +char const *const ConfigManager::kCloudDomain = "cloud"; +#endif + #pragma mark - @@ -67,6 +71,9 @@ void ConfigManager::copyFrom(ConfigManager &source) { #ifdef ENABLE_KEYMAPPER _keymapperDomain = source._keymapperDomain; #endif +#ifdef USE_CLOUD + _cloudDomain = source._cloudDomain; +#endif _domainSaveOrder = source._domainSaveOrder; _activeDomainName = source._activeDomainName; _activeDomain = &_gameDomains[_activeDomainName]; @@ -121,6 +128,10 @@ void ConfigManager::addDomain(const String &domainName, const ConfigManager::Dom } else if (domainName == kKeymapperDomain) { _keymapperDomain = domain; #endif +#ifdef USE_CLOUD + } else if (domainName == kCloudDomain) { + _cloudDomain = domain; +#endif } else if (domain.contains("gameid")) { // If the domain contains "gameid" we assume it's a game domain if (_gameDomains.contains(domainName)) @@ -160,6 +171,9 @@ void ConfigManager::loadFromStream(SeekableReadStream &stream) { #ifdef ENABLE_KEYMAPPER _keymapperDomain.clear(); #endif +#ifdef USE_CLOUD + _cloudDomain.clear(); +#endif // TODO: Detect if a domain occurs multiple times (or likewise, if // a key occurs multiple times inside one domain). @@ -272,6 +286,10 @@ void ConfigManager::flushToDisk() { // Write the keymapper domain writeDomain(*stream, kKeymapperDomain, _keymapperDomain); #endif +#ifdef USE_CLOUD + // Write the cloud domain + writeDomain(*stream, kCloudDomain, _cloudDomain); +#endif DomainMap::const_iterator d; @@ -359,6 +377,10 @@ const ConfigManager::Domain *ConfigManager::getDomain(const String &domName) con if (domName == kKeymapperDomain) return &_keymapperDomain; #endif +#ifdef USE_CLOUD + if (domName == kCloudDomain) + return &_cloudDomain; +#endif if (_gameDomains.contains(domName)) return &_gameDomains[domName]; if (_miscDomains.contains(domName)) @@ -379,6 +401,10 @@ ConfigManager::Domain *ConfigManager::getDomain(const String &domName) { if (domName == kKeymapperDomain) return &_keymapperDomain; #endif +#ifdef USE_CLOUD + if (domName == kCloudDomain) + return &_cloudDomain; +#endif if (_gameDomains.contains(domName)) return &_gameDomains[domName]; if (_miscDomains.contains(domName)) diff --git a/common/config-manager.h b/common/config-manager.h index 14f911f69d..669faaaf88 100644 --- a/common/config-manager.h +++ b/common/config-manager.h @@ -94,6 +94,11 @@ public: static char const *const kKeymapperDomain; #endif +#ifdef USE_CLOUD + /** The name of cloud domain used to store user's tokens */ + static char const *const kCloudDomain; +#endif + void loadDefaultConfigFile(); void loadConfigFile(const String &filename); @@ -188,6 +193,10 @@ private: Domain _keymapperDomain; #endif +#ifdef USE_CLOUD + Domain _cloudDomain; +#endif + Array<String> _domainSaveOrder; String _activeDomainName; diff --git a/common/dcl.cpp b/common/dcl.cpp index 2f4cdeda6b..75a533aa9d 100644 --- a/common/dcl.cpp +++ b/common/dcl.cpp @@ -30,17 +30,15 @@ namespace Common { class DecompressorDCL { public: - bool unpack(ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked); + bool unpack(SeekableReadStream *sourceStream, WriteStream *targetStream, uint32 targetSize, bool targetFixedSize); protected: /** * Initialize decompressor. - * @param src source stream to read from - * @param dest destination stream to write to - * @param nPacked size of packed data - * @param nUnpacked size of unpacked data + * @param sourceStream source stream to read from + * @param targetStream target memory stream to write to */ - void init(ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked); + void init(SeekableReadStream *sourceStream, WriteStream *targetStream, uint32 targetSize, bool targetFixedSize); /** * Get a number of bits from _src stream, starting with the least @@ -66,36 +64,38 @@ protected: int huffman_lookup(const int *tree); - uint32 _dwBits; ///< bits buffer - byte _nBits; ///< number of unread bits in _dwBits - uint32 _szPacked; ///< size of the compressed data - uint32 _szUnpacked; ///< size of the decompressed data - uint32 _dwRead; ///< number of bytes read from _src - uint32 _dwWrote; ///< number of bytes written to _dest - ReadStream *_src; - byte *_dest; + uint32 _dwBits; ///< bits buffer + byte _nBits; ///< number of unread bits in _dwBits + uint32 _sourceSize; ///< size of the source stream + uint32 _targetSize; ///< size of the target stream (if fixed) + bool _targetFixedSize; ///< if target stream is fixed size or dynamic size + uint32 _bytesRead; ///< number of bytes read from _sourceStream + uint32 _bytesWritten; ///< number of bytes written to _targetStream + SeekableReadStream *_sourceStream; + WriteStream *_targetStream; }; -void DecompressorDCL::init(ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked) { - _src = src; - _dest = dest; - _szPacked = nPacked; - _szUnpacked = nUnpacked; +void DecompressorDCL::init(SeekableReadStream *sourceStream, WriteStream *targetStream, uint32 targetSize, bool targetFixedSize) { + _sourceStream = sourceStream; + _targetStream = targetStream; + _sourceSize = sourceStream->size(); + _targetSize = targetSize; + _targetFixedSize = targetFixedSize; _nBits = 0; - _dwRead = _dwWrote = 0; + _bytesRead = _bytesWritten = 0; _dwBits = 0; } void DecompressorDCL::fetchBitsLSB() { while (_nBits <= 24) { - _dwBits |= ((uint32)_src->readByte()) << _nBits; + _dwBits |= ((uint32)_sourceStream->readByte()) << _nBits; _nBits += 8; - _dwRead++; + _bytesRead++; } } uint32 DecompressorDCL::getBitsLSB(int n) { - // fetching more data to buffer if needed + // Fetching more data to buffer if needed if (_nBits < n) fetchBitsLSB(); uint32 ret = (_dwBits & ~((~0) << n)); @@ -109,7 +109,8 @@ byte DecompressorDCL::getByteLSB() { } void DecompressorDCL::putByte(byte b) { - _dest[_dwWrote++] = b; + _targetStream->writeByte(b); + _bytesWritten++; } #define HUFFMAN_LEAF 0x40000000 @@ -331,97 +332,191 @@ int DecompressorDCL::huffman_lookup(const int *tree) { #define DCL_BINARY_MODE 0 #define DCL_ASCII_MODE 1 -bool DecompressorDCL::unpack(ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked) { - init(src, dest, nPacked, nUnpacked); +#define MIDI_SETUP_BUNDLE_FILE_MAXIMUM_DICTIONARY_SIZE 4096 +bool DecompressorDCL::unpack(SeekableReadStream *sourceStream, WriteStream *targetStream, uint32 targetSize, bool targetFixedSize) { + byte dictionary[MIDI_SETUP_BUNDLE_FILE_MAXIMUM_DICTIONARY_SIZE]; + uint16 dictionaryPos = 0; + uint16 dictionarySize = 0; + uint16 dictionaryMask = 0; int value; - uint32 val_distance, val_length; + uint16 tokenOffset = 0; + uint16 tokenLength = 0; - int mode = getByteLSB(); - int length_param = getByteLSB(); + init(sourceStream, targetStream, targetSize, targetFixedSize); + + byte mode = getByteLSB(); + byte dictionaryType = getByteLSB(); if (mode != DCL_BINARY_MODE && mode != DCL_ASCII_MODE) { warning("DCL-INFLATE: Error: Encountered mode %02x, expected 00 or 01", mode); return false; } - if (length_param < 3 || length_param > 6) - warning("Unexpected length_param value %d (expected in [3,6])", length_param); + // TODO: original code supported 3 as well??? + // Was this an accident or on purpose? And the original code did just give out a warning + // and didn't error out at all + switch (dictionaryType) { + case 4: + dictionarySize = 1024; + break; + case 5: + dictionarySize = 2048; + break; + case 6: + dictionarySize = 4096; + break; + default: + warning("DCL-INFLATE: Error: unsupported dictionary type %02x", dictionaryType); + return false; + } + dictionaryMask = dictionarySize - 1; - while (_dwWrote < _szUnpacked) { + while ((!targetFixedSize) || (_bytesWritten < _targetSize)) { if (getBitsLSB(1)) { // (length,distance) pair value = huffman_lookup(length_tree); if (value < 8) - val_length = value + 2; + tokenLength = value + 2; else - val_length = 8 + (1 << (value - 7)) + getBitsLSB(value - 7); + tokenLength = 8 + (1 << (value - 7)) + getBitsLSB(value - 7); + + if (tokenLength == 519) + break; // End of stream signal debug(8, " | "); value = huffman_lookup(distance_tree); - if (val_length == 2) - val_distance = (value << 2) | getBitsLSB(2); + if (tokenLength == 2) + tokenOffset = (value << 2) | getBitsLSB(2); else - val_distance = (value << length_param) | getBitsLSB(length_param); - val_distance ++; + tokenOffset = (value << dictionaryType) | getBitsLSB(dictionaryType); + tokenOffset++; - debug(8, "\nCOPY(%d from %d)\n", val_length, val_distance); + debug(8, "\nCOPY(%d from %d)\n", tokenLength, tokenOffset); - if (val_length + _dwWrote > _szUnpacked) { - warning("DCL-INFLATE Error: Write out of bounds while copying %d bytes (declared unpacked size is %d bytes, current is %d + %d bytes)", - val_length, _szUnpacked, _dwWrote, val_length); - return false; + if (_targetFixedSize) { + if (tokenLength + _bytesWritten > _targetSize) { + warning("DCL-INFLATE Error: Write out of bounds while copying %d bytes (declared unpacked size is %d bytes, current is %d + %d bytes)", + tokenLength, _targetSize, _bytesWritten, tokenLength); + return false; + } } - if (_dwWrote < val_distance) { + if (_bytesWritten < tokenOffset) { warning("DCL-INFLATE Error: Attempt to copy from before beginning of input stream (declared unpacked size is %d bytes, current is %d bytes)", - _szUnpacked, _dwWrote); + _targetSize, _bytesWritten); return false; } - while (val_length) { - uint32 copy_length = (val_length > val_distance) ? val_distance : val_length; - assert(val_distance >= copy_length); - uint32 pos = _dwWrote - val_distance; - for (uint32 i = 0; i < copy_length; i++) - putByte(dest[pos + i]); + uint16 dictionaryBaseIndex = (dictionaryPos - tokenOffset) & dictionaryMask; + uint16 dictionaryIndex = dictionaryBaseIndex; + uint16 dictionaryNextIndex = dictionaryPos; + + while (tokenLength) { + // Write byte from dictionary + putByte(dictionary[dictionaryIndex]); + debug(9, "\33[32;31m%02x\33[37;37m ", dictionary[dictionaryIndex]); + + dictionary[dictionaryNextIndex] = dictionary[dictionaryIndex]; - for (uint32 i = 0; i < copy_length; i++) - debug(9, "\33[32;31m%02x\33[37;37m ", dest[pos + i]); - debug(9, "\n"); + dictionaryNextIndex = (dictionaryNextIndex + 1) & dictionaryMask; + dictionaryIndex = (dictionaryIndex + 1) & dictionaryMask; - val_length -= copy_length; - val_distance += copy_length; + if (dictionaryIndex == dictionaryPos) + dictionaryIndex = dictionaryBaseIndex; + if (dictionaryNextIndex == dictionarySize) + dictionaryNextIndex = 0; + + tokenLength--; } + dictionaryPos = dictionaryNextIndex; + debug(9, "\n"); } else { // Copy byte verbatim value = (mode == DCL_ASCII_MODE) ? huffman_lookup(ascii_tree) : getByteLSB(); putByte(value); + + // Also remember it inside dictionary + dictionary[dictionaryPos] = value; + dictionaryPos++; + if (dictionaryPos >= dictionarySize) + dictionaryPos = 0; + debug(9, "\33[32;31m%02x \33[37;37m", value); } } - return _dwWrote == _szUnpacked; + if (_targetFixedSize) { + if (_bytesWritten != _targetSize) + warning("DCL-INFLATE Error: Inconsistent bytes written (%d) and target buffer size (%d)", _bytesWritten, _targetSize); + return _bytesWritten == _targetSize; + } + return true; // For targets featuring dynamic size we always succeed } bool decompressDCL(ReadStream *src, byte *dest, uint32 packedSize, uint32 unpackedSize) { + bool success = false; + DecompressorDCL dcl; + if (!src || !dest) return false; + byte *sourceBufferPtr = (byte *)malloc(packedSize); + if (!sourceBufferPtr) + return false; + + // Read source into memory + src->read(sourceBufferPtr, packedSize); + + Common::MemoryReadStream *sourceStream = new MemoryReadStream(sourceBufferPtr, packedSize, DisposeAfterUse::YES); + Common::MemoryWriteStream *targetStream = new MemoryWriteStream(dest, unpackedSize); + + success = dcl.unpack(sourceStream, targetStream, unpackedSize, true); + delete sourceStream; + delete targetStream; + return success; +} + +SeekableReadStream *decompressDCL(SeekableReadStream *sourceStream, uint32 packedSize, uint32 unpackedSize) { + bool success = false; + byte *targetPtr = nullptr; + Common::MemoryWriteStream *targetStream; DecompressorDCL dcl; - return dcl.unpack(src, dest, packedSize, unpackedSize); + + targetPtr = (byte *)malloc(unpackedSize); + if (!targetPtr) + return nullptr; + + targetStream = new MemoryWriteStream(targetPtr, unpackedSize); + + success = dcl.unpack(sourceStream, targetStream, unpackedSize, true); + delete targetStream; + + if (!success) { + free(targetPtr); + return nullptr; + } + return new MemoryReadStream(targetPtr, unpackedSize, DisposeAfterUse::YES); } -SeekableReadStream *decompressDCL(ReadStream *src, uint32 packedSize, uint32 unpackedSize) { - byte *data = (byte *)malloc(unpackedSize); +// This one figures out the unpacked size by itself +// Needed for at least Simon 2, because the unpacked size is not stored anywhere +SeekableReadStream *decompressDCL(SeekableReadStream *sourceStream) { + Common::MemoryWriteStreamDynamic *targetStream; + DecompressorDCL dcl; - if (decompressDCL(src, data, packedSize, unpackedSize)) - return new MemoryReadStream(data, unpackedSize, DisposeAfterUse::YES); + targetStream = new MemoryWriteStreamDynamic(DisposeAfterUse::NO); - free(data); - return 0; + if (dcl.unpack(sourceStream, targetStream, 0, false)) { + byte *targetPtr = targetStream->getData(); + uint32 unpackedSize = targetStream->size(); + delete targetStream; + return new MemoryReadStream(targetPtr, unpackedSize, DisposeAfterUse::YES); + } + delete targetStream; + return nullptr; } } // End of namespace Common diff --git a/common/dcl.h b/common/dcl.h index 0e96f74c07..ade7ebd985 100644 --- a/common/dcl.h +++ b/common/dcl.h @@ -22,8 +22,10 @@ /** * @file - * PKWARE DCL ("explode") decompressor used in engines: + * PKWARE DCL ("explode") ("PKWARE data compression library") decompressor used in engines: + * - agos (exclusively for Simon 2 setup.shr file) * - mohawk + * - neverhood * - sci */ @@ -38,16 +40,22 @@ class ReadStream; class SeekableReadStream; /** - * Try to decompress a PKWARE DCL compressed stream. Returns true if + * Try to decompress a PKWARE DCL (PKWARE data compression library) compressed stream. Returns true if * successful. */ -bool decompressDCL(ReadStream *src, byte *dest, uint32 packedSize, uint32 unpackedSize); +bool decompressDCL(ReadStream *sourceStream, byte *dest, uint32 packedSize, uint32 unpackedSize); /** - * Try to decompress a PKWARE DCL compressed stream. Returns a valid pointer + * Try to decompress a PKWARE DCL (PKWARE data compression library) compressed stream. Returns a valid pointer * if successful and 0 otherwise. */ -SeekableReadStream *decompressDCL(ReadStream *src, uint32 packedSize, uint32 unpackedSize); +SeekableReadStream *decompressDCL(SeekableReadStream *sourceStream, uint32 packedSize, uint32 unpackedSize); + +/** + * Try to decompress a PKWARE DCL (PKWARE data compression library) compressed stream. Returns a valid pointer + * if successful and 0 otherwise. This method is meant for cases, where the unpacked size is not known. + */ +SeekableReadStream *decompressDCL(SeekableReadStream *sourceStream); } // End of namespace Common diff --git a/common/debug.cpp b/common/debug.cpp index 182b28afdf..c61fc63dea 100644 --- a/common/debug.cpp +++ b/common/debug.cpp @@ -30,6 +30,7 @@ // TODO: Move gDebugLevel into namespace Common. int gDebugLevel = -1; +bool gDebugChannelsOnly = false; namespace Common { @@ -119,6 +120,18 @@ bool DebugManager::isDebugChannelEnabled(uint32 channel) { } // End of namespace Common +bool debugLevelSet(int level) { + return level <= gDebugLevel; +} + +bool debugChannelSet(int level, uint32 debugChannels) { + if (gDebugLevel != 11) + if (level > gDebugLevel || !(DebugMan.isDebugChannelEnabled(debugChannels))) + return false; + + return true; +} + #ifndef DISABLE_TEXT_CONSOLE @@ -137,6 +150,9 @@ static void debugHelper(const char *s, va_list va, bool caret = true) { void debug(const char *s, ...) { va_list va; + if (gDebugChannelsOnly) + return; + va_start(va, s); debugHelper(s, va); va_end(va); @@ -145,7 +161,7 @@ void debug(const char *s, ...) { void debug(int level, const char *s, ...) { va_list va; - if (level > gDebugLevel) + if (level > gDebugLevel || gDebugChannelsOnly) return; va_start(va, s); @@ -157,6 +173,9 @@ void debug(int level, const char *s, ...) { void debugN(const char *s, ...) { va_list va; + if (gDebugChannelsOnly) + return; + va_start(va, s); debugHelper(s, va, false); va_end(va); @@ -165,7 +184,7 @@ void debugN(const char *s, ...) { void debugN(int level, const char *s, ...) { va_list va; - if (level > gDebugLevel) + if (level > gDebugLevel || gDebugChannelsOnly) return; va_start(va, s); diff --git a/common/debug.h b/common/debug.h index b6e0679a12..883a0bf29d 100644 --- a/common/debug.h +++ b/common/debug.h @@ -31,11 +31,10 @@ inline void debug(const char *s, ...) {} inline void debug(int level, const char *s, ...) {} inline void debugN(const char *s, ...) {} inline void debugN(int level, const char *s, ...) {} -inline void debugC(int level, uint32 engineChannel, const char *s, ...) {} -inline void debugC(uint32 engineChannel, const char *s, ...) {} -inline void debugCN(int level, uint32 engineChannel, const char *s, ...) {} -inline void debugCN(uint32 engineChannel, const char *s, ...) {} - +inline void debugC(int level, uint32 debugChannels, const char *s, ...) {} +inline void debugC(uint32 debugChannels, const char *s, ...) {} +inline void debugCN(int level, uint32 debugChannels, const char *s, ...) {} +inline void debugCN(uint32 debugChannels, const char *s, ...) {} #else @@ -111,6 +110,18 @@ void debugCN(uint32 debugChannels, const char *s, ...) GCC_PRINTF(2, 3); #endif /** + * Returns true if the debug level is set to the specified level + */ +bool debugLevelSet(int level); + +/** + * Returns true if the debug level and channel are active + * + * @see enableDebugChannel + */ +bool debugChannelSet(int level, uint32 debugChannels); + +/** * The debug level. Initially set to -1, indicating that no debug output * should be shown. Positive values usually imply an increasing number of * debug output shall be generated, the higher the value, the more verbose the @@ -118,6 +129,15 @@ void debugCN(uint32 debugChannels, const char *s, ...) GCC_PRINTF(2, 3); */ extern int gDebugLevel; +/** + * Specify if we want to show only the debug channels and suppress + * the non-channeled output. + * + * This option is useful when you want to have higher levels of channels + * visible without the noise from other subsystems or OSystem. + */ +extern bool gDebugChannelsOnly; + //Global constant for EventRecorder debug channel enum GlobalDebugLevels { kDebugLevelEventRec = 1 << 30 diff --git a/common/endian.h b/common/endian.h index 0c6b3db621..7278265961 100644 --- a/common/endian.h +++ b/common/endian.h @@ -322,7 +322,7 @@ #ifdef HAVE_INT64 inline uint64 READ_UINT64(const void *ptr) { const uint8 *b = (const uint8 *)ptr; - return (b[7] << 56) | (b[6] << 48) | (b[5] << 40) | (b[4] << 32) | (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]); + return ((uint64)b[7] << 56) | ((uint64)b[6] << 48) | ((uint64)b[5] << 40) | ((uint64)b[4] << 32) | ((uint64)b[3] << 24) | ((uint64)b[2] << 16) | ((uint64)b[1] << 8) | ((uint64)b[0]); } inline void WRITE_UINT64(void *ptr, uint64 value) { uint8 *b = (uint8 *)ptr; @@ -362,7 +362,7 @@ #ifdef HAVE_INT64 inline uint64 READ_UINT64(const void *ptr) { const uint8 *b = (const uint8 *)ptr; - return (b[0] << 56) | (b[1] << 48) | (b[2] << 40) | (b[3] << 32) | (b[4] << 24) | (b[5] << 16) | (b[6] << 8) | (b[7]); + return ((uint64)b[0] << 56) | ((uint64)b[1] << 48) | ((uint64)b[2] << 40) | ((uint64)b[3] << 32) | ((uint64)b[4] << 24) | ((uint64)b[5] << 16) | ((uint64)b[6] << 8) | ((uint64)b[7]); } inline void WRITE_UINT64(void *ptr, uint64 value) { uint8 *b = (uint8 *)ptr; @@ -446,7 +446,7 @@ #ifdef HAVE_INT64 inline uint64 READ_BE_UINT64(const void *ptr) { const uint8 *b = (const uint8 *)ptr; - return (b[0] << 56) | (b[1] << 48) | (b[2] << 40) | (b[3] << 32) | (b[4] << 24) | (b[5] << 16) | (b[6] << 8) | (b[7]); + return ((uint64)b[0] << 56) | ((uint64)b[1] << 48) | ((uint64)b[2] << 40) | ((uint64)b[3] << 32) | ((uint64)b[4] << 24) | ((uint64)b[5] << 16) | ((uint64)b[6] << 8) | ((uint64)b[7]); } inline void WRITE_BE_UINT64(void *ptr, uint64 value) { uint8 *b = (uint8 *)ptr; @@ -550,7 +550,7 @@ #ifdef HAVE_INT64 inline uint64 READ_LE_UINT64(const void *ptr) { const uint8 *b = (const uint8 *)ptr; - return (b[7] << 56) | (b[6] << 48) | (b[5] << 40) | (b[4] << 32) | (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]); + return ((uint64)b[7] << 56) | ((uint64)b[6] << 48) | ((uint64)b[5] << 40) | ((uint64)b[4] << 32) | ((uint64)b[3] << 24) | ((uint64)b[2] << 16) | ((uint64)b[1] << 8) | ((uint64)b[0]); } inline void WRITE_LE_UINT64(void *ptr, uint64 value) { uint8 *b = (uint8 *)ptr; diff --git a/common/fft.cpp b/common/fft.cpp index ac7386083f..27a04abb6a 100644 --- a/common/fft.cpp +++ b/common/fft.cpp @@ -56,11 +56,19 @@ FFT::FFT(int bits, int inverse) : _bits(bits), _inverse(inverse) { } FFT::~FFT() { + for (int i = 0; i < ARRAYSIZE(_cosTables); i++) { + delete _cosTables[i]; + } + delete[] _revTab; delete[] _expTab; delete[] _tmpBuf; } +const uint16 *FFT::getRevTab() const { + return _revTab; +} + void FFT::permute(Complex *z) { int np = 1 << _bits; diff --git a/common/fft.h b/common/fft.h index 6eb72c3f84..ed66d32b71 100644 --- a/common/fft.h +++ b/common/fft.h @@ -47,6 +47,8 @@ public: FFT(int bits, int inverse); ~FFT(); + const uint16 *getRevTab() const; + /** Do the permutation needed BEFORE calling calc(). */ void permute(Complex *z); diff --git a/common/file.cpp b/common/file.cpp index 16e6a0df1a..9797bcaa69 100644 --- a/common/file.cpp +++ b/common/file.cpp @@ -25,6 +25,8 @@ #include "common/file.h" #include "common/fs.h" #include "common/textconsole.h" +#include "common/system.h" +#include "backends/fs/fs-factory.h" namespace Common { @@ -149,10 +151,23 @@ DumpFile::~DumpFile() { close(); } -bool DumpFile::open(const String &filename) { +bool DumpFile::open(const String &filename, bool createPath) { assert(!filename.empty()); assert(!_handle); + if (createPath) { + for (uint32 i = 0; i < filename.size(); ++i) { + if (filename[i] == '/' || filename[i] == '\\') { + Common::String subpath = filename; + subpath.erase(i); + if (subpath.empty()) continue; + AbstractFSNode *node = g_system->getFilesystemFactory()->makeFileNodePath(subpath); + if (node->exists()) continue; + if (!node->create(true)) warning("DumpFile: unable to create directories from path prefix"); + } + } + } + FSNode node(filename); return open(node); } @@ -202,4 +217,6 @@ bool DumpFile::flush() { return _handle->flush(); } +int32 DumpFile::pos() const { return _handle->pos(); } + } // End of namespace Common diff --git a/common/file.h b/common/file.h index c055acc57d..8ad6249d6d 100644 --- a/common/file.h +++ b/common/file.h @@ -143,7 +143,7 @@ public: DumpFile(); virtual ~DumpFile(); - virtual bool open(const String &filename); + virtual bool open(const String &filename, bool createPath = false); virtual bool open(const FSNode &node); virtual void close(); @@ -161,6 +161,8 @@ public: virtual uint32 write(const void *dataPtr, uint32 dataSize); virtual bool flush(); + + virtual int32 pos() const; }; } // End of namespace Common diff --git a/common/fs.h b/common/fs.h index b5b88ba8cb..f516bf7a9c 100644 --- a/common/fs.h +++ b/common/fs.h @@ -57,7 +57,14 @@ class FSList : public Array<FSNode> {}; */ class FSNode : public ArchiveMember { private: + friend class ::AbstractFSNode; SharedPtr<AbstractFSNode> _realNode; + /** + * Construct a FSNode from a backend's AbstractFSNode implementation. + * + * @param realNode Pointer to a heap allocated instance. FSNode will take + * ownership of the pointer. + */ FSNode(AbstractFSNode *realNode); public: diff --git a/common/gui_options.cpp b/common/gui_options.cpp index d79bf1b82f..df880f4fee 100644 --- a/common/gui_options.cpp +++ b/common/gui_options.cpp @@ -53,15 +53,18 @@ const struct GameOpt { { GUIO_NOASPECT, "noAspect" }, - { GUIO_RENDERHERCGREEN, "hercGreen" }, - { GUIO_RENDERHERCAMBER, "hercAmber" }, - { GUIO_RENDERCGA, "cga" }, - { GUIO_RENDEREGA, "ega" }, - { GUIO_RENDERVGA, "vga" }, - { GUIO_RENDERAMIGA, "amiga" }, - { GUIO_RENDERFMTOWNS, "fmtowns" }, - { GUIO_RENDERPC9821, "pc9821" }, - { GUIO_RENDERPC9801, "pc9801" }, + { GUIO_RENDERHERCGREEN, "hercGreen" }, + { GUIO_RENDERHERCAMBER, "hercAmber" }, + { GUIO_RENDERCGA, "cga" }, + { GUIO_RENDEREGA, "ega" }, + { GUIO_RENDERVGA, "vga" }, + { GUIO_RENDERAMIGA, "amiga" }, + { GUIO_RENDERFMTOWNS, "fmtowns" }, + { GUIO_RENDERPC9821, "pc9821" }, + { GUIO_RENDERPC9801, "pc9801" }, + { GUIO_RENDERAPPLE2GS, "2gs" }, + { GUIO_RENDERATARIST, "atari" }, + { GUIO_RENDERMACINTOSH, "macintosh" }, { GUIO_GAMEOPTIONS1, "gameOption1" }, { GUIO_GAMEOPTIONS2, "gameOption2" }, @@ -70,6 +73,8 @@ const struct GameOpt { { GUIO_GAMEOPTIONS5, "gameOption5" }, { GUIO_GAMEOPTIONS6, "gameOption6" }, { GUIO_GAMEOPTIONS7, "gameOption7" }, + { GUIO_GAMEOPTIONS8, "gameOption8" }, + { GUIO_GAMEOPTIONS9, "gameOption9" }, { GUIO_NONE, 0 } }; diff --git a/common/gui_options.h b/common/gui_options.h index 78e9cc7199..d17f45cac1 100644 --- a/common/gui_options.h +++ b/common/gui_options.h @@ -23,47 +23,52 @@ #ifndef COMMON_GUI_OPTIONS_H #define COMMON_GUI_OPTIONS_H -#define GUIO_NONE "\000" -#define GUIO_NOSUBTITLES "\001" -#define GUIO_NOMUSIC "\002" -#define GUIO_NOSPEECH "\003" -#define GUIO_NOSFX "\004" -#define GUIO_NOMIDI "\005" -#define GUIO_NOLAUNCHLOAD "\006" +#define GUIO_NONE "\000" +#define GUIO_NOSUBTITLES "\001" +#define GUIO_NOMUSIC "\002" +#define GUIO_NOSPEECH "\003" +#define GUIO_NOSFX "\004" +#define GUIO_NOMIDI "\005" +#define GUIO_NOLAUNCHLOAD "\006" -#define GUIO_MIDIPCSPK "\007" -#define GUIO_MIDICMS "\010" -#define GUIO_MIDIPCJR "\011" -#define GUIO_MIDIADLIB "\012" -#define GUIO_MIDIC64 "\013" -#define GUIO_MIDIAMIGA "\014" -#define GUIO_MIDIAPPLEIIGS "\015" -#define GUIO_MIDITOWNS "\016" -#define GUIO_MIDIPC98 "\017" -#define GUIO_MIDIMT32 "\020" -#define GUIO_MIDIGM "\021" +#define GUIO_MIDIPCSPK "\007" +#define GUIO_MIDICMS "\010" +#define GUIO_MIDIPCJR "\011" +#define GUIO_MIDIADLIB "\012" +#define GUIO_MIDIC64 "\013" +#define GUIO_MIDIAMIGA "\014" +#define GUIO_MIDIAPPLEIIGS "\015" +#define GUIO_MIDITOWNS "\016" +#define GUIO_MIDIPC98 "\017" +#define GUIO_MIDIMT32 "\020" +#define GUIO_MIDIGM "\021" -#define GUIO_NOASPECT "\022" +#define GUIO_NOASPECT "\022" -#define GUIO_RENDERHERCGREEN "\030" -#define GUIO_RENDERHERCAMBER "\031" -#define GUIO_RENDERCGA "\032" -#define GUIO_RENDEREGA "\033" -#define GUIO_RENDERVGA "\034" -#define GUIO_RENDERAMIGA "\035" -#define GUIO_RENDERFMTOWNS "\036" -#define GUIO_RENDERPC9821 "\037" -#define GUIO_RENDERPC9801 "\040" +#define GUIO_RENDERHERCGREEN "\030" +#define GUIO_RENDERHERCAMBER "\031" +#define GUIO_RENDERCGA "\032" +#define GUIO_RENDEREGA "\033" +#define GUIO_RENDERVGA "\034" +#define GUIO_RENDERAMIGA "\035" +#define GUIO_RENDERFMTOWNS "\036" +#define GUIO_RENDERPC9821 "\037" +#define GUIO_RENDERPC9801 "\040" +#define GUIO_RENDERAPPLE2GS "\041" +#define GUIO_RENDERATARIST "\042" +#define GUIO_RENDERMACINTOSH "\043" // Special GUIO flags for the AdvancedDetector's caching of game specific // options. -#define GUIO_GAMEOPTIONS1 "\041" -#define GUIO_GAMEOPTIONS2 "\042" -#define GUIO_GAMEOPTIONS3 "\043" -#define GUIO_GAMEOPTIONS4 "\044" -#define GUIO_GAMEOPTIONS5 "\045" -#define GUIO_GAMEOPTIONS6 "\046" -#define GUIO_GAMEOPTIONS7 "\047" +#define GUIO_GAMEOPTIONS1 "\050" +#define GUIO_GAMEOPTIONS2 "\051" +#define GUIO_GAMEOPTIONS3 "\052" +#define GUIO_GAMEOPTIONS4 "\053" +#define GUIO_GAMEOPTIONS5 "\054" +#define GUIO_GAMEOPTIONS6 "\055" +#define GUIO_GAMEOPTIONS7 "\056" +#define GUIO_GAMEOPTIONS8 "\057" +#define GUIO_GAMEOPTIONS9 "\060" #define GUIO0() (GUIO_NONE) #define GUIO1(a) (a) diff --git a/common/json.cpp b/common/json.cpp new file mode 100644 index 0000000000..d0e585ac3d --- /dev/null +++ b/common/json.cpp @@ -0,0 +1,1099 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +/* +* Files JSON.cpp and JSONValue.cpp part of the SimpleJSON Library - http://mjpa.in/json +* +* Copyright (C) 2010 Mike Anchor +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "common/json.h" + +#ifdef __MINGW32__ +#define wcsncasecmp wcsnicmp +#endif + +// Macros to free an array/object +#define FREE_ARRAY(x) { JSONArray::iterator iter; for (iter = x.begin(); iter != x.end(); iter++) { delete *iter; } } +#define FREE_OBJECT(x) { JSONObject::iterator iter; for (iter = x.begin(); iter != x.end(); iter++) { delete (*iter)._value; } } + +namespace Common { + +/** +* Blocks off the public constructor +* +* @access private +* +*/ +JSON::JSON() {} + +/** +* Parses a complete JSON encoded string (UNICODE input version) +* +* @access public +* +* @param char* data The JSON text +* +* @return JSONValue* Returns a JSON Value representing the root, or NULL on error +*/ +JSONValue *JSON::parse(const char *data) { + // Skip any preceding whitespace, end of data = no JSON = fail + if (!skipWhitespace(&data)) + return NULL; + + // We need the start of a value here now... + JSONValue *value = JSONValue::parse(&data); + if (value == NULL) + return NULL; + + // Can be white space now and should be at the end of the string then... + if (skipWhitespace(&data)) { + delete value; + return NULL; + } + + // We're now at the end of the string + return value; +} + +/** +* Turns the passed in JSONValue into a JSON encode string +* +* @access public +* +* @param JSONValue* value The root value +* +* @return String Returns a JSON encoded string representation of the given value +*/ +String JSON::stringify(const JSONValue *value) { + if (value != NULL) + return value->stringify(); + else + return ""; +} + +/** +* Skips over any whitespace characters (space, tab, \r or \n) defined by the JSON spec +* +* @access protected +* +* @param char** data Pointer to a char* that contains the JSON text +* +* @return bool Returns true if there is more data, or false if the end of the text was reached +*/ +bool JSON::skipWhitespace(const char **data) { + while (**data != 0 && (**data == ' ' || **data == '\t' || **data == '\r' || **data == '\n')) + (*data)++; + + return **data != 0; +} + +/** +* Extracts a JSON String as defined by the spec - "<some chars>" +* Any escaped characters are swapped out for their unescaped values +* +* @access protected +* +* @param char** data Pointer to a char* that contains the JSON text +* @param String& str Reference to a String to receive the extracted string +* +* @return bool Returns true on success, false on failure +*/ +bool JSON::extractString(const char **data, String &str) { + str = ""; + + while (**data != 0) { + // Save the char so we can change it if need be + char next_char = **data; + + // Escaping something? + if (next_char == '\\') { + // Move over the escape char + (*data)++; + + // Deal with the escaped char + switch (**data) { + case '"': next_char = '"'; + break; + case '\\': next_char = '\\'; + break; + case '/': next_char = '/'; + break; + case 'b': next_char = '\b'; + break; + case 'f': next_char = '\f'; + break; + case 'n': next_char = '\n'; + break; + case 'r': next_char = '\r'; + break; + case 't': next_char = '\t'; + break; + case 'u': { + // We need 5 chars (4 hex + the 'u') or its not valid + if (!simplejson_wcsnlen(*data, 5)) + return false; + + // Deal with the chars + next_char = 0; + for (int i = 0; i < 4; i++) { + // Do it first to move off the 'u' and leave us on the + // final hex digit as we move on by one later on + (*data)++; + + next_char <<= 4; + + // Parse the hex digit + if (**data >= '0' && **data <= '9') + next_char |= (**data - '0'); + else if (**data >= 'A' && **data <= 'F') + next_char |= (10 + (**data - 'A')); + else if (**data >= 'a' && **data <= 'f') + next_char |= (10 + (**data - 'a')); + else { + // Invalid hex digit = invalid JSON + return false; + } + } + break; + } + + // By the spec, only the above cases are allowed + default: + return false; + } + } + + // End of the string? + else if (next_char == '"') { + (*data)++; + //str.reserve(); // Remove unused capacity //TODO + return true; + } + + // Disallowed char? + else if (next_char < ' ' && next_char != '\t') { + // SPEC Violation: Allow tabs due to real world cases + return false; + } + + // Add the next char + str += next_char; + + // Move on + (*data)++; + } + + // If we're here, the string ended incorrectly + return false; +} + +/** +* Parses some text as though it is an integer +* +* @access protected +* +* @param char** data Pointer to a char* that contains the JSON text +* +* @return double Returns the double value of the number found +*/ +double JSON::parseInt(const char **data) { + double integer = 0; + while (**data != 0 && **data >= '0' && **data <= '9') + integer = integer * 10 + (*(*data)++ - '0'); + + return integer; +} + +/** +* Parses some text as though it is a decimal +* +* @access protected +* +* @param char** data Pointer to a char* that contains the JSON text +* +* @return double Returns the double value of the decimal found +*/ +double JSON::parseDecimal(const char **data) { + double decimal = 0.0; + double factor = 0.1; + while (**data != 0 && **data >= '0' && **data <= '9') { + int digit = (*(*data)++ - '0'); + decimal = decimal + digit * factor; + factor *= 0.1; + } + return decimal; +} + +/** +* Parses a JSON encoded value to a JSONValue object +* +* @access protected +* +* @param char** data Pointer to a char* that contains the data +* +* @return JSONValue* Returns a pointer to a JSONValue object on success, NULL on error +*/ +JSONValue *JSONValue::parse(const char **data) { + // Is it a string? + if (**data == '"') { + String str; + if (!JSON::extractString(&(++(*data)), str)) + return NULL; + else + return new JSONValue(str); + } + + // Is it a boolean? + else if ((simplejson_wcsnlen(*data, 4) && scumm_strnicmp(*data, "true", 4) == 0) || (simplejson_wcsnlen(*data, 5) && scumm_strnicmp(*data, "false", 5) == 0)) { + bool value = scumm_strnicmp(*data, "true", 4) == 0; + (*data) += value ? 4 : 5; + return new JSONValue(value); + } + + // Is it a null? + else if (simplejson_wcsnlen(*data, 4) && scumm_strnicmp(*data, "null", 4) == 0) { + (*data) += 4; + return new JSONValue(); + } + + // Is it a number? + else if (**data == '-' || (**data >= '0' && **data <= '9')) { + // Negative? + bool neg = **data == '-'; + if (neg) (*data)++; + + long long int integer = 0; + double number = 0.0; + bool onlyInteger = true; + + // Parse the whole part of the number - only if it wasn't 0 + if (**data == '0') + (*data)++; + else if (**data >= '1' && **data <= '9') + number = integer = JSON::parseInt(data); + else + return NULL; + + // Could be a decimal now... + if (**data == '.') { + (*data)++; + + // Not get any digits? + if (!(**data >= '0' && **data <= '9')) + return NULL; + + // Find the decimal and sort the decimal place out + // Use ParseDecimal as ParseInt won't work with decimals less than 0.1 + // thanks to Javier Abadia for the report & fix + double decimal = JSON::parseDecimal(data); + + // Save the number + number += decimal; + onlyInteger = false; + } + + // Could be an exponent now... + if (**data == 'E' || **data == 'e') { + (*data)++; + + // Check signage of expo + bool neg_expo = false; + if (**data == '-' || **data == '+') { + neg_expo = **data == '-'; + (*data)++; + } + + // Not get any digits? + if (!(**data >= '0' && **data <= '9')) + return NULL; + + // Sort the expo out + double expo = JSON::parseInt(data); + for (double i = 0.0; i < expo; i++) + number = neg_expo ? (number / 10.0) : (number * 10.0); + onlyInteger = false; + } + + // Was it neg? + if (neg) number *= -1; + + if (onlyInteger) + return new JSONValue(neg ? -integer : integer); + + return new JSONValue(number); + } + + // An object? + else if (**data == '{') { + JSONObject object; + + (*data)++; + + while (**data != 0) { + // Whitespace at the start? + if (!JSON::skipWhitespace(data)) { + FREE_OBJECT(object); + return NULL; + } + + // Special case - empty object + if (object.size() == 0 && **data == '}') { + (*data)++; + return new JSONValue(object); + } + + // We want a string now... + String name; + if (!JSON::extractString(&(++(*data)), name)) { + FREE_OBJECT(object); + return NULL; + } + + // More whitespace? + if (!JSON::skipWhitespace(data)) { + FREE_OBJECT(object); + return NULL; + } + + // Need a : now + if (*((*data)++) != ':') { + FREE_OBJECT(object); + return NULL; + } + + // More whitespace? + if (!JSON::skipWhitespace(data)) { + FREE_OBJECT(object); + return NULL; + } + + // The value is here + JSONValue *value = parse(data); + if (value == NULL) { + FREE_OBJECT(object); + return NULL; + } + + // Add the name:value + if (object.find(name) != object.end()) + delete object[name]; + object[name] = value; + + // More whitespace? + if (!JSON::skipWhitespace(data)) { + FREE_OBJECT(object); + return NULL; + } + + // End of object? + if (**data == '}') { + (*data)++; + return new JSONValue(object); + } + + // Want a , now + if (**data != ',') { + FREE_OBJECT(object); + return NULL; + } + + (*data)++; + } + + // Only here if we ran out of data + FREE_OBJECT(object); + return NULL; + } + + // An array? + else if (**data == '[') { + JSONArray array; + + (*data)++; + + while (**data != 0) { + // Whitespace at the start? + if (!JSON::skipWhitespace(data)) { + FREE_ARRAY(array); + return NULL; + } + + // Special case - empty array + if (array.size() == 0 && **data == ']') { + (*data)++; + return new JSONValue(array); + } + + // Get the value + JSONValue *value = parse(data); + if (value == NULL) { + FREE_ARRAY(array); + return NULL; + } + + // Add the value + array.push_back(value); + + // More whitespace? + if (!JSON::skipWhitespace(data)) { + FREE_ARRAY(array); + return NULL; + } + + // End of array? + if (**data == ']') { + (*data)++; + return new JSONValue(array); + } + + // Want a , now + if (**data != ',') { + FREE_ARRAY(array); + return NULL; + } + + (*data)++; + } + + // Only here if we ran out of data + FREE_ARRAY(array); + return NULL; + } + + // Ran out of possibilites, it's bad! + else { + return NULL; + } +} + +/** +* Basic constructor for creating a JSON Value of type NULL +* +* @access public +*/ +JSONValue::JSONValue(/*NULL*/) { + _type = JSONType_Null; +} + +/** +* Basic constructor for creating a JSON Value of type String +* +* @access public +* +* @param char* m_char_value The string to use as the value +*/ +JSONValue::JSONValue(const char *charValue) { + _type = JSONType_String; + _stringValue = new String(String(charValue)); +} + +/** +* Basic constructor for creating a JSON Value of type String +* +* @access public +* +* @param String m_string_value The string to use as the value +*/ +JSONValue::JSONValue(const String &stringValue) { + _type = JSONType_String; + _stringValue = new String(stringValue); +} + +/** +* Basic constructor for creating a JSON Value of type Bool +* +* @access public +* +* @param bool m_bool_value The bool to use as the value +*/ +JSONValue::JSONValue(bool boolValue) { + _type = JSONType_Bool; + _boolValue = boolValue; +} + +/** +* Basic constructor for creating a JSON Value of type Number +* +* @access public +* +* @param double m_number_value The number to use as the value +*/ +JSONValue::JSONValue(double numberValue) { + _type = JSONType_Number; + _numberValue = numberValue; +} + +/** +* Basic constructor for creating a JSON Value of type Number (Integer) +* +* @access public +* +* @param int numberValue The number to use as the value +*/ +JSONValue::JSONValue(long long int numberValue) { + _type = JSONType_IntegerNumber; + _integerValue = numberValue; +} + +/** +* Basic constructor for creating a JSON Value of type Array +* +* @access public +* +* @param JSONArray m_array_value The JSONArray to use as the value +*/ +JSONValue::JSONValue(const JSONArray &arrayValue) { + _type = JSONType_Array; + _arrayValue = new JSONArray(arrayValue); +} + +/** +* Basic constructor for creating a JSON Value of type Object +* +* @access public +* +* @param JSONObject m_object_value The JSONObject to use as the value +*/ +JSONValue::JSONValue(const JSONObject &objectValue) { + _type = JSONType_Object; + _objectValue = new JSONObject(objectValue); +} + +/** +* Copy constructor to perform a deep copy of array / object values +* +* @access public +* +* @param JSONValue m_source The source JSONValue that is being copied +*/ +JSONValue::JSONValue(const JSONValue &source) { + _type = source._type; + + switch (_type) { + case JSONType_String: + _stringValue = new String(*source._stringValue); + break; + + case JSONType_Bool: + _boolValue = source._boolValue; + break; + + case JSONType_Number: + _numberValue = source._numberValue; + break; + + case JSONType_IntegerNumber: + _integerValue = source._integerValue; + break; + + case JSONType_Array: { + JSONArray source_array = *source._arrayValue; + JSONArray::iterator iter; + _arrayValue = new JSONArray(); + for (iter = source_array.begin(); iter != source_array.end(); iter++) + _arrayValue->push_back(new JSONValue(**iter)); + break; + } + + case JSONType_Object: { + JSONObject source_object = *source._objectValue; + _objectValue = new JSONObject(); + JSONObject::iterator iter; + for (iter = source_object.begin(); iter != source_object.end(); iter++) { + String name = (*iter)._key; + (*_objectValue)[name] = new JSONValue(*((*iter)._value)); + } + break; + } + + case JSONType_Null: + // Nothing to do. + break; + } +} + +/** +* The destructor for the JSON Value object +* Handles deleting the objects in the array or the object value +* +* @access public +*/ +JSONValue::~JSONValue() { + if (_type == JSONType_Array) { + JSONArray::iterator iter; + for (iter = _arrayValue->begin(); iter != _arrayValue->end(); iter++) + delete *iter; + delete _arrayValue; + } else if (_type == JSONType_Object) { + JSONObject::iterator iter; + for (iter = _objectValue->begin(); iter != _objectValue->end(); iter++) { + delete (*iter)._value; + } + delete _objectValue; + } else if (_type == JSONType_String) { + delete _stringValue; + } +} + +/** +* Checks if the value is a NULL +* +* @access public +* +* @return bool Returns true if it is a NULL value, false otherwise +*/ +bool JSONValue::isNull() const { + return _type == JSONType_Null; +} + +/** +* Checks if the value is a String +* +* @access public +* +* @return bool Returns true if it is a String value, false otherwise +*/ +bool JSONValue::isString() const { + return _type == JSONType_String; +} + +/** +* Checks if the value is a Bool +* +* @access public +* +* @return bool Returns true if it is a Bool value, false otherwise +*/ +bool JSONValue::isBool() const { + return _type == JSONType_Bool; +} + +/** +* Checks if the value is a Number +* +* @access public +* +* @return bool Returns true if it is a Number value, false otherwise +*/ +bool JSONValue::isNumber() const { + return _type == JSONType_Number; +} + +/** +* Checks if the value is an Integer +* +* @access public +* +* @return bool Returns true if it is an Integer value, false otherwise +*/ +bool JSONValue::isIntegerNumber() const { + return _type == JSONType_IntegerNumber; +} + +/** +* Checks if the value is an Array +* +* @access public +* +* @return bool Returns true if it is an Array value, false otherwise +*/ +bool JSONValue::isArray() const { + return _type == JSONType_Array; +} + +/** +* Checks if the value is an Object +* +* @access public +* +* @return bool Returns true if it is an Object value, false otherwise +*/ +bool JSONValue::isObject() const { + return _type == JSONType_Object; +} + +/** +* Retrieves the String value of this JSONValue +* Use isString() before using this method. +* +* @access public +* +* @return String Returns the string value +*/ +const String &JSONValue::asString() const { + return (*_stringValue); +} + +/** +* Retrieves the Bool value of this JSONValue +* Use isBool() before using this method. +* +* @access public +* +* @return bool Returns the bool value +*/ +bool JSONValue::asBool() const { + return _boolValue; +} + +/** +* Retrieves the Number value of this JSONValue +* Use isNumber() before using this method. +* +* @access public +* +* @return double Returns the number value +*/ +double JSONValue::asNumber() const { + return _numberValue; +} + +/** +* Retrieves the Integer value of this JSONValue +* Use isIntegerNumber() before using this method. +* +* @access public +* +* @return int Returns the number value +*/ +long long int JSONValue::asIntegerNumber() const { + return _integerValue; +} + +/** +* Retrieves the Array value of this JSONValue +* Use isArray() before using this method. +* +* @access public +* +* @return JSONArray Returns the array value +*/ +const JSONArray &JSONValue::asArray() const { + return (*_arrayValue); +} + +/** +* Retrieves the Object value of this JSONValue +* Use isObject() before using this method. +* +* @access public +* +* @return JSONObject Returns the object value +*/ +const JSONObject &JSONValue::asObject() const { + return (*_objectValue); +} + +/** +* Retrieves the number of children of this JSONValue. +* This number will be 0 or the actual number of children +* if isArray() or isObject(). +* +* @access public +* +* @return The number of children. +*/ +std::size_t JSONValue::countChildren() const { + switch (_type) { + case JSONType_Array: + return _arrayValue->size(); + case JSONType_Object: + return _objectValue->size(); + default: + return 0; + } +} + +/** +* Checks if this JSONValue has a child at the given index. +* Use isArray() before using this method. +* +* @access public +* +* @return bool Returns true if the array has a value at the given index. +*/ +bool JSONValue::hasChild(std::size_t index) const { + if (_type == JSONType_Array) { + return index < _arrayValue->size(); + } else { + return false; + } +} + +/** +* Retrieves the child of this JSONValue at the given index. +* Use isArray() before using this method. +* +* @access public +* +* @return JSONValue* Returns JSONValue at the given index or NULL +* if it doesn't exist. +*/ +JSONValue *JSONValue::child(std::size_t index) { + if (index < _arrayValue->size()) { + return (*_arrayValue)[index]; + } else { + return NULL; + } +} + +/** +* Checks if this JSONValue has a child at the given key. +* Use isObject() before using this method. +* +* @access public +* +* @return bool Returns true if the object has a value at the given key. +*/ +bool JSONValue::hasChild(const char *name) const { + if (_type == JSONType_Object) { + return _objectValue->find(name) != _objectValue->end(); + } else { + return false; + } +} + +/** +* Retrieves the child of this JSONValue at the given key. +* Use isObject() before using this method. +* +* @access public +* +* @return JSONValue* Returns JSONValue for the given key in the object +* or NULL if it doesn't exist. +*/ +JSONValue *JSONValue::child(const char *name) { + JSONObject::const_iterator it = _objectValue->find(name); + if (it != _objectValue->end()) { + return it->_value; + } else { + return NULL; + } +} + +/** +* Retrieves the keys of the JSON Object or an empty vector +* if this value is not an object. +* +* @access public +* +* @return std::vector<String> A vector containing the keys. +*/ +Array<String> JSONValue::objectKeys() const { + Array<String> keys; + + if (_type == JSONType_Object) { + JSONObject::const_iterator iter = _objectValue->begin(); + while (iter != _objectValue->end()) { + keys.push_back(iter->_key); + + iter++; + } + } + + return keys; +} + +/** +* Creates a JSON encoded string for the value with all necessary characters escaped +* +* @access public +* +* @param bool prettyprint Enable prettyprint +* +* @return String Returns the JSON string +*/ +String JSONValue::stringify(bool const prettyprint) const { + size_t const indentDepth = prettyprint ? 1 : 0; + return stringifyImpl(indentDepth); +} + + +/** +* Creates a JSON encoded string for the value with all necessary characters escaped +* +* @access private +* +* @param size_t indentDepth The prettyprint indentation depth (0 : no prettyprint) +* +* @return String Returns the JSON string +*/ +String JSONValue::stringifyImpl(size_t const indentDepth) const { + String ret_string; + size_t const indentDepth1 = indentDepth ? indentDepth + 1 : 0; + String const indentStr = indent(indentDepth); + String const indentStr1 = indent(indentDepth1); + + switch (_type) { + case JSONType_Null: + ret_string = "null"; + break; + + case JSONType_String: + ret_string = stringifyString(*_stringValue); + break; + + case JSONType_Bool: + ret_string = _boolValue ? "true" : "false"; + break; + + case JSONType_Number: { + if (isinf(_numberValue) || isnan(_numberValue)) + ret_string = "null"; + else { + char str[80]; + sprintf(str, "%lg", _numberValue); + ret_string = str; + } + break; + } + + case JSONType_IntegerNumber: { + char str[80]; + sprintf(str, "%lld", _integerValue); + ret_string = str; + break; + } + + case JSONType_Array: { + ret_string = indentDepth ? "[\n" + indentStr1 : "["; + JSONArray::const_iterator iter = _arrayValue->begin(); + while (iter != _arrayValue->end()) { + ret_string += (*iter)->stringifyImpl(indentDepth1); + + // Not at the end - add a separator + if (++iter != _arrayValue->end()) + ret_string += ","; + } + ret_string += indentDepth ? "\n" + indentStr + "]" : "]"; + break; + } + + case JSONType_Object: { + ret_string = indentDepth ? "{\n" + indentStr1 : "{"; + JSONObject::const_iterator iter = _objectValue->begin(); + while (iter != _objectValue->end()) { + ret_string += stringifyString((*iter)._key); + ret_string += ":"; + ret_string += (*iter)._value->stringifyImpl(indentDepth1); + + // Not at the end - add a separator + if (++iter != _objectValue->end()) + ret_string += ","; + } + ret_string += indentDepth ? "\n" + indentStr + "}" : "}"; + break; + } + } + + return ret_string; +} + +/** +* Creates a JSON encoded string with all required fields escaped +* Works from http://www.ecma-internationl.org/publications/files/ECMA-ST/ECMA-262.pdf +* Section 15.12.3. +* +* @access private +* +* @param String str The string that needs to have the characters escaped +* +* @return String Returns the JSON string +*/ +String JSONValue::stringifyString(const String &str) { + String str_out = "\""; + + String::const_iterator iter = str.begin(); + while (iter != str.end()) { + char chr = *iter; + + if (chr == '"' || chr == '\\' || chr == '/') { + str_out += '\\'; + str_out += chr; + } else if (chr == '\b') { + str_out += "\\b"; + } else if (chr == '\f') { + str_out += "\\f"; + } else if (chr == '\n') { + str_out += "\\n"; + } else if (chr == '\r') { + str_out += "\\r"; + } else if (chr == '\t') { + str_out += "\\t"; + } else if (chr < ' ' || chr > 126) { + str_out += "\\u"; + for (int i = 0; i < 4; i++) { + int value = (chr >> 12) & 0xf; + if (value >= 0 && value <= 9) + str_out += (char)('0' + value); + else if (value >= 10 && value <= 15) + str_out += (char)('A' + (value - 10)); + chr <<= 4; + } + } else { + str_out += chr; + } + + iter++; + } + + str_out += "\""; + return str_out; +} + +/** +* Creates the indentation string for the depth given +* +* @access private +* +* @param size_t indent The prettyprint indentation depth (0 : no indentation) +* +* @return String Returns the string +*/ +String JSONValue::indent(size_t depth) { + const size_t indent_step = 2; + depth ? --depth : 0; + String indentStr; + for (int i = 0; i < depth * indent_step; ++i) indentStr += ' '; + return indentStr; +} + +} // End of namespace Common diff --git a/common/json.h b/common/json.h new file mode 100644 index 0000000000..d9bbbdb77c --- /dev/null +++ b/common/json.h @@ -0,0 +1,166 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +/* +* Files JSON.h and JSONValue.h part of the SimpleJSON Library - http://mjpa.in/json +* +* Copyright (C) 2010 Mike Anchor +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#ifndef COMMON_JSON_H +#define COMMON_JSON_H + +#include "common/array.h" +#include "common/hashmap.h" +#include "common/hash-str.h" +#include "common/str.h" + +// Win32 incompatibilities +#if defined(WIN32) && !defined(__GNUC__) +static inline bool isnan(double x) { + return x != x; +} + +static inline bool isinf(double x) { + return !isnan(x) && isnan(x - x); +} +#endif + +// Simple function to check a string 's' has at least 'n' characters +static inline bool simplejson_wcsnlen(const char *s, size_t n) { + if (s == 0) + return false; + + const char *save = s; + while (n-- > 0) { + if (*(save++) == 0) return false; + } + + return true; +} + +namespace Common { + +// Custom types +class JSONValue; +typedef Array<JSONValue*> JSONArray; +typedef HashMap<String, JSONValue*> JSONObject; + +class JSON; + +enum JSONType { JSONType_Null, JSONType_String, JSONType_Bool, JSONType_Number, JSONType_IntegerNumber, JSONType_Array, JSONType_Object }; + +class JSONValue { + friend class JSON; + +public: + JSONValue(/*NULL*/); + JSONValue(const char *charValue); + JSONValue(const String &stringValue); + JSONValue(bool boolValue); + JSONValue(double numberValue); + JSONValue(long long int numberValue); + JSONValue(const JSONArray &arrayValue); + JSONValue(const JSONObject &objectValue); + JSONValue(const JSONValue &source); + ~JSONValue(); + + bool isNull() const; + bool isString() const; + bool isBool() const; + bool isNumber() const; + bool isIntegerNumber() const; + bool isArray() const; + bool isObject() const; + + const String &asString() const; + bool asBool() const; + double asNumber() const; + long long int asIntegerNumber() const; + const JSONArray &asArray() const; + const JSONObject &asObject() const; + + size_t countChildren() const; + bool hasChild(size_t index) const; + JSONValue *child(size_t index); + bool hasChild(const char *name) const; + JSONValue *child(const char *name); + Array<String> objectKeys() const; + + String stringify(bool const prettyprint = false) const; +protected: + static JSONValue *parse(const char **data); + +private: + static String stringifyString(const String &str); + String stringifyImpl(size_t const indentDepth) const; + static String indent(size_t depth); + + JSONType _type; + + union { + bool _boolValue; + double _numberValue; + long long int _integerValue; + String *_stringValue; + JSONArray *_arrayValue; + JSONObject *_objectValue; + }; + +}; + +class JSON { + friend class JSONValue; + +public: + static JSONValue *parse(const char *data); + static String stringify(const JSONValue *value); +protected: + static bool skipWhitespace(const char **data); + static bool extractString(const char **data, String &str); + static double parseInt(const char **data); + static double parseDecimal(const char **data); +private: + JSON(); +}; + +} // End of namespace Common + +#endif diff --git a/common/macresman.cpp b/common/macresman.cpp index d83bde8fd8..adca1ea10b 100644 --- a/common/macresman.cpp +++ b/common/macresman.cpp @@ -29,6 +29,7 @@ #include "common/md5.h" #include "common/substream.h" #include "common/textconsole.h" +#include "common/archive.h" #ifdef MACOSX #include "common/config-manager.h" @@ -261,6 +262,76 @@ bool MacResManager::exists(const String &fileName) { return false; } +void MacResManager::listFiles(StringArray &files, const String &pattern) { + // Base names discovered so far. + typedef HashMap<String, bool, IgnoreCase_Hash, IgnoreCase_EqualTo> BaseNameSet; + BaseNameSet baseNames; + + // List files itself. + ArchiveMemberList memberList; + SearchMan.listMatchingMembers(memberList, pattern); + SearchMan.listMatchingMembers(memberList, pattern + ".rsrc"); + SearchMan.listMatchingMembers(memberList, pattern + ".bin"); + SearchMan.listMatchingMembers(memberList, constructAppleDoubleName(pattern)); + + for (ArchiveMemberList::const_iterator i = memberList.begin(), end = memberList.end(); i != end; ++i) { + String filename = (*i)->getName(); + + // For raw resource forks and MacBinary files we strip the extension + // here to obtain a valid base name. + int lastDotPos = filename.size() - 1; + for (; lastDotPos >= 0; --lastDotPos) { + if (filename[lastDotPos] == '.') { + break; + } + } + + if (lastDotPos != -1) { + const char *extension = filename.c_str() + lastDotPos + 1; + bool removeExtension = false; + + // TODO: Should we really keep filenames suggesting raw resource + // forks or MacBinary files but not being such around? This might + // depend on the pattern the client requests... + if (!scumm_stricmp(extension, "rsrc")) { + SeekableReadStream *stream = (*i)->createReadStream(); + removeExtension = stream && isRawFork(*stream); + delete stream; + } else if (!scumm_stricmp(extension, "bin")) { + SeekableReadStream *stream = (*i)->createReadStream(); + removeExtension = stream && isMacBinary(*stream); + delete stream; + } + + if (removeExtension) { + filename.erase(lastDotPos); + } + } + + // Strip AppleDouble '._' prefix if applicable. + bool isAppleDoubleName = false; + const String filenameAppleDoubleStripped = disassembleAppleDoubleName(filename, &isAppleDoubleName); + + if (isAppleDoubleName) { + SeekableReadStream *stream = (*i)->createReadStream(); + if (stream->readUint32BE() == 0x00051607) { + filename = filenameAppleDoubleStripped; + } + // TODO: Should we really keep filenames suggesting AppleDouble + // but not being AppleDouble around? This might depend on the + // pattern the client requests... + delete stream; + } + + baseNames[filename] = true; + } + + // Append resulting base names to list to indicate found files. + for (BaseNameSet::const_iterator i = baseNames.begin(), end = baseNames.end(); i != end; ++i) { + files.push_back(i->_key); + } +} + bool MacResManager::loadFromAppleDouble(SeekableReadStream &stream) { if (stream.readUint32BE() != 0x00051607) // tag return false; @@ -314,6 +385,18 @@ bool MacResManager::isMacBinary(SeekableReadStream &stream) { return true; } +bool MacResManager::isRawFork(SeekableReadStream &stream) { + // TODO: Is there a better way to detect whether this is a raw fork? + const uint32 dataOffset = stream.readUint32BE(); + const uint32 mapOffset = stream.readUint32BE(); + const uint32 dataLength = stream.readUint32BE(); + const uint32 mapLength = stream.readUint32BE(); + + return !stream.eos() && !stream.err() + && dataOffset < (uint32)stream.size() && dataOffset + dataLength <= (uint32)stream.size() + && mapOffset < (uint32)stream.size() && mapOffset + mapLength <= (uint32)stream.size(); +} + bool MacResManager::loadFromMacBinary(SeekableReadStream &stream) { byte infoHeader[MBI_INFOHDR]; stream.read(infoHeader, MBI_INFOHDR); @@ -592,4 +675,32 @@ String MacResManager::constructAppleDoubleName(String name) { return name; } +String MacResManager::disassembleAppleDoubleName(String name, bool *isAppleDouble) { + if (isAppleDouble) { + *isAppleDouble = false; + } + + // Remove "._" before the last portion of a path name. + for (int i = name.size() - 1; i >= 0; --i) { + if (i == 0) { + if (name.size() > 2 && name[0] == '.' && name[1] == '_') { + name.erase(0, 2); + if (isAppleDouble) { + *isAppleDouble = true; + } + } + } else if (name[i] == '/') { + if ((uint)(i + 2) < name.size() && name[i + 1] == '.' && name[i + 2] == '_') { + name.erase(i + 1, 2); + if (isAppleDouble) { + *isAppleDouble = true; + } + } + break; + } + } + + return name; +} + } // End of namespace Common diff --git a/common/macresman.h b/common/macresman.h index 43ec8d8e2c..05b2a875f4 100644 --- a/common/macresman.h +++ b/common/macresman.h @@ -33,6 +33,7 @@ #include "common/array.h" #include "common/fs.h" #include "common/str.h" +#include "common/str-array.h" #ifndef COMMON_MACRESMAN_H #define COMMON_MACRESMAN_H @@ -82,6 +83,16 @@ public: static bool exists(const String &fileName); /** + * List all filenames matching pattern for opening with open(). + * + * @param files Array containing all matching filenames discovered. Only + * adds to the list. + * @param pattern Pattern to match against. Taking String::matchPattern's + * format. + */ + static void listFiles(StringArray &files, const String &pattern); + + /** * Close the Mac data/resource fork pair. */ void close(); @@ -176,6 +187,7 @@ private: bool loadFromAppleDouble(SeekableReadStream &stream); static String constructAppleDoubleName(String name); + static String disassembleAppleDoubleName(String name, bool *isAppleDouble); /** * Check if the given stream is in the MacBinary format. @@ -183,6 +195,13 @@ private: */ static bool isMacBinary(SeekableReadStream &stream); + /** + * Do a sanity check whether the given stream is a raw resource fork. + * + * @param stream Stream object to check. Will not preserve its position. + */ + static bool isRawFork(SeekableReadStream &stream); + enum { kResForkNone = 0, kResForkRaw, diff --git a/common/memstream.h b/common/memstream.h index 5ecc553454..25fdde91c7 100644 --- a/common/memstream.h +++ b/common/memstream.h @@ -25,6 +25,7 @@ #include "common/stream.h" #include "common/types.h" +#include "common/util.h" namespace Common { @@ -110,7 +111,7 @@ public: return dataSize; } - uint32 pos() const { return _pos; } + int32 pos() const { return _pos; } uint32 size() const { return _bufSize; } virtual bool err() const { return _err; } @@ -156,7 +157,7 @@ public: * that grows as it's written to. */ class MemoryWriteStreamDynamic : public WriteStream { -private: +protected: uint32 _capacity; uint32 _size; byte *_ptr; @@ -170,7 +171,7 @@ private: byte *old_data = _data; - _capacity = new_len + 32; + _capacity = MAX(new_len + 32, _capacity * 2); _data = (byte *)malloc(_capacity); _ptr = _data + _pos; @@ -200,7 +201,7 @@ public: return dataSize; } - uint32 pos() const { return _pos; } + int32 pos() const { return _pos; } uint32 size() const { return _size; } byte *getData() { return _data; } @@ -208,6 +209,89 @@ public: bool seek(int32 offset, int whence = SEEK_SET); }; +/** +* MemoryStream based on RingBuffer. Grows if has insufficient buffer size. +*/ +class MemoryReadWriteStream : public WriteStream { +private: + uint32 _capacity; + uint32 _size; + byte *_data; + uint32 _writePos, _readPos, _pos, _length; + DisposeAfterUse::Flag _disposeMemory; + + void ensureCapacity(uint32 new_len) { + if (new_len <= _capacity) + return; + + byte *old_data = _data; + uint32 oldCapacity = _capacity; + + _capacity = MAX(new_len + 32, _capacity * 2); + _data = (byte *)malloc(_capacity); + + if (old_data) { + // Copy old data + if (_readPos < _writePos) { + memcpy(_data, old_data + _readPos, _writePos - _readPos); + _writePos = _length; + _readPos = 0; + } else { + memcpy(_data, old_data + _readPos, oldCapacity - _readPos); + memcpy(_data + oldCapacity - _readPos, old_data, _writePos); + _writePos = _length; + _readPos = 0; + } + free(old_data); + } + } +public: + MemoryReadWriteStream(DisposeAfterUse::Flag disposeMemory = DisposeAfterUse::NO) : _capacity(0), _size(0), _data(0), _writePos(0), _readPos(0), _pos(0), _length(0), _disposeMemory(disposeMemory) {} + + ~MemoryReadWriteStream() { + if (_disposeMemory) + free(_data); + } + + uint32 write(const void *dataPtr, uint32 dataSize) { + ensureCapacity(_length + dataSize); + if (_writePos + dataSize < _capacity) { + memcpy(_data + _writePos, dataPtr, dataSize); + } else { + memcpy(_data + _writePos, dataPtr, _capacity - _writePos); + const byte *shiftedPtr = (const byte *)dataPtr + _capacity - _writePos; + memcpy(_data, shiftedPtr, dataSize - (_capacity - _writePos)); + } + _writePos = (_writePos + dataSize) % _capacity; + _pos += dataSize; + _length += dataSize; + if (_pos > _size) + _size = _pos; + return dataSize; + } + + virtual uint32 read(void *dataPtr, uint32 dataSize) { + uint32 length = _length; + if (length < dataSize) dataSize = length; + if (dataSize == 0 || _capacity == 0) return 0; + if (_readPos + dataSize < _capacity) { + memcpy(dataPtr, _data + _readPos, dataSize); + } else { + memcpy(dataPtr, _data + _readPos, _capacity - _readPos); + byte *shiftedPtr = (byte *)dataPtr + _capacity - _readPos; + memcpy(shiftedPtr, _data, dataSize - (_capacity - _readPos)); + } + _readPos = (_readPos + dataSize) % _capacity; + _length -= dataSize; + return dataSize; + } + + int32 pos() const { return _pos - _length; } //'read' position in the stream + uint32 size() const { return _size; } //that's also 'write' position in the stream, as it's append-only + + byte *getData() { return _data; } +}; + } // End of namespace Common #endif diff --git a/common/module.mk b/common/module.mk index 67c498df00..54aa16f557 100644 --- a/common/module.mk +++ b/common/module.mk @@ -16,6 +16,7 @@ MODULE_OBJS := \ iff_container.o \ ini-file.o \ installshield_cab.o \ + json.o \ language.o \ localization.o \ macresman.o \ @@ -56,5 +57,10 @@ MODULE_OBJS += \ recorderfile.o endif +ifdef USE_UPDATES +MODULE_OBJS += \ + updates.o +endif + # Include common rules include $(srcdir)/rules.mk diff --git a/common/platform.cpp b/common/platform.cpp index 636c1ddb52..280185d862 100644 --- a/common/platform.cpp +++ b/common/platform.cpp @@ -27,6 +27,7 @@ namespace Common { const PlatformDescription g_platforms[] = { { "2gs", "2gs", "2gs", "Apple IIgs", kPlatformApple2GS }, + { "apple2", "apple2", "apple2", "Apple II", kPlatformApple2 }, { "3do", "3do", "3do", "3DO", kPlatform3DO }, { "acorn", "acorn", "acorn", "Acorn", kPlatformAcorn }, { "amiga", "ami", "amiga", "Amiga", kPlatformAmiga }, diff --git a/common/platform.h b/common/platform.h index 17a332b851..15bcddb62e 100644 --- a/common/platform.h +++ b/common/platform.h @@ -50,6 +50,7 @@ enum Platform { kPlatformSegaCD, kPlatform3DO, kPlatformPCEngine, + kPlatformApple2, kPlatformApple2GS, kPlatformPC98, kPlatformWii, diff --git a/common/rational.h b/common/rational.h index 55fb361774..89caaf25b4 100644 --- a/common/rational.h +++ b/common/rational.h @@ -84,6 +84,8 @@ public: int getNumerator() const { return _num; } int getDenominator() const { return _denom; } + bool isOne() const { return _num == _denom; } + void debugPrint(int debuglevel = 0, const char *caption = "Rational:") const; private: diff --git a/common/rect.h b/common/rect.h index 32424d3e6a..e6534e55d3 100644 --- a/common/rect.h +++ b/common/rect.h @@ -163,7 +163,8 @@ struct Rect { * * @param r the rectangle to check * - * @return true if the given rectangle is inside the rectangle, false otherwise + * @return true if the given rectangle has a non-empty intersection with + * this rectangle, false otherwise */ bool intersects(const Rect &r) const { return (left < r.right) && (r.left < right) && (top < r.bottom) && (r.top < bottom); diff --git a/common/rendermode.cpp b/common/rendermode.cpp index 6115666399..e07cac4b4e 100644 --- a/common/rendermode.cpp +++ b/common/rendermode.cpp @@ -38,9 +38,12 @@ const RenderModeDescription g_renderModes[] = { { "ega", "EGA", kRenderEGA }, { "vga", "VGA", kRenderVGA }, { "amiga", "Amiga", kRenderAmiga }, - { "fmtowns", "FM-Towns", kRenderFMTowns }, - { "pc9821", "PC-9821 (256 Colors)", kRenderPC9821 }, - { "pc9801", "PC-9801 (16 Colors)", kRenderPC9801 }, + { "fmtowns", "FM-TOWNS", kRenderFMTowns }, + { "pc9821", _s("PC-9821 (256 Colors)"), kRenderPC9821 }, + { "pc9801", _s("PC-9801 (16 Colors)"), kRenderPC9801 }, + { "2gs", "Apple IIgs", kRenderApple2GS }, + { "atari", "Atari ST", kRenderAtariST }, + { "macintosh", "Macintosh", kRenderMacintosh }, {0, 0, kRenderDefault} }; @@ -53,15 +56,18 @@ struct RenderGUIOMapping { // could be used to indicate "any" mode when passed to renderMode2GUIO (if // we wanted to merge allRenderModesGUIOs back into) static const RenderGUIOMapping s_renderGUIOMapping[] = { - { kRenderHercG, GUIO_RENDERHERCGREEN }, - { kRenderHercA, GUIO_RENDERHERCAMBER }, - { kRenderCGA, GUIO_RENDERCGA }, - { kRenderEGA, GUIO_RENDEREGA }, - { kRenderVGA, GUIO_RENDERVGA }, - { kRenderAmiga, GUIO_RENDERAMIGA }, - { kRenderFMTowns, GUIO_RENDERFMTOWNS }, - { kRenderPC9821, GUIO_RENDERPC9821 }, - { kRenderPC9801, GUIO_RENDERPC9801 } + { kRenderHercG, GUIO_RENDERHERCGREEN }, + { kRenderHercA, GUIO_RENDERHERCAMBER }, + { kRenderCGA, GUIO_RENDERCGA }, + { kRenderEGA, GUIO_RENDEREGA }, + { kRenderVGA, GUIO_RENDERVGA }, + { kRenderAmiga, GUIO_RENDERAMIGA }, + { kRenderFMTowns, GUIO_RENDERFMTOWNS }, + { kRenderPC9821, GUIO_RENDERPC9821 }, + { kRenderPC9801, GUIO_RENDERPC9801 }, + { kRenderApple2GS, GUIO_RENDERAPPLE2GS }, + { kRenderAtariST, GUIO_RENDERATARIST }, + { kRenderMacintosh, GUIO_RENDERMACINTOSH } }; DECLARE_TRANSLATION_ADDITIONAL_CONTEXT("Hercules Green", "lowres") diff --git a/common/rendermode.h b/common/rendermode.h index 59fa860c6c..ae1a7bc790 100644 --- a/common/rendermode.h +++ b/common/rendermode.h @@ -45,7 +45,10 @@ enum RenderMode { kRenderAmiga = 6, kRenderFMTowns = 7, kRenderPC9821 = 8, - kRenderPC9801 = 9 + kRenderPC9801 = 9, + kRenderApple2GS = 10, + kRenderAtariST = 11, + kRenderMacintosh = 12 }; struct RenderModeDescription { diff --git a/common/savefile.h b/common/savefile.h index b0c4d31f53..eb7e6f946e 100644 --- a/common/savefile.h +++ b/common/savefile.h @@ -28,6 +28,7 @@ #include "common/stream.h" #include "common/str-array.h" #include "common/error.h" +#include "common/ptr.h" namespace Common { @@ -44,8 +45,21 @@ typedef SeekableReadStream InSaveFile; * That typically means "save games", but also includes things like the * IQ points in Indy3. */ -typedef WriteStream OutSaveFile; +class OutSaveFile: public WriteStream { +protected: + ScopedPtr<WriteStream> _wrapped; +public: + OutSaveFile(WriteStream *w); + virtual ~OutSaveFile(); + + virtual bool err() const; + virtual void clearErr(); + virtual void finalize(); + virtual bool flush(); + virtual uint32 write(const void *dataPtr, uint32 dataSize); + virtual int32 pos() const; +}; /** * The SaveFileManager is serving as a factory for InSaveFile @@ -56,6 +70,12 @@ typedef WriteStream OutSaveFile; * i.e. typically save states, but also configuration files and similar * things. * + * Savefile names represent SaveFiles. These names are case insensitive, that + * means a name of "Kq1.000" represents the same savefile as "kq1.000". In + * addition, SaveFileManager does not allow for names which contain path + * separators like '/' or '\'. This is because we do not support directories + * in SaveFileManager. + * * While not declared as a singleton, it is effectively used as such, * with OSystem::getSavefileManager returning a pointer to the single * SaveFileManager instances to be used. @@ -115,52 +135,75 @@ public: * exports from the Quest for Glory series. QfG5 is a 3D game and won't be * supported by ScummVM. * - * @param name the name of the savefile - * @param compress toggles whether to compress the resulting save file - * (default) or not. - * @return pointer to an OutSaveFile, or NULL if an error occurred. + * @param name The name of the savefile. + * @param compress Toggles whether to compress the resulting save file + * (default) or not. + * @return Pointer to an OutSaveFile, or NULL if an error occurred. */ virtual OutSaveFile *openForSaving(const String &name, bool compress = true) = 0; /** * Open the file with the specified name in the given directory for loading. - * @param name the name of the savefile - * @return pointer to an InSaveFile, or NULL if an error occurred. + * + * @param name The name of the savefile. + * @return Pointer to an InSaveFile, or NULL if an error occurred. */ virtual InSaveFile *openForLoading(const String &name) = 0; /** + * Open the file with the specified name in the given directory for loading. + * In contrast to openForLoading(), it returns raw file instead of unpacked. + * + * @param name The name of the savefile. + * @return Pointer to an InSaveFile, or NULL if an error occurred. + */ + virtual InSaveFile *openRawFile(const String &name) = 0; + + /** * Removes the given savefile from the system. - * @param name the name of the savefile to be removed. + * + * @param name The name of the savefile to be removed. * @return true if no error occurred, false otherwise. */ virtual bool removeSavefile(const String &name) = 0; /** * Renames the given savefile. - * @param oldName Old name. - * @param newName New name. + * + * @param oldName Old name. + * @param newName New name. * @return true if no error occurred. false otherwise. */ virtual bool renameSavefile(const String &oldName, const String &newName); /** * Copy the given savefile. - * @param oldName Old name. - * @param newName New name. + * + * @param oldName Old name. + * @param newName New name. * @return true if no error occurred. false otherwise. */ virtual bool copySavefile(const String &oldName, const String &newName); /** - * Request a list of available savegames with a given DOS-style pattern, - * also known as "glob" in the POSIX world. Refer to the Common::matchString() - * function to learn about the precise pattern format. - * @param pattern Pattern to match. Wildcards like * or ? are available. - * @return list of strings for all present file names. + * List available savegames matching a given pattern. + * + * Our pattern format is based on DOS paterns, also known as "glob" in the + * POSIX world. Please refer to the Common::matchString() function to learn + * about the precise pattern format. + * + * @param pattern Pattern to match. Wildcards like * or ? are available. + * @return List of strings for all present file names. * @see Common::matchString() */ virtual StringArray listSavefiles(const String &pattern) = 0; + + /** + * Refreshes the save files list (because some new files could've been added) + * and remembers the "locked" files list. These files could not be used + * for saving or loading because they are being synced by CloudManager. + */ + virtual void updateSavefilesList(StringArray &lockedFiles) = 0; }; } // End of namespace Common diff --git a/common/scummsys.h b/common/scummsys.h index 0c4687e03e..959c67a404 100644 --- a/common/scummsys.h +++ b/common/scummsys.h @@ -23,13 +23,17 @@ #ifndef COMMON_SCUMMSYS_H #define COMMON_SCUMMSYS_H -#ifndef __has_feature // Optional of course. - #define __has_feature(x) 0 // Compatibility with non-clang compilers. +#ifndef __has_feature // Optional of course. + #define __has_feature(x) 0 // Compatibility with non-clang compilers. #endif // This is a convenience macro to test whether the compiler used is a GCC // version, which is at least major.minor. -#define GCC_ATLEAST(major, minor) (defined(__GNUC__) && (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))) +#ifdef __GNUC__ + #define GCC_ATLEAST(major, minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) +#else + #define GCC_ATLEAST(major, minor) 0 +#endif #if defined(_WIN32_WCE) && _WIN32_WCE < 300 #define NONSTANDARD_PORT @@ -46,7 +50,7 @@ #if defined(WIN32) - #ifdef _MSC_VER + #if defined(_MSC_VER) && _MSC_VER <= 1800 // FIXME: The placement of the workaround functions for MSVC below // require us to include stdio.h and stdarg.h for MSVC here. This @@ -251,6 +255,7 @@ #if defined(__DC__) || \ defined(__DS__) || \ + defined(__3DS__) || \ defined(__GP32__) || \ defined(IPHONE) || \ defined(__PLAYSTATION2__) || \ @@ -367,11 +372,11 @@ #endif #ifndef STRINGBUFLEN - #if defined(__N64__) || defined(__DS__) - #define STRINGBUFLEN 256 - #else - #define STRINGBUFLEN 1024 - #endif + #if defined(__N64__) || defined(__DS__) || defined(__3DS__) + #define STRINGBUFLEN 256 + #else + #define STRINGBUFLEN 1024 + #endif #endif #ifndef MAXPATHLEN diff --git a/common/str.cpp b/common/str.cpp index faf84d722f..90bd539790 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -75,7 +75,7 @@ void String::initWithCStr(const char *str, uint32 len) { } String::String(const String &str) - : _size(str._size) { + : _size(str._size) { if (str.isStorageIntern()) { // String in internal storage: just copy it memcpy(_storage, str._storage, _builtinCapacity); @@ -91,7 +91,7 @@ String::String(const String &str) } String::String(char c) - : _size(0), _str(_storage) { + : _size(0), _str(_storage) { _storage[0] = c; _storage[1] = 0; @@ -132,24 +132,19 @@ void String::ensureCapacity(uint32 new_size, bool keep_old) { if (!isShared && new_size < curCapacity) return; - if (isShared && new_size < _builtinCapacity) { - // We share the storage, but there is enough internal storage: Use that. - newStorage = _storage; - newCapacity = _builtinCapacity; - } else { - // We need to allocate storage on the heap! - - // Compute a suitable new capacity limit - // If the current capacity is sufficient we use the same capacity - if (new_size < curCapacity) - newCapacity = curCapacity; - else - newCapacity = MAX(curCapacity * 2, computeCapacity(new_size+1)); - - // Allocate new storage - newStorage = new char[newCapacity]; - assert(newStorage); - } + // We need to allocate storage on the heap! + + // Compute a suitable new capacity limit + // If the current capacity is sufficient we use the same capacity + if (new_size < curCapacity) + newCapacity = curCapacity; + else + newCapacity = MAX(curCapacity * 2, computeCapacity(new_size+1)); + + // Allocate new storage + newStorage = new char[newCapacity]; + assert(newStorage); + // Copy old data if needed, elsewise reset the new storage. if (keep_old) { @@ -340,6 +335,15 @@ bool String::contains(char x) const { return strchr(c_str(), x) != NULL; } +uint64 String::asUint64() const { + uint64 result = 0; + for (uint32 i = 0; i < _size; ++i) { + if (_str[i] < '0' || _str[i] > '9') break; + result = result * 10L + (_str[i] - '0'); + } + return result; +} + bool String::matchString(const char *pat, bool ignoreCase, bool pathMode) const { return Common::matchString(c_str(), pat, ignoreCase, pathMode); } @@ -444,6 +448,58 @@ uint String::hash() const { return hashit(c_str()); } +void String::replace(uint32 pos, uint32 count, const String &str) { + replace(pos, count, str, 0, str._size); +} + +void String::replace(uint32 pos, uint32 count, const char *str) { + replace(pos, count, str, 0, strlen(str)); +} + +void String::replace(iterator begin_, iterator end_, const String &str) { + replace(begin_ - _str, end_ - begin_, str._str, 0, str._size); +} + +void String::replace(iterator begin_, iterator end_, const char *str) { + replace(begin_ - _str, end_ - begin_, str, 0, strlen(str)); +} + +void String::replace(uint32 posOri, uint32 countOri, const String &str, + uint32 posDest, uint32 countDest) { + replace(posOri, countOri, str._str, posDest, countDest); +} + +void String::replace(uint32 posOri, uint32 countOri, const char *str, + uint32 posDest, uint32 countDest) { + + ensureCapacity(_size + countDest - countOri, true); + + // Prepare string for the replaced text. + if (countOri < countDest) { + uint32 offset = countDest - countOri; ///< Offset to copy the characters + uint32 newSize = _size + offset; + _size = newSize; + + // Push the old characters to the end of the string + for (uint32 i = _size; i >= posOri + countDest; i--) + _str[i] = _str[i - offset]; + + } else if (countOri > countDest){ + uint32 offset = countOri - countDest; ///< Number of positions that we have to pull back + + // Pull the remainder string back + for (uint32 i = posOri + countDest; i < _size; i++) + _str[i] = _str[i + offset]; + + _size -= offset; + } + + // Copy the replaced part of the string + for (uint32 i = 0; i < countDest; i++) + _str[posOri + i] = str[posDest + i]; + +} + // static String String::format(const char *fmt, ...) { String output; @@ -751,6 +807,13 @@ bool matchString(const char *str, const char *pat, bool ignoreCase, bool pathMod return true; break; + case '#': + if (!isDigit(*str)) + return false; + pat++; + str++; + break; + default: if ((!ignoreCase && *pat != *str) || (ignoreCase && tolower(*pat) != tolower(*str))) { @@ -775,6 +838,15 @@ bool matchString(const char *str, const char *pat, bool ignoreCase, bool pathMod } } +void replace(Common::String &source, const Common::String &what, const Common::String &with) { + const char *cstr = source.c_str(); + const char *position = strstr(cstr, what.c_str()); + if (position) { + uint32 index = position - cstr; + source.replace(index, what.size(), with); + } +} + String tag2string(uint32 tag) { char str[5]; str[0] = (char)(tag >> 24); diff --git a/common/str.h b/common/str.h index dede87a005..d55ba072a9 100644 --- a/common/str.h +++ b/common/str.h @@ -46,6 +46,17 @@ namespace Common { class String { public: static const uint32 npos = 0xFFFFFFFF; + + typedef char value_type; + /** + * Unsigned version of the underlying type. This can be used to cast + * individual string characters to bigger integer types without sign + * extension happening. + */ + typedef unsigned char unsigned_type; + typedef char * iterator; + typedef const char * const_iterator; + protected: /** * The size of the internal storage. Increasing this means less heap @@ -151,6 +162,9 @@ public: bool contains(const char *x) const; bool contains(char x) const; + /** Return uint64 corrensponding to String's contents. */ + uint64 asUint64() const; + /** * Simple DOS-style pattern matching function (understands * and ? like used in DOS). * Taken from exult/files/listfiles.cc @@ -158,6 +172,7 @@ public: * Token meaning: * "*": any character, any amount of times. * "?": any character, only once. + * "#": any decimal digit, only once. * * Example strings/patterns: * String: monkey.s01 Pattern: monkey.s?? => true @@ -165,6 +180,8 @@ public: * String: monkey.s99 Pattern: monkey.s?1 => false * String: monkey.s101 Pattern: monkey.s* => true * String: monkey.s99 Pattern: monkey.s*1 => false + * String: monkey.s01 Pattern: monkey.s## => true + * String: monkey.s01 Pattern: monkey.### => false * * @param pat Glob pattern. * @param ignoreCase Whether to ignore the case when doing pattern match @@ -180,6 +197,7 @@ public: inline uint size() const { return _size; } inline bool empty() const { return (_size == 0); } + char firstChar() const { return (_size > 0) ? _str[0] : 0; } char lastChar() const { return (_size > 0) ? _str[_size - 1] : 0; } char operator[](int idx) const { @@ -219,6 +237,38 @@ public: uint hash() const; + /**@{ + * Functions to replace some amount of chars with chars from some other string. + * + * @note The implementation follows that of the STL's std::string: + * http://www.cplusplus.com/reference/string/string/replace/ + * + * @param pos Starting position for the replace in the original string. + * @param count Number of chars to replace from the original string. + * @param str Source of the new chars. + * @param posOri Same as pos + * @param countOri Same as count + * @param posDest Initial position to read str from. + * @param countDest Number of chars to read from str. npos by default. + */ + // Replace 'count' bytes, starting from 'pos' with str. + void replace(uint32 pos, uint32 count, const String &str); + // The same as above, but accepts a C-like array of characters. + void replace(uint32 pos, uint32 count, const char *str); + // Replace the characters in [begin, end) with str._str. + void replace(iterator begin, iterator end, const String &str); + // Replace the characters in [begin, end) with str. + void replace(iterator begin, iterator end, const char *str); + // Replace _str[posOri, posOri + countOri) with + // str._str[posDest, posDest + countDest) + void replace(uint32 posOri, uint32 countOri, const String &str, + uint32 posDest, uint32 countDest); + // Replace _str[posOri, posOri + countOri) with + // str[posDest, posDest + countDest) + void replace(uint32 posOri, uint32 countOri, const char *str, + uint32 posDest, uint32 countDest); + /**@}*/ + /** * Print formatted data into a String object. Similar to sprintf, * except that it stores the result in (variably sized) String @@ -234,15 +284,6 @@ public: static String vformat(const char *fmt, va_list args); public: - typedef char value_type; - /** - * Unsigned version of the underlying type. This can be used to cast - * individual string characters to bigger integer types without sign - * extension happening. - */ - typedef unsigned char unsigned_type; - typedef char * iterator; - typedef const char * const_iterator; iterator begin() { // Since the user could potentially @@ -329,6 +370,7 @@ String normalizePath(const String &path, const char sep); * Token meaning: * "*": any character, any amount of times. * "?": any character, only once. + * "#": any decimal digit, only once. * * Example strings/patterns: * String: monkey.s01 Pattern: monkey.s?? => true @@ -336,6 +378,8 @@ String normalizePath(const String &path, const char sep); * String: monkey.s99 Pattern: monkey.s?1 => false * String: monkey.s101 Pattern: monkey.s* => true * String: monkey.s99 Pattern: monkey.s*1 => false + * String: monkey.s01 Pattern: monkey.s## => true + * String: monkey.s01 Pattern: monkey.### => false * * @param str Text to be matched against the given pattern. * @param pat Glob pattern. @@ -346,6 +390,15 @@ String normalizePath(const String &path, const char sep); */ bool matchString(const char *str, const char *pat, bool ignoreCase = false, bool pathMode = false); +/** + * Function which replaces substring with the other. It happens in place. + * If there is no substring found, original string is not changed. + * + * @param source String to search and replace substring in. + * @param what Substring to replace. + * @param with String to replace with. + */ +void replace(Common::String &source, const Common::String &what, const Common::String &with); /** * Take a 32 bit value and turn it into a four character string, where each of diff --git a/common/stream.cpp b/common/stream.cpp index 45060b9db5..a8446a9086 100644 --- a/common/stream.cpp +++ b/common/stream.cpp @@ -500,6 +500,8 @@ public: virtual bool flush() { return flushBuffer(); } + virtual int32 pos() const { return _pos; } + }; } // End of anonymous namespace diff --git a/common/stream.h b/common/stream.h index abe5192b70..c6c300fa97 100644 --- a/common/stream.h +++ b/common/stream.h @@ -103,6 +103,14 @@ public: flush(); } + /** + * Obtains the current value of the stream position indicator of the + * stream. + * + * @return the current position indicator, or -1 if an error occurred. + */ + virtual int32 pos() const = 0; + // The remaining methods all have default implementations; subclasses // need not (and should not) overload them. diff --git a/common/system.cpp b/common/system.cpp index 53f28cafa1..131a7d2580 100644 --- a/common/system.cpp +++ b/common/system.cpp @@ -30,9 +30,6 @@ #include "common/taskbar.h" #include "common/updates.h" #include "common/textconsole.h" -#ifdef ENABLE_EVENTRECORDER -#include "gui/EventRecorder.h" -#endif #include "backends/audiocd/default/default-audiocd.h" #include "backends/fs/fs-factory.h" @@ -161,9 +158,5 @@ Common::TimerManager *OSystem::getTimerManager() { } Common::SaveFileManager *OSystem::getSavefileManager() { -#ifdef ENABLE_EVENTRECORDER - return g_eventRec.getSaveManager(_savefileManager); -#else return _savefileManager; -#endif } diff --git a/common/system.h b/common/system.h index 8896554f76..805eba68ed 100644 --- a/common/system.h +++ b/common/system.h @@ -314,7 +314,15 @@ public: * * This feature has no associated state. */ - kFeatureDisplayLogFile + kFeatureDisplayLogFile, + + /** + * The presence of this feature indicates whether the hasTextInClipboard() + * and getTextFromClipboard() calls are supported. + * + * This feature has no associated state. + */ + kFeatureClipboardSupport }; /** @@ -1086,11 +1094,50 @@ public: virtual void displayMessageOnOSD(const char *msg) = 0; /** + * Blit a bitmap to the 'on screen display'. + * + * If the current pixel format has one byte per pixel, the graphics data + * uses 8 bits per pixel, using the palette specified via setPalette. + * If more than one byte per pixel is in use, the graphics data uses the + * pixel format returned by getScreenFormat. + * + * @param buf the buffer containing the graphics data source + * @param pitch the pitch of the buffer (number of bytes in a scanline) + * @param x the x coordinate of the destination rectangle + * @param y the y coordinate of the destination rectangle + * @param w the width of the destination rectangle + * @param h the height of the destination rectangle + * + * @note The specified destination rectangle must be completly contained + * in the visible screen space, and must be non-empty. If not, a + * backend may or may not perform clipping, trigger an assert or + * silently corrupt memory. + * + * @see updateScreen + * @see getScreenFormat + * @see copyRectToScreen + */ + + virtual void copyRectToOSD(const void *buf, int pitch, int x, int y, int w, int h) = 0; + + /** + * Clears 'on screen display' from everything drawn on it. + */ + + virtual void clearOSD() = 0; + + /** + * Returns 'on screen display' pixel format. + */ + + virtual Graphics::PixelFormat getOSDFormat() = 0; + + /** * Return the SaveFileManager, used to store and load savestates * and other modifiable persistent game data. For more information, * refer to the SaveFileManager documentation. */ - Common::SaveFileManager *getSavefileManager(); + virtual Common::SaveFileManager *getSavefileManager(); #if defined(USE_TASKBAR) /** @@ -1200,6 +1247,28 @@ public: virtual bool displayLogFile() { return false; } /** + * Returns whether there is text available in the clipboard. + * + * The kFeatureClipboardSupport feature flag can be used to + * test whether this call has been implemented by the active + * backend. + * + * @return true if there is text in the clipboard, false otherwise + */ + virtual bool hasTextInClipboard() { return false; } + + /** + * Returns clipboard contents as a String. + * + * The kFeatureClipboardSupport feature flag can be used to + * test whether this call has been implemented by the active + * backend. + * + * @return clipboard contents ("" if hasTextInClipboard() == false) + */ + virtual Common::String getTextFromClipboard() { return ""; } + + /** * Returns the locale of the system. * * This returns the currently set up locale of the system, on which diff --git a/common/taskbar.h b/common/taskbar.h index b4ec673739..f1a9adb2d9 100644 --- a/common/taskbar.h +++ b/common/taskbar.h @@ -123,7 +123,7 @@ public: virtual void addRecent(const String &name, const String &description) {} /** - * Notifies the user an error occured through the taskbar icon + * Notifies the user an error occurred through the taskbar icon * * This will for example show the taskbar icon as red (using progress of 100% and an error state) * on Windows, and set the launcher icon in the urgent state on Unity diff --git a/common/updates.cpp b/common/updates.cpp new file mode 100644 index 0000000000..087002a7d3 --- /dev/null +++ b/common/updates.cpp @@ -0,0 +1,68 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include "common/system.h" +#include "common/updates.h" +#include "common/translation.h" + +namespace Common { + +static const int updateIntervals[] = { + UpdateManager::kUpdateIntervalNotSupported, + UpdateManager::kUpdateIntervalOneDay, + UpdateManager::kUpdateIntervalOneWeek, + UpdateManager::kUpdateIntervalOneMonth, + -1 +}; + +const int *UpdateManager::getUpdateIntervals() { + return updateIntervals; +} + +int UpdateManager::normalizeInterval(int interval) { + const int *val = updateIntervals; + + while (*val != -1) { + if (*val >= interval) + return *val; + val++; + } + + return val[-1]; // Return maximal acceptable value +} + +const char *UpdateManager::updateIntervalToString(int interval) { + switch (interval) { + case kUpdateIntervalNotSupported: + return _("Never"); + case kUpdateIntervalOneDay: + return _("Daily"); + case kUpdateIntervalOneWeek: + return _("Weekly"); + case kUpdateIntervalOneMonth: + return _("Monthly"); + default: + return _("<Bad value>"); + } +} + +} // End of namespace Common diff --git a/common/updates.h b/common/updates.h index 4c30987c38..3a3049d4df 100644 --- a/common/updates.h +++ b/common/updates.h @@ -20,8 +20,8 @@ * */ -#ifndef BACKENDS_UPDATES_ABSTRACT_H -#define BACKENDS_UPDATES_ABSTRACT_H +#ifndef COMMON_UPDATES_H +#define COMMON_UPDATES_H #if defined(USE_UPDATES) @@ -85,18 +85,50 @@ public: * * @param interval The interval. */ - virtual void setUpdateCheckInterval(UpdateInterval interval) {} + virtual void setUpdateCheckInterval(int interval) {} /** * Gets the update check interval. * * @return the update check interval. */ - virtual UpdateInterval getUpdateCheckInterval() { return kUpdateIntervalNotSupported; } + virtual int getUpdateCheckInterval() { return kUpdateIntervalNotSupported; } + + /** + * Gets last update check time + * + * @param t TimeDate struct to fill out + * @return flag indicating success + */ + virtual bool getLastUpdateCheckTimeAndDate(TimeDate &t) { return false; } + + /** + * Returns list of supported uptate intervals. + * Ending with '-1' which is not acceptable value. + * + * @return list of integer values representing update intervals in seconds. + */ + static const int *getUpdateIntervals(); + + /** + * Returns string representation of a given interval. + * + * @param interval The interval. + * @return pointer to localized string of given interval. + */ + static const char *updateIntervalToString(int interval); + + /** + * Rounds up the given interval to acceptable value. + * + * @param interval The interval. + * @return rounded up interval + */ + static int normalizeInterval(int interval); }; } // End of namespace Common #endif -#endif // BACKENDS_UPDATES_ABSTRACT_H +#endif // COMMON_UPDATES_H diff --git a/common/util.cpp b/common/util.cpp index 8e0a2fd61f..62a1baf6ac 100644 --- a/common/util.cpp +++ b/common/util.cpp @@ -28,6 +28,7 @@ #define FORBIDDEN_SYMBOL_EXCEPTION_isspace #define FORBIDDEN_SYMBOL_EXCEPTION_isupper #define FORBIDDEN_SYMBOL_EXCEPTION_isprint +#define FORBIDDEN_SYMBOL_EXCEPTION_ispunct #include "common/util.h" @@ -150,4 +151,10 @@ bool isPrint(int c) { ENSURE_ASCII_CHAR(c); return isprint((byte)c); } + +bool isPunct(int c) { + ENSURE_ASCII_CHAR(c); + return ispunct((byte)c); +} + } // End of namespace Common diff --git a/common/util.h b/common/util.h index f51aa00925..1f635f3654 100644 --- a/common/util.h +++ b/common/util.h @@ -177,6 +177,17 @@ bool isUpper(int c); * @return true if the character is printable, false otherwise. */ bool isPrint(int c); + + +/** + * Test whether the given character is a punctuation character, + * (i.e not alphanumeric. + * + * @param c the character to test + * @return true if the character is punctuation, false otherwise. + */ +bool isPunct(int c); + } // End of namespace Common #endif diff --git a/common/xmlparser.cpp b/common/xmlparser.cpp index 67a3d36cec..4470180710 100644 --- a/common/xmlparser.cpp +++ b/common/xmlparser.cpp @@ -97,35 +97,37 @@ bool XMLParser::parserError(const String &errStr) { assert(_stream->pos() == startPosition); currentPosition = startPosition; - int keyOpening = 0; - int keyClosing = 0; + Common::String errorMessage = Common::String::format("\n File <%s>, line %d:\n", _fileName.c_str(), lineCount); - while (currentPosition-- && keyOpening == 0) { - _stream->seek(-2, SEEK_CUR); - c = _stream->readByte(); + if (startPosition > 1) { + int keyOpening = 0; + int keyClosing = 0; - if (c == '<') - keyOpening = currentPosition - 1; - else if (c == '>') - keyClosing = currentPosition; - } + while (currentPosition-- && keyOpening == 0) { + _stream->seek(-2, SEEK_CUR); + c = _stream->readByte(); - _stream->seek(startPosition, SEEK_SET); - currentPosition = startPosition; - while (keyClosing == 0 && c && currentPosition++) { - c = _stream->readByte(); + if (c == '<') + keyOpening = currentPosition - 1; + else if (c == '>') + keyClosing = currentPosition; + } - if (c == '>') - keyClosing = currentPosition; - } + _stream->seek(startPosition, SEEK_SET); + currentPosition = startPosition; + while (keyClosing == 0 && c && currentPosition++) { + c = _stream->readByte(); - Common::String errorMessage = Common::String::format("\n File <%s>, line %d:\n", _fileName.c_str(), lineCount); + if (c == '>') + keyClosing = currentPosition; + } - currentPosition = (keyClosing - keyOpening); - _stream->seek(keyOpening, SEEK_SET); + currentPosition = (keyClosing - keyOpening); + _stream->seek(keyOpening, SEEK_SET); - while (currentPosition--) - errorMessage += (char)_stream->readByte(); + while (currentPosition--) + errorMessage += (char)_stream->readByte(); + } errorMessage += "\n\nParser error: "; errorMessage += errStr; diff --git a/common/zlib.cpp b/common/zlib.cpp index c22ea1e660..39130beb4e 100644 --- a/common/zlib.cpp +++ b/common/zlib.cpp @@ -316,6 +316,7 @@ protected: ScopedPtr<WriteStream> _wrapped; z_stream _stream; int _zlibErr; + uint32 _pos; void processData(int flushType) { // This function is called by both write() and finalize(). @@ -333,7 +334,7 @@ protected: } public: - GZipWriteStream(WriteStream *w) : _wrapped(w), _stream() { + GZipWriteStream(WriteStream *w) : _wrapped(w), _stream(), _pos(0) { assert(w != 0); // Adding 16 to windowBits indicates to zlib that it is supposed to @@ -403,8 +404,11 @@ public: // ... and flush it to disk processData(Z_NO_FLUSH); + _pos += dataSize - _stream.avail_in; return dataSize - _stream.avail_in; } + + virtual int32 pos() const { return _pos; } }; #endif // USE_ZLIB |