From 5cdc5aa58d7daf1097fd11af59c18dff7bfa7cb1 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Tue, 16 Jan 2018 03:31:03 +0100 Subject: [PATCH 001/161] Add automatic JSON serialization/deserialization Abstract serialization concept to pure virtaul class RsSerializable from which every other serializable class must inherit from Use RapidJSON for JSON manipulation Add TO_JSON and FROM_JSON SerializeJob Deprecate unused SerializationFormat Remove some unused old piece of code Adjust many lines to max 80 columns for better readability on little screens Clean up documentation and code, remove old cruft Add copyright notice on edited files that miss it --- libretroshare/src/chat/rschatitems.cc | 2 +- libretroshare/src/gxstrans/p3gxstransitems.cc | 12 +- libretroshare/src/libretroshare.pro | 6 +- .../src/retroshare/rsgxsifacetypes.h | 52 +- libretroshare/src/retroshare/rsidentity.h | 257 ++++++--- libretroshare/src/rsitems/rsitem.h | 116 ++-- libretroshare/src/serialiser/rsbaseserial.cc | 3 +- libretroshare/src/serialiser/rsserializable.h | 137 +++++ libretroshare/src/serialiser/rsserializer.h | 172 +++--- .../src/serialiser/rstypeserializer.cc | 487 ++++++++++++++--- .../src/serialiser/rstypeserializer.h | 510 ++++++++++++++---- libretroshare/src/services/p3idservice.cc | 35 +- 12 files changed, 1384 insertions(+), 405 deletions(-) create mode 100644 libretroshare/src/serialiser/rsserializable.h diff --git a/libretroshare/src/chat/rschatitems.cc b/libretroshare/src/chat/rschatitems.cc index 24aef56f9..370cef5d3 100644 --- a/libretroshare/src/chat/rschatitems.cc +++ b/libretroshare/src/chat/rschatitems.cc @@ -207,7 +207,7 @@ void RsPrivateChatMsgConfigItem::get(RsChatMsgItem *ci) /* Necessary to serialize `store` that is an STL container with RsChatMsgItem * inside which is a subtype of RsItem */ -RS_REGISTER_ITEM_TYPE(RsChatMsgItem) +RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsChatMsgItem) void PrivateOugoingMapItem::serial_process( RsGenericSerializer::SerializeJob j, diff --git a/libretroshare/src/gxstrans/p3gxstransitems.cc b/libretroshare/src/gxstrans/p3gxstransitems.cc index cebbd9e69..8ea0f4557 100644 --- a/libretroshare/src/gxstrans/p3gxstransitems.cc +++ b/libretroshare/src/gxstrans/p3gxstransitems.cc @@ -1,6 +1,6 @@ /* * GXS Mailing Service - * Copyright (C) 2016-2017 Gioacchino Mazzurco + * Copyright (C) 2016-2018 Gioacchino Mazzurco * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -38,11 +38,15 @@ OutgoingRecord::OutgoingRecord( RsGxsId rec, GxsTransSubServices cs, memcpy(&mailData[0], data, size); } +// for mailItem +RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsGxsTransMailItem) -RS_REGISTER_ITEM_TYPE(RsGxsTransMailItem) // for mailItem -RS_REGISTER_ITEM_TYPE(RsNxsTransPresignedReceipt) // for presignedReceipt +// for presignedReceipt +RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsNxsTransPresignedReceipt) -void OutgoingRecord_deprecated::serial_process(RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx) +void OutgoingRecord_deprecated::serial_process( + RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) { RS_REGISTER_SERIAL_MEMBER_TYPED(status, uint8_t); RS_REGISTER_SERIAL_MEMBER(recipient); diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 1521f5574..99499c686 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -553,7 +553,7 @@ SOURCES += ft/ftchunkmap.cc \ ft/ftfilesearch.cc \ ft/ftserver.cc \ ft/fttransfermodule.cc \ - ft/ftturtlefiletransferitem.cc + ft/ftturtlefiletransferitem.cc SOURCES += crypto/chacha20.cpp \ crypto/hashstream.cc @@ -648,6 +648,7 @@ SOURCES += serialiser/rsbaseserial.cc \ rsitems/rsgxsupdateitems.cc \ rsitems/rsserviceinfoitems.cc \ + SOURCES += services/autoproxy/rsautoproxymonitor.cc \ services/autoproxy/p3i2pbob.cc \ services/p3msgservice.cc \ @@ -786,7 +787,8 @@ SOURCES += gxstunnel/p3gxstunnel.cc \ gxstunnel/rsgxstunnelitems.cc # new serialization code -HEADERS += serialiser/rsserializer.h \ +HEADERS += serialiser/rsserializable.h \ + serialiser/rsserializer.h \ serialiser/rstypeserializer.h SOURCES += serialiser/rsserializer.cc \ diff --git a/libretroshare/src/retroshare/rsgxsifacetypes.h b/libretroshare/src/retroshare/rsgxsifacetypes.h index fd477fc9f..659a87e2f 100644 --- a/libretroshare/src/retroshare/rsgxsifacetypes.h +++ b/libretroshare/src/retroshare/rsgxsifacetypes.h @@ -1,3 +1,23 @@ +/* + * rsgxsifacetypes.h + * + * Copyright (C) 2013 crispy + * Copyright (C) 2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + /* * rsgxsifacetypes.h * @@ -13,8 +33,10 @@ #include #include -#include -#include +#include "retroshare/rstypes.h" +#include "retroshare/rsids.h" +#include "serialiser/rsserializable.h" +#include "serialiser/rstypeserializer.h" typedef GXSGroupId RsGxsGroupId; typedef Sha1CheckSum RsGxsMessageId; @@ -34,7 +56,7 @@ typedef std::map > MsgMetaResult; class RsGxsGrpMetaData; class RsGxsMsgMetaData; -struct RsGroupMetaData +struct RsGroupMetaData : RsSerializable { // (csoler) The correct default value to be used in mCircleType is GXS_CIRCLE_TYPE_PUBLIC, which is defined in rsgxscircles.h, // but because of a loop in the includes, I cannot include it here. So I replaced with its current value 0x0001. @@ -73,6 +95,30 @@ struct RsGroupMetaData std::string mServiceString; // Service Specific Free-Form extra storage. RsPeerId mOriginator; RsGxsCircleId mInternalCircle; + + /// @see RsSerializable + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) + { + RS_REGISTER_SERIAL_MEMBER(mGroupId); + RS_REGISTER_SERIAL_MEMBER(mGroupName); + RS_REGISTER_SERIAL_MEMBER(mGroupFlags); + RS_REGISTER_SERIAL_MEMBER(mSignFlags); + RS_REGISTER_SERIAL_MEMBER(mPublishTs); + RS_REGISTER_SERIAL_MEMBER(mAuthorId); + RS_REGISTER_SERIAL_MEMBER(mCircleId); + RS_REGISTER_SERIAL_MEMBER(mCircleType); + RS_REGISTER_SERIAL_MEMBER(mAuthenFlags); + RS_REGISTER_SERIAL_MEMBER(mParentGrpId); + RS_REGISTER_SERIAL_MEMBER(mSubscribeFlags); + RS_REGISTER_SERIAL_MEMBER(mPop); + RS_REGISTER_SERIAL_MEMBER(mVisibleMsgCount); + RS_REGISTER_SERIAL_MEMBER(mLastPost); + RS_REGISTER_SERIAL_MEMBER(mGroupStatus); + RS_REGISTER_SERIAL_MEMBER(mServiceString); + RS_REGISTER_SERIAL_MEMBER(mOriginator); + RS_REGISTER_SERIAL_MEMBER(mInternalCircle); + } }; diff --git a/libretroshare/src/retroshare/rsidentity.h b/libretroshare/src/retroshare/rsidentity.h index bf650d462..b79cbe8fd 100644 --- a/libretroshare/src/retroshare/rsidentity.h +++ b/libretroshare/src/retroshare/rsidentity.h @@ -6,7 +6,8 @@ * * RetroShare C++ Interface. * - * Copyright 2012-2012 by Robert Fernie. + * Copyright (C) 2012 Robert Fernie. + * Copyright (C) 2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -36,6 +37,9 @@ #include "retroshare/rsids.h" #include "serialiser/rstlvimage.h" #include "retroshare/rsgxscommon.h" +#include "serialiser/rsserializable.h" +#include "serialiser/rstypeserializer.h" +#include "util/rsdeprecate.h" /* The Main Interface Class - for information about your Peers */ class RsIdentity; @@ -63,6 +67,7 @@ extern RsIdentity *rsIdentity; #define RSID_RELATION_OTHER 0x0008 #define RSID_RELATION_UNKNOWN 0x0010 +/// @deprecated remove toghether with RsGxsIdGroup::mRecognTags #define RSRECOGN_MAX_TAGINFO 5 // Unicode symbols. NOT utf-8 bytes, because of multi byte characters @@ -77,27 +82,36 @@ static const uint32_t RS_IDENTITY_FLAGS_PGP_KNOWN = 0x0004; static const uint32_t RS_IDENTITY_FLAGS_IS_OWN_ID = 0x0008; static const uint32_t RS_IDENTITY_FLAGS_IS_DEPRECATED= 0x0010; // used to denote keys with deprecated fingerprint format. -class GxsReputation +struct GxsReputation : RsSerializable { - public: - GxsReputation(); + GxsReputation(); - bool updateIdScore(bool pgpLinked, bool pgpKnown); - bool update(); // checks ranges and calculates overall score. - int mOverallScore; - int mIdScore; // PGP, Known, etc. - int mOwnOpinion; - int mPeerOpinion; + bool updateIdScore(bool pgpLinked, bool pgpKnown); + bool update(); /// checks ranges and calculates overall score. + + int32_t mOverallScore; + int32_t mIdScore; /// PGP, Known, etc. + int32_t mOwnOpinion; + int32_t mPeerOpinion; + + /// @see RsSerializable + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) + { + RS_REGISTER_SERIAL_MEMBER(mOverallScore); + RS_REGISTER_SERIAL_MEMBER(mIdScore); + RS_REGISTER_SERIAL_MEMBER(mOwnOpinion); + RS_REGISTER_SERIAL_MEMBER(mPeerOpinion); + } }; -struct RsGxsIdGroup +struct RsGxsIdGroup : RsSerializable { RsGxsIdGroup() : mLastUsageTS(0), mPgpKnown(false), mIsAContact(false) {} ~RsGxsIdGroup() {} - RsGroupMetaData mMeta; // In GroupMetaData. @@ -120,7 +134,7 @@ struct RsGxsIdGroup std::string mPgpIdSign; // Recognition Strings. MAX# defined above. - std::list mRecognTags; + RS_DEPRECATED std::list mRecognTags; // Avatar RsGxsImage mImage ; @@ -131,8 +145,11 @@ struct RsGxsIdGroup bool mIsAContact; // change that into flags one day RsPgpId mPgpId; GxsReputation mReputation; -}; + /// @see RsSerializable + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ); +}; std::ostream &operator<<(std::ostream &out, const RsGxsIdGroup &group); @@ -149,12 +166,11 @@ class RsRecognTag }; -class RsRecognTagDetails +struct RsRecognTagDetails { - public: - RsRecognTagDetails() - :valid_from(0), valid_to(0), tag_class(0), tag_type(0), - is_valid(false), is_pending(false) { return; } + RsRecognTagDetails() : + valid_from(0), valid_to(0), tag_class(0), tag_type(0), is_valid(false), + is_pending(false) {} time_t valid_from; time_t valid_to; @@ -167,105 +183,168 @@ class RsRecognTagDetails bool is_pending; }; -class RsIdOpinion -{ - public: - RsGxsId id; - int rating; -}; - -class RsIdentityParameters +struct RsIdentityParameters { - public: - RsIdentityParameters(): isPgpLinked(false) { return; } + RsIdentityParameters() : + isPgpLinked(false) {} + bool isPgpLinked; - std::string nickname; - RsGxsImage mImage ; + std::string nickname; + RsGxsImage mImage; }; -class RsIdentityUsage +struct RsIdentityUsage : RsSerializable { -public: - enum UsageCode { UNKNOWN_USAGE = 0x00, - GROUP_ADMIN_SIGNATURE_CREATION = 0x01, // These 2 are normally not normal GXS identities, but nothing prevents it to happen either. - GROUP_ADMIN_SIGNATURE_VALIDATION = 0x02, - GROUP_AUTHOR_SIGNATURE_CREATION = 0x03, // not typically used, since most services do not require group author signatures - GROUP_AUTHOR_SIGNATURE_VALIDATION = 0x04, - MESSAGE_AUTHOR_SIGNATURE_CREATION = 0x05, // most common use case. Messages are signed by authors in e.g. forums. - MESSAGE_AUTHOR_SIGNATURE_VALIDATION = 0x06, - GROUP_AUTHOR_KEEP_ALIVE = 0x07, // Identities are stamped regularly by crawlign the set of messages for all groups. That helps keepign the useful identities in hand. - MESSAGE_AUTHOR_KEEP_ALIVE = 0x08, // Identities are stamped regularly by crawlign the set of messages for all groups. That helps keepign the useful identities in hand. - CHAT_LOBBY_MSG_VALIDATION = 0x09, // Chat lobby msgs are signed, so each time one comes, or a chat lobby event comes, a signature verificaiton happens. - GLOBAL_ROUTER_SIGNATURE_CHECK = 0x0a, // Global router message validation - GLOBAL_ROUTER_SIGNATURE_CREATION = 0x0b, // Global router message signature - GXS_TUNNEL_DH_SIGNATURE_CHECK = 0x0c, // - GXS_TUNNEL_DH_SIGNATURE_CREATION = 0x0d, // - IDENTITY_DATA_UPDATE = 0x0e, // Group update on that identity data. Can be avatar, name, etc. - IDENTITY_GENERIC_SIGNATURE_CHECK = 0x0f, // Any signature verified for that identity - IDENTITY_GENERIC_SIGNATURE_CREATION = 0x10, // Any signature made by that identity - IDENTITY_GENERIC_ENCRYPTION = 0x11, - IDENTITY_GENERIC_DECRYPTION = 0x12, - CIRCLE_MEMBERSHIP_CHECK = 0x13 - } ; + enum UsageCode : uint8_t + { + UNKNOWN_USAGE = 0x00, - explicit RsIdentityUsage(uint16_t service,const RsIdentityUsage::UsageCode& code,const RsGxsGroupId& gid=RsGxsGroupId(),const RsGxsMessageId& mid=RsGxsMessageId(),uint64_t additional_id=0,const std::string& comment = std::string()); + /** These 2 are normally not normal GXS identities, but nothing prevents + * it to happen either. */ + GROUP_ADMIN_SIGNATURE_CREATION = 0x01, + GROUP_ADMIN_SIGNATURE_VALIDATION = 0x02, - uint16_t mServiceId; // Id of the service using that identity, as understood by rsServiceControl - UsageCode mUsageCode; // Specific code to use. Will allow forming the correct translated message in the GUI if necessary. - RsGxsGroupId mGrpId; // Group ID using the identity + /** Not typically used, since most services do not require group author + * signatures */ + GROUP_AUTHOR_SIGNATURE_CREATION = 0x03, + GROUP_AUTHOR_SIGNATURE_VALIDATION = 0x04, - RsGxsMessageId mMsgId; // Message ID using the identity - uint64_t mAdditionalId; // Some additional ID. Can be used for e.g. chat lobbies. - std::string mComment ; // additional comment to be used mainly for debugging, but not GUI display + /// most common use case. Messages are signed by authors in e.g. forums. + MESSAGE_AUTHOR_SIGNATURE_CREATION = 0x05, + MESSAGE_AUTHOR_SIGNATURE_VALIDATION = 0x06, - bool operator<(const RsIdentityUsage& u) const - { - return mHash < u.mHash ; - } - RsFileHash mHash ; + /** Identities are stamped regularly by crawlign the set of messages for + * all groups. That helps keepign the useful identities in hand. */ + GROUP_AUTHOR_KEEP_ALIVE = 0x07, + MESSAGE_AUTHOR_KEEP_ALIVE = 0x08, + + /** Chat lobby msgs are signed, so each time one comes, or a chat lobby + * event comes, a signature verificaiton happens. */ + CHAT_LOBBY_MSG_VALIDATION = 0x09, + + /// Global router message validation + GLOBAL_ROUTER_SIGNATURE_CHECK = 0x0a, + + /// Global router message signature + GLOBAL_ROUTER_SIGNATURE_CREATION = 0x0b, + + GXS_TUNNEL_DH_SIGNATURE_CHECK = 0x0c, + GXS_TUNNEL_DH_SIGNATURE_CREATION = 0x0d, + + /// Group update on that identity data. Can be avatar, name, etc. + IDENTITY_DATA_UPDATE = 0x0e, + + /// Any signature verified for that identity + IDENTITY_GENERIC_SIGNATURE_CHECK = 0x0f, + + /// Any signature made by that identity + IDENTITY_GENERIC_SIGNATURE_CREATION = 0x10, + + IDENTITY_GENERIC_ENCRYPTION = 0x11, + IDENTITY_GENERIC_DECRYPTION = 0x12, + CIRCLE_MEMBERSHIP_CHECK = 0x13 + } ; + + RsIdentityUsage( uint16_t service, const RsIdentityUsage::UsageCode& code, + const RsGxsGroupId& gid = RsGxsGroupId(), + const RsGxsMessageId& mid = RsGxsMessageId(), + uint64_t additional_id=0, + const std::string& comment = std::string() ); + + /// Id of the service using that identity, as understood by rsServiceControl + uint16_t mServiceId; + + /** Specific code to use. Will allow forming the correct translated message + * in the GUI if necessary. */ + UsageCode mUsageCode; + + /// Group ID using the identity + RsGxsGroupId mGrpId; + + /// Message ID using the identity + RsGxsMessageId mMsgId; + + /// Some additional ID. Can be used for e.g. chat lobbies. + uint64_t mAdditionalId; + + /// additional comment to be used mainly for debugging, but not GUI display + std::string mComment; + + bool operator<(const RsIdentityUsage& u) const { return mHash < u.mHash; } + RsFileHash mHash ; + + /// @see RsSerializable + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) + { + RS_REGISTER_SERIAL_MEMBER(mServiceId); + RS_REGISTER_SERIAL_MEMBER_TYPED(mUsageCode, uint8_t); + RS_REGISTER_SERIAL_MEMBER(mGrpId); + RS_REGISTER_SERIAL_MEMBER(mMsgId); + RS_REGISTER_SERIAL_MEMBER(mAdditionalId); + RS_REGISTER_SERIAL_MEMBER(mComment); + RS_REGISTER_SERIAL_MEMBER(mHash); + } + + friend class RsTypeSerializer; +private: + /** Accessible only to friend class RsTypeSerializer needed for + * deserialization */ + RsIdentityUsage(); }; -class RsIdentityDetails +RS_REGISTER_SERIALIZABLE_TYPE_DECL(RsIdentityUsage) + + +struct RsIdentityDetails : RsSerializable { -public: - RsIdentityDetails() - : mFlags(0), mLastUsageTS(0) { return; } + RsIdentityDetails() : mFlags(0), mLastUsageTS(0) {} RsGxsId mId; - // identity details. std::string mNickname; - uint32_t mFlags ; + uint32_t mFlags; - // PGP Stuff. - RsPgpId mPgpId; + RsPgpId mPgpId; - // Recogn details. - std::list mRecognTags; + /// @deprecated Recogn details. + RS_DEPRECATED std::list mRecognTags; - // Cyril: Reputation details. At some point we might want to merge information - // between the two into a single global score. Since the old reputation system - // is not finished yet, I leave this in place. We should decide what to do with it. - RsReputations::ReputationInfo mReputation; + /** Cyril: Reputation details. At some point we might want to merge + * information between the two into a single global score. Since the old + * reputation system is not finished yet, I leave this in place. We should + * decide what to do with it. + */ + RsReputations::ReputationInfo mReputation; - // avatar - RsGxsImage mAvatar ; + RsGxsImage mAvatar; - // last usage - time_t mLastUsageTS ; - std::map mUseCases ; + time_t mLastUsageTS; + + std::map mUseCases; + + /// @see RsSerializable + virtual void serial_process(RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx) + { + RS_REGISTER_SERIAL_MEMBER(mId); + RS_REGISTER_SERIAL_MEMBER(mNickname); + RS_REGISTER_SERIAL_MEMBER(mFlags); + RS_REGISTER_SERIAL_MEMBER(mPgpId); + //RS_REGISTER_SERIAL_MEMBER_TYPED(mReputation, RsSerializable); + //RS_REGISTER_SERIAL_MEMBER_TYPED(mAvatar, RsSerializable); + RS_REGISTER_SERIAL_MEMBER(mLastUsageTS); + RS_REGISTER_SERIAL_MEMBER(mUseCases); + } }; -class RsIdentity: public RsGxsIfaceHelper +struct RsIdentity : RsGxsIfaceHelper { - -public: - explicit RsIdentity(RsGxsIface *gxs): RsGxsIfaceHelper(gxs) {} virtual ~RsIdentity() {} diff --git a/libretroshare/src/rsitems/rsitem.h b/libretroshare/src/rsitems/rsitem.h index 056bbe084..fb76d1562 100644 --- a/libretroshare/src/rsitems/rsitem.h +++ b/libretroshare/src/rsitems/rsitem.h @@ -1,76 +1,96 @@ #pragma once +/* + * RetroShare Serialiser. + * Copyright (C) 2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ #include // for typeid #include "util/smallobject.h" #include "retroshare/rstypes.h" #include "serialiser/rsserializer.h" +#include "serialiser/rsserializable.h" #include "util/stacktrace.h" #include -class RsItem: public RsMemoryManagement::SmallObject +struct RsItem : RsMemoryManagement::SmallObject, RsSerializable { - public: - explicit RsItem(uint32_t t); - RsItem(uint8_t ver, uint8_t cls, uint8_t t, uint8_t subtype); + explicit RsItem(uint32_t t); + RsItem(uint8_t ver, uint8_t cls, uint8_t t, uint8_t subtype); #ifdef DO_STATISTICS - void *operator new(size_t s) ; - void operator delete(void *,size_t s) ; + void *operator new(size_t s) ; + void operator delete(void *,size_t s) ; #endif - virtual ~RsItem(); + virtual ~RsItem(); - /// TODO: Do this make sense with the new serialization system? - virtual void clear() = 0; + /// TODO: Do this make sense with the new serialization system? + virtual void clear() = 0; - virtual std::ostream &print(std::ostream &out, uint16_t /* indent */ = 0) - { - RsGenericSerializer::SerializeContext ctx(NULL,0,RsGenericSerializer::FORMAT_BINARY,RsGenericSerializer::SERIALIZATION_FLAG_NONE); - serial_process(RsGenericSerializer::PRINT,ctx) ; - return out; - } + virtual std::ostream &print(std::ostream &out, uint16_t /* indent */ = 0) + { + RsGenericSerializer::SerializeContext ctx( + NULL, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE ); + serial_process(RsGenericSerializer::PRINT,ctx); + return out; + } - void print_string(std::string &out, uint16_t indent = 0); + void print_string(std::string &out, uint16_t indent = 0); - /* source / destination id */ - const RsPeerId& PeerId() const { return peerId; } - void PeerId(const RsPeerId& id) { peerId = id; } + /// source / destination id + const RsPeerId& PeerId() const { return peerId; } + void PeerId(const RsPeerId& id) { peerId = id; } - /* complete id */ - uint32_t PacketId() const; + /// complete id + uint32_t PacketId() const; - /* id parts */ - uint8_t PacketVersion(); - uint8_t PacketClass(); - uint8_t PacketType(); - uint8_t PacketSubType() const; + /// id parts + uint8_t PacketVersion(); + uint8_t PacketClass(); + uint8_t PacketType(); + uint8_t PacketSubType() const; - /* For Service Packets */ - RsItem(uint8_t ver, uint16_t service, uint8_t subtype); - uint16_t PacketService() const; /* combined Packet class/type (mid 16bits) */ - void setPacketService(uint16_t service); + /// For Service Packets + RsItem(uint8_t ver, uint16_t service, uint8_t subtype); + uint16_t PacketService() const; /* combined Packet class/type (mid 16bits) */ + void setPacketService(uint16_t service); - inline uint8_t priority_level() const { return _priority_level ;} - inline void setPriorityLevel(uint8_t l) { _priority_level = l ;} + inline uint8_t priority_level() const { return _priority_level ;} + inline void setPriorityLevel(uint8_t l) { _priority_level = l ;} - /** - * TODO: This should be made pure virtual as soon as all the codebase - * is ported to the new serialization system - */ - virtual void serial_process(RsGenericSerializer::SerializeJob, - RsGenericSerializer::SerializeContext&)// = 0; - { - std::cerr << "(EE) RsItem::serial_process() called by an item using" - << "new serialization classes, but not derived! Class is " - << typeid(*this).name() << std::endl; - print_stacktrace(); - } + /** + * TODO: This default implementation should be removed and childs structs + * implement RsSerializable(...) as soon as all the codebase is ported to + * the new serialization system + */ + virtual void serial_process(RsGenericSerializer::SerializeJob, + RsGenericSerializer::SerializeContext&)// = 0; + { + std::cerr << "(EE) RsItem::serial_process(...) called by an item using" + << "new serialization classes, but not derived! Class is " + << typeid(*this).name() << std::endl; + print_stacktrace(); + } - protected: - uint32_t type; - RsPeerId peerId; - uint8_t _priority_level ; +protected: + uint32_t type; + RsPeerId peerId; + uint8_t _priority_level; }; /// TODO: Do this make sense with the new serialization system? diff --git a/libretroshare/src/serialiser/rsbaseserial.cc b/libretroshare/src/serialiser/rsbaseserial.cc index 44bd249d1..ef862db5c 100644 --- a/libretroshare/src/serialiser/rsbaseserial.cc +++ b/libretroshare/src/serialiser/rsbaseserial.cc @@ -238,8 +238,7 @@ uint32_t getRawStringSize(const std::string &outStr) bool getRawString(const void *data, uint32_t size, uint32_t *offset, std::string &outStr) { -#warning Gio: "I had to change this. It seems like a bug to not clear the string. Should make sure it's not introducing any side effect." - outStr.clear(); + outStr.clear(); uint32_t len = 0; if (!getRawUInt32(data, size, offset, &len)) diff --git a/libretroshare/src/serialiser/rsserializable.h b/libretroshare/src/serialiser/rsserializable.h new file mode 100644 index 000000000..b90e6c666 --- /dev/null +++ b/libretroshare/src/serialiser/rsserializable.h @@ -0,0 +1,137 @@ +#pragma once +/* + * RetroShare Serialiser. + * Copyright (C) 2016-2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "serialiser/rsserializer.h" + +/** @brief Minimal ancestor for all serializable structs in RetroShare + * If you want your struct to be easly serializable you should inherit from this + * struct. + */ +struct RsSerializable +{ + /** Register struct members to serialize in this method taking advantage of + * the helper macros + * @see RS_REGISTER_SERIAL_MEMBER(I) + * @see RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) + * @see RS_REGISTER_SERIALIZABLE_TYPE(T) + */ + virtual void serial_process(RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx) = 0; +}; + + +/** @def RS_REGISTER_SERIAL_MEMBER(I) + * Use this macro to register the members of `YourSerializable` for serial + * processing inside `YourSerializable::serial_process(j, ctx)` + * + * Inspired by http://stackoverflow.com/a/39345864 + */ +#define RS_REGISTER_SERIAL_MEMBER(I) \ + do { RsTypeSerializer::serial_process(j, ctx, I, #I); } while(0) + + +/** @def RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) + * This macro usage is similar to @see RS_REGISTER_SERIAL_MEMBER(I) but it + * permit to force serialization/deserialization type, it is expecially useful + * with enum class members or RsTlvItem derivative members, be very careful with + * the type you pass, as reinterpret_cast on a reference is used that is + * expecially permissive so you can shot your feet if not carefull enough. + * + * If you are using this with an RsSerializable derivative (so passing + * RsSerializable as T) consider to register your item type with + * @see RS_REGISTER_SERIALIZABLE_TYPE(T) in + * association with @see RS_REGISTER_SERIAL_MEMBER(I) that rely on template + * function generation, as in this particular case + * RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) would cause the serial code rely on + * C++ dynamic dispatching that may have a noticeable impact on runtime + * performances. + */ +#pragma GCC diagnostic ignored "-Wstrict-aliasing" +#define RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) do {\ + RsTypeSerializer::serial_process(j, ctx, reinterpret_cast(I), #I);\ + } while(0) +#pragma GCC diagnostic pop + + +/** @def RS_REGISTER_SERIALIZABLE_TYPE(T) + * Use this macro into `youritem.cc` only if you need to process members of + * subtypes of RsSerializable. + * + * The usage of this macro is strictly needed only in some cases, for example if + * you are registering for serialization a member of a container that contains + * items of subclasses of RsSerializable like + * `std::map` + * + * @code{.cpp} +struct PrivateOugoingMapItem : RsChatItem +{ + PrivateOugoingMapItem() : RsChatItem(RS_PKT_SUBTYPE_OUTGOING_MAP) {} + + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ); + + std::map store; +}; + +RS_REGISTER_SERIALIZABLE_TYPE(RsChatMsgItem) + +void PrivateOugoingMapItem::serial_process( + RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) +{ + // store is of type + RS_REGISTER_SERIAL_MEMBER(store); +} + * @endcode + * + * If you use this macro with a lot of different item types this can cause the + * generated binary grow in size, consider the usage of + * @see RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) passing RsSerializable as type in + * that case. + */ +#define RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) template<> /*static*/\ + void RsTypeSerializer::serial_process( \ + RsGenericSerializer::SerializeJob j,\ + RsGenericSerializer::SerializeContext& ctx, T& item,\ + const std::string& objName ) \ +{ \ + RsTypeSerializer::serial_process( j, \ + ctx, static_cast(item), objName ); \ +} + + +/** @def RS_REGISTER_SERIALIZABLE_TYPE_DECL(T) + * The usage of this macro into your header file is needed only in case you + * needed @see RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) in your definitions file, + * but it was not enough and the compiler kept complanining about undefined + * references to serialize, deserialize, serial_size, print_data, to_JSON, + * from_JSON for your RsSerializable derrived type. + * + * One example of such case is RsIdentityUsage that is declared in + * retroshare/rsidentity.h but defined in services/p3idservice.cc, also if + * RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsIdentityUsage) was used in p3idservice.cc + * for some reason it was not enough for the compiler to see it so + * RS_REGISTER_SERIALIZABLE_TYPE_DECL(RsIdentityUsage) has been added in + * rsidentity.h too and now the compiler is happy. + */ +#define RS_REGISTER_SERIALIZABLE_TYPE_DECL(T) template<> /*static*/\ + void RsTypeSerializer::serial_process( \ + RsGenericSerializer::SerializeJob j,\ + RsGenericSerializer::SerializeContext& ctx, T& item,\ + const std::string& /*objName*/ ); diff --git a/libretroshare/src/serialiser/rsserializer.h b/libretroshare/src/serialiser/rsserializer.h index 2cae93ab7..63b565a9b 100644 --- a/libretroshare/src/serialiser/rsserializer.h +++ b/libretroshare/src/serialiser/rsserializer.h @@ -3,7 +3,8 @@ * * RetroShare Serialiser. * - * Copyright 2016 by Cyril Soler + * Copyright (C) 2016 Cyril Soler + * Copyright (C) 2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -153,9 +154,11 @@ #include #include #include +#include #include "retroshare/rsflags.h" #include "serialiser/rsserial.h" +#include "util/rsdeprecate.h" class RsItem ; @@ -194,106 +197,125 @@ class RsRawSerialiser: public RsSerialType virtual RsItem * deserialise(void *data, uint32_t *size); }; -// Top class for all services and config serializers. +typedef rapidjson::Document RsJson; -class RsGenericSerializer: public RsSerialType +/// Top class for all services and config serializers. +struct RsGenericSerializer : RsSerialType { -public: - typedef enum { SIZE_ESTIMATE = 0x01, SERIALIZE = 0x02, DESERIALIZE = 0x03, PRINT=0x04 } SerializeJob ; - typedef enum { FORMAT_BINARY = 0x01, FORMAT_JSON = 0x02 } SerializationFormat ; - - class SerializeContext - { - public: + typedef enum + { + SIZE_ESTIMATE = 0x01, + SERIALIZE = 0x02, + DESERIALIZE = 0x03, + PRINT = 0x04, + TO_JSON, + FROM_JSON + } SerializeJob; - SerializeContext(uint8_t *data,uint32_t size,SerializationFormat format,SerializationFlags flags) - : mData(data),mSize(size),mOffset(0),mOk(true),mFormat(format),mFlags(flags) {} + /** @deprecated use SerializeJob instead */ + RS_DEPRECATED typedef enum + { + FORMAT_BINARY = 0x01, + FORMAT_JSON = 0x02 + } SerializationFormat; - unsigned char *mData ; - uint32_t mSize ; - uint32_t mOffset ; - bool mOk ; - SerializationFormat mFormat ; - SerializationFlags mFlags ; - }; + struct SerializeContext + { + /** Allow shared allocator usage to avoid costly JSON deepcopy for + * nested RsSerializable */ + SerializeContext( + uint8_t *data, uint32_t size, SerializationFormat format, + SerializationFlags flags, + RsJson::AllocatorType* allocator = nullptr) : + mData(data), mSize(size), mOffset(0), mOk(true), mFormat(format), + mFlags(flags), mJson(rapidjson::kObjectType, allocator) {} - // These are convenience flags to be used by the items when processing the data. The names of the flags - // are not very important. What matters is that the serial_process() method of each item correctly - // deals with the data when it sees the flags, if the serialiser sets them. By default the flags are not - // set and shouldn't be handled. - // When deriving a new serializer, the user can set his own flags, using compatible values + unsigned char *mData; + uint32_t mSize; + uint32_t mOffset; + bool mOk; + RS_DEPRECATED SerializationFormat mFormat; + SerializationFlags mFlags; + RsJson mJson; + }; - static const SerializationFlags SERIALIZATION_FLAG_NONE ; // 0x0000 - static const SerializationFlags SERIALIZATION_FLAG_CONFIG ; // 0x0001 - static const SerializationFlags SERIALIZATION_FLAG_SIGNATURE ; // 0x0002 - static const SerializationFlags SERIALIZATION_FLAG_SKIP_HEADER ; // 0x0004 + /** These are convenience flags to be used by the items when processing the + * data. The names of the flags are not very important. What matters is that + * the serial_process() method of each item correctly deals with the data + * when it sees the flags, if the serialiser sets them. + * By default the flags are not set and shouldn't be handled. + * When deriving a new serializer, the user can set his own flags, using + * compatible values + */ + static const SerializationFlags SERIALIZATION_FLAG_NONE; // 0x0000 + static const SerializationFlags SERIALIZATION_FLAG_CONFIG; // 0x0001 + static const SerializationFlags SERIALIZATION_FLAG_SIGNATURE; // 0x0002 + static const SerializationFlags SERIALIZATION_FLAG_SKIP_HEADER; // 0x0004 - // The following functions overload RsSerialType. They *should not* need to be further overloaded. - - RsItem *deserialise(void *data,uint32_t *size) =0; - bool serialise(RsItem *item,void *data,uint32_t *size) ; - uint32_t size(RsItem *item) ; - void print(RsItem *item) ; + /** + * The following functions overload RsSerialType. + * They *should not* need to be further overloaded. + */ + RsItem *deserialise(void *data,uint32_t *size) = 0; + bool serialise(RsItem *item,void *data,uint32_t *size); + uint32_t size(RsItem *item); + void print(RsItem *item); protected: - RsGenericSerializer(uint8_t serial_class, - uint8_t serial_type, - SerializationFormat format, - SerializationFlags flags ) - : RsSerialType(RS_PKT_VERSION1,serial_class,serial_type), mFormat(format),mFlags(flags) - {} + RsGenericSerializer( + uint8_t serial_class, uint8_t serial_type, + SerializationFormat format, SerializationFlags flags ) : + RsSerialType( RS_PKT_VERSION1, serial_class, serial_type), + mFormat(format), mFlags(flags) {} - RsGenericSerializer(uint16_t service, - SerializationFormat format, - SerializationFlags flags ) - : RsSerialType(RS_PKT_VERSION_SERVICE,service), mFormat(format),mFlags(flags) - {} - - SerializationFormat mFormat ; - SerializationFlags mFlags ; + RsGenericSerializer( + uint16_t service, SerializationFormat format, + SerializationFlags flags ) : + RsSerialType( RS_PKT_VERSION_SERVICE, service ), mFormat(format), + mFlags(flags) {} + SerializationFormat mFormat; + SerializationFlags mFlags; }; -// Top class for service serializers. Derive your on service serializer from this class and overload creat_item(). -class RsServiceSerializer: public RsGenericSerializer +/** Top class for service serializers. + * Derive your on service serializer from this class and overload creat_item(). + */ +struct RsServiceSerializer : RsGenericSerializer { -public: - RsServiceSerializer(uint16_t service_id, - SerializationFormat format = FORMAT_BINARY, - SerializationFlags flags = SERIALIZATION_FLAG_NONE) + RsServiceSerializer( + uint16_t service_id, SerializationFormat format = FORMAT_BINARY, + SerializationFlags flags = SERIALIZATION_FLAG_NONE ) : + RsGenericSerializer(service_id, format, flags) {} - : RsGenericSerializer(service_id,format,flags) {} + /*! should be overloaded to create the correct type of item depending on the + * data */ + virtual RsItem *create_item( uint16_t /* service */, + uint8_t /* item_sub_id */ ) const = 0; - /*! create_item - * should be overloaded to create the correct type of item depending on the data - */ - virtual RsItem *create_item(uint16_t /* service */, uint8_t /* item_sub_id */) const=0; - - RsItem *deserialise(void *data,uint32_t *size) ; + RsItem *deserialise(void *data, uint32_t *size); }; -// Top class for config serializers. Config serializers are only used internally by RS core. The development of new services or plugins do not need this. -class RsConfigSerializer: public RsGenericSerializer +/** Top class for config serializers. + * Config serializers are only used internally by RS core. + * The development of new services or plugins do not need this. + */ +struct RsConfigSerializer : RsGenericSerializer { -public: RsConfigSerializer(uint8_t config_class, uint8_t config_type, SerializationFormat format = FORMAT_BINARY, - SerializationFlags flags = SERIALIZATION_FLAG_NONE) + SerializationFlags flags = SERIALIZATION_FLAG_NONE) : + RsGenericSerializer(config_class,config_type,format,flags) {} - : RsGenericSerializer(config_class,config_type,format,flags) {} + /*! should be overloaded to create the correct type of item depending on the + * data */ + virtual RsItem *create_item(uint8_t /* item_type */, + uint8_t /* item_sub_type */) const = 0; - /*! create_item - * should be overloaded to create the correct type of item depending on the data - */ - virtual RsItem *create_item(uint8_t /* item_type */, uint8_t /* item_sub_type */) const=0; - - RsItem *deserialise(void *data,uint32_t *size) ; + RsItem *deserialise(void *data,uint32_t *size); }; - - - diff --git a/libretroshare/src/serialiser/rstypeserializer.cc b/libretroshare/src/serialiser/rstypeserializer.cc index 8be8660ae..c2b0ba905 100644 --- a/libretroshare/src/serialiser/rstypeserializer.cc +++ b/libretroshare/src/serialiser/rstypeserializer.cc @@ -3,7 +3,8 @@ * * RetroShare Serialiser. * - * Copyright 2017 by Cyril Soler + * Copyright (C) 2017 Cyril Soler + * Copyright (C) 2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -26,27 +27,48 @@ #include "serialiser/rstypeserializer.h" #include "serialiser/rsbaseserial.h" #include "serialiser/rstlvkeys.h" - -#include "rsitems/rsitem.h" +#include "serialiser/rsserializable.h" #include "util/rsprint.h" #include #include #include +#include +#include // for typeid +#include +#include //static const uint32_t MAX_SERIALIZED_ARRAY_SIZE = 500 ; static const uint32_t MAX_SERIALIZED_CHUNK_SIZE = 10*1024*1024 ; // 10 MB. -//=================================================================================================// -// Integer types // -//=================================================================================================// +#define SAFE_GET_JSON_V() \ + const char* mName = memberName.c_str(); \ + bool ret = jVal.HasMember(mName); \ + if(!ret) \ + { \ + std::cerr << __PRETTY_FUNCTION__ << " \"" << memberName \ + << "\" not found in JSON:" << std::endl \ + << jVal << std::endl << std::endl; \ + print_stacktrace(); \ + return false; \ + } \ + rapidjson::Value& v = jVal[mName] + +//============================================================================// +// Integer types // +//============================================================================// template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const bool& member) { return setRawUInt8(data,size,&offset,member); } +template<> bool RsTypeSerializer::serialize(uint8_t /*data*/[], uint32_t /*size*/, uint32_t& /*offset*/, const int32_t& /*member*/) +{ + // TODO: nice to have but not used ATM + return false; +} template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const uint8_t& member) { return setRawUInt8(data,size,&offset,member); @@ -70,10 +92,15 @@ template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint3 template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, bool& member) { - uint8_t m ; + uint8_t m; bool ok = getRawUInt8(data,size,&offset,&m); - member = m ; - return ok; + member = m; + return ok; +} +template<> bool RsTypeSerializer::deserialize(const uint8_t /*data*/[], uint32_t /*size*/, uint32_t& /*offset*/, int32_t& /*member*/) +{ + // TODO: nice to have but not used ATM + return false; } template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, uint8_t& member) { @@ -100,6 +127,10 @@ template<> uint32_t RsTypeSerializer::serial_size(const bool& /* member*/) { return 1; } +template<> uint32_t RsTypeSerializer::serial_size(const int32_t& /* member*/) +{ + return 4; +} template<> uint32_t RsTypeSerializer::serial_size(const uint8_t& /* member*/) { return 1; @@ -125,6 +156,10 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const bool & { std::cerr << " [bool ] " << n << ": " << V << std::endl; } +template<> void RsTypeSerializer::print_data(const std::string& n, const int32_t& V) +{ + std::cerr << " [int32_t ] " << n << ": " << V << std::endl; +} template<> void RsTypeSerializer::print_data(const std::string& n, const uint8_t & V) { std::cerr << " [uint8_t ] " << n << ": " << V << std::endl; @@ -146,10 +181,104 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const time_t& std::cerr << " [time_t ] " << n << ": " << V << " (" << time(NULL)-V << " secs ago)" << std::endl; } +#define SIMPLE_TO_JSON_DEF(T) \ +template<> bool RsTypeSerializer::to_JSON( const std::string& memberName, \ + const T& member, RsJson& jDoc ) \ +{ \ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); \ + \ + rapidjson::Value key; \ + key.SetString(memberName.c_str(), memberName.length(), allocator); \ + \ + rapidjson::Value value(member); \ + \ + jDoc.AddMember(key, value, allocator); \ + \ + return true; \ +} -//=================================================================================================// -// FLoats // -//=================================================================================================// +SIMPLE_TO_JSON_DEF(bool) +SIMPLE_TO_JSON_DEF(int32_t) +SIMPLE_TO_JSON_DEF(time_t) +SIMPLE_TO_JSON_DEF(uint8_t) +SIMPLE_TO_JSON_DEF(uint16_t) +SIMPLE_TO_JSON_DEF(uint32_t) +SIMPLE_TO_JSON_DEF(uint64_t) + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, bool& member, + RsJson& jVal ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsBool(); + if(ret) member = v.GetBool(); + return ret; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + int32_t& member, RsJson& jVal ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsInt(); + if(ret) member = v.GetInt(); + return ret; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, time_t& member, + RsJson& jVal ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsUint(); + if(ret) member = v.GetUint(); + return ret; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + uint8_t& member, RsJson& jVal ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsUint(); + if(ret) member = v.GetUint(); + return ret; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + uint16_t& member, RsJson& jVal ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsUint(); + if(ret) member = v.GetUint(); + return ret; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + uint32_t& member, RsJson& jVal ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsUint(); + if(ret) member = v.GetUint(); + return ret; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + uint64_t& member, RsJson& jVal ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsUint(); + if(ret) member = v.GetUint(); + return ret; +} + + +//============================================================================// +// FLoats // +//============================================================================// template<> uint32_t RsTypeSerializer::serial_size(const float&){ return 4; } @@ -166,10 +295,75 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const float& std::cerr << " [float ] " << n << ": " << V << std::endl; } +SIMPLE_TO_JSON_DEF(float) -//=================================================================================================// -// TlvString with subtype // -//=================================================================================================// +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + float& member, RsJson& jVal ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsFloat(); + if(ret) member = v.GetFloat(); + return ret; +} + + +//============================================================================// +// std::string // +//============================================================================// + +template<> uint32_t RsTypeSerializer::serial_size(const std::string& str) +{ + return getRawStringSize(str); +} +template<> bool RsTypeSerializer::serialize( uint8_t data[], uint32_t size, + uint32_t& offset, + const std::string& str ) +{ + return setRawString(data, size, &offset, str); +} +template<> bool RsTypeSerializer::deserialize( const uint8_t data[], + uint32_t size, uint32_t &offset, + std::string& str ) +{ + return getRawString(data, size, &offset, str); +} +template<> void RsTypeSerializer::print_data( const std::string& n, + const std::string& str ) +{ + std::cerr << " [std::string] " << n << ": " << str << std::endl; +} +template<> /*static*/ +bool RsTypeSerializer::to_JSON( const std::string& membername, + const std::string& member, RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + rapidjson::Value key; + key.SetString(membername.c_str(), membername.length(), allocator); + + rapidjson::Value value;; + value.SetString(member.c_str(), member.length(), allocator); + + jDoc.AddMember(key, value, allocator); + + return true; +} +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + std::string& member, RsJson& jVal ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsString(); + if(ret) member = v.GetString(); + return ret; +} + + + +//============================================================================// +// TlvString with subtype // +//============================================================================// template<> uint32_t RsTypeSerializer::serial_size(uint16_t /* type_subtype */,const std::string& s) { @@ -188,46 +382,130 @@ template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t siz template<> void RsTypeSerializer::print_data(const std::string& n, uint16_t type_substring,const std::string& V) { - std::cerr << " [TlvString ] " << n << ": type=" << std::hex < uint32_t RsTypeSerializer::serial_size(uint16_t /* type_subtype */,const uint32_t& /*s*/) +template<> /*static*/ +bool RsTypeSerializer::to_JSON( const std::string& memberName, + uint16_t /*sub_type*/, + const std::string& member, RsJson& jDoc ) { - return GetTlvUInt32Size() ; + return to_JSON(memberName, member, jDoc); } -template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset,uint16_t sub_type,const uint32_t& s) +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + uint16_t /*sub_type*/, + std::string& member, RsJson& jVal ) { - return SetTlvUInt32(data,size,&offset,sub_type,s) ; + return from_JSON(memberName, member, jVal); } -template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size,uint32_t& offset,uint16_t sub_type,uint32_t& s) +//============================================================================// +// TlvInt with subtype // +//============================================================================// + +template<> uint32_t RsTypeSerializer::serial_size( uint16_t /* type_subtype */, + const uint32_t& /*s*/ ) { - return GetTlvUInt32((void*)data,size,&offset,sub_type,&s) ; + return GetTlvUInt32Size(); +} + +template<> bool RsTypeSerializer::serialize( uint8_t data[], uint32_t size, + uint32_t &offset,uint16_t sub_type, + const uint32_t& s) +{ + return SetTlvUInt32(data,size,&offset,sub_type,s); +} + +template<> bool RsTypeSerializer::deserialize( const uint8_t data[], + uint32_t size, uint32_t& offset, + uint16_t sub_type, uint32_t& s) +{ + return GetTlvUInt32((void*)data, size, &offset, sub_type, &s); } template<> void RsTypeSerializer::print_data(const std::string& n, uint16_t sub_type,const uint32_t& V) { - std::cerr << " [TlvUInt32 ] " << n << ": type=" << std::hex < void RsTypeSerializer::print_data(const std::string& n, const std::string& V) +template<> /*static*/ +bool RsTypeSerializer::to_JSON( const std::string& memberName, + uint16_t /*sub_type*/, + const uint32_t& member, RsJson& jDoc ) { - std::cerr << " [std::string] " << n << ": " << V << std::endl; + return to_JSON(memberName, member, jDoc); } -//=================================================================================================// -// Binary blocks // -//=================================================================================================// +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + uint16_t /*sub_type*/, + uint32_t& member, RsJson& jVal ) +{ + return from_JSON(memberName, member, jVal); +} + + +//============================================================================// +// TlvItems // +//============================================================================// + +template<> uint32_t RsTypeSerializer::serial_size(const RsTlvItem& s) +{ + return s.TlvSize(); +} + +template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, + uint32_t &offset,const RsTlvItem& s) +{ + return s.SetTlv(data,size,&offset); +} + +template<> bool RsTypeSerializer::deserialize(const uint8_t data[], + uint32_t size,uint32_t& offset, + RsTlvItem& s) +{ + return s.GetTlv((void*)data,size,&offset) ; +} + +template<> void RsTypeSerializer::print_data( const std::string& n, + const RsTlvItem& s ) +{ + std::cerr << " [" << typeid(s).name() << "] " << n << std::endl; +} + +template<> /*static*/ +bool RsTypeSerializer::to_JSON( const std::string& memberName, + const RsTlvItem& member, RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + rapidjson::Value value; + const char* tName = typeid(member).name(); + value.SetString(tName, allocator); + + jDoc.AddMember(key, value, allocator); + + return true; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& /*memberName*/, + RsTlvItem& /*member*/, RsJson& /*jVal*/) +{ + return true; +} + + +//============================================================================// +// Binary blocks // +//============================================================================// template<> uint32_t RsTypeSerializer::serial_size(const RsTypeSerializer::TlvMemBlock_proxy& r) { return 4 + r.second ; } @@ -286,82 +564,141 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const RsTypeS std::cerr << " [Binary data] " << n << ", length=" << s.second << " data=" << RsUtil::BinToHex((uint8_t*)s.first,std::min(50u,s.second)) << ((s.second>50)?"...":"") << std::endl; } -//=================================================================================================// -// TlvItems // -//=================================================================================================// - -template<> uint32_t RsTypeSerializer::serial_size(const RsTlvItem& s) -{ - return s.TlvSize() ; -} - -template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset,const RsTlvItem& s) -{ - return s.SetTlv(data,size,&offset) ; -} - -template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size,uint32_t& offset,RsTlvItem& s) -{ - return s.GetTlv((void*)data,size,&offset) ; -} - -template<> void RsTypeSerializer::print_data(const std::string& n, const RsTlvItem& s) -{ - // can we call TlvPrint inside this? - - std::cerr << " [" << typeid(s).name() << "] " << n << std::endl; -} //============================================================================// -// RsItem and derivated // +// RsSerializable and derivated // //============================================================================// -template<> uint32_t RsTypeSerializer::serial_size(const RsItem& s) +template<> uint32_t RsTypeSerializer::serial_size(const RsSerializable& s) { RsGenericSerializer::SerializeContext ctx( NULL, 0, RsGenericSerializer::FORMAT_BINARY, RsGenericSerializer::SERIALIZATION_FLAG_NONE ); ctx.mOffset = 8; // header size - const_cast(s).serial_process(RsGenericSerializer::SIZE_ESTIMATE, - ctx); + const_cast(s).serial_process( + RsGenericSerializer::SIZE_ESTIMATE, ctx ); return ctx.mOffset; } template<> bool RsTypeSerializer::serialize( uint8_t data[], uint32_t size, - uint32_t &offset, const RsItem& s ) + uint32_t &offset, + const RsSerializable& s ) { RsGenericSerializer::SerializeContext ctx( data, size, RsGenericSerializer::FORMAT_BINARY, RsGenericSerializer::SERIALIZATION_FLAG_NONE ); ctx.mOffset = offset; - const_cast(s).serial_process(RsGenericSerializer::SERIALIZE, - ctx); + const_cast(s).serial_process( + RsGenericSerializer::SERIALIZE, ctx ); return true; } template<> bool RsTypeSerializer::deserialize( const uint8_t data[], uint32_t size, uint32_t& offset, - RsItem& s ) + RsSerializable& s ) { RsGenericSerializer::SerializeContext ctx( const_cast(data), size, RsGenericSerializer::FORMAT_BINARY, RsGenericSerializer::SERIALIZATION_FLAG_NONE ); ctx.mOffset = offset; - const_cast(s).serial_process(RsGenericSerializer::DESERIALIZE, - ctx); + const_cast(s).serial_process( + RsGenericSerializer::DESERIALIZE, ctx ); return true; } template<> void RsTypeSerializer::print_data( const std::string& /*n*/, - const RsItem& s ) + const RsSerializable& s ) { RsGenericSerializer::SerializeContext ctx( NULL, 0, RsGenericSerializer::FORMAT_BINARY, RsGenericSerializer::SERIALIZATION_FLAG_NONE ); - const_cast(s).serial_process(RsGenericSerializer::PRINT, - ctx); + const_cast(s).serial_process( RsGenericSerializer::PRINT, + ctx ); +} + +template<> /*static*/ +bool RsTypeSerializer::to_JSON( const std::string& memberName, + const RsSerializable& member, RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + // Reuse allocator to avoid deep copy later + RsGenericSerializer::SerializeContext ctx( + NULL, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE, + &allocator ); + + const_cast(member).serial_process( + RsGenericSerializer::TO_JSON, ctx ); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + /* Because the passed allocator is reused it doesn't go out of scope and + * there is no need of deep copy and we can take advantage of the much + * faster rapidjson move semantic */ + jDoc.AddMember(key, ctx.mJson, allocator); + + return ctx.mOk; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + RsSerializable& member, RsJson& jVal ) +{ + SAFE_GET_JSON_V(); + + if(ret) + { + RsGenericSerializer::SerializeContext ctx( + NULL, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE ); + + ctx.mJson.SetObject() = v; // Beware of move semantic!! + member.serial_process(RsGenericSerializer::FROM_JSON, ctx); + ret = ret && ctx.mOk; + } + + return ret; +} + +template<> /*static*/ +void RsTypeSerializer::serial_process( + RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx, RsSerializable& object, + const std::string& objName ) +{ + switch (j) + { + case RsGenericSerializer::SIZE_ESTIMATE: // fall-through + case RsGenericSerializer::SERIALIZE: // fall-through + case RsGenericSerializer::DESERIALIZE: // fall-through + case RsGenericSerializer::PRINT: // fall-through + object.serial_process(j, ctx); + break; + case RsGenericSerializer::TO_JSON: + ctx.mOk = ctx.mOk && to_JSON(objName, object, ctx.mJson); + break; + case RsGenericSerializer::FROM_JSON: + ctx.mOk = ctx.mOk && from_JSON(objName, object, ctx.mJson); + break; + default: break; + } +} + + +//============================================================================// +// RsJson std:ostream support // +//============================================================================// + +std::ostream &operator<<(std::ostream &out, const RsJson &jDoc) +{ + rapidjson::StringBuffer buffer; buffer.Clear(); + rapidjson::PrettyWriter writer(buffer); + jDoc.Accept(writer); + return out << buffer.GetString(); } diff --git a/libretroshare/src/serialiser/rstypeserializer.h b/libretroshare/src/serialiser/rstypeserializer.h index f4867f10f..1de678d50 100644 --- a/libretroshare/src/serialiser/rstypeserializer.h +++ b/libretroshare/src/serialiser/rstypeserializer.h @@ -3,7 +3,8 @@ * * RetroShare Serialiser. * - * Copyright 2017 by Cyril Soler + * Copyright (C) 2017 Cyril Soler + * Copyright (C) 2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -32,83 +33,91 @@ #include "retroshare/rsids.h" #include "serialiser/rsserializer.h" +#include "serialiser/rsserializable.h" -/** @def RS_REGISTER_SERIAL_MEMBER(I) - * Use this macro to register the members of `YourItem` for serial processing - * inside `YourItem::serial_process(j, ctx)` - * - * Inspired by http://stackoverflow.com/a/39345864 +#include + +/** INTERNAL ONLY helper to avoid copy paste code for std::{vector,list,set} + * Can't use a template function because T is needed for const_cast */ +#define RsTypeSerializer_PRIVATE_TO_JSON_ARRAY() do \ +{ \ + using namespace rapidjson; \ +\ + Document::AllocatorType& allocator = ctx.mJson.GetAllocator(); \ +\ + Value arrKey; arrKey.SetString(memberName.c_str(), allocator); \ +\ + Value arr(kArrayType); \ +\ + for (auto& el : v) \ + { \ + /* Use same allocator to avoid deep copy */\ + RsGenericSerializer::SerializeContext elCtx(\ + nullptr, 0, RsGenericSerializer::FORMAT_BINARY,\ + RsGenericSerializer::SERIALIZATION_FLAG_NONE,\ + &allocator );\ +\ + /* If el is const the default serial_process template is matched */ \ + /* also when specialization is necessary so the compilation break */ \ + serial_process(j, elCtx, const_cast(el), memberName); \ +\ + elCtx.mOk = elCtx.mOk && elCtx.mJson.HasMember(arrKey);\ + if(elCtx.mOk) arr.PushBack(elCtx.mJson[arrKey], allocator);\ + else\ + {\ + ctx.mOk = false;\ + break;\ + }\ + }\ +\ + ctx.mJson.AddMember(arrKey, arr, allocator);\ +} while (false) + +/** INTERNAL ONLY helper to avoid copy paste code for std::{vector,list,set} + * Can't use a template function because std::{vector,list,set} has different + * name for insert/push_back function */ -#define RS_REGISTER_SERIAL_MEMBER(I) \ - do { RsTypeSerializer::serial_process(j, ctx, I, #I); } while(0) +#define RsTypeSerializer_PRIVATE_FROM_JSON_ARRAY(INSERT_FUN) do\ +{\ + using namespace rapidjson;\ +\ + bool& ok(ctx.mOk);\ + Document& jDoc(ctx.mJson);\ + Document::AllocatorType& allocator = jDoc.GetAllocator();\ +\ + Value arrKey;\ + arrKey.SetString(memberName.c_str(), memberName.length());\ +\ + ok = ok && jDoc.IsObject();\ + ok = ok && jDoc.HasMember(arrKey);\ +\ + if(ok && jDoc[arrKey].IsArray())\ + {\ + for (auto&& arrEl : jDoc[arrKey].GetArray())\ + {\ + RsGenericSerializer::SerializeContext elCtx(\ + nullptr, 0, RsGenericSerializer::FORMAT_BINARY,\ + RsGenericSerializer::SERIALIZATION_FLAG_NONE,\ + &allocator );\ + elCtx.mJson.AddMember(arrKey, arrEl, allocator);\ +\ + T el;\ + serial_process(j, elCtx, el, memberName); \ + ok = ok && elCtx.mOk;\ +\ + if(ok) v.INSERT_FUN(el);\ + else break;\ + }\ + }\ +} while(false) -/** @def RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) - * This macro usage is similar to @see RS_REGISTER_SERIAL_MEMBER(I) but it - * permit to force serialization/deserialization type, it is expecially useful - * with enum class members or RsTlvItem derivative members, be very careful with - * the type you pass, as reinterpret_cast on a reference is used that is - * expecially permissive so you can shot your feet if not carefull enough. - * - * If you are using this with an RsItem derivative (so passing RsItem as T) - * consider to register your item type with @see RS_REGISTER_ITEM_TYPE(T) in - * association with @see RS_REGISTER_SERIAL_MEMBER(I) that rely on template - * function generation, as in this particular case - * RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) would cause the serial code rely on - * C++ dynamic dispatching that may have a noticeable impact on runtime - * performances. - */ -#pragma GCC diagnostic ignored "-Wstrict-aliasing" -#define RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) do {\ - RsTypeSerializer::serial_process(j, ctx, reinterpret_cast(I), #I);\ - } while(0) -#pragma GCC diagnostic pop - -/** @def RS_REGISTER_ITEM_TYPE(T) - * Use this macro into `youritem.cc` only if you need to process members of - * subtypes of RsItem. - * - * The usage of this macro is strictly needed only in some cases, for example if - * you are registering for serialization a member of a container that contains - * items of subclasses of RsItem like * `std::map` - * - * @code{.cpp} -struct PrivateOugoingMapItem : RsChatItem -{ - PrivateOugoingMapItem() : RsChatItem(RS_PKT_SUBTYPE_OUTGOING_MAP) {} - - void serial_process( RsGenericSerializer::SerializeJob j, - RsGenericSerializer::SerializeContext& ctx ); - - std::map store; -}; - -RS_REGISTER_ITEM_TYPE(RsChatMsgItem) - -void PrivateOugoingMapItem::serial_process( - RsGenericSerializer::SerializeJob j, - RsGenericSerializer::SerializeContext& ctx ) -{ - // store is of type - RS_REGISTER_SERIAL_MEMBER(store); -} - * @endcode - * - * If you use this macro with a lot of different item types this can cause the - * generated binary grow in size, consider the usage of - * @see RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) passing RsItem as type in that - * case. - */ -#define RS_REGISTER_ITEM_TYPE(T) template<> \ - void RsTypeSerializer::serial_process( \ - RsGenericSerializer::SerializeJob j,\ - RsGenericSerializer::SerializeContext& ctx, T& item,\ - const std::string& /*name*/) { item.serial_process(j, ctx); } +std::ostream &operator<<(std::ostream &out, const RsJson &jDoc); struct RsTypeSerializer { /** This type should be used to pass a parameter to drive the serialisation * if needed */ - struct TlvMemBlock_proxy: public std::pair + struct TlvMemBlock_proxy : std::pair { TlvMemBlock_proxy(void*& p, uint32_t& s) : std::pair(p,s) {} @@ -120,7 +129,7 @@ struct RsTypeSerializer template static void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, - T& member,const std::string& member_name) + T& member, const std::string& member_name ) { switch(j) { @@ -138,6 +147,12 @@ struct RsTypeSerializer case RsGenericSerializer::PRINT: print_data(member_name,member); break; + case RsGenericSerializer::TO_JSON: + ctx.mOk = ctx.mOk && to_JSON(member_name, member, ctx.mJson); + break; + case RsGenericSerializer::FROM_JSON: + ctx.mOk = ctx.mOk && from_JSON(member_name, member, ctx.mJson); + break; default: ctx.mOk = false; throw std::runtime_error("Unknown serial job"); @@ -165,7 +180,15 @@ struct RsTypeSerializer serialize(ctx.mData,ctx.mSize,ctx.mOffset,type_id,member); break; case RsGenericSerializer::PRINT: - print_data(member_name,type_id,member); + print_data(member_name, member); + break; + case RsGenericSerializer::TO_JSON: + ctx.mOk = ctx.mOk && + to_JSON(member_name, type_id, member, ctx.mJson); + break; + case RsGenericSerializer::FROM_JSON: + ctx.mOk = ctx.mOk && + from_JSON(member_name, type_id, member, ctx.mJson); break; default: ctx.mOk = false; @@ -178,7 +201,7 @@ struct RsTypeSerializer static void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, std::map& v, - const std::string& member_name ) + const std::string& memberName ) { switch(j) { @@ -225,11 +248,11 @@ struct RsTypeSerializer case RsGenericSerializer::PRINT: { if(v.empty()) - std::cerr << " Empty map \"" << member_name << "\"" + std::cerr << " Empty map \"" << memberName << "\"" << std::endl; else std::cerr << " std::map of " << v.size() << " elements: \"" - << member_name << "\"" << std::endl; + << memberName << "\"" << std::endl; for(typename std::map::iterator it(v.begin());it!=v.end();++it) { @@ -242,6 +265,95 @@ struct RsTypeSerializer } break; } + case RsGenericSerializer::TO_JSON: + { + using namespace rapidjson; + + Document::AllocatorType& allocator = ctx.mJson.GetAllocator(); + Value arrKey; arrKey.SetString(memberName.c_str(), + memberName.length(), allocator); + Value arr(kArrayType); + + for (auto& kv : v) + { + // Use same allocator to avoid deep copy + RsGenericSerializer::SerializeContext kCtx( + nullptr, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE, + &allocator ); + serial_process(j, kCtx, const_cast(kv.first), "key"); + + RsGenericSerializer::SerializeContext vCtx( + nullptr, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE, + &allocator ); + serial_process(j, vCtx, const_cast(kv.second), "value"); + + if(kCtx.mOk && vCtx.mOk) + { + Value el(kObjectType); + el.AddMember("key", kCtx.mJson["key"], allocator); + el.AddMember("value", vCtx.mJson["value"], allocator); + + arr.PushBack(el, allocator); + } + } + + ctx.mJson.AddMember(arrKey, arr, allocator); + + break; + } + case RsGenericSerializer::FROM_JSON: + { + using namespace rapidjson; + + bool& ok(ctx.mOk); + Document& jDoc(ctx.mJson); + Document::AllocatorType& allocator = jDoc.GetAllocator(); + + Value arrKey; + arrKey.SetString(memberName.c_str(), memberName.length()); + + ok = ok && jDoc.IsObject(); + ok = ok && jDoc.HasMember(arrKey); + + if(ok && jDoc[arrKey].IsArray()) + { + for (auto&& kvEl : jDoc[arrKey].GetArray()) + { + ok = ok && kvEl.IsObject(); + ok = ok && kvEl.HasMember("key"); + ok = ok && kvEl.HasMember("value"); + if (!ok) break; + + RsGenericSerializer::SerializeContext kCtx( + nullptr, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE, + &allocator ); + ok && (kCtx.mJson. + AddMember("key", kvEl["key"], allocator), true); + + T key; + ok = ok && (serial_process(j, kCtx, key, "key"), kCtx.mOk); + + RsGenericSerializer::SerializeContext vCtx( + nullptr, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE, + &allocator ); + ok && (vCtx.mJson. + AddMember("value", kvEl["value"], allocator), true); + + U value; + ok = ok && ( serial_process(j, vCtx, value, "value"), + vCtx.mOk ); + + if(ok) v.insert(std::pair(key,value)); + else break; + } + } + + break; + } default: break; } } @@ -251,7 +363,7 @@ struct RsTypeSerializer static void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, std::vector& v, - const std::string& member_name ) + const std::string& memberName ) { switch(j) { @@ -259,7 +371,7 @@ struct RsTypeSerializer { ctx.mOffset += 4; for(uint32_t i=0;i static void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, - std::set& v, const std::string& member_name ) + std::set& v, const std::string& memberName ) { switch(j) { @@ -311,7 +429,7 @@ struct RsTypeSerializer for(typename std::set::iterator it(v.begin());it!=v.end();++it) // the const cast here is a hack to avoid serial_process to // instantiate serialise(const T&) - serial_process(j,ctx,const_cast(*it) ,member_name); + serial_process(j,ctx,const_cast(*it) ,memberName); break; } case RsGenericSerializer::DESERIALIZE: @@ -321,7 +439,7 @@ struct RsTypeSerializer for(uint32_t i=0; i(j,ctx,tmp,member_name); + serial_process(j,ctx,tmp,memberName); v.insert(tmp); } break; @@ -333,7 +451,7 @@ struct RsTypeSerializer for(typename std::set::iterator it(v.begin());it!=v.end();++it) // the const cast here is a hack to avoid serial_process to // instantiate serialise(const T&) - serial_process(j,ctx,const_cast(*it) ,member_name); + serial_process(j,ctx,const_cast(*it) ,memberName); break; } case RsGenericSerializer::PRINT: @@ -343,6 +461,12 @@ struct RsTypeSerializer << std::endl; break; } + case RsGenericSerializer::TO_JSON: + RsTypeSerializer_PRIVATE_TO_JSON_ARRAY(); + break; + case RsGenericSerializer::FROM_JSON: + RsTypeSerializer_PRIVATE_FROM_JSON_ARRAY(insert); + break; default: break; } } @@ -352,7 +476,7 @@ struct RsTypeSerializer static void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, std::list& v, - const std::string& member_name ) + const std::string& memberName ) { switch(j) { @@ -360,7 +484,7 @@ struct RsTypeSerializer { ctx.mOffset += 4; for(typename std::list::iterator it(v.begin());it!=v.end();++it) - serial_process(j,ctx,*it ,member_name); + serial_process(j,ctx,*it ,memberName); break; } case RsGenericSerializer::DESERIALIZE: @@ -370,7 +494,7 @@ struct RsTypeSerializer for(uint32_t i=0;i(j,ctx,tmp,member_name); + serial_process(j,ctx,tmp,memberName); v.push_back(tmp); } break; @@ -380,7 +504,7 @@ struct RsTypeSerializer uint32_t n=v.size(); serial_process(j,ctx,n,"temporary size"); for(typename std::list::iterator it(v.begin());it!=v.end();++it) - serial_process(j,ctx,*it ,member_name); + serial_process(j,ctx,*it ,memberName); break; } case RsGenericSerializer::PRINT: @@ -390,6 +514,12 @@ struct RsTypeSerializer << std::endl; break; } + case RsGenericSerializer::TO_JSON: + RsTypeSerializer_PRIVATE_TO_JSON_ARRAY(); + break; + case RsGenericSerializer::FROM_JSON: + RsTypeSerializer_PRIVATE_FROM_JSON_ARRAY(push_back); + break; default: break; } } @@ -399,7 +529,7 @@ struct RsTypeSerializer static void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, t_RsFlags32& v, - const std::string& /*member_name*/) + const std::string& memberName ) { switch(j) { @@ -421,16 +551,27 @@ struct RsTypeSerializer std::cerr << " Flags of type " << std::hex << N << " : " << v.toUInt32() << std::endl; break; + case RsGenericSerializer::TO_JSON: + ctx.mOk = to_JSON(memberName, v.toUInt32(), ctx.mJson); + break; + case RsGenericSerializer::FROM_JSON: + { + uint32_t f; + ctx.mOk = from_JSON(memberName, f, ctx.mJson); + v = t_RsFlags32(f); + break; + } + default: break; } } -/** TODO - * Serialization format is inside context, but context is not passed to - * following functions, that need to know the format to do the job, actually - * RsGenericSerializer::FORMAT_BINARY is assumed in all of them!! - */ protected: + +//============================================================================// +// Generic types declarations // +//============================================================================// + template static bool serialize( uint8_t data[], uint32_t size, uint32_t &offset, const T& member ); @@ -442,17 +583,41 @@ protected: template static void print_data( const std::string& name, const T& member); + template static bool to_JSON( const std::string& membername, + const T& member, RsJson& jDoc ); + + template static bool from_JSON( const std::string& memberName, + T& member, RsJson& jDoc ); + +//============================================================================// +// Generic types + type_id declarations // +//============================================================================// template static bool serialize( uint8_t data[], uint32_t size, uint32_t &offset, uint16_t type_id, const T& member ); + template static bool deserialize( const uint8_t data[], uint32_t size, uint32_t &offset, uint16_t type_id, T& member ); + template static uint32_t serial_size( uint16_t type_id,const T& member ); - template static void print_data( - const std::string& name,uint16_t type_id,const T& member ); + + template static void print_data( const std::string& n, + uint16_t type_id,const T& member ); + + template static bool to_JSON( const std::string& membername, + uint16_t type_id, + const T& member, RsJson& jVal ); + + template static bool from_JSON( const std::string& memberName, + uint16_t type_id, + T& member, RsJson& jDoc ); + +//============================================================================// +// t_RsGenericId<...> declarations // +//============================================================================// template static bool serialize( @@ -466,13 +631,29 @@ protected: template static uint32_t serial_size( - const t_RsGenericIdType& member ); + const t_RsGenericIdType< + ID_SIZE_IN_BYTES,UPPER_CASE,UNIQUE_IDENTIFIER>& member ); template static void print_data( const std::string& name, const t_RsGenericIdType& member ); + template + static bool to_JSON( + const std::string& membername, + const t_RsGenericIdType& member, + RsJson& jVal ); + + template + static bool from_JSON( + const std::string& memberName, + t_RsGenericIdType& member, + RsJson& jDoc ); + +//============================================================================// +// t_RsTlvList<...> declarations // +//============================================================================// template static bool serialize( @@ -491,16 +672,33 @@ protected: static void print_data( const std::string& name, const t_RsTlvList& member); + + template + static bool to_JSON( const std::string& membername, + const t_RsTlvList& member, + RsJson& jVal ); + + template + static bool from_JSON( const std::string& memberName, + t_RsTlvList& member, + RsJson& jDoc ); }; +//============================================================================// +// t_RsGenericId<...> // +//============================================================================// -// t_RsGenericId<> template bool RsTypeSerializer::serialize ( uint8_t data[], uint32_t size, uint32_t &offset, - const t_RsGenericIdType& member ) -{ return (*const_cast *>(&member)).serialise(data,size,offset); } + const t_RsGenericIdType< + ID_SIZE_IN_BYTES,UPPER_CASE,UNIQUE_IDENTIFIER>& member ) +{ + return (*const_cast *>(&member) + ).serialise(data,size,offset); +} template bool RsTypeSerializer::deserialize( @@ -522,8 +720,45 @@ void RsTypeSerializer::print_data( << member << std::endl; } +template +bool RsTypeSerializer::to_JSON( const std::string& memberName, + const t_RsGenericIdType& member, + RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + const std::string vStr = member.toStdString(); + rapidjson::Value value; + value.SetString(vStr.c_str(), vStr.length(), allocator); + + jDoc.AddMember(key, value, allocator); + + return true; +} + +template +bool RsTypeSerializer::from_JSON( const std::string& membername, + t_RsGenericIdType& member, + RsJson& jVal ) +{ + const char* mName = membername.c_str(); + bool ret = jVal.HasMember(mName); + if(ret) + { + rapidjson::Value& v = jVal[mName]; + ret = ret && v.IsString(); + ret && (member = t_RsGenericIdType(std::string(v.GetString())), false); + } + return ret; +} + +//============================================================================// +// t_RsTlvList<...> // +//============================================================================// -// t_RsTlvList<> template bool RsTypeSerializer::serialize( uint8_t data[], uint32_t size, uint32_t &offset, @@ -553,3 +788,76 @@ void RsTypeSerializer::print_data( std::cerr << " [t_RsTlvString<" << std::hex << TLV_TYPE << ">] : size=" << member.mList.size() << std::endl; } + +template /* static */ +bool RsTypeSerializer::to_JSON( const std::string& memberName, + const t_RsTlvList& member, + RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + rapidjson::Value value; + const char* tName = typeid(member).name(); + value.SetString(tName, allocator); + + jDoc.AddMember(key, value, allocator); + + std::cerr << __PRETTY_FUNCTION__ << " JSON serialization for type " + << typeid(member).name() << " " << memberName + << " not available." << std::endl; + print_stacktrace(); + return true; +} + +template +bool RsTypeSerializer::from_JSON( const std::string& memberName, + t_RsTlvList& member, + RsJson& /*jVal*/ ) +{ + std::cerr << __PRETTY_FUNCTION__ << " JSON deserialization for type " + << typeid(member).name() << " " << memberName + << " not available." << std::endl; + print_stacktrace(); + return true; +} + + +//============================================================================// +// Generic types // +//============================================================================// + +template /*static*/ +bool RsTypeSerializer::to_JSON(const std::string& memberName, const T& member, + RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + rapidjson::Value value; + const char* tName = typeid(member).name(); + value.SetString(tName, allocator); + + jDoc.AddMember(key, value, allocator); + + std::cerr << __PRETTY_FUNCTION__ << " JSON serialization for type " + << typeid(member).name() << " " << memberName + << " not available." << std::endl; + print_stacktrace(); + return true; +} + +template /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + T& member, RsJson& /*jDoc*/ ) +{ + std::cerr << __PRETTY_FUNCTION__ << " JSON deserialization for type " + << typeid(member).name() << " " << memberName + << " not available." << std::endl; + print_stacktrace(); + return true; +} diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index 8695280a7..6dfb43ce8 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -3,7 +3,8 @@ * * Id interface for RetroShare. * - * Copyright 2012-2012 by Robert Fernie. + * Copyright (C) 2012 Robert Fernie + * Copyright (C) 2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -4505,11 +4506,30 @@ void p3IdService::handle_event(uint32_t event_type, const std::string &/*elabel* } } -RsIdentityUsage::RsIdentityUsage(uint16_t service,const RsIdentityUsage::UsageCode& code,const RsGxsGroupId& gid,const RsGxsMessageId& mid,uint64_t additional_id,const std::string& comment) - : mServiceId(service), mUsageCode(code), mGrpId(gid), mMsgId(mid),mAdditionalId(additional_id),mComment(comment) +void RsGxsIdGroup::serial_process( + RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) +{ + RS_REGISTER_SERIAL_MEMBER_TYPED(mMeta, RsSerializable); + RS_REGISTER_SERIAL_MEMBER(mPgpIdHash); + //RS_REGISTER_SERIAL_MEMBER(mPgpIdSign); + RS_REGISTER_SERIAL_MEMBER(mRecognTags); + //RS_REGISTER_SERIAL_MEMBER(mImage); + RS_REGISTER_SERIAL_MEMBER(mLastUsageTS); + RS_REGISTER_SERIAL_MEMBER(mPgpKnown); + RS_REGISTER_SERIAL_MEMBER(mIsAContact); + RS_REGISTER_SERIAL_MEMBER(mPgpId); + RS_REGISTER_SERIAL_MEMBER_TYPED(mReputation, RsSerializable); +} + +RsIdentityUsage::RsIdentityUsage( + uint16_t service, const RsIdentityUsage::UsageCode& code, + const RsGxsGroupId& gid, const RsGxsMessageId& mid, + uint64_t additional_id,const std::string& comment ) : + mServiceId(service), mUsageCode(code), mGrpId(gid), mMsgId(mid), + mAdditionalId(additional_id), mComment(comment) { #ifdef DEBUG_IDS - // This is a hack, since it will hash also mHash, but because it is initialized to 0, and only computed in the constructor here, it should be ok. std::cerr << "New identity usage: " << std::endl; std::cerr << " service=" << std::hex << service << std::endl; std::cerr << " code =" << std::hex << code << std::endl; @@ -4519,6 +4539,9 @@ RsIdentityUsage::RsIdentityUsage(uint16_t service,const RsIdentityUsage::UsageCo std::cerr << " commnt =\"" << std::hex << comment << "\"" << std::endl; #endif + /* This is a hack, since it will hash also mHash, but because it is + * initialized to 0, and only computed in the constructor here, it should + * be ok. */ librs::crypto::HashStream hs(librs::crypto::HashStream::SHA1) ; hs << (uint32_t)service ; @@ -4535,5 +4558,7 @@ RsIdentityUsage::RsIdentityUsage(uint16_t service,const RsIdentityUsage::UsageCo #endif } +RsIdentityUsage::RsIdentityUsage() : + mServiceId(0), mUsageCode(UNKNOWN_USAGE), mAdditionalId(0) {} - +RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsIdentityUsage) From 18891645e0a5549308bc2fd03744f2f8be79ef6c Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Tue, 16 Jan 2018 11:42:20 +0100 Subject: [PATCH 002/161] Add rapidjson installation in appveyor.yml --- appveyor.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index cc1cbef95..4878efe71 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -12,7 +12,7 @@ #---------------------------------# # version format -version: RetroShare 0.6.0.{build}-{branch} +version: RetroShare-git-{branch}-{build} # you can use {branch} name in version format too # version: 1.0.{build}-{branch} @@ -106,6 +106,7 @@ install: - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-ffmpeg mingw-w64-x86_64-ffmpeg" - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-libmicrohttpd mingw-w64-x86_64-libmicrohttpd" - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-libxslt mingw-w64-x86_64-libxslt" + - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-rapidjson mingw-w64-x86_64-rapidjson" # Hack for new MSys2 - copy C:\msys64\mingw32\i686-w64-mingw32\bin\ar.exe C:\msys64\mingw32\bin\i686-w64-mingw32-ar.exe From 9d40d416f6cf5d478386284835260764823dcb86 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sun, 21 Jan 2018 20:27:49 +0100 Subject: [PATCH 003/161] Better naming for serialization helper macro --- libretroshare/src/chat/rschatitems.cc | 2 +- libretroshare/src/gxstrans/p3gxstransitems.cc | 30 +++++++-------- libretroshare/src/gxstrans/p3gxstransitems.h | 8 ++-- .../src/retroshare/rsgxsifacetypes.h | 36 +++++++++--------- libretroshare/src/retroshare/rsidentity.h | 38 +++++++++---------- libretroshare/src/rsitems/rsnxsitems.cc | 12 +++--- libretroshare/src/serialiser/rsserializable.h | 25 ++++++------ libretroshare/src/services/p3idservice.cc | 20 +++++----- 8 files changed, 86 insertions(+), 85 deletions(-) diff --git a/libretroshare/src/chat/rschatitems.cc b/libretroshare/src/chat/rschatitems.cc index 370cef5d3..00511c1f8 100644 --- a/libretroshare/src/chat/rschatitems.cc +++ b/libretroshare/src/chat/rschatitems.cc @@ -212,4 +212,4 @@ RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsChatMsgItem) void PrivateOugoingMapItem::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) -{ RS_REGISTER_SERIAL_MEMBER(store); } +{ RS_PROCESS_SERIAL_MEMBER(store); } diff --git a/libretroshare/src/gxstrans/p3gxstransitems.cc b/libretroshare/src/gxstrans/p3gxstransitems.cc index 8ea0f4557..43745aa4d 100644 --- a/libretroshare/src/gxstrans/p3gxstransitems.cc +++ b/libretroshare/src/gxstrans/p3gxstransitems.cc @@ -48,24 +48,24 @@ void OutgoingRecord_deprecated::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_REGISTER_SERIAL_MEMBER_TYPED(status, uint8_t); - RS_REGISTER_SERIAL_MEMBER(recipient); - RS_REGISTER_SERIAL_MEMBER(mailItem); - RS_REGISTER_SERIAL_MEMBER(mailData); - RS_REGISTER_SERIAL_MEMBER_TYPED(clientService, uint16_t); - RS_REGISTER_SERIAL_MEMBER(presignedReceipt); + RS_PROCESS_SERIAL_MEMBER_TYPED(status, uint8_t); + RS_PROCESS_SERIAL_MEMBER(recipient); + RS_PROCESS_SERIAL_MEMBER(mailItem); + RS_PROCESS_SERIAL_MEMBER(mailData); + RS_PROCESS_SERIAL_MEMBER_TYPED(clientService, uint16_t); + RS_PROCESS_SERIAL_MEMBER(presignedReceipt); } void OutgoingRecord::serial_process(RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx) { - RS_REGISTER_SERIAL_MEMBER_TYPED(status, uint8_t); - RS_REGISTER_SERIAL_MEMBER(recipient); - RS_REGISTER_SERIAL_MEMBER(author); - RS_REGISTER_SERIAL_MEMBER(group_id); - RS_REGISTER_SERIAL_MEMBER(sent_ts); - RS_REGISTER_SERIAL_MEMBER(mailItem); - RS_REGISTER_SERIAL_MEMBER(mailData); - RS_REGISTER_SERIAL_MEMBER_TYPED(clientService, uint16_t); - RS_REGISTER_SERIAL_MEMBER(presignedReceipt); + RS_PROCESS_SERIAL_MEMBER_TYPED(status, uint8_t); + RS_PROCESS_SERIAL_MEMBER(recipient); + RS_PROCESS_SERIAL_MEMBER(author); + RS_PROCESS_SERIAL_MEMBER(group_id); + RS_PROCESS_SERIAL_MEMBER(sent_ts); + RS_PROCESS_SERIAL_MEMBER(mailItem); + RS_PROCESS_SERIAL_MEMBER(mailData); + RS_PROCESS_SERIAL_MEMBER_TYPED(clientService, uint16_t); + RS_PROCESS_SERIAL_MEMBER(presignedReceipt); } diff --git a/libretroshare/src/gxstrans/p3gxstransitems.h b/libretroshare/src/gxstrans/p3gxstransitems.h index f158d0bdd..41b1b2d1b 100644 --- a/libretroshare/src/gxstrans/p3gxstransitems.h +++ b/libretroshare/src/gxstrans/p3gxstransitems.h @@ -55,7 +55,7 @@ public: void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) - { RS_REGISTER_SERIAL_MEMBER_TYPED(mailId, uint64_t); } + { RS_PROCESS_SERIAL_MEMBER_TYPED(mailId, uint64_t); } }; class RsGxsTransPresignedReceipt : public RsGxsTransBaseMsgItem @@ -140,9 +140,9 @@ public: RsGenericSerializer::SerializeContext& ctx ) { RsGxsTransBaseMsgItem::serial_process(j, ctx); - RS_REGISTER_SERIAL_MEMBER_TYPED(cryptoType, uint8_t); - RS_REGISTER_SERIAL_MEMBER(recipientHint); - RS_REGISTER_SERIAL_MEMBER(payload); + RS_PROCESS_SERIAL_MEMBER_TYPED(cryptoType, uint8_t); + RS_PROCESS_SERIAL_MEMBER(recipientHint); + RS_PROCESS_SERIAL_MEMBER(payload); } void clear() diff --git a/libretroshare/src/retroshare/rsgxsifacetypes.h b/libretroshare/src/retroshare/rsgxsifacetypes.h index 659a87e2f..52daf1023 100644 --- a/libretroshare/src/retroshare/rsgxsifacetypes.h +++ b/libretroshare/src/retroshare/rsgxsifacetypes.h @@ -100,24 +100,24 @@ struct RsGroupMetaData : RsSerializable void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_REGISTER_SERIAL_MEMBER(mGroupId); - RS_REGISTER_SERIAL_MEMBER(mGroupName); - RS_REGISTER_SERIAL_MEMBER(mGroupFlags); - RS_REGISTER_SERIAL_MEMBER(mSignFlags); - RS_REGISTER_SERIAL_MEMBER(mPublishTs); - RS_REGISTER_SERIAL_MEMBER(mAuthorId); - RS_REGISTER_SERIAL_MEMBER(mCircleId); - RS_REGISTER_SERIAL_MEMBER(mCircleType); - RS_REGISTER_SERIAL_MEMBER(mAuthenFlags); - RS_REGISTER_SERIAL_MEMBER(mParentGrpId); - RS_REGISTER_SERIAL_MEMBER(mSubscribeFlags); - RS_REGISTER_SERIAL_MEMBER(mPop); - RS_REGISTER_SERIAL_MEMBER(mVisibleMsgCount); - RS_REGISTER_SERIAL_MEMBER(mLastPost); - RS_REGISTER_SERIAL_MEMBER(mGroupStatus); - RS_REGISTER_SERIAL_MEMBER(mServiceString); - RS_REGISTER_SERIAL_MEMBER(mOriginator); - RS_REGISTER_SERIAL_MEMBER(mInternalCircle); + RS_PROCESS_SERIAL_MEMBER(mGroupId); + RS_PROCESS_SERIAL_MEMBER(mGroupName); + RS_PROCESS_SERIAL_MEMBER(mGroupFlags); + RS_PROCESS_SERIAL_MEMBER(mSignFlags); + RS_PROCESS_SERIAL_MEMBER(mPublishTs); + RS_PROCESS_SERIAL_MEMBER(mAuthorId); + RS_PROCESS_SERIAL_MEMBER(mCircleId); + RS_PROCESS_SERIAL_MEMBER(mCircleType); + RS_PROCESS_SERIAL_MEMBER(mAuthenFlags); + RS_PROCESS_SERIAL_MEMBER(mParentGrpId); + RS_PROCESS_SERIAL_MEMBER(mSubscribeFlags); + RS_PROCESS_SERIAL_MEMBER(mPop); + RS_PROCESS_SERIAL_MEMBER(mVisibleMsgCount); + RS_PROCESS_SERIAL_MEMBER(mLastPost); + RS_PROCESS_SERIAL_MEMBER(mGroupStatus); + RS_PROCESS_SERIAL_MEMBER(mServiceString); + RS_PROCESS_SERIAL_MEMBER(mOriginator); + RS_PROCESS_SERIAL_MEMBER(mInternalCircle); } }; diff --git a/libretroshare/src/retroshare/rsidentity.h b/libretroshare/src/retroshare/rsidentity.h index b79cbe8fd..b53c929fc 100644 --- a/libretroshare/src/retroshare/rsidentity.h +++ b/libretroshare/src/retroshare/rsidentity.h @@ -98,10 +98,10 @@ struct GxsReputation : RsSerializable void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_REGISTER_SERIAL_MEMBER(mOverallScore); - RS_REGISTER_SERIAL_MEMBER(mIdScore); - RS_REGISTER_SERIAL_MEMBER(mOwnOpinion); - RS_REGISTER_SERIAL_MEMBER(mPeerOpinion); + RS_PROCESS_SERIAL_MEMBER(mOverallScore); + RS_PROCESS_SERIAL_MEMBER(mIdScore); + RS_PROCESS_SERIAL_MEMBER(mOwnOpinion); + RS_PROCESS_SERIAL_MEMBER(mPeerOpinion); } }; @@ -278,13 +278,13 @@ struct RsIdentityUsage : RsSerializable void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_REGISTER_SERIAL_MEMBER(mServiceId); - RS_REGISTER_SERIAL_MEMBER_TYPED(mUsageCode, uint8_t); - RS_REGISTER_SERIAL_MEMBER(mGrpId); - RS_REGISTER_SERIAL_MEMBER(mMsgId); - RS_REGISTER_SERIAL_MEMBER(mAdditionalId); - RS_REGISTER_SERIAL_MEMBER(mComment); - RS_REGISTER_SERIAL_MEMBER(mHash); + RS_PROCESS_SERIAL_MEMBER(mServiceId); + RS_PROCESS_SERIAL_MEMBER_TYPED(mUsageCode, uint8_t); + RS_PROCESS_SERIAL_MEMBER(mGrpId); + RS_PROCESS_SERIAL_MEMBER(mMsgId); + RS_PROCESS_SERIAL_MEMBER(mAdditionalId); + RS_PROCESS_SERIAL_MEMBER(mComment); + RS_PROCESS_SERIAL_MEMBER(mHash); } friend class RsTypeSerializer; @@ -329,14 +329,14 @@ struct RsIdentityDetails : RsSerializable virtual void serial_process(RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx) { - RS_REGISTER_SERIAL_MEMBER(mId); - RS_REGISTER_SERIAL_MEMBER(mNickname); - RS_REGISTER_SERIAL_MEMBER(mFlags); - RS_REGISTER_SERIAL_MEMBER(mPgpId); - //RS_REGISTER_SERIAL_MEMBER_TYPED(mReputation, RsSerializable); - //RS_REGISTER_SERIAL_MEMBER_TYPED(mAvatar, RsSerializable); - RS_REGISTER_SERIAL_MEMBER(mLastUsageTS); - RS_REGISTER_SERIAL_MEMBER(mUseCases); + RS_PROCESS_SERIAL_MEMBER(mId); + RS_PROCESS_SERIAL_MEMBER(mNickname); + RS_PROCESS_SERIAL_MEMBER(mFlags); + RS_PROCESS_SERIAL_MEMBER(mPgpId); + //RS_PROCESS_SERIAL_MEMBER_TYPED(mReputation, RsSerializable); + //RS_PROCESS_SERIAL_MEMBER_TYPED(mAvatar, RsSerializable); + RS_PROCESS_SERIAL_MEMBER(mLastUsageTS); + RS_PROCESS_SERIAL_MEMBER(mUseCases); } }; diff --git a/libretroshare/src/rsitems/rsnxsitems.cc b/libretroshare/src/rsitems/rsnxsitems.cc index 91c35ce72..383d36614 100644 --- a/libretroshare/src/rsitems/rsnxsitems.cc +++ b/libretroshare/src/rsitems/rsnxsitems.cc @@ -73,12 +73,12 @@ void RsNxsSyncMsgItem::serial_process(RsGenericSerializer::SerializeJob j,RsGene void RsNxsMsg::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_REGISTER_SERIAL_MEMBER_TYPED(transactionNumber, uint32_t); - RS_REGISTER_SERIAL_MEMBER_TYPED(pos, uint8_t); - RS_REGISTER_SERIAL_MEMBER(msgId); - RS_REGISTER_SERIAL_MEMBER(grpId); - RS_REGISTER_SERIAL_MEMBER_TYPED(msg, RsTlvItem); - RS_REGISTER_SERIAL_MEMBER_TYPED(meta, RsTlvItem); + RS_PROCESS_SERIAL_MEMBER_TYPED(transactionNumber, uint32_t); + RS_PROCESS_SERIAL_MEMBER_TYPED(pos, uint8_t); + RS_PROCESS_SERIAL_MEMBER(msgId); + RS_PROCESS_SERIAL_MEMBER(grpId); + RS_PROCESS_SERIAL_MEMBER_TYPED(msg, RsTlvItem); + RS_PROCESS_SERIAL_MEMBER_TYPED(meta, RsTlvItem); } void RsNxsGrp::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) diff --git a/libretroshare/src/serialiser/rsserializable.h b/libretroshare/src/serialiser/rsserializable.h index b90e6c666..2c520de4c 100644 --- a/libretroshare/src/serialiser/rsserializable.h +++ b/libretroshare/src/serialiser/rsserializable.h @@ -22,32 +22,33 @@ /** @brief Minimal ancestor for all serializable structs in RetroShare * If you want your struct to be easly serializable you should inherit from this * struct. + * If you want your struct to be serializable as part of a container like an + * `std::vector` @see RS_REGISTER_SERIALIZABLE_TYPE(T) */ struct RsSerializable { /** Register struct members to serialize in this method taking advantage of * the helper macros - * @see RS_REGISTER_SERIAL_MEMBER(I) - * @see RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) - * @see RS_REGISTER_SERIALIZABLE_TYPE(T) + * @see RS_PROCESS_SERIAL_MEMBER(I) + * @see RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) */ virtual void serial_process(RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx) = 0; }; -/** @def RS_REGISTER_SERIAL_MEMBER(I) +/** @def RS_PROCESS_SERIAL_MEMBER(I) * Use this macro to register the members of `YourSerializable` for serial * processing inside `YourSerializable::serial_process(j, ctx)` * * Inspired by http://stackoverflow.com/a/39345864 */ -#define RS_REGISTER_SERIAL_MEMBER(I) \ +#define RS_PROCESS_SERIAL_MEMBER(I) \ do { RsTypeSerializer::serial_process(j, ctx, I, #I); } while(0) -/** @def RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) - * This macro usage is similar to @see RS_REGISTER_SERIAL_MEMBER(I) but it +/** @def RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) + * This macro usage is similar to @see RS_PROCESS_SERIAL_MEMBER(I) but it * permit to force serialization/deserialization type, it is expecially useful * with enum class members or RsTlvItem derivative members, be very careful with * the type you pass, as reinterpret_cast on a reference is used that is @@ -56,14 +57,14 @@ struct RsSerializable * If you are using this with an RsSerializable derivative (so passing * RsSerializable as T) consider to register your item type with * @see RS_REGISTER_SERIALIZABLE_TYPE(T) in - * association with @see RS_REGISTER_SERIAL_MEMBER(I) that rely on template + * association with @see RS_PROCESS_SERIAL_MEMBER(I) that rely on template * function generation, as in this particular case - * RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) would cause the serial code rely on + * RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) would cause the serial code rely on * C++ dynamic dispatching that may have a noticeable impact on runtime * performances. */ #pragma GCC diagnostic ignored "-Wstrict-aliasing" -#define RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) do {\ +#define RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) do {\ RsTypeSerializer::serial_process(j, ctx, reinterpret_cast(I), #I);\ } while(0) #pragma GCC diagnostic pop @@ -96,13 +97,13 @@ void PrivateOugoingMapItem::serial_process( RsGenericSerializer::SerializeContext& ctx ) { // store is of type - RS_REGISTER_SERIAL_MEMBER(store); + RS_PROCESS_SERIAL_MEMBER(store); } * @endcode * * If you use this macro with a lot of different item types this can cause the * generated binary grow in size, consider the usage of - * @see RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) passing RsSerializable as type in + * @see RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) passing RsSerializable as type in * that case. */ #define RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) template<> /*static*/\ diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index 6dfb43ce8..d29c80906 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -4510,16 +4510,16 @@ void RsGxsIdGroup::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_REGISTER_SERIAL_MEMBER_TYPED(mMeta, RsSerializable); - RS_REGISTER_SERIAL_MEMBER(mPgpIdHash); - //RS_REGISTER_SERIAL_MEMBER(mPgpIdSign); - RS_REGISTER_SERIAL_MEMBER(mRecognTags); - //RS_REGISTER_SERIAL_MEMBER(mImage); - RS_REGISTER_SERIAL_MEMBER(mLastUsageTS); - RS_REGISTER_SERIAL_MEMBER(mPgpKnown); - RS_REGISTER_SERIAL_MEMBER(mIsAContact); - RS_REGISTER_SERIAL_MEMBER(mPgpId); - RS_REGISTER_SERIAL_MEMBER_TYPED(mReputation, RsSerializable); + RS_PROCESS_SERIAL_MEMBER_TYPED(mMeta, RsSerializable); + RS_PROCESS_SERIAL_MEMBER(mPgpIdHash); + //RS_PROCESS_SERIAL_MEMBER(mPgpIdSign); + RS_PROCESS_SERIAL_MEMBER(mRecognTags); + //RS_PROCESS_SERIAL_MEMBER(mImage); + RS_PROCESS_SERIAL_MEMBER(mLastUsageTS); + RS_PROCESS_SERIAL_MEMBER(mPgpKnown); + RS_PROCESS_SERIAL_MEMBER(mIsAContact); + RS_PROCESS_SERIAL_MEMBER(mPgpId); + RS_PROCESS_SERIAL_MEMBER_TYPED(mReputation, RsSerializable); } RsIdentityUsage::RsIdentityUsage( From bc0990e3c8e772e3b0dfe176662f1c5a97bee983 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sun, 21 Jan 2018 20:52:59 +0100 Subject: [PATCH 004/161] Install rapidJSON in travis CI --- .travis.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.travis.yml b/.travis.yml index 90a8f8514..92828940b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,13 @@ matrix: sudo: false before_install: + - wget https://github.com/Tencent/rapidjson/archive/v1.1.0.tar.gz + - tar -xf v1.1.0.tar.gz + - if [ $TRAVIS_OS_NAME == osx ]; then ln -s rapidjson-1.1.0/include/rapidjson/ /usr/local/include/rapidjson ; fi + - if [ $TRAVIS_OS_NAME == linux ]; then sudo cp -r rapidjson-1.1.0/include/rapidjson/ /usr/include/rapidjson ; fi + - find /usr/local/include/ + - find /usr/include/ + - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get update; fi - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get install -y build-essential checkinstall cmake libavutil-dev libavcodec-dev libavformat-dev libbz2-dev libcurl4-openssl-dev libcv-dev libopencv-highgui-dev libhighgui-dev libgnome-keyring-dev libgstreamer-plugins-base0.10-dev libgstreamer0.10-dev libjasper-dev libjpeg-dev libmicrohttpd-dev libopencv-dev libprotobuf-dev libqt4-dev libspeex-dev libspeexdsp-dev libsqlite3-dev libssl-dev libswscale-dev libtbb-dev libtiff4-dev libupnp-dev libv4l-dev libxine-dev libxslt1-dev libxss-dev pkg-config protobuf-compiler python-dev qtmobility-dev gdb ; fi From b95e3380c0501fcaec9e488b08624c4081d43b16 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 22 Jan 2018 11:09:00 +0100 Subject: [PATCH 005/161] Fix Android compilation --- android-prepare-toolchain.sh | 9 +++++++++ .../src/serialiser/rstypeserializer.cc | 20 +++++++++++++++++++ .../src/serialiser/rstypeserializer.h | 2 ++ 3 files changed, 31 insertions(+) diff --git a/android-prepare-toolchain.sh b/android-prepare-toolchain.sh index d89e071e2..13c130c31 100755 --- a/android-prepare-toolchain.sh +++ b/android-prepare-toolchain.sh @@ -145,8 +145,17 @@ build_libmicrohttpd() cd .. } +build_rapidjson() +{ + B_dir="rapidjson-1.1.0" + [ -f $B_dir.tar.gz ] || wget -O $B_dir.tar.gz https://github.com/Tencent/rapidjson/archive/v1.1.0.tar.gz + tar -xf $B_dir.tar.gz + cp -r rapidjson-1.1.0/include/rapidjson/ "${SYSROOT}/usr/include/rapidjson" +} + build_toolchain build_bzlib build_openssl build_sqlite build_libupnp +build_rapidjson diff --git a/libretroshare/src/serialiser/rstypeserializer.cc b/libretroshare/src/serialiser/rstypeserializer.cc index c2b0ba905..68c1cde37 100644 --- a/libretroshare/src/serialiser/rstypeserializer.cc +++ b/libretroshare/src/serialiser/rstypeserializer.cc @@ -199,7 +199,27 @@ template<> bool RsTypeSerializer::to_JSON( const std::string& memberName, \ SIMPLE_TO_JSON_DEF(bool) SIMPLE_TO_JSON_DEF(int32_t) + +#ifdef __ANDROID__ +template<> bool RsTypeSerializer::to_JSON( const std::string& memberName, + const time_t& member, RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + int64_t tValue = member; + rapidjson::Value value(tValue); + + jDoc.AddMember(key, value, allocator); + + return true; +} +#else SIMPLE_TO_JSON_DEF(time_t) +#endif + SIMPLE_TO_JSON_DEF(uint8_t) SIMPLE_TO_JSON_DEF(uint16_t) SIMPLE_TO_JSON_DEF(uint32_t) diff --git a/libretroshare/src/serialiser/rstypeserializer.h b/libretroshare/src/serialiser/rstypeserializer.h index 1de678d50..68adacaf7 100644 --- a/libretroshare/src/serialiser/rstypeserializer.h +++ b/libretroshare/src/serialiser/rstypeserializer.h @@ -36,6 +36,8 @@ #include "serialiser/rsserializable.h" #include +#include // for typeid + /** INTERNAL ONLY helper to avoid copy paste code for std::{vector,list,set} * Can't use a template function because T is needed for const_cast */ From 6c40bd4212ad2a9389936d7acbbf11bef6c46ee0 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 22 Jan 2018 12:11:13 +0100 Subject: [PATCH 006/161] Fix Travix CI on mac osx --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 92828940b..864e86a74 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ matrix: before_install: - wget https://github.com/Tencent/rapidjson/archive/v1.1.0.tar.gz - tar -xf v1.1.0.tar.gz - - if [ $TRAVIS_OS_NAME == osx ]; then ln -s rapidjson-1.1.0/include/rapidjson/ /usr/local/include/rapidjson ; fi + - if [ $TRAVIS_OS_NAME == osx ]; then cp -r rapidjson-1.1.0/include/rapidjson/ /usr/local/include/rapidjson ; fi - if [ $TRAVIS_OS_NAME == linux ]; then sudo cp -r rapidjson-1.1.0/include/rapidjson/ /usr/include/rapidjson ; fi - find /usr/local/include/ - find /usr/include/ From 04d32d2f4428d8cafce7afee590ac17f35c4564c Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 22 Jan 2018 12:55:22 +0100 Subject: [PATCH 007/161] Remove duplicated include --- libretroshare/src/serialiser/rstypeserializer.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/libretroshare/src/serialiser/rstypeserializer.cc b/libretroshare/src/serialiser/rstypeserializer.cc index 68c1cde37..9b59d8ade 100644 --- a/libretroshare/src/serialiser/rstypeserializer.cc +++ b/libretroshare/src/serialiser/rstypeserializer.cc @@ -32,7 +32,6 @@ #include "util/rsprint.h" #include -#include #include #include #include // for typeid From 9c68bcbca4e2cb25fc028c6f673b142b2209d334 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 22 Jan 2018 14:50:07 +0100 Subject: [PATCH 008/161] Fix some warnings --- libretroshare/src/retroshare/rsidentity.h | 4 ++-- libretroshare/src/serialiser/rstypeserializer.h | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libretroshare/src/retroshare/rsidentity.h b/libretroshare/src/retroshare/rsidentity.h index b53c929fc..d9ec9c5a7 100644 --- a/libretroshare/src/retroshare/rsidentity.h +++ b/libretroshare/src/retroshare/rsidentity.h @@ -42,7 +42,7 @@ #include "util/rsdeprecate.h" /* The Main Interface Class - for information about your Peers */ -class RsIdentity; +struct RsIdentity; extern RsIdentity *rsIdentity; @@ -287,7 +287,7 @@ struct RsIdentityUsage : RsSerializable RS_PROCESS_SERIAL_MEMBER(mHash); } - friend class RsTypeSerializer; + friend struct RsTypeSerializer; private: /** Accessible only to friend class RsTypeSerializer needed for * deserialization */ diff --git a/libretroshare/src/serialiser/rstypeserializer.h b/libretroshare/src/serialiser/rstypeserializer.h index 68adacaf7..452700cf2 100644 --- a/libretroshare/src/serialiser/rstypeserializer.h +++ b/libretroshare/src/serialiser/rstypeserializer.h @@ -332,8 +332,8 @@ struct RsTypeSerializer nullptr, 0, RsGenericSerializer::FORMAT_BINARY, RsGenericSerializer::SERIALIZATION_FLAG_NONE, &allocator ); - ok && (kCtx.mJson. - AddMember("key", kvEl["key"], allocator), true); + if(ok) + kCtx.mJson.AddMember("key", kvEl["key"], allocator); T key; ok = ok && (serial_process(j, kCtx, key, "key"), kCtx.mOk); @@ -342,8 +342,8 @@ struct RsTypeSerializer nullptr, 0, RsGenericSerializer::FORMAT_BINARY, RsGenericSerializer::SERIALIZATION_FLAG_NONE, &allocator ); - ok && (vCtx.mJson. - AddMember("value", kvEl["value"], allocator), true); + if(ok) + vCtx.mJson.AddMember("value", kvEl["value"], allocator); U value; ok = ok && ( serial_process(j, vCtx, value, "value"), @@ -752,7 +752,7 @@ bool RsTypeSerializer::from_JSON( const std::string& membername, { rapidjson::Value& v = jVal[mName]; ret = ret && v.IsString(); - ret && (member = t_RsGenericIdType(std::string(v.GetString())), false); + if(ret) member = t_RsGenericIdType(std::string(v.GetString())); } return ret; } From 443ffb9f85d553da7e06028e88cf2d2ea53f1b15 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 22 Jan 2018 15:02:33 +0100 Subject: [PATCH 009/161] Fix some warnings --- libretroshare/src/chat/distributedchat.h | 2 +- libretroshare/src/grouter/grouterclientservice.h | 2 +- libretroshare/src/grouter/groutermatrix.h | 2 +- libretroshare/src/pqi/pqihandler.h | 2 +- libretroshare/src/pqi/pqiloopback.cc | 2 +- libretroshare/src/pqi/pqiloopback.h | 2 +- libretroshare/src/pqi/pqistreamer.h | 2 +- libretroshare/src/serialiser/rsserial.h | 2 +- libretroshare/src/serialiser/rsserializer.h | 2 +- libretroshare/src/turtle/turtleclientservice.h | 2 +- tests/librssimulator/testing/IsolatedServiceTester.h | 2 +- tests/librssimulator/testing/SetFilter.h | 2 +- tests/librssimulator/testing/SetPacket.h | 2 +- tests/librssimulator/testing/SetServiceTester.h | 2 +- 14 files changed, 14 insertions(+), 14 deletions(-) diff --git a/libretroshare/src/chat/distributedchat.h b/libretroshare/src/chat/distributedchat.h index ed5a438f9..ca7155dd2 100644 --- a/libretroshare/src/chat/distributedchat.h +++ b/libretroshare/src/chat/distributedchat.h @@ -30,7 +30,7 @@ typedef RsPeerId ChatLobbyVirtualPeerId ; -class RsItem ; +struct RsItem; class p3HistoryMgr ; class p3IdService ; class p3ServiceControl; diff --git a/libretroshare/src/grouter/grouterclientservice.h b/libretroshare/src/grouter/grouterclientservice.h index a6bef29e1..35c22614a 100644 --- a/libretroshare/src/grouter/grouterclientservice.h +++ b/libretroshare/src/grouter/grouterclientservice.h @@ -34,7 +34,7 @@ #include #include -class RsItem ; +struct RsItem; static const uint32_t GROUTER_CLIENT_SERVICE_DATA_STATUS_UNKNOWN = 0x0000 ; // unused. static const uint32_t GROUTER_CLIENT_SERVICE_DATA_STATUS_RECEIVED = 0x0001 ; // sent when data has been received and a receipt is available. diff --git a/libretroshare/src/grouter/groutermatrix.h b/libretroshare/src/grouter/groutermatrix.h index 99c9ae1bd..cb567b9bc 100644 --- a/libretroshare/src/grouter/groutermatrix.h +++ b/libretroshare/src/grouter/groutermatrix.h @@ -31,7 +31,7 @@ #include "retroshare/rsgrouter.h" #include "groutertypes.h" -class RsItem ; +struct RsItem; // The routing matrix records the event clues received from each friend // diff --git a/libretroshare/src/pqi/pqihandler.h b/libretroshare/src/pqi/pqihandler.h index cf1f19429..c285c4ef7 100644 --- a/libretroshare/src/pqi/pqihandler.h +++ b/libretroshare/src/pqi/pqihandler.h @@ -38,7 +38,7 @@ class PQInterface; class RSTrafficClue; class RsBwRates; -class RsItem; +struct RsItem; class RsRawItem; class SearchModule diff --git a/libretroshare/src/pqi/pqiloopback.cc b/libretroshare/src/pqi/pqiloopback.cc index ba4078ce2..8a8ba342b 100644 --- a/libretroshare/src/pqi/pqiloopback.cc +++ b/libretroshare/src/pqi/pqiloopback.cc @@ -27,7 +27,7 @@ #include // for NULL -class RsItem; +struct RsItem; /*** #define LOOPBACK_DEBUG 1 diff --git a/libretroshare/src/pqi/pqiloopback.h b/libretroshare/src/pqi/pqiloopback.h index 7186dd440..82c3923d8 100644 --- a/libretroshare/src/pqi/pqiloopback.h +++ b/libretroshare/src/pqi/pqiloopback.h @@ -31,7 +31,7 @@ #include "pqi/pqi_base.h" // for NetInterface (ptr only), PQInterface #include "retroshare/rstypes.h" // for RsPeerId -class RsItem; +struct RsItem; class pqiloopback: public PQInterface { diff --git a/libretroshare/src/pqi/pqistreamer.h b/libretroshare/src/pqi/pqistreamer.h index fe2754854..9908651b2 100644 --- a/libretroshare/src/pqi/pqistreamer.h +++ b/libretroshare/src/pqi/pqistreamer.h @@ -38,7 +38,7 @@ #include "retroshare/rstypes.h" // for RsPeerId #include "util/rsthreads.h" // for RsMutex -class RsItem; +struct RsItem; class RsSerialiser; struct PartialPacketRecord diff --git a/libretroshare/src/serialiser/rsserial.h b/libretroshare/src/serialiser/rsserial.h index 206f69608..ee3a79537 100644 --- a/libretroshare/src/serialiser/rsserial.h +++ b/libretroshare/src/serialiser/rsserial.h @@ -64,7 +64,7 @@ const uint8_t RS_PKT_CLASS_CONFIG = 0x02; const uint8_t RS_PKT_SUBTYPE_DEFAULT = 0x01; /* if only one subtype */ -class RsItem ; +struct RsItem; class RsSerialType ; diff --git a/libretroshare/src/serialiser/rsserializer.h b/libretroshare/src/serialiser/rsserializer.h index 63b565a9b..dcc4d089f 100644 --- a/libretroshare/src/serialiser/rsserializer.h +++ b/libretroshare/src/serialiser/rsserializer.h @@ -160,7 +160,7 @@ #include "serialiser/rsserial.h" #include "util/rsdeprecate.h" -class RsItem ; +struct RsItem; #define SERIALIZE_ERROR() std::cerr << __PRETTY_FUNCTION__ << " : " diff --git a/libretroshare/src/turtle/turtleclientservice.h b/libretroshare/src/turtle/turtleclientservice.h index d9552c662..51a9dbdc3 100644 --- a/libretroshare/src/turtle/turtleclientservice.h +++ b/libretroshare/src/turtle/turtleclientservice.h @@ -36,7 +36,7 @@ #include #include -class RsItem ; +struct RsItem; class p3turtle ; class RsTurtleClientService diff --git a/tests/librssimulator/testing/IsolatedServiceTester.h b/tests/librssimulator/testing/IsolatedServiceTester.h index aca9695ed..3fa5e2afc 100644 --- a/tests/librssimulator/testing/IsolatedServiceTester.h +++ b/tests/librssimulator/testing/IsolatedServiceTester.h @@ -4,7 +4,7 @@ #include "retroshare/rsids.h" #include "pqi/p3linkmgr.h" -class RsItem; +struct RsItem; class PeerNode; class RsSerialType; class RsSerialiser; diff --git a/tests/librssimulator/testing/SetFilter.h b/tests/librssimulator/testing/SetFilter.h index 9b9a8b15d..50df26955 100644 --- a/tests/librssimulator/testing/SetFilter.h +++ b/tests/librssimulator/testing/SetFilter.h @@ -4,7 +4,7 @@ #include "retroshare/rsids.h" #include "SetPacket.h" -class RsItem; +struct RsItem; class SetFilter { diff --git a/tests/librssimulator/testing/SetPacket.h b/tests/librssimulator/testing/SetPacket.h index ae529f37b..1e377191e 100644 --- a/tests/librssimulator/testing/SetPacket.h +++ b/tests/librssimulator/testing/SetPacket.h @@ -5,7 +5,7 @@ #include #include "retroshare/rsids.h" -class RsItem; +struct RsItem; class SetPacket { diff --git a/tests/librssimulator/testing/SetServiceTester.h b/tests/librssimulator/testing/SetServiceTester.h index ad03aed4e..862fc43e0 100644 --- a/tests/librssimulator/testing/SetServiceTester.h +++ b/tests/librssimulator/testing/SetServiceTester.h @@ -8,7 +8,7 @@ #include "SetPacket.h" #include "SetFilter.h" -class RsItem; +struct RsItem; class PeerNode; class RsSerialType; class RsSerialiser; From 76b607c210589006ebd8de7e1ad6d90b3b34ed87 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 22 Jan 2018 15:26:18 +0100 Subject: [PATCH 010/161] Fix warning in libresapi --- libresapi/src/api/FileSharingHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libresapi/src/api/FileSharingHandler.cpp b/libresapi/src/api/FileSharingHandler.cpp index d948a9511..dff941c1b 100644 --- a/libresapi/src/api/FileSharingHandler.cpp +++ b/libresapi/src/api/FileSharingHandler.cpp @@ -54,7 +54,7 @@ void FileSharingHandler::handleForceCheck(Request&, Response& resp) resp.setOk(); } -void FileSharingHandler::handleGetSharedDir(Request& req, Response& resp) +void FileSharingHandler::handleGetSharedDir(Request& /*req*/, Response& resp) { DirDetails dirDetails; mRsFiles->RequestDirDetails(NULL, dirDetails, RS_FILE_HINTS_LOCAL); From 234625ad28e9189cd96a90d42a37e942845b2efb Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 22 Jan 2018 21:50:51 +0100 Subject: [PATCH 011/161] Remove extra debugging in Travis CI --- .travis.yml | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 864e86a74..2a173b915 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,13 +12,6 @@ matrix: sudo: false before_install: - - wget https://github.com/Tencent/rapidjson/archive/v1.1.0.tar.gz - - tar -xf v1.1.0.tar.gz - - if [ $TRAVIS_OS_NAME == osx ]; then cp -r rapidjson-1.1.0/include/rapidjson/ /usr/local/include/rapidjson ; fi - - if [ $TRAVIS_OS_NAME == linux ]; then sudo cp -r rapidjson-1.1.0/include/rapidjson/ /usr/include/rapidjson ; fi - - find /usr/local/include/ - - find /usr/include/ - - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get update; fi - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get install -y build-essential checkinstall cmake libavutil-dev libavcodec-dev libavformat-dev libbz2-dev libcurl4-openssl-dev libcv-dev libopencv-highgui-dev libhighgui-dev libgnome-keyring-dev libgstreamer-plugins-base0.10-dev libgstreamer0.10-dev libjasper-dev libjpeg-dev libmicrohttpd-dev libopencv-dev libprotobuf-dev libqt4-dev libspeex-dev libspeexdsp-dev libsqlite3-dev libssl-dev libswscale-dev libtbb-dev libtiff4-dev libupnp-dev libv4l-dev libxine-dev libxslt1-dev libxss-dev pkg-config protobuf-compiler python-dev qtmobility-dev gdb ; fi @@ -38,6 +31,11 @@ before_install: - if [ $TRAVIS_OS_NAME == osx ]; then ln -s /usr/local/opt/openssl/lib/*.a /usr/local/lib/; fi - if [ $TRAVIS_OS_NAME == osx ]; then ln -s /usr/local/opt/openssl/lib/*.dylib /usr/local/lib/; fi + - wget https://github.com/Tencent/rapidjson/archive/v1.1.0.tar.gz + - tar -xf v1.1.0.tar.gz + - if [ $TRAVIS_OS_NAME == osx ]; then cp -r rapidjson-1.1.0/include/rapidjson/ /usr/local/include/rapidjson ; fi + - if [ $TRAVIS_OS_NAME == linux ]; then sudo cp -r rapidjson-1.1.0/include/rapidjson/ /usr/include/rapidjson ; fi + # - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get update && sudo apt-get install -y llvm-3.4 llvm-3.4-dev; fi # - rvm use $RVM --install --binary --fuzzy # - gem update --system From 7409de5170b4bb9f3b62c33c98d69fe5265a37a5 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 22 Jan 2018 21:53:12 +0100 Subject: [PATCH 012/161] Fix compilation on systems with atipic time_t --- libretroshare/src/serialiser/rstypeserializer.cc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/libretroshare/src/serialiser/rstypeserializer.cc b/libretroshare/src/serialiser/rstypeserializer.cc index 9b59d8ade..88585e45a 100644 --- a/libretroshare/src/serialiser/rstypeserializer.cc +++ b/libretroshare/src/serialiser/rstypeserializer.cc @@ -199,7 +199,6 @@ template<> bool RsTypeSerializer::to_JSON( const std::string& memberName, \ SIMPLE_TO_JSON_DEF(bool) SIMPLE_TO_JSON_DEF(int32_t) -#ifdef __ANDROID__ template<> bool RsTypeSerializer::to_JSON( const std::string& memberName, const time_t& member, RsJson& jDoc ) { @@ -208,6 +207,7 @@ template<> bool RsTypeSerializer::to_JSON( const std::string& memberName, rapidjson::Value key; key.SetString(memberName.c_str(), memberName.length(), allocator); + // without this compilation may break depending on how time_t is defined int64_t tValue = member; rapidjson::Value value(tValue); @@ -215,9 +215,6 @@ template<> bool RsTypeSerializer::to_JSON( const std::string& memberName, return true; } -#else -SIMPLE_TO_JSON_DEF(time_t) -#endif SIMPLE_TO_JSON_DEF(uint8_t) SIMPLE_TO_JSON_DEF(uint16_t) From 13d4a2c9162ab2ba74249c0fe4ed6bdb1a817e01 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Thu, 25 Jan 2018 11:37:16 +0100 Subject: [PATCH 013/161] Safer and elgant serial helper macros --- libretroshare/src/chat/rschatitems.cc | 2 +- libretroshare/src/gxstrans/p3gxstransitems.cc | 30 +++--- libretroshare/src/gxstrans/p3gxstransitems.h | 8 +- .../src/retroshare/rsgxsifacetypes.h | 36 +++---- libretroshare/src/retroshare/rsidentity.h | 38 +++---- libretroshare/src/rsitems/rsnxsitems.cc | 12 +-- libretroshare/src/serialiser/rsserializable.h | 102 ++++++++++-------- libretroshare/src/services/p3idservice.cc | 20 ++-- 8 files changed, 133 insertions(+), 115 deletions(-) diff --git a/libretroshare/src/chat/rschatitems.cc b/libretroshare/src/chat/rschatitems.cc index 00511c1f8..1eee404b6 100644 --- a/libretroshare/src/chat/rschatitems.cc +++ b/libretroshare/src/chat/rschatitems.cc @@ -212,4 +212,4 @@ RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsChatMsgItem) void PrivateOugoingMapItem::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) -{ RS_PROCESS_SERIAL_MEMBER(store); } +{ RS_SERIAL_PROCESS(store); } diff --git a/libretroshare/src/gxstrans/p3gxstransitems.cc b/libretroshare/src/gxstrans/p3gxstransitems.cc index 43745aa4d..1db07c70a 100644 --- a/libretroshare/src/gxstrans/p3gxstransitems.cc +++ b/libretroshare/src/gxstrans/p3gxstransitems.cc @@ -48,24 +48,24 @@ void OutgoingRecord_deprecated::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_PROCESS_SERIAL_MEMBER_TYPED(status, uint8_t); - RS_PROCESS_SERIAL_MEMBER(recipient); - RS_PROCESS_SERIAL_MEMBER(mailItem); - RS_PROCESS_SERIAL_MEMBER(mailData); - RS_PROCESS_SERIAL_MEMBER_TYPED(clientService, uint16_t); - RS_PROCESS_SERIAL_MEMBER(presignedReceipt); + RS_SERIAL_PROCESS(status); + RS_SERIAL_PROCESS(recipient); + RS_SERIAL_PROCESS(mailItem); + RS_SERIAL_PROCESS(mailData); + RS_SERIAL_PROCESS(clientService); + RS_SERIAL_PROCESS(presignedReceipt); } void OutgoingRecord::serial_process(RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx) { - RS_PROCESS_SERIAL_MEMBER_TYPED(status, uint8_t); - RS_PROCESS_SERIAL_MEMBER(recipient); - RS_PROCESS_SERIAL_MEMBER(author); - RS_PROCESS_SERIAL_MEMBER(group_id); - RS_PROCESS_SERIAL_MEMBER(sent_ts); - RS_PROCESS_SERIAL_MEMBER(mailItem); - RS_PROCESS_SERIAL_MEMBER(mailData); - RS_PROCESS_SERIAL_MEMBER_TYPED(clientService, uint16_t); - RS_PROCESS_SERIAL_MEMBER(presignedReceipt); + RS_SERIAL_PROCESS(status); + RS_SERIAL_PROCESS(recipient); + RS_SERIAL_PROCESS(author); + RS_SERIAL_PROCESS(group_id); + RS_SERIAL_PROCESS(sent_ts); + RS_SERIAL_PROCESS(mailItem); + RS_SERIAL_PROCESS(mailData); + RS_SERIAL_PROCESS(clientService); + RS_SERIAL_PROCESS(presignedReceipt); } diff --git a/libretroshare/src/gxstrans/p3gxstransitems.h b/libretroshare/src/gxstrans/p3gxstransitems.h index 41b1b2d1b..bd13c9e71 100644 --- a/libretroshare/src/gxstrans/p3gxstransitems.h +++ b/libretroshare/src/gxstrans/p3gxstransitems.h @@ -55,7 +55,7 @@ public: void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) - { RS_PROCESS_SERIAL_MEMBER_TYPED(mailId, uint64_t); } + { RS_SERIAL_PROCESS(mailId); } }; class RsGxsTransPresignedReceipt : public RsGxsTransBaseMsgItem @@ -140,9 +140,9 @@ public: RsGenericSerializer::SerializeContext& ctx ) { RsGxsTransBaseMsgItem::serial_process(j, ctx); - RS_PROCESS_SERIAL_MEMBER_TYPED(cryptoType, uint8_t); - RS_PROCESS_SERIAL_MEMBER(recipientHint); - RS_PROCESS_SERIAL_MEMBER(payload); + RS_SERIAL_PROCESS(cryptoType); + RS_SERIAL_PROCESS(recipientHint); + RS_SERIAL_PROCESS(payload); } void clear() diff --git a/libretroshare/src/retroshare/rsgxsifacetypes.h b/libretroshare/src/retroshare/rsgxsifacetypes.h index 52daf1023..18637f1b1 100644 --- a/libretroshare/src/retroshare/rsgxsifacetypes.h +++ b/libretroshare/src/retroshare/rsgxsifacetypes.h @@ -100,24 +100,24 @@ struct RsGroupMetaData : RsSerializable void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_PROCESS_SERIAL_MEMBER(mGroupId); - RS_PROCESS_SERIAL_MEMBER(mGroupName); - RS_PROCESS_SERIAL_MEMBER(mGroupFlags); - RS_PROCESS_SERIAL_MEMBER(mSignFlags); - RS_PROCESS_SERIAL_MEMBER(mPublishTs); - RS_PROCESS_SERIAL_MEMBER(mAuthorId); - RS_PROCESS_SERIAL_MEMBER(mCircleId); - RS_PROCESS_SERIAL_MEMBER(mCircleType); - RS_PROCESS_SERIAL_MEMBER(mAuthenFlags); - RS_PROCESS_SERIAL_MEMBER(mParentGrpId); - RS_PROCESS_SERIAL_MEMBER(mSubscribeFlags); - RS_PROCESS_SERIAL_MEMBER(mPop); - RS_PROCESS_SERIAL_MEMBER(mVisibleMsgCount); - RS_PROCESS_SERIAL_MEMBER(mLastPost); - RS_PROCESS_SERIAL_MEMBER(mGroupStatus); - RS_PROCESS_SERIAL_MEMBER(mServiceString); - RS_PROCESS_SERIAL_MEMBER(mOriginator); - RS_PROCESS_SERIAL_MEMBER(mInternalCircle); + RS_SERIAL_PROCESS(mGroupId); + RS_SERIAL_PROCESS(mGroupName); + RS_SERIAL_PROCESS(mGroupFlags); + RS_SERIAL_PROCESS(mSignFlags); + RS_SERIAL_PROCESS(mPublishTs); + RS_SERIAL_PROCESS(mAuthorId); + RS_SERIAL_PROCESS(mCircleId); + RS_SERIAL_PROCESS(mCircleType); + RS_SERIAL_PROCESS(mAuthenFlags); + RS_SERIAL_PROCESS(mParentGrpId); + RS_SERIAL_PROCESS(mSubscribeFlags); + RS_SERIAL_PROCESS(mPop); + RS_SERIAL_PROCESS(mVisibleMsgCount); + RS_SERIAL_PROCESS(mLastPost); + RS_SERIAL_PROCESS(mGroupStatus); + RS_SERIAL_PROCESS(mServiceString); + RS_SERIAL_PROCESS(mOriginator); + RS_SERIAL_PROCESS(mInternalCircle); } }; diff --git a/libretroshare/src/retroshare/rsidentity.h b/libretroshare/src/retroshare/rsidentity.h index d9ec9c5a7..f021252b3 100644 --- a/libretroshare/src/retroshare/rsidentity.h +++ b/libretroshare/src/retroshare/rsidentity.h @@ -98,10 +98,10 @@ struct GxsReputation : RsSerializable void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_PROCESS_SERIAL_MEMBER(mOverallScore); - RS_PROCESS_SERIAL_MEMBER(mIdScore); - RS_PROCESS_SERIAL_MEMBER(mOwnOpinion); - RS_PROCESS_SERIAL_MEMBER(mPeerOpinion); + RS_SERIAL_PROCESS(mOverallScore); + RS_SERIAL_PROCESS(mIdScore); + RS_SERIAL_PROCESS(mOwnOpinion); + RS_SERIAL_PROCESS(mPeerOpinion); } }; @@ -278,13 +278,13 @@ struct RsIdentityUsage : RsSerializable void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_PROCESS_SERIAL_MEMBER(mServiceId); - RS_PROCESS_SERIAL_MEMBER_TYPED(mUsageCode, uint8_t); - RS_PROCESS_SERIAL_MEMBER(mGrpId); - RS_PROCESS_SERIAL_MEMBER(mMsgId); - RS_PROCESS_SERIAL_MEMBER(mAdditionalId); - RS_PROCESS_SERIAL_MEMBER(mComment); - RS_PROCESS_SERIAL_MEMBER(mHash); + RS_SERIAL_PROCESS(mServiceId); + RS_SERIAL_PROCESS(mUsageCode); + RS_SERIAL_PROCESS(mGrpId); + RS_SERIAL_PROCESS(mMsgId); + RS_SERIAL_PROCESS(mAdditionalId); + RS_SERIAL_PROCESS(mComment); + RS_SERIAL_PROCESS(mHash); } friend struct RsTypeSerializer; @@ -329,14 +329,14 @@ struct RsIdentityDetails : RsSerializable virtual void serial_process(RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx) { - RS_PROCESS_SERIAL_MEMBER(mId); - RS_PROCESS_SERIAL_MEMBER(mNickname); - RS_PROCESS_SERIAL_MEMBER(mFlags); - RS_PROCESS_SERIAL_MEMBER(mPgpId); - //RS_PROCESS_SERIAL_MEMBER_TYPED(mReputation, RsSerializable); - //RS_PROCESS_SERIAL_MEMBER_TYPED(mAvatar, RsSerializable); - RS_PROCESS_SERIAL_MEMBER(mLastUsageTS); - RS_PROCESS_SERIAL_MEMBER(mUseCases); + RS_SERIAL_PROCESS(mId); + RS_SERIAL_PROCESS(mNickname); + RS_SERIAL_PROCESS(mFlags); + RS_SERIAL_PROCESS(mPgpId); + //RS_SERIAL_PROCESS(mReputation); + //RS_SERIAL_PROCESS(mAvatar); + RS_SERIAL_PROCESS(mLastUsageTS); + RS_SERIAL_PROCESS(mUseCases); } }; diff --git a/libretroshare/src/rsitems/rsnxsitems.cc b/libretroshare/src/rsitems/rsnxsitems.cc index 383d36614..8c6775275 100644 --- a/libretroshare/src/rsitems/rsnxsitems.cc +++ b/libretroshare/src/rsitems/rsnxsitems.cc @@ -73,12 +73,12 @@ void RsNxsSyncMsgItem::serial_process(RsGenericSerializer::SerializeJob j,RsGene void RsNxsMsg::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_PROCESS_SERIAL_MEMBER_TYPED(transactionNumber, uint32_t); - RS_PROCESS_SERIAL_MEMBER_TYPED(pos, uint8_t); - RS_PROCESS_SERIAL_MEMBER(msgId); - RS_PROCESS_SERIAL_MEMBER(grpId); - RS_PROCESS_SERIAL_MEMBER_TYPED(msg, RsTlvItem); - RS_PROCESS_SERIAL_MEMBER_TYPED(meta, RsTlvItem); + RS_SERIAL_PROCESS(transactionNumber); + RS_SERIAL_PROCESS(pos); + RS_SERIAL_PROCESS(msgId); + RS_SERIAL_PROCESS(grpId); + RS_SERIAL_PROCESS(msg); + RS_SERIAL_PROCESS(meta); } void RsNxsGrp::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) diff --git a/libretroshare/src/serialiser/rsserializable.h b/libretroshare/src/serialiser/rsserializable.h index 2c520de4c..acfe995af 100644 --- a/libretroshare/src/serialiser/rsserializable.h +++ b/libretroshare/src/serialiser/rsserializable.h @@ -17,60 +17,48 @@ * along with this program. If not, see . */ +#include + #include "serialiser/rsserializer.h" -/** @brief Minimal ancestor for all serializable structs in RetroShare + +/** @brief Minimal ancestor for all serializable structs in RetroShare. * If you want your struct to be easly serializable you should inherit from this * struct. * If you want your struct to be serializable as part of a container like an - * `std::vector` @see RS_REGISTER_SERIALIZABLE_TYPE(T) + * `std::vector` @see RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) and + * @see RS_REGISTER_SERIALIZABLE_TYPE_DECL(T) */ struct RsSerializable { /** Register struct members to serialize in this method taking advantage of - * the helper macros - * @see RS_PROCESS_SERIAL_MEMBER(I) - * @see RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) + * the helper macro @see RS_SERIAL_PROCESS(I) */ virtual void serial_process(RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx) = 0; }; -/** @def RS_PROCESS_SERIAL_MEMBER(I) +/** @def RS_SERIAL_PROCESS(I) * Use this macro to register the members of `YourSerializable` for serial * processing inside `YourSerializable::serial_process(j, ctx)` * + * Pay special attention for member of enum type which must be declared + * specifying the underlying type otherwise the serialization format may differ + * in an uncompatible way depending on the compiler/platform. + * + * If your member is a derivative of RsSerializable in some cases it may be + * convenient also to register your item type with + * @see RS_REGISTER_SERIALIZABLE_TYPE_DEF(T). + * * Inspired by http://stackoverflow.com/a/39345864 */ -#define RS_PROCESS_SERIAL_MEMBER(I) \ - do { RsTypeSerializer::serial_process(j, ctx, I, #I); } while(0) - - -/** @def RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) - * This macro usage is similar to @see RS_PROCESS_SERIAL_MEMBER(I) but it - * permit to force serialization/deserialization type, it is expecially useful - * with enum class members or RsTlvItem derivative members, be very careful with - * the type you pass, as reinterpret_cast on a reference is used that is - * expecially permissive so you can shot your feet if not carefull enough. - * - * If you are using this with an RsSerializable derivative (so passing - * RsSerializable as T) consider to register your item type with - * @see RS_REGISTER_SERIALIZABLE_TYPE(T) in - * association with @see RS_PROCESS_SERIAL_MEMBER(I) that rely on template - * function generation, as in this particular case - * RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) would cause the serial code rely on - * C++ dynamic dispatching that may have a noticeable impact on runtime - * performances. - */ -#pragma GCC diagnostic ignored "-Wstrict-aliasing" -#define RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) do {\ - RsTypeSerializer::serial_process(j, ctx, reinterpret_cast(I), #I);\ +#define RS_SERIAL_PROCESS(I) do { \ + RsTypeSerializer::serial_process(j, ctx, __priv_to_RS_S_TYPE(I), #I ); \ } while(0) -#pragma GCC diagnostic pop -/** @def RS_REGISTER_SERIALIZABLE_TYPE(T) +/** @def RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) * Use this macro into `youritem.cc` only if you need to process members of * subtypes of RsSerializable. * @@ -90,21 +78,15 @@ struct PrivateOugoingMapItem : RsChatItem std::map store; }; -RS_REGISTER_SERIALIZABLE_TYPE(RsChatMsgItem) +RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsChatMsgItem) void PrivateOugoingMapItem::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - // store is of type - RS_PROCESS_SERIAL_MEMBER(store); + RS_SERIAL_PROCESS(store); } * @endcode - * - * If you use this macro with a lot of different item types this can cause the - * generated binary grow in size, consider the usage of - * @see RS_PROCESS_SERIAL_MEMBER_TYPED(I, T) passing RsSerializable as type in - * that case. */ #define RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) template<> /*static*/\ void RsTypeSerializer::serial_process( \ @@ -118,11 +100,11 @@ void PrivateOugoingMapItem::serial_process( /** @def RS_REGISTER_SERIALIZABLE_TYPE_DECL(T) - * The usage of this macro into your header file is needed only in case you + * The usage of this macro into your header file may be needed only in case you * needed @see RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) in your definitions file, * but it was not enough and the compiler kept complanining about undefined * references to serialize, deserialize, serial_size, print_data, to_JSON, - * from_JSON for your RsSerializable derrived type. + * from_JSON for your RsSerializable derived type. * * One example of such case is RsIdentityUsage that is declared in * retroshare/rsidentity.h but defined in services/p3idservice.cc, also if @@ -135,4 +117,40 @@ void PrivateOugoingMapItem::serial_process( void RsTypeSerializer::serial_process( \ RsGenericSerializer::SerializeJob j,\ RsGenericSerializer::SerializeContext& ctx, T& item,\ - const std::string& /*objName*/ ); + const std::string& objName ); + + +//============================================================================// +// Private type conversion helpers // +//============================================================================// + +/** + * @brief Privates type conversion helpers for RS_SERIAL_PROCESS. + * @private DO NOT use explicitely outside this file. + * Used to cast enums and derived type to matching types supported by + * RsTypeSerializer::serial_process(...) templated methods. + */ +template::value>::type> +inline typename std::underlying_type::type& __priv_to_RS_S_TYPE(T& i) +{ + return reinterpret_cast::type&>(i); +} + +template::value>::type> +inline RsSerializable& __priv_to_RS_S_TYPE(T& i) +{ + return static_cast(i); +} + +struct RsTlvItem; +template::value>::type> +inline RsTlvItem& __priv_to_RS_S_TYPE(T& i) +{ + return static_cast(i); +} + +template::value||std::is_base_of::value||std::is_base_of::value)>::type> +inline T& __priv_to_RS_S_TYPE(T& i) +{ + return i; +} diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index d29c80906..4796b3005 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -4510,16 +4510,16 @@ void RsGxsIdGroup::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_PROCESS_SERIAL_MEMBER_TYPED(mMeta, RsSerializable); - RS_PROCESS_SERIAL_MEMBER(mPgpIdHash); - //RS_PROCESS_SERIAL_MEMBER(mPgpIdSign); - RS_PROCESS_SERIAL_MEMBER(mRecognTags); - //RS_PROCESS_SERIAL_MEMBER(mImage); - RS_PROCESS_SERIAL_MEMBER(mLastUsageTS); - RS_PROCESS_SERIAL_MEMBER(mPgpKnown); - RS_PROCESS_SERIAL_MEMBER(mIsAContact); - RS_PROCESS_SERIAL_MEMBER(mPgpId); - RS_PROCESS_SERIAL_MEMBER_TYPED(mReputation, RsSerializable); + RS_SERIAL_PROCESS(mMeta); + RS_SERIAL_PROCESS(mPgpIdHash); + //RS_SERIAL_PROCESS(mPgpIdSign); + RS_SERIAL_PROCESS(mRecognTags); + //RS_SERIAL_PROCESS(mImage); + RS_SERIAL_PROCESS(mLastUsageTS); + RS_SERIAL_PROCESS(mPgpKnown); + RS_SERIAL_PROCESS(mIsAContact); + RS_SERIAL_PROCESS(mPgpId); + RS_SERIAL_PROCESS(mReputation); } RsIdentityUsage::RsIdentityUsage( From ba6f2d7e816208db4a98feff6c44521cfc00128c Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Fri, 26 Jan 2018 17:18:05 +0100 Subject: [PATCH 014/161] Remove default template for to/from_JSON This way the compiler will complain if a type is added directly to RsTypeSerializer without specify all needed serial operations --- .../src/gxstunnel/rsgxstunnelitems.cc | 3 + libretroshare/src/retroshare/rstypes.h | 35 +++++++--- libretroshare/src/rsitems/rsconfigitems.cc | 29 +------- .../src/rsitems/rsfiletransferitems.cc | 42 ----------- libretroshare/src/rsitems/rsgxsupdateitems.cc | 23 +----- libretroshare/src/rsitems/rsgxsupdateitems.h | 23 +++--- libretroshare/src/rsserver/rstypes.cc | 2 +- .../src/serialiser/rstypeserializer.cc | 37 ++++++++-- .../src/serialiser/rstypeserializer.h | 70 +++++++++++-------- libretroshare/src/turtle/rsturtleitem.cc | 6 ++ 10 files changed, 126 insertions(+), 144 deletions(-) diff --git a/libretroshare/src/gxstunnel/rsgxstunnelitems.cc b/libretroshare/src/gxstunnel/rsgxstunnelitems.cc index 8e488c190..d653b03ea 100644 --- a/libretroshare/src/gxstunnel/rsgxstunnelitems.cc +++ b/libretroshare/src/gxstunnel/rsgxstunnelitems.cc @@ -102,6 +102,9 @@ template<> void RsTypeSerializer::print_data(const std::string& name,BIGNUM std::cerr << "[BIGNUM] : " << name << std::endl; } +RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(BIGNUM*) +RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(BIGNUM*) + void RsGxsTunnelStatusItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { RsTypeSerializer::serial_process(j,ctx,status,"status") ; diff --git a/libretroshare/src/retroshare/rstypes.h b/libretroshare/src/retroshare/rstypes.h index 9c098db59..84c3fc45d 100644 --- a/libretroshare/src/retroshare/rstypes.h +++ b/libretroshare/src/retroshare/rstypes.h @@ -36,6 +36,8 @@ #include #include +#include +#include #define USE_NEW_CHUNK_CHECKING_CODE @@ -118,13 +120,21 @@ class Condition std::string name; }; -class PeerBandwidthLimits +struct PeerBandwidthLimits : RsSerializable { -public: - PeerBandwidthLimits() : max_up_rate_kbs(0), max_dl_rate_kbs(0) {} - - uint32_t max_up_rate_kbs ; - uint32_t max_dl_rate_kbs ; + PeerBandwidthLimits() : max_up_rate_kbs(0), max_dl_rate_kbs(0) {} + + uint32_t max_up_rate_kbs; + uint32_t max_dl_rate_kbs; + + + /// @see RsSerializable + void serial_process(RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx) + { + RS_SERIAL_PROCESS(max_up_rate_kbs); + RS_SERIAL_PROCESS(max_dl_rate_kbs); + } }; //class SearchRequest // unused stuff. @@ -295,7 +305,7 @@ class FileChunksInfo std::map > pending_slices ; }; -class CompressedChunkMap +class CompressedChunkMap : public RsSerializable { public: CompressedChunkMap() {} @@ -345,10 +355,17 @@ class CompressedChunkMap inline void set(uint32_t j) { _map[j >> 5] |= (1 << (j & 31)) ; } inline void reset(uint32_t j) { _map[j >> 5] &= ~(1 << (j & 31)) ; } - /// compressed map, one bit per chunk - std::vector _map ; + /// compressed map, one bit per chunk + std::vector _map; + + /// @see RsSerializable + void serial_process(RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx) + { RS_SERIAL_PROCESS(_map); } }; +RS_REGISTER_SERIALIZABLE_TYPE_DECL(CompressedChunkMap) + template class t_CRCMap { public: diff --git a/libretroshare/src/rsitems/rsconfigitems.cc b/libretroshare/src/rsitems/rsconfigitems.cc index f6296bd04..b673b354a 100644 --- a/libretroshare/src/rsitems/rsconfigitems.cc +++ b/libretroshare/src/rsitems/rsconfigitems.cc @@ -28,6 +28,7 @@ #include "rsitems/rsconfigitems.h" #include "retroshare/rspeers.h" // Needed for RsGroupInfo. +#include "serialiser/rsserializable.h" #include "serialiser/rstypeserializer.h" /*** * #define RSSERIAL_DEBUG 1 @@ -89,7 +90,7 @@ void RsFileTransfer::serial_process(RsGenericSerializer::SerializeJob j,RsGeneri RsTypeSerializer::serial_process (j,ctx,flags,"flags") ; RsTypeSerializer::serial_process (j,ctx,chunk_strategy,"chunk_strategy") ; - RsTypeSerializer::serial_process (j,ctx,compressed_chunk_map,"compressed_chunk_map") ; + RS_SERIAL_PROCESS(compressed_chunk_map); } void RsFileConfigItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) @@ -192,31 +193,7 @@ void RsPeerStunItem::serial_process(RsGenericSerializer::SerializeJob j,RsGeneri RsTypeSerializer::serial_process(j,ctx,stunList,"stunList") ; } -template<> uint32_t RsTypeSerializer::serial_size(const PeerBandwidthLimits& /*s*/) -{ - return 4+4 ; -} - -template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset,const PeerBandwidthLimits& s) -{ - bool ok = true ; - ok = ok && setRawUInt32(data,size,&offset,s.max_up_rate_kbs); - ok = ok && setRawUInt32(data,size,&offset,s.max_dl_rate_kbs); - return ok; -} - -template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size,uint32_t& offset,PeerBandwidthLimits& s) -{ - bool ok = true ; - ok = ok && getRawUInt32(data,size,&offset,&s.max_up_rate_kbs); - ok = ok && getRawUInt32(data,size,&offset,&s.max_dl_rate_kbs); - return ok; -} - -template<> void RsTypeSerializer::print_data(const std::string& /*n*/, const PeerBandwidthLimits& s) -{ - std::cerr << " [Peer BW limit] " << s.max_up_rate_kbs << " / " << s.max_dl_rate_kbs << std::endl; -} +RS_REGISTER_SERIALIZABLE_TYPE_DEF(PeerBandwidthLimits) RsNodeGroupItem::RsNodeGroupItem(const RsGroupInfo& g) :RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_PEER_CONFIG, RS_PKT_SUBTYPE_NODE_GROUP) diff --git a/libretroshare/src/rsitems/rsfiletransferitems.cc b/libretroshare/src/rsitems/rsfiletransferitems.cc index 7579c9560..4241081a7 100644 --- a/libretroshare/src/rsitems/rsfiletransferitems.cc +++ b/libretroshare/src/rsitems/rsfiletransferitems.cc @@ -91,48 +91,6 @@ void RsFileTransferSingleChunkCrcItem::serial_process(RsGenericSerializer::Seria RsTypeSerializer::serial_process (j,ctx,check_sum, "check_sum") ; } -//===================================================================================================// -// CompressedChunkMap // -//===================================================================================================// - -template<> uint32_t RsTypeSerializer::serial_size(const CompressedChunkMap& s) -{ - return 4 + 4*s._map.size() ; -} - -template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset,const CompressedChunkMap& s) -{ - bool ok = true ; - - ok &= setRawUInt32(data, size, &offset, s._map.size()); - - for(uint32_t i=0;i bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size,uint32_t& offset,CompressedChunkMap& s) -{ - uint32_t S =0; - bool ok = getRawUInt32(data, size, &offset, &S); - - if(ok) - { - s._map.resize(S) ; - - for(uint32_t i=0;i void RsTypeSerializer::print_data(const std::string& n, const CompressedChunkMap& s) -{ - std::cerr << " [Compressed chunk map] " << n << " : length=" << s._map.size() << std::endl; -} - //===================================================================================================// // Serializer // //===================================================================================================// diff --git a/libretroshare/src/rsitems/rsgxsupdateitems.cc b/libretroshare/src/rsitems/rsgxsupdateitems.cc index 50f154cb2..6e18ed774 100644 --- a/libretroshare/src/rsitems/rsgxsupdateitems.cc +++ b/libretroshare/src/rsitems/rsgxsupdateitems.cc @@ -97,30 +97,9 @@ void RsGxsMsgUpdateItem::serial_process(RsGenericSerializer::SerializeJob j,RsGe RsTypeSerializer::serial_process(j,ctx,msgUpdateInfos,"msgUpdateInfos"); } -template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const RsGxsMsgUpdateItem::MsgUpdateInfo& info) -{ - bool ok = true ; +RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsGxsMsgUpdate::MsgUpdateInfo) - ok = ok && setRawUInt32(data,size,&offset,info.time_stamp); - ok = ok && setRawUInt32(data,size,&offset,info.message_count); - return ok; -} -template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, RsGxsMsgUpdateItem::MsgUpdateInfo& info) -{ - bool ok = true ; - - ok = ok && getRawUInt32(data,size,&offset,&info.time_stamp); - ok = ok && getRawUInt32(data,size,&offset,&info.message_count); - - return ok; -} -template<> uint32_t RsTypeSerializer::serial_size(const RsGxsMsgUpdateItem::MsgUpdateInfo& /* info */) { return 8; } - -template<> void RsTypeSerializer::print_data(const std::string& name,const RsGxsMsgUpdateItem::MsgUpdateInfo& info) -{ - std::cerr << "[MsgUpdateInfo]: " << name << ": " << info.time_stamp << ", " << info.message_count << std::endl; -} void RsGxsServerMsgUpdateItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { diff --git a/libretroshare/src/rsitems/rsgxsupdateitems.h b/libretroshare/src/rsitems/rsgxsupdateitems.h index c9725426c..e0549b2b3 100644 --- a/libretroshare/src/rsitems/rsgxsupdateitems.h +++ b/libretroshare/src/rsitems/rsgxsupdateitems.h @@ -28,18 +28,11 @@ -#if 0 -#include -#include "rsitems/rsserviceids.h" -#include "serialiser/rsserial.h" -#include "serialiser/rstlvbase.h" -#include "serialiser/rstlvtypes.h" -#include "serialiser/rstlvkeys.h" -#endif - #include "gxs/rsgxs.h" #include "gxs/rsgxsdata.h" #include "serialiser/rstlvidset.h" +#include "serialiser/rstypeserializer.h" +#include "serialiser/rsserializable.h" const uint8_t RS_PKT_SUBTYPE_GXS_GRP_UPDATE = 0x01; @@ -140,17 +133,27 @@ public: class RsGxsMsgUpdate { public: - struct MsgUpdateInfo + struct MsgUpdateInfo : RsSerializable { MsgUpdateInfo(): time_stamp(0), message_count(0) {} uint32_t time_stamp ; uint32_t message_count ; + + /// @see RsSerializable + void serial_process(RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx) + { + RS_SERIAL_PROCESS(time_stamp); + RS_SERIAL_PROCESS(message_count); + } }; std::map msgUpdateInfos; }; +RS_REGISTER_SERIALIZABLE_TYPE_DECL(RsGxsMsgUpdate::MsgUpdateInfo) + class RsGxsMsgUpdateItem : public RsGxsNetServiceItem, public RsGxsMsgUpdate { public: diff --git a/libretroshare/src/rsserver/rstypes.cc b/libretroshare/src/rsserver/rstypes.cc index 209a7da88..c13adef7a 100644 --- a/libretroshare/src/rsserver/rstypes.cc +++ b/libretroshare/src/rsserver/rstypes.cc @@ -76,4 +76,4 @@ std::ostream &operator<<(std::ostream &out, const FileInfo &info) return out; } - +RS_REGISTER_SERIALIZABLE_TYPE_DEF(CompressedChunkMap) diff --git a/libretroshare/src/serialiser/rstypeserializer.cc b/libretroshare/src/serialiser/rstypeserializer.cc index 88585e45a..c03c64684 100644 --- a/libretroshare/src/serialiser/rstypeserializer.cc +++ b/libretroshare/src/serialiser/rstypeserializer.cc @@ -65,7 +65,8 @@ template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint3 } template<> bool RsTypeSerializer::serialize(uint8_t /*data*/[], uint32_t /*size*/, uint32_t& /*offset*/, const int32_t& /*member*/) { - // TODO: nice to have but not used ATM + std::cerr << __PRETTY_FUNCTION__ << " Not implemented!" << std::endl; + print_stacktrace(); return false; } template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const uint8_t& member) @@ -98,7 +99,8 @@ template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t siz } template<> bool RsTypeSerializer::deserialize(const uint8_t /*data*/[], uint32_t /*size*/, uint32_t& /*offset*/, int32_t& /*member*/) { - // TODO: nice to have but not used ATM + std::cerr << __PRETTY_FUNCTION__ << " Not implemented!" << std::endl; + print_stacktrace(); return false; } template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, uint8_t& member) @@ -128,7 +130,9 @@ template<> uint32_t RsTypeSerializer::serial_size(const bool& /* member*/) } template<> uint32_t RsTypeSerializer::serial_size(const int32_t& /* member*/) { - return 4; + std::cerr << __PRETTY_FUNCTION__ << " Not implemented!" << std::endl; + print_stacktrace(); + return 0; } template<> uint32_t RsTypeSerializer::serial_size(const uint8_t& /* member*/) { @@ -513,8 +517,9 @@ bool RsTypeSerializer::to_JSON( const std::string& memberName, template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& /*memberName*/, - RsTlvItem& /*member*/, RsJson& /*jVal*/) + RsTlvItem& member, RsJson& /*jVal*/) { + member.TlvClear(); return true; } @@ -580,6 +585,30 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const RsTypeS std::cerr << " [Binary data] " << n << ", length=" << s.second << " data=" << RsUtil::BinToHex((uint8_t*)s.first,std::min(50u,s.second)) << ((s.second>50)?"...":"") << std::endl; } +template<> /*static*/ +bool RsTypeSerializer::to_JSON( + const std::string& memberName, + const RsTypeSerializer::TlvMemBlock_proxy& member, RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + rapidjson::Value value; + const char* tName = typeid(member).name(); + value.SetString(tName, allocator); + + jDoc.AddMember(key, value, allocator); + + return true; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& /*memberName*/, + RsTypeSerializer::TlvMemBlock_proxy&, + RsJson& /*jVal*/) +{ return true; } //============================================================================// // RsSerializable and derivated // diff --git a/libretroshare/src/serialiser/rstypeserializer.h b/libretroshare/src/serialiser/rstypeserializer.h index 452700cf2..4b5486b88 100644 --- a/libretroshare/src/serialiser/rstypeserializer.h +++ b/libretroshare/src/serialiser/rstypeserializer.h @@ -828,38 +828,48 @@ bool RsTypeSerializer::from_JSON( const std::string& memberName, //============================================================================// -// Generic types // +// Not implemented types macros // //============================================================================// -template /*static*/ -bool RsTypeSerializer::to_JSON(const std::string& memberName, const T& member, - RsJson& jDoc ) -{ - rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); - - rapidjson::Value key; - key.SetString(memberName.c_str(), memberName.length(), allocator); - - rapidjson::Value value; - const char* tName = typeid(member).name(); - value.SetString(tName, allocator); - - jDoc.AddMember(key, value, allocator); - - std::cerr << __PRETTY_FUNCTION__ << " JSON serialization for type " - << typeid(member).name() << " " << memberName - << " not available." << std::endl; - print_stacktrace(); - return true; +/** + * @def RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(T) + * @def RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(T) + * Helper macros for types that has not yet implemented to/from JSON + * should be deleted from the code as soon as they are not needed anymore + */ +#define RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(T) \ +template<> /*static*/ \ +bool RsTypeSerializer::to_JSON(const std::string& memberName, T const& member, \ + RsJson& jDoc ) \ +{ \ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); \ + \ + rapidjson::Value key; \ + key.SetString(memberName.c_str(), memberName.length(), allocator); \ + \ + rapidjson::Value value; \ + const char* tName = typeid(member).name(); \ + value.SetString(tName, allocator); \ + \ + jDoc.AddMember(key, value, allocator); \ + \ + std::cerr << __FILE__ << __LINE__ << __PRETTY_FUNCTION__ \ + << " JSON serialization for type " \ + << typeid(member).name() << " " << memberName \ + << " not available." << std::endl; \ + print_stacktrace(); \ + return true; \ } -template /*static*/ -bool RsTypeSerializer::from_JSON( const std::string& memberName, - T& member, RsJson& /*jDoc*/ ) -{ - std::cerr << __PRETTY_FUNCTION__ << " JSON deserialization for type " - << typeid(member).name() << " " << memberName - << " not available." << std::endl; - print_stacktrace(); - return true; +#define RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(T) \ +template<> /*static*/ \ +bool RsTypeSerializer::from_JSON( const std::string& memberName, \ + T& member, RsJson& /*jDoc*/ ) \ +{ \ + std::cerr << __FILE__ << __LINE__ << __PRETTY_FUNCTION__ \ + << " JSON deserialization for type " \ + << typeid(member).name() << " " << memberName \ + << " not available." << std::endl; \ + print_stacktrace(); \ + return true; \ } diff --git a/libretroshare/src/turtle/rsturtleitem.cc b/libretroshare/src/turtle/rsturtleitem.cc index 35b250117..cf206a153 100644 --- a/libretroshare/src/turtle/rsturtleitem.cc +++ b/libretroshare/src/turtle/rsturtleitem.cc @@ -140,6 +140,9 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const RsRegul std::cerr << " [RegExpr ] " << n << ", tokens=" << expr._tokens.size() << " ints=" << expr._ints.size() << " strings=" << expr._strings.size() << std::endl; } +RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(RsRegularExpression::LinearizedExpression) +RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(RsRegularExpression::LinearizedExpression) + void RsTurtleSearchResultItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { RsTypeSerializer::serial_process(j,ctx,request_id,"request_id") ; @@ -193,6 +196,9 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const TurtleF std::cerr << " [FileInfo ] " << n << " size=" << i.size << " hash=" << i.hash << ", name=" << i.name << std::endl; } +RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(TurtleFileInfo) +RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(TurtleFileInfo) + void RsTurtleOpenTunnelItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { RsTypeSerializer::serial_process (j,ctx,file_hash ,"file_hash") ; From 8b774595d78e13623af7bb34730a0456f2b1b0b8 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 27 Jan 2018 17:57:33 +0100 Subject: [PATCH 015/161] Better usage of type traits Don't need register types for serializationanymore Don't need casting helpers for serialization --- libretroshare/src/chat/rschatitems.cc | 3 - libretroshare/src/gxstrans/p3gxstransitems.cc | 6 - libretroshare/src/retroshare/rsidentity.h | 2 - libretroshare/src/retroshare/rstypes.h | 1 - libretroshare/src/rsitems/rsconfigitems.cc | 1 - libretroshare/src/rsitems/rsgxsupdateitems.cc | 6 - libretroshare/src/rsitems/rsgxsupdateitems.h | 1 - libretroshare/src/rsserver/rstypes.cc | 2 - libretroshare/src/serialiser/rsserializable.h | 107 +----------- .../src/serialiser/rstypeserializer.cc | 162 ++---------------- .../src/serialiser/rstypeserializer.h | 131 +++++++++++++- libretroshare/src/services/p3idservice.cc | 2 - 12 files changed, 145 insertions(+), 279 deletions(-) diff --git a/libretroshare/src/chat/rschatitems.cc b/libretroshare/src/chat/rschatitems.cc index 1eee404b6..7ae680105 100644 --- a/libretroshare/src/chat/rschatitems.cc +++ b/libretroshare/src/chat/rschatitems.cc @@ -205,9 +205,6 @@ void RsPrivateChatMsgConfigItem::get(RsChatMsgItem *ci) ci->recvTime = recvTime; } -/* Necessary to serialize `store` that is an STL container with RsChatMsgItem - * inside which is a subtype of RsItem */ -RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsChatMsgItem) void PrivateOugoingMapItem::serial_process( RsGenericSerializer::SerializeJob j, diff --git a/libretroshare/src/gxstrans/p3gxstransitems.cc b/libretroshare/src/gxstrans/p3gxstransitems.cc index 1db07c70a..5d28ec582 100644 --- a/libretroshare/src/gxstrans/p3gxstransitems.cc +++ b/libretroshare/src/gxstrans/p3gxstransitems.cc @@ -38,12 +38,6 @@ OutgoingRecord::OutgoingRecord( RsGxsId rec, GxsTransSubServices cs, memcpy(&mailData[0], data, size); } -// for mailItem -RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsGxsTransMailItem) - -// for presignedReceipt -RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsNxsTransPresignedReceipt) - void OutgoingRecord_deprecated::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) diff --git a/libretroshare/src/retroshare/rsidentity.h b/libretroshare/src/retroshare/rsidentity.h index f021252b3..47da42de4 100644 --- a/libretroshare/src/retroshare/rsidentity.h +++ b/libretroshare/src/retroshare/rsidentity.h @@ -294,8 +294,6 @@ private: RsIdentityUsage(); }; -RS_REGISTER_SERIALIZABLE_TYPE_DECL(RsIdentityUsage) - struct RsIdentityDetails : RsSerializable { diff --git a/libretroshare/src/retroshare/rstypes.h b/libretroshare/src/retroshare/rstypes.h index 84c3fc45d..1e36d3832 100644 --- a/libretroshare/src/retroshare/rstypes.h +++ b/libretroshare/src/retroshare/rstypes.h @@ -364,7 +364,6 @@ class CompressedChunkMap : public RsSerializable { RS_SERIAL_PROCESS(_map); } }; -RS_REGISTER_SERIALIZABLE_TYPE_DECL(CompressedChunkMap) template class t_CRCMap { diff --git a/libretroshare/src/rsitems/rsconfigitems.cc b/libretroshare/src/rsitems/rsconfigitems.cc index b673b354a..83f508794 100644 --- a/libretroshare/src/rsitems/rsconfigitems.cc +++ b/libretroshare/src/rsitems/rsconfigitems.cc @@ -193,7 +193,6 @@ void RsPeerStunItem::serial_process(RsGenericSerializer::SerializeJob j,RsGeneri RsTypeSerializer::serial_process(j,ctx,stunList,"stunList") ; } -RS_REGISTER_SERIALIZABLE_TYPE_DEF(PeerBandwidthLimits) RsNodeGroupItem::RsNodeGroupItem(const RsGroupInfo& g) :RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_PEER_CONFIG, RS_PKT_SUBTYPE_NODE_GROUP) diff --git a/libretroshare/src/rsitems/rsgxsupdateitems.cc b/libretroshare/src/rsitems/rsgxsupdateitems.cc index 6e18ed774..a5f3ecf02 100644 --- a/libretroshare/src/rsitems/rsgxsupdateitems.cc +++ b/libretroshare/src/rsitems/rsgxsupdateitems.cc @@ -97,10 +97,6 @@ void RsGxsMsgUpdateItem::serial_process(RsGenericSerializer::SerializeJob j,RsGe RsTypeSerializer::serial_process(j,ctx,msgUpdateInfos,"msgUpdateInfos"); } -RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsGxsMsgUpdate::MsgUpdateInfo) - - - void RsGxsServerMsgUpdateItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { RsTypeSerializer::serial_process (j,ctx,grpId,"grpId"); @@ -113,5 +109,3 @@ void RsGxsGrpConfigItem::serial_process(RsGenericSerializer::SerializeJob j,RsGe RsTypeSerializer::serial_process(j,ctx,msg_send_delay,"msg_send_delay") ; RsTypeSerializer::serial_process(j,ctx,msg_req_delay,"msg_req_delay") ; } - - diff --git a/libretroshare/src/rsitems/rsgxsupdateitems.h b/libretroshare/src/rsitems/rsgxsupdateitems.h index e0549b2b3..5d13028ff 100644 --- a/libretroshare/src/rsitems/rsgxsupdateitems.h +++ b/libretroshare/src/rsitems/rsgxsupdateitems.h @@ -152,7 +152,6 @@ public: std::map msgUpdateInfos; }; -RS_REGISTER_SERIALIZABLE_TYPE_DECL(RsGxsMsgUpdate::MsgUpdateInfo) class RsGxsMsgUpdateItem : public RsGxsNetServiceItem, public RsGxsMsgUpdate { diff --git a/libretroshare/src/rsserver/rstypes.cc b/libretroshare/src/rsserver/rstypes.cc index c13adef7a..4574c48e8 100644 --- a/libretroshare/src/rsserver/rstypes.cc +++ b/libretroshare/src/rsserver/rstypes.cc @@ -75,5 +75,3 @@ std::ostream &operator<<(std::ostream &out, const FileInfo &info) out << "Hash: " << info.hash; return out; } - -RS_REGISTER_SERIALIZABLE_TYPE_DEF(CompressedChunkMap) diff --git a/libretroshare/src/serialiser/rsserializable.h b/libretroshare/src/serialiser/rsserializable.h index acfe995af..f55378cf1 100644 --- a/libretroshare/src/serialiser/rsserializable.h +++ b/libretroshare/src/serialiser/rsserializable.h @@ -17,8 +17,6 @@ * along with this program. If not, see . */ -#include - #include "serialiser/rsserializer.h" @@ -38,7 +36,6 @@ struct RsSerializable RsGenericSerializer::SerializeContext& ctx) = 0; }; - /** @def RS_SERIAL_PROCESS(I) * Use this macro to register the members of `YourSerializable` for serial * processing inside `YourSerializable::serial_process(j, ctx)` @@ -47,110 +44,8 @@ struct RsSerializable * specifying the underlying type otherwise the serialization format may differ * in an uncompatible way depending on the compiler/platform. * - * If your member is a derivative of RsSerializable in some cases it may be - * convenient also to register your item type with - * @see RS_REGISTER_SERIALIZABLE_TYPE_DEF(T). - * * Inspired by http://stackoverflow.com/a/39345864 */ #define RS_SERIAL_PROCESS(I) do { \ - RsTypeSerializer::serial_process(j, ctx, __priv_to_RS_S_TYPE(I), #I ); \ + RsTypeSerializer::serial_process(j, ctx, I, #I ); \ } while(0) - - -/** @def RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) - * Use this macro into `youritem.cc` only if you need to process members of - * subtypes of RsSerializable. - * - * The usage of this macro is strictly needed only in some cases, for example if - * you are registering for serialization a member of a container that contains - * items of subclasses of RsSerializable like - * `std::map` - * - * @code{.cpp} -struct PrivateOugoingMapItem : RsChatItem -{ - PrivateOugoingMapItem() : RsChatItem(RS_PKT_SUBTYPE_OUTGOING_MAP) {} - - void serial_process( RsGenericSerializer::SerializeJob j, - RsGenericSerializer::SerializeContext& ctx ); - - std::map store; -}; - -RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsChatMsgItem) - -void PrivateOugoingMapItem::serial_process( - RsGenericSerializer::SerializeJob j, - RsGenericSerializer::SerializeContext& ctx ) -{ - RS_SERIAL_PROCESS(store); -} - * @endcode - */ -#define RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) template<> /*static*/\ - void RsTypeSerializer::serial_process( \ - RsGenericSerializer::SerializeJob j,\ - RsGenericSerializer::SerializeContext& ctx, T& item,\ - const std::string& objName ) \ -{ \ - RsTypeSerializer::serial_process( j, \ - ctx, static_cast(item), objName ); \ -} - - -/** @def RS_REGISTER_SERIALIZABLE_TYPE_DECL(T) - * The usage of this macro into your header file may be needed only in case you - * needed @see RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) in your definitions file, - * but it was not enough and the compiler kept complanining about undefined - * references to serialize, deserialize, serial_size, print_data, to_JSON, - * from_JSON for your RsSerializable derived type. - * - * One example of such case is RsIdentityUsage that is declared in - * retroshare/rsidentity.h but defined in services/p3idservice.cc, also if - * RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsIdentityUsage) was used in p3idservice.cc - * for some reason it was not enough for the compiler to see it so - * RS_REGISTER_SERIALIZABLE_TYPE_DECL(RsIdentityUsage) has been added in - * rsidentity.h too and now the compiler is happy. - */ -#define RS_REGISTER_SERIALIZABLE_TYPE_DECL(T) template<> /*static*/\ - void RsTypeSerializer::serial_process( \ - RsGenericSerializer::SerializeJob j,\ - RsGenericSerializer::SerializeContext& ctx, T& item,\ - const std::string& objName ); - - -//============================================================================// -// Private type conversion helpers // -//============================================================================// - -/** - * @brief Privates type conversion helpers for RS_SERIAL_PROCESS. - * @private DO NOT use explicitely outside this file. - * Used to cast enums and derived type to matching types supported by - * RsTypeSerializer::serial_process(...) templated methods. - */ -template::value>::type> -inline typename std::underlying_type::type& __priv_to_RS_S_TYPE(T& i) -{ - return reinterpret_cast::type&>(i); -} - -template::value>::type> -inline RsSerializable& __priv_to_RS_S_TYPE(T& i) -{ - return static_cast(i); -} - -struct RsTlvItem; -template::value>::type> -inline RsTlvItem& __priv_to_RS_S_TYPE(T& i) -{ - return static_cast(i); -} - -template::value||std::is_base_of::value||std::is_base_of::value)>::type> -inline T& __priv_to_RS_S_TYPE(T& i) -{ - return i; -} diff --git a/libretroshare/src/serialiser/rstypeserializer.cc b/libretroshare/src/serialiser/rstypeserializer.cc index c03c64684..f4dc2d3c1 100644 --- a/libretroshare/src/serialiser/rstypeserializer.cc +++ b/libretroshare/src/serialiser/rstypeserializer.cc @@ -44,16 +44,17 @@ static const uint32_t MAX_SERIALIZED_CHUNK_SIZE = 10*1024*1024 ; // 10 MB. #define SAFE_GET_JSON_V() \ const char* mName = memberName.c_str(); \ - bool ret = jVal.HasMember(mName); \ + bool ret = jDoc.HasMember(mName); \ if(!ret) \ { \ std::cerr << __PRETTY_FUNCTION__ << " \"" << memberName \ << "\" not found in JSON:" << std::endl \ - << jVal << std::endl << std::endl; \ + << jDoc << std::endl << std::endl; \ print_stacktrace(); \ return false; \ } \ - rapidjson::Value& v = jVal[mName] + rapidjson::Value& v = jDoc[mName] + //============================================================================// // Integer types // @@ -227,7 +228,7 @@ SIMPLE_TO_JSON_DEF(uint64_t) template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& memberName, bool& member, - RsJson& jVal ) + RsJson& jDoc ) { SAFE_GET_JSON_V(); ret = ret && v.IsBool(); @@ -237,7 +238,7 @@ bool RsTypeSerializer::from_JSON( const std::string& memberName, bool& member, template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& memberName, - int32_t& member, RsJson& jVal ) + int32_t& member, RsJson& jDoc ) { SAFE_GET_JSON_V(); ret = ret && v.IsInt(); @@ -247,7 +248,7 @@ bool RsTypeSerializer::from_JSON( const std::string& memberName, template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& memberName, time_t& member, - RsJson& jVal ) + RsJson& jDoc ) { SAFE_GET_JSON_V(); ret = ret && v.IsUint(); @@ -257,7 +258,7 @@ bool RsTypeSerializer::from_JSON( const std::string& memberName, time_t& member, template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& memberName, - uint8_t& member, RsJson& jVal ) + uint8_t& member, RsJson& jDoc ) { SAFE_GET_JSON_V(); ret = ret && v.IsUint(); @@ -267,7 +268,7 @@ bool RsTypeSerializer::from_JSON( const std::string& memberName, template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& memberName, - uint16_t& member, RsJson& jVal ) + uint16_t& member, RsJson& jDoc ) { SAFE_GET_JSON_V(); ret = ret && v.IsUint(); @@ -277,7 +278,7 @@ bool RsTypeSerializer::from_JSON( const std::string& memberName, template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& memberName, - uint32_t& member, RsJson& jVal ) + uint32_t& member, RsJson& jDoc ) { SAFE_GET_JSON_V(); ret = ret && v.IsUint(); @@ -287,7 +288,7 @@ bool RsTypeSerializer::from_JSON( const std::string& memberName, template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& memberName, - uint64_t& member, RsJson& jVal ) + uint64_t& member, RsJson& jDoc ) { SAFE_GET_JSON_V(); ret = ret && v.IsUint(); @@ -319,7 +320,7 @@ SIMPLE_TO_JSON_DEF(float) template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& memberName, - float& member, RsJson& jVal ) + float& member, RsJson& jDoc ) { SAFE_GET_JSON_V(); ret = ret && v.IsFloat(); @@ -371,7 +372,7 @@ bool RsTypeSerializer::to_JSON( const std::string& membername, } template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& memberName, - std::string& member, RsJson& jVal ) + std::string& member, RsJson& jDoc ) { SAFE_GET_JSON_V(); ret = ret && v.IsString(); @@ -416,9 +417,9 @@ bool RsTypeSerializer::to_JSON( const std::string& memberName, template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& memberName, uint16_t /*sub_type*/, - std::string& member, RsJson& jVal ) + std::string& member, RsJson& jDoc ) { - return from_JSON(memberName, member, jVal); + return from_JSON(memberName, member, jDoc); } //============================================================================// @@ -463,9 +464,9 @@ bool RsTypeSerializer::to_JSON( const std::string& memberName, template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& memberName, uint16_t /*sub_type*/, - uint32_t& member, RsJson& jVal ) + uint32_t& member, RsJson& jDoc ) { - return from_JSON(memberName, member, jVal); + return from_JSON(memberName, member, jDoc); } @@ -517,7 +518,7 @@ bool RsTypeSerializer::to_JSON( const std::string& memberName, template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& /*memberName*/, - RsTlvItem& member, RsJson& /*jVal*/) + RsTlvItem& member, RsJson& /*jDoc*/) { member.TlvClear(); return true; @@ -607,134 +608,9 @@ bool RsTypeSerializer::to_JSON( template<> /*static*/ bool RsTypeSerializer::from_JSON( const std::string& /*memberName*/, RsTypeSerializer::TlvMemBlock_proxy&, - RsJson& /*jVal*/) + RsJson& /*jDoc*/) { return true; } -//============================================================================// -// RsSerializable and derivated // -//============================================================================// - -template<> uint32_t RsTypeSerializer::serial_size(const RsSerializable& s) -{ - RsGenericSerializer::SerializeContext ctx( - NULL, 0, RsGenericSerializer::FORMAT_BINARY, - RsGenericSerializer::SERIALIZATION_FLAG_NONE ); - - ctx.mOffset = 8; // header size - const_cast(s).serial_process( - RsGenericSerializer::SIZE_ESTIMATE, ctx ); - - return ctx.mOffset; -} - -template<> bool RsTypeSerializer::serialize( uint8_t data[], uint32_t size, - uint32_t &offset, - const RsSerializable& s ) -{ - RsGenericSerializer::SerializeContext ctx( - data, size, RsGenericSerializer::FORMAT_BINARY, - RsGenericSerializer::SERIALIZATION_FLAG_NONE ); - ctx.mOffset = offset; - const_cast(s).serial_process( - RsGenericSerializer::SERIALIZE, ctx ); - return true; -} - -template<> bool RsTypeSerializer::deserialize( const uint8_t data[], - uint32_t size, uint32_t& offset, - RsSerializable& s ) -{ - RsGenericSerializer::SerializeContext ctx( - const_cast(data), size, - RsGenericSerializer::FORMAT_BINARY, - RsGenericSerializer::SERIALIZATION_FLAG_NONE ); - ctx.mOffset = offset; - const_cast(s).serial_process( - RsGenericSerializer::DESERIALIZE, ctx ); - return true; -} - -template<> void RsTypeSerializer::print_data( const std::string& /*n*/, - const RsSerializable& s ) -{ - RsGenericSerializer::SerializeContext ctx( - NULL, 0, - RsGenericSerializer::FORMAT_BINARY, - RsGenericSerializer::SERIALIZATION_FLAG_NONE ); - const_cast(s).serial_process( RsGenericSerializer::PRINT, - ctx ); -} - -template<> /*static*/ -bool RsTypeSerializer::to_JSON( const std::string& memberName, - const RsSerializable& member, RsJson& jDoc ) -{ - rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); - - // Reuse allocator to avoid deep copy later - RsGenericSerializer::SerializeContext ctx( - NULL, 0, RsGenericSerializer::FORMAT_BINARY, - RsGenericSerializer::SERIALIZATION_FLAG_NONE, - &allocator ); - - const_cast(member).serial_process( - RsGenericSerializer::TO_JSON, ctx ); - - rapidjson::Value key; - key.SetString(memberName.c_str(), memberName.length(), allocator); - - /* Because the passed allocator is reused it doesn't go out of scope and - * there is no need of deep copy and we can take advantage of the much - * faster rapidjson move semantic */ - jDoc.AddMember(key, ctx.mJson, allocator); - - return ctx.mOk; -} - -template<> /*static*/ -bool RsTypeSerializer::from_JSON( const std::string& memberName, - RsSerializable& member, RsJson& jVal ) -{ - SAFE_GET_JSON_V(); - - if(ret) - { - RsGenericSerializer::SerializeContext ctx( - NULL, 0, RsGenericSerializer::FORMAT_BINARY, - RsGenericSerializer::SERIALIZATION_FLAG_NONE ); - - ctx.mJson.SetObject() = v; // Beware of move semantic!! - member.serial_process(RsGenericSerializer::FROM_JSON, ctx); - ret = ret && ctx.mOk; - } - - return ret; -} - -template<> /*static*/ -void RsTypeSerializer::serial_process( - RsGenericSerializer::SerializeJob j, - RsGenericSerializer::SerializeContext& ctx, RsSerializable& object, - const std::string& objName ) -{ - switch (j) - { - case RsGenericSerializer::SIZE_ESTIMATE: // fall-through - case RsGenericSerializer::SERIALIZE: // fall-through - case RsGenericSerializer::DESERIALIZE: // fall-through - case RsGenericSerializer::PRINT: // fall-through - object.serial_process(j, ctx); - break; - case RsGenericSerializer::TO_JSON: - ctx.mOk = ctx.mOk && to_JSON(objName, object, ctx.mJson); - break; - case RsGenericSerializer::FROM_JSON: - ctx.mOk = ctx.mOk && from_JSON(objName, object, ctx.mJson); - break; - default: break; - } -} - //============================================================================// // RsJson std:ostream support // diff --git a/libretroshare/src/serialiser/rstypeserializer.h b/libretroshare/src/serialiser/rstypeserializer.h index 4b5486b88..f08a43239 100644 --- a/libretroshare/src/serialiser/rstypeserializer.h +++ b/libretroshare/src/serialiser/rstypeserializer.h @@ -37,6 +37,8 @@ #include #include // for typeid +#include +#include /** INTERNAL ONLY helper to avoid copy paste code for std::{vector,list,set} @@ -129,9 +131,10 @@ struct RsTypeSerializer /// Generic types template - static void serial_process( RsGenericSerializer::SerializeJob j, + typename std::enable_if::value || !(std::is_base_of::value || std::is_enum::value || std::is_base_of::value)>::type + static /*void*/ serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, - T& member, const std::string& member_name ) + T& member, const std::string& member_name) { switch(j) { @@ -156,8 +159,10 @@ struct RsTypeSerializer ctx.mOk = ctx.mOk && from_JSON(member_name, member, ctx.mJson); break; default: - ctx.mOk = false; - throw std::runtime_error("Unknown serial job"); + std::cerr << __PRETTY_FUNCTION__ << " Unknown serial job: " + << static_cast::type>(j) + << std::endl; + exit(EINVAL); } } @@ -193,8 +198,10 @@ struct RsTypeSerializer from_JSON(member_name, type_id, member, ctx.mJson); break; default: - ctx.mOk = false; - throw std::runtime_error("Unknown serial job"); + std::cerr << __PRETTY_FUNCTION__ << " Unknown serial job: " + << static_cast::type>(j) + << std::endl; + exit(EINVAL); } } @@ -567,6 +574,118 @@ struct RsTypeSerializer } } + /** + * @brief serial process enum types + * + * On declaration of your member of enum type you must specify the + * underlying type otherwise the serialization format may differ in an + * uncompatible way depending on the compiler/platform. + */ + template + typename std::enable_if::value>::type + static /*void*/ serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx, + E& member, + const std::string& memberName ) + { +#ifdef RSSERIAL_DEBUG + std::cerr << __PRETTY_FUNCTION__ << " processing enum: " + << typeid(E).name() << " as " + << typeid(typename std::underlying_type::type).name() + << std::endl; +#endif + serial_process( + j, ctx, + reinterpret_cast::type&>(member), + memberName ); + } + + /// RsSerializable and derivatives + template + typename std::enable_if::value>::type + static /*void*/ serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx, + T& member, + const std::string& memberName ) + { + switch(j) + { + case RsGenericSerializer::SIZE_ESTIMATE: // fallthrough + case RsGenericSerializer::DESERIALIZE: // fallthrough + case RsGenericSerializer::SERIALIZE: // fallthrough + case RsGenericSerializer::PRINT: + member.serial_process(j, ctx); + break; + case RsGenericSerializer::TO_JSON: + { + rapidjson::Document& jDoc(ctx.mJson); + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + // Reuse allocator to avoid deep copy later + RsGenericSerializer::SerializeContext lCtx( + nullptr, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE, + &allocator ); + + member.serial_process(j, lCtx); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + /* Because the passed allocator is reused it doesn't go out of scope and + * there is no need of deep copy and we can take advantage of the much + * faster rapidjson move semantic */ + jDoc.AddMember(key, lCtx.mJson, allocator); + + ctx.mOk = ctx.mOk && lCtx.mOk; + break; + } + case RsGenericSerializer::FROM_JSON: + { + rapidjson::Document& jDoc(ctx.mJson); + const char* mName = memberName.c_str(); + ctx.mOk = ctx.mOk && jDoc.HasMember(mName); + + if(!ctx.mOk) + { + std::cerr << __PRETTY_FUNCTION__ << " \"" << memberName + << "\" not found in JSON:" << std::endl + << jDoc << std::endl << std::endl; + print_stacktrace(); + break; + } + + rapidjson::Value& v = jDoc[mName]; + + RsGenericSerializer::SerializeContext lCtx( + nullptr, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE ); + lCtx.mJson.SetObject() = v; // Beware of move semantic!! + + member.serial_process(j, lCtx); + ctx.mOk = ctx.mOk && lCtx.mOk; + + break; + } + default: + std::cerr << __PRETTY_FUNCTION__ << " Unknown serial job: " + << static_cast::type>(j) + << std::endl; + exit(EINVAL); + break; + } + } + + /// RsTlvItem derivatives only + template + typename std::enable_if::value && !std::is_same::value>::type + static /*void*/ serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx, + T& member, + const std::string& memberName ) + { + serial_process(j, ctx, static_cast(member), memberName); + } protected: diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index 4796b3005..5a3878a7d 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -4560,5 +4560,3 @@ RsIdentityUsage::RsIdentityUsage( RsIdentityUsage::RsIdentityUsage() : mServiceId(0), mUsageCode(UNKNOWN_USAGE), mAdditionalId(0) {} - -RS_REGISTER_SERIALIZABLE_TYPE_DEF(RsIdentityUsage) From ef6681b6f9c4b8bc506a59f9de0c3e92e6ceef9f Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 27 Jan 2018 18:39:17 +0100 Subject: [PATCH 016/161] Remove outdated documentation --- libretroshare/src/rsitems/rsitem.h | 2 +- libretroshare/src/serialiser/rsserializable.h | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/libretroshare/src/rsitems/rsitem.h b/libretroshare/src/rsitems/rsitem.h index fb76d1562..545870631 100644 --- a/libretroshare/src/rsitems/rsitem.h +++ b/libretroshare/src/rsitems/rsitem.h @@ -75,7 +75,7 @@ struct RsItem : RsMemoryManagement::SmallObject, RsSerializable /** * TODO: This default implementation should be removed and childs structs - * implement RsSerializable(...) as soon as all the codebase is ported to + * implement ::serial_process(...) as soon as all the codebase is ported to * the new serialization system */ virtual void serial_process(RsGenericSerializer::SerializeJob, diff --git a/libretroshare/src/serialiser/rsserializable.h b/libretroshare/src/serialiser/rsserializable.h index f55378cf1..c717afc4b 100644 --- a/libretroshare/src/serialiser/rsserializable.h +++ b/libretroshare/src/serialiser/rsserializable.h @@ -23,13 +23,10 @@ /** @brief Minimal ancestor for all serializable structs in RetroShare. * If you want your struct to be easly serializable you should inherit from this * struct. - * If you want your struct to be serializable as part of a container like an - * `std::vector` @see RS_REGISTER_SERIALIZABLE_TYPE_DEF(T) and - * @see RS_REGISTER_SERIALIZABLE_TYPE_DECL(T) */ struct RsSerializable { - /** Register struct members to serialize in this method taking advantage of + /** Process struct members to serialize in this method taking advantage of * the helper macro @see RS_SERIAL_PROCESS(I) */ virtual void serial_process(RsGenericSerializer::SerializeJob j, @@ -37,7 +34,7 @@ struct RsSerializable }; /** @def RS_SERIAL_PROCESS(I) - * Use this macro to register the members of `YourSerializable` for serial + * Use this macro to process the members of `YourSerializable` for serial * processing inside `YourSerializable::serial_process(j, ctx)` * * Pay special attention for member of enum type which must be declared From 86ce17722885fb53215f5ca0a748b9ba6f2f7d46 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 27 Jan 2018 19:57:15 +0100 Subject: [PATCH 017/161] time_t may be signed in JSON --- libretroshare/src/serialiser/rstypeserializer.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libretroshare/src/serialiser/rstypeserializer.cc b/libretroshare/src/serialiser/rstypeserializer.cc index f4dc2d3c1..8d7ba5daf 100644 --- a/libretroshare/src/serialiser/rstypeserializer.cc +++ b/libretroshare/src/serialiser/rstypeserializer.cc @@ -251,8 +251,8 @@ bool RsTypeSerializer::from_JSON( const std::string& memberName, time_t& member, RsJson& jDoc ) { SAFE_GET_JSON_V(); - ret = ret && v.IsUint(); - if(ret) member = v.GetUint(); + ret = ret && v.IsInt(); + if(ret) member = v.GetInt(); return ret; } @@ -298,7 +298,7 @@ bool RsTypeSerializer::from_JSON( const std::string& memberName, //============================================================================// -// FLoats // +// Floats // //============================================================================// template<> uint32_t RsTypeSerializer::serial_size(const float&){ return 4; } From 51a43d00f732d7d6f059986d825d0bb7ffacd861 Mon Sep 17 00:00:00 2001 From: Phenom Date: Sat, 24 Feb 2018 18:15:54 +0100 Subject: [PATCH 018/161] Add a warning in ToolTip if Max files reach in flat view mode Dir Tree. --- .../src/gui/FileTransfer/SharedFilesDialog.cpp | 16 ++++++++++++++++ .../src/gui/FileTransfer/SharedFilesDialog.h | 2 ++ retroshare-gui/src/gui/RemoteDirModel.cpp | 13 ++++++++++--- retroshare-gui/src/gui/RemoteDirModel.h | 2 ++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index 88c925882..34ea6c2a0 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -163,6 +163,7 @@ SharedFilesDialog::SharedFilesDialog(RetroshareDirModel *_tree_model,RetroshareD tree_model = _tree_model ; flat_model = _flat_model ; + connect(flat_model, SIGNAL(layoutChanged()), this, SLOT(updateDirTreeView()) ); tree_proxyModel = new SFDSortFilterProxyModel(tree_model, this); tree_proxyModel->setSourceModel(tree_model); @@ -1344,6 +1345,21 @@ void SharedFilesDialog::startFilter() FilterItems(); } +void SharedFilesDialog::updateDirTreeView() +{ + if (model == flat_model) + { + size_t maxSize = 0; + FlatStyle_RDM* flat = dynamic_cast(flat_model); + if (flat && flat->isMaxRefsTableSize(&maxSize)) + { + ui.dirTreeView->setToolTip(tr("Warning: You reach max (%1) files in flat list. No more will be added.").arg(maxSize)); + return; + } + } + ui.dirTreeView->setToolTip(""); +} + // This macro make the search expand all items that contain the searched text. // A bug however, makes RS expand everything when nothing is selected, which is a pain. diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h index f7c866d70..6c48ea4c0 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h @@ -77,6 +77,8 @@ private slots: void clearFilter(); void startFilter(); + void updateDirTreeView(); + public slots: void changeCurrentViewModel(int viewTypeIndex); signals: diff --git a/retroshare-gui/src/gui/RemoteDirModel.cpp b/retroshare-gui/src/gui/RemoteDirModel.cpp index ee0ea3ac5..94cf7c6bf 100644 --- a/retroshare-gui/src/gui/RemoteDirModel.cpp +++ b/retroshare-gui/src/gui/RemoteDirModel.cpp @@ -46,7 +46,7 @@ ****/ static const uint32_t FLAT_VIEW_MAX_REFS_PER_SECOND = 2000 ; -static const uint32_t FLAT_VIEW_MAX_REFS_TABLE_SIZE = 10000 ; // +static const size_t FLAT_VIEW_MAX_REFS_TABLE_SIZE = 10000 ; // static const uint32_t FLAT_VIEW_MIN_DELAY_BETWEEN_UPDATES = 120 ; // dont rebuild ref list more than every 2 mins. RetroshareDirModel::RetroshareDirModel(bool mode, QObject *parent) @@ -516,6 +516,13 @@ void FlatStyle_RDM::update() } } +bool FlatStyle_RDM::isMaxRefsTableSize(size_t *maxSize/*=NULL*/) +{ + if (maxSize) + *maxSize = FLAT_VIEW_MAX_REFS_TABLE_SIZE; + + return (_ref_entries.size() >= FLAT_VIEW_MAX_REFS_TABLE_SIZE); +} QString FlatStyle_RDM::computeDirectoryPath(const DirDetails& details) const { QString dir ; @@ -1515,7 +1522,7 @@ void FlatStyle_RDM::updateRefs() { RS_STACK_MUTEX(_ref_mutex) ; - while(!_ref_stack.empty()) + while( !_ref_stack.empty() && (_ref_entries.size() <= FLAT_VIEW_MAX_REFS_TABLE_SIZE) ) { void *ref = _ref_stack.back() ; #ifdef RDM_DEBUG @@ -1539,7 +1546,7 @@ void FlatStyle_RDM::updateRefs() // Limit the size of the table to display, otherwise it becomes impossible to Qt. if(_ref_entries.size() > FLAT_VIEW_MAX_REFS_TABLE_SIZE) - return ; + continue; if(++nb_treated_refs > FLAT_VIEW_MAX_REFS_PER_SECOND) // we've done enough, let's give back hand to { // the user and setup a timer to finish the job later. diff --git a/retroshare-gui/src/gui/RemoteDirModel.h b/retroshare-gui/src/gui/RemoteDirModel.h index 284cc7d81..fb0272ae3 100644 --- a/retroshare-gui/src/gui/RemoteDirModel.h +++ b/retroshare-gui/src/gui/RemoteDirModel.h @@ -217,6 +217,8 @@ class FlatStyle_RDM: public RetroshareDirModel virtual void update() ; + bool isMaxRefsTableSize(size_t* maxSize = NULL); + protected slots: void updateRefs() ; From efa5827fff049ce1491ba724bb8b553a3e003df3 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 2 Mar 2018 18:01:56 +0100 Subject: [PATCH 019/161] fixed missing peer availability maps in FT dialog --- .../src/gui/FileTransfer/TransfersDialog.cpp | 36 ++++++------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp index 384d5cf5c..460f97169 100644 --- a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp @@ -446,7 +446,7 @@ public: { FileChunksInfo fcinfo; if (!rsFiles->FileDownloadChunksDetails(fileInfo.hash, fcinfo)) - return -1; + return QVariant(); FileProgressInfo pinfo; pinfo.cmap = fcinfo.chunks; @@ -480,34 +480,20 @@ public: { case COLUMN_PROGRESS: { - FileProgressInfo peerpinfo ; - - if(!rsFiles->FileUploadChunksDetails(fileInfo.hash, fileInfo.peers[source_id].peerId, peerpinfo.cmap) ) + FileChunksInfo fcinfo; + if (!rsFiles->FileDownloadChunksDetails(fileInfo.hash, fcinfo)) return QVariant(); - // Estimate the completion. We need something more accurate, meaning that we need to - // transmit the completion info. - // - uint32_t chunk_size = 1024*1024 ; - uint32_t nb_chunks = (uint32_t)((fileInfo.size + (uint64_t)chunk_size - 1) / (uint64_t)(chunk_size)) ; + RsPeerId pid = fileInfo.peers[source_id].peerId; + CompressedChunkMap& cmap(fcinfo.compressed_peer_availability_maps[pid]) ; - uint32_t filled_chunks = peerpinfo.cmap.filledChunks(nb_chunks) ; - peerpinfo.type = FileProgressInfo::UPLOAD_LINE ; - peerpinfo.nb_chunks = peerpinfo.cmap._map.empty()?0:nb_chunks ; - qlonglong completed ; + FileProgressInfo pinfo; + pinfo.cmap = cmap; + pinfo.type = FileProgressInfo::DOWNLOAD_SOURCE; + pinfo.progress = 0.0; // we dont display completion for sources + pinfo.nb_chunks = pinfo.cmap._map.empty() ? 0 : fcinfo.chunks.size(); - if(filled_chunks > 0 && nb_chunks > 0) - { - completed = peerpinfo.cmap.computeProgress(fileInfo.size,chunk_size) ; - peerpinfo.progress = completed / (float)fileInfo.size * 100.0f ; - } - else - { - completed = fileInfo.peers[source_id].transfered % chunk_size ; // use the position with respect to last request. - peerpinfo.progress = (fileInfo.size>0)?((fileInfo.peers[source_id].transfered % chunk_size)*100.0/fileInfo.size):0 ; - } - - return QVariant::fromValue(peerpinfo); + return QVariant::fromValue(pinfo); } case COLUMN_ID: return QVariant(QString::fromStdString(fileInfo.hash.toStdString()) + QString::fromStdString(fileInfo.peers[source_id].peerId.toStdString())); From ca402c2ba144e33dc9a6bb69437d78aca55da5c3 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 2 Mar 2018 18:05:11 +0100 Subject: [PATCH 020/161] fixed column sizing in FT dialog --- .../src/gui/FileTransfer/TransfersDialog.cpp | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp index 460f97169..94e6f924e 100644 --- a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp @@ -264,22 +264,24 @@ public: QVariant sizeHintRole(int col) const { + float factor = QFontMetricsF(font())/14.0f ; + switch(col) { default: - case COLUMN_NAME: return QVariant( 170 ); - case COLUMN_SIZE: return QVariant( 70 ); - case COLUMN_COMPLETED: return QVariant( 75 ); - case COLUMN_DLSPEED: return QVariant( 75 ); - case COLUMN_PROGRESS: return QVariant( 170 ); - case COLUMN_SOURCES: return QVariant( 90 ); - case COLUMN_STATUS: return QVariant( 100 ); - case COLUMN_PRIORITY: return QVariant( 100 ); - case COLUMN_REMAINING: return QVariant( 100 ); - case COLUMN_DOWNLOADTIME: return QVariant( 100 ); - case COLUMN_ID: return QVariant( 100 ); - case COLUMN_LASTDL: return QVariant( 100 ); - case COLUMN_PATH: return QVariant( 100 ); + case COLUMN_NAME: return QVariant( factor * 170 ); + case COLUMN_SIZE: return QVariant( factor * 70 ); + case COLUMN_COMPLETED: return QVariant( factor * 75 ); + case COLUMN_DLSPEED: return QVariant( factor * 75 ); + case COLUMN_PROGRESS: return QVariant( factor * 170 ); + case COLUMN_SOURCES: return QVariant( factor * 90 ); + case COLUMN_STATUS: return QVariant( factor * 100 ); + case COLUMN_PRIORITY: return QVariant( factor * 100 ); + case COLUMN_REMAINING: return QVariant( factor * 100 ); + case COLUMN_DOWNLOADTIME: return QVariant( factor * 100 ); + case COLUMN_ID: return QVariant( factor * 100 ); + case COLUMN_LASTDL: return QVariant( factor * 100 ); + case COLUMN_PATH: return QVariant( factor * 100 ); } } From f2a737ec99c715fa180175685f92f8f0e5ea8df3 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 2 Mar 2018 18:13:09 +0100 Subject: [PATCH 021/161] fixed font size compilation error --- retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp index 94e6f924e..1a67d95f8 100644 --- a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp @@ -264,7 +264,7 @@ public: QVariant sizeHintRole(int col) const { - float factor = QFontMetricsF(font())/14.0f ; + float factor = QFontMetricsF(QApplication::font()).height()/14.0f ; switch(col) { From e88eebf3e30a546a78d01921c3f6c7a49b28e2e6 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 2 Mar 2018 21:26:38 +0100 Subject: [PATCH 022/161] attempt to fix wrong number of rows in DL list --- .../src/gui/FileTransfer/TransfersDialog.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp index 1a67d95f8..d1a212dea 100644 --- a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp @@ -544,7 +544,7 @@ public: else if(mDownloads.size() < old_size) { beginRemoveRows(QModelIndex(), mDownloads.size(), old_size-1); - removeRows(old_size, old_size - mDownloads.size()); + removeRows(mDownloads.size(), old_size - mDownloads.size()); endRemoveRows(); } @@ -555,7 +555,24 @@ public: for(auto it(downHashes.begin());it!=downHashes.end();++it,++i) { FileInfo& fileInfo(mDownloads[i]); + int old_size = fileInfo.peers.size() ; + rsFiles->FileDetails(*it, RS_FILE_HINTS_DOWNLOAD, fileInfo); + + int new_size = fileInfo.peers.size() ; + + if(old_size < new_size) + { + beginInsertRows(index(i,0), old_size, new_size-1); + insertRows(old_size, new_size - old_size,index(i,0)); + endInsertRows(); + } + else if(new_size < old_size) + { + beginRemoveRows(index(i,0), new_size, old_size-1); + removeRows(new_size, old_size - new_size,index(i,0)); + endRemoveRows(); + } } // endResetModel(); From 9386bb991de3a2ba9ecf8f840f3515054591f3d0 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 2 Mar 2018 22:57:14 +0100 Subject: [PATCH 023/161] tried to fix FT list bug. --- .../src/gui/FileTransfer/TransfersDialog.cpp | 79 +++++-------------- 1 file changed, 19 insertions(+), 60 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp index d1a212dea..20f6eb059 100644 --- a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp @@ -118,7 +118,7 @@ public: uint32_t entry = 0 ; int source_id ; - if(!convertRefPointerToTabEntry(ref,entry,source_id) || entry >= mDownloads.size()) + if(!convertRefPointerToTabEntry(ref,entry,source_id) || entry >= mDownloads.size() || source_id > -1) return 0 ; return mDownloads[entry].peers.size(); // costly @@ -160,7 +160,7 @@ public: return createIndex(row,column,subref) ; } - if(!convertRefPointerToTabEntry(ref,entry,source_id) || entry >= mDownloads.size() || int(mDownloads[entry].peers.size()) <= source_id) + if(!convertRefPointerToTabEntry(ref,entry,source_id) || entry >= mDownloads.size() || int(mDownloads[entry].peers.size()) <= row) return QModelIndex() ; if(source_id != -1) @@ -169,6 +169,7 @@ public: if(!convertTabEntryToRefPointer(entry,row,subref)) return QModelIndex() ; + std::cerr << "Creating index (" << row << " sid=" << source_id << " ref=" << subref << ")" << std::endl; return createIndex(row,column,subref) ; } QModelIndex parent(const QModelIndex& child) const @@ -186,12 +187,12 @@ public: if(source_id < 0) return QModelIndex() ; - void *subref =NULL; + void *parref =NULL; - if(!convertTabEntryToRefPointer(entry,-1,subref)) + if(!convertTabEntryToRefPointer(entry,-1,parref)) return QModelIndex() ; - return createIndex(entry,0,subref) ; + return createIndex(entry,0,parref) ; } QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const @@ -250,6 +251,11 @@ public: return QVariant() ; } + if(source_id >= mDownloads[entry].peers.size()) + return QVariant() ; + + std::cerr << "data row=" << index.row() << " col=" << index.column() << " entry=" << entry << " source=" << source_id << std::endl; + const FileInfo& finfo(mDownloads[entry]) ; switch(role) @@ -495,6 +501,7 @@ public: pinfo.progress = 0.0; // we dont display completion for sources pinfo.nb_chunks = pinfo.cmap._map.empty() ? 0 : fcinfo.chunks.size(); + //std::cerr << "User role of source id " << source_id << std::endl; return QVariant::fromValue(pinfo); } @@ -565,73 +572,25 @@ public: { beginInsertRows(index(i,0), old_size, new_size-1); insertRows(old_size, new_size - old_size,index(i,0)); + std::cerr << "called insert rows ( " << old_size << ", " << new_size - old_size << ")" << std::endl; endInsertRows(); } else if(new_size < old_size) { beginRemoveRows(index(i,0), new_size, old_size-1); removeRows(new_size, old_size - new_size,index(i,0)); + std::cerr << "called remove rows ( " << old_size << ", " << old_size - new_size << ")" << std::endl; endRemoveRows(); } } // endResetModel(); - QModelIndex topLeft = createIndex(0,0), bottomRight = createIndex(mDownloads.size()-1, COLUMN_COUNT-1); - emit dataChanged(topLeft, bottomRight); - - //shit code follow (rewrite this please) - // size_t old_size = neighs.size(), new_size = 0; - // std::list old_neighs = neighs; - // - // new_size = new_neighs.size(); - // //set model data to new cleaned up data - // neighs = new_neighs; - // neighs.sort(); - // neighs.unique(); //remove possible dups - // - // //reflect actual row count in model - // if(old_size < new_size) - // { - // beginInsertRows(QModelIndex(), old_size, new_size); - // insertRows(old_size, new_size - old_size); - // endInsertRows(); - // } - // else if(new_size < old_size) - // { - // beginRemoveRows(QModelIndex(), new_size, old_size); - // removeRows(old_size, old_size - new_size); - // endRemoveRows(); - // } - // //update data in ui, to avoid unnecessary redraw and ui updates, updating only changed elements - // //TODO: libretroshare should implement a way to obtain only changed elements via some signalling non-blocking api. - // { - // size_t ii1 = 0; - // for(auto i1 = neighs.begin(), end1 = neighs.end(), i2 = old_neighs.begin(), end2 = old_neighs.end(); i1 != end1; ++i1, ++i2, ii1++) - // { - // if(i2 == end2) - // break; - // if(*i1 != *i2) - // { - // QModelIndex topLeft = createIndex(ii1,0), bottomRight = createIndex(ii1, COLUMN_COUNT-1); - // emit dataChanged(topLeft, bottomRight); - // } - // } - // } - // if(new_size > old_size) - // { - // QModelIndex topLeft = createIndex(old_size ? old_size -1 : 0 ,0), bottomRight = createIndex(new_size -1, COLUMN_COUNT-1); - // emit dataChanged(topLeft, bottomRight); - // } - // //dirty solution for initial data fetch - // //TODO: do it properly! - // if(!old_size) - // { - // beginResetModel(); - // endResetModel(); - // } - - + if(!mDownloads.empty()) + { + QModelIndex topLeft = createIndex(0,0), bottomRight = createIndex(mDownloads.size()-1, COLUMN_COUNT-1); + emit dataChanged(topLeft, bottomRight); + } } private: static const uint32_t TRANSFERS_NB_DOWNLOADS_BITS_32BITS = 22 ; // Means 2^22 simultaneous transfers From ab45a972b04560362c896d32cc1b4e6935113980 Mon Sep 17 00:00:00 2001 From: Phenom Date: Sat, 3 Mar 2018 12:05:13 +0100 Subject: [PATCH 024/161] Revert "Fix SharedFilesDialog show old hidden column." This reverts commit c141eae8289a9e93a02158005f832c696268613e. --- .../gui/FileTransfer/SharedFilesDialog.cpp | 23 ++----------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index 88c925882..652337b50 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -229,12 +229,6 @@ LocalSharedFilesDialog::LocalSharedFilesDialog(QWidget *parent) // load settings processSettings(true); - // Force to show columns even if hidden in setting - ui.dirTreeView->setColumnHidden(COLUMN_NAME, false) ; - ui.dirTreeView->setColumnHidden(COLUMN_FILENB, false) ; - ui.dirTreeView->setColumnHidden(COLUMN_SIZE, false) ; - ui.dirTreeView->setColumnHidden(COLUMN_AGE, false) ; - ui.dirTreeView->setColumnHidden(COLUMN_FRIEND_ACCESS, false) ; // Setup the current view model. // changeCurrentViewModel(ui.viewType_CB->currentIndex()) ; @@ -260,16 +254,11 @@ RemoteSharedFilesDialog::RemoteSharedFilesDialog(QWidget *parent) ui.checkButton->hide() ; connect(ui.downloadButton, SIGNAL(clicked()), this, SLOT(downloadRemoteSelected())); - connect(ui.dirTreeView, SIGNAL( expanded(const QModelIndex & ) ), this, SLOT( expanded(const QModelIndex & ) ) ); - connect(ui.dirTreeView, SIGNAL( doubleClicked(const QModelIndex & ) ), this, SLOT( expanded(const QModelIndex & ) ) ); + connect(ui.dirTreeView, SIGNAL( expanded(const QModelIndex & ) ), this, SLOT( expanded(const QModelIndex & ) ) ); + connect(ui.dirTreeView, SIGNAL( doubleClicked(const QModelIndex & ) ), this, SLOT( expanded(const QModelIndex & ) ) ); // load settings processSettings(true); - // Force to show columns even if hidden in setting - ui.dirTreeView->setColumnHidden(COLUMN_NAME, false) ; - ui.dirTreeView->setColumnHidden(COLUMN_FILENB, false) ; - ui.dirTreeView->setColumnHidden(COLUMN_SIZE, false) ; - ui.dirTreeView->setColumnHidden(COLUMN_AGE, false) ; // Setup the current view model. // changeCurrentViewModel(ui.viewType_CB->currentIndex()) ; @@ -425,10 +414,6 @@ void SharedFilesDialog::changeCurrentViewModel(int viewTypeIndex) void LocalSharedFilesDialog::showProperColumns() { - ui.dirTreeView->setColumnHidden(COLUMN_NAME, false) ; - ui.dirTreeView->setColumnHidden(COLUMN_SIZE, false) ; - ui.dirTreeView->setColumnHidden(COLUMN_AGE, false) ; - if(model == tree_model) { ui.dirTreeView->setColumnHidden(COLUMN_FILENB, false) ; @@ -454,10 +439,6 @@ void LocalSharedFilesDialog::showProperColumns() } void RemoteSharedFilesDialog::showProperColumns() { - ui.dirTreeView->setColumnHidden(COLUMN_NAME, false) ; - ui.dirTreeView->setColumnHidden(COLUMN_SIZE, false) ; - ui.dirTreeView->setColumnHidden(COLUMN_AGE, false) ; - if(model == tree_model) { ui.dirTreeView->setColumnHidden(COLUMN_FILENB, false) ; From 58d418da67cb1d1a4a12b5e92a1e9798ec7ed72f Mon Sep 17 00:00:00 2001 From: Phenom Date: Sat, 3 Mar 2018 12:08:21 +0100 Subject: [PATCH 025/161] Fix SharedFilesDialog show old hidden column. Renamed group as calling setColumnHidden(COLUMN_AGE, false) doesn't work. --- retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index 652337b50..88ed90a41 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -335,7 +335,7 @@ void LocalSharedFilesDialog::processSettings(bool bLoad) } void RemoteSharedFilesDialog::processSettings(bool bLoad) { - Settings->beginGroup("SharedFilesDialog"); + Settings->beginGroup("RemoteSharedFilesDialog"); if (bLoad) { // load settings From 1a1dc648bd0e58a14d20b135ea4128e4b793f79e Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 3 Mar 2018 15:49:45 +0100 Subject: [PATCH 026/161] fixed transfers list bug causing ghost entries to appear --- .../src/gui/FileTransfer/TransfersDialog.cpp | 182 +++++++++++++----- 1 file changed, 133 insertions(+), 49 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp index 20f6eb059..d6e9dc067 100644 --- a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp @@ -96,8 +96,15 @@ #define IMAGE_TUNNEL_ANON ":/images/blue_lock_open.png" #define IMAGE_TUNNEL_FRIEND ":/icons/avatar_128.png" +//#define DEBUG_DOWNLOADLIST 1 + Q_DECLARE_METATYPE(FileProgressInfo) +std::ostream& operator<<(std::ostream& o, const QModelIndex& i) +{ + return o << i.row() << "," << i.column() << "," << i.internalPointer() ; +} + class RsDownloadListModel : public QAbstractItemModel { // Q_OBJECT @@ -113,19 +120,32 @@ public: void *ref = (parent.isValid())?parent.internalPointer():NULL ; if(!ref) + { +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "rowCount-1(" << parent << ") : " << mDownloads.size() << std::endl; +#endif return mDownloads.size() ; + } uint32_t entry = 0 ; int source_id ; if(!convertRefPointerToTabEntry(ref,entry,source_id) || entry >= mDownloads.size() || source_id > -1) + { +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "rowCount-2(" << parent << ") : " << 0 << std::endl; +#endif return 0 ; + } - return mDownloads[entry].peers.size(); // costly +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "rowCount-3(" << parent << ") : " << mDownloads[entry].peers.size() << std::endl; +#endif + return mDownloads[entry].peers.size(); } int columnCount(const QModelIndex &parent = QModelIndex()) const { - return 13 ; + return COLUMN_COUNT ; } bool hasChildren(const QModelIndex &parent = QModelIndex()) const { @@ -134,65 +154,101 @@ public: int source_id=0 ; if(!ref) + { +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "hasChildren-1(" << parent << ") : " << true << std::endl; +#endif return true ; + } if(!convertRefPointerToTabEntry(ref,entry,source_id) || entry >= mDownloads.size() || source_id > -1) + { +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "hasChildren-2(" << parent << ") : " << false << std::endl; +#endif return false ; + } +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "hasChildren-3(" << parent << ") : " << !mDownloads[entry].peers.empty() << std::endl; +#endif return !mDownloads[entry].peers.empty(); } QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const { - if(row < 0) + if(row < 0 || column < 0 || column >= COLUMN_COUNT) return QModelIndex(); - void *ref = (parent.isValid())?parent.internalPointer():NULL ; + void *parent_ref = (parent.isValid())?parent.internalPointer():NULL ; uint32_t entry = 0; int source_id=0 ; - void *subref = NULL ; - if(!ref) // top level. The entry is that of a transfer + if(!parent_ref) // top level. The entry is that of a transfer { - if(row >= mDownloads.size() || !convertTabEntryToRefPointer(row,-1,subref)) - return QModelIndex() ; + void *ref = NULL ; - return createIndex(row,column,subref) ; + if(row >= (int)mDownloads.size() || !convertTabEntryToRefPointer(row,-1,ref)) + { +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "index-1(" << row << "," << column << " parent=" << parent << ") : " << "NULL" << std::endl; +#endif + return QModelIndex() ; + } + +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "index-2(" << row << "," << column << " parent=" << parent << ") : " << createIndex(row,column,ref) << std::endl; +#endif + return createIndex(row,column,ref) ; } - if(!convertRefPointerToTabEntry(ref,entry,source_id) || entry >= mDownloads.size() || int(mDownloads[entry].peers.size()) <= row) + if(!convertRefPointerToTabEntry(parent_ref,entry,source_id) || entry >= mDownloads.size() || int(mDownloads[entry].peers.size()) <= row) + { +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "index-5(" << row << "," << column << " parent=" << parent << ") : " << "NULL"<< std::endl ; +#endif return QModelIndex() ; + } if(source_id != -1) std::cerr << "ERROR: parent.source_id != -1 in index()" << std::endl; - if(!convertTabEntryToRefPointer(entry,row,subref)) - return QModelIndex() ; + void *ref = NULL ; - std::cerr << "Creating index (" << row << " sid=" << source_id << " ref=" << subref << ")" << std::endl; - return createIndex(row,column,subref) ; + if(!convertTabEntryToRefPointer(entry,row,ref)) + { +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "index-4(" << row << "," << column << " parent=" << parent << ") : " << "NULL" << std::endl; +#endif + return QModelIndex() ; + } + +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "index-3(" << row << "," << column << " parent=" << parent << ") : " << createIndex(row,column,ref) << std::endl; +#endif + return createIndex(row,column,ref) ; } QModelIndex parent(const QModelIndex& child) const { - void *ref = (child.isValid())?child.internalPointer():NULL ; + void *child_ref = (child.isValid())?child.internalPointer():NULL ; uint32_t entry = 0; int source_id=0 ; - if(!ref) + if(!child_ref) return QModelIndex() ; - if(!convertRefPointerToTabEntry(ref,entry,source_id) || entry >= mDownloads.size() || int(mDownloads[entry].peers.size()) <= source_id) + if(!convertRefPointerToTabEntry(child_ref,entry,source_id) || entry >= mDownloads.size() || int(mDownloads[entry].peers.size()) <= source_id) return QModelIndex() ; if(source_id < 0) return QModelIndex() ; - void *parref =NULL; + void *parent_ref =NULL; - if(!convertTabEntryToRefPointer(entry,-1,parref)) + if(!convertTabEntryToRefPointer(entry,-1,parent_ref)) return QModelIndex() ; - return createIndex(entry,0,parref) ; + return createIndex(entry,child.column(),parent_ref) ; } QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const @@ -242,22 +298,44 @@ public: uint32_t entry = 0; int source_id=0 ; - if(!ref) - return QVariant() ; +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "data(" << index << ")" ; +#endif - if(!convertRefPointerToTabEntry(ref,entry,source_id) || entry >= mDownloads.size()) + if(!ref) { - std::cerr << "Bad pointer: " << (void*)ref << std::endl; +#ifdef DEBUG_DOWNLOADLIST + std::cerr << " [empty]" << std::endl; +#endif return QVariant() ; } - if(source_id >= mDownloads[entry].peers.size()) + if(!convertRefPointerToTabEntry(ref,entry,source_id) || entry >= mDownloads.size()) + { +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "Bad pointer: " << (void*)ref << std::endl; +#endif return QVariant() ; + } - std::cerr << "data row=" << index.row() << " col=" << index.column() << " entry=" << entry << " source=" << source_id << std::endl; +#ifdef DEBUG_DOWNLOADLIST + std::cerr << " source_id=" << source_id ; +#endif + + if(source_id >= int(mDownloads[entry].peers.size())) + { +#ifdef DEBUG_DOWNLOADLIST + std::cerr << " [empty]" << std::endl; +#endif + return QVariant() ; + } const FileInfo& finfo(mDownloads[entry]) ; +#ifdef DEBUG_DOWNLOADLIST + std::cerr << " [ok]" << std::endl; +#endif + switch(role) { case Qt::DisplayRole: return displayRole (finfo,source_id,index.column()) ; @@ -533,8 +611,6 @@ public: void update_transfers() { -// beginResetModel(); - std::list downHashes; rsFiles->FileDownloads(downHashes); @@ -555,13 +631,11 @@ public: endRemoveRows(); } - //std::cerr << "updating file list: found " << mDownloads.size() << " transfers." << std::endl; - uint32_t i=0; for(auto it(downHashes.begin());it!=downHashes.end();++it,++i) { - FileInfo& fileInfo(mDownloads[i]); + FileInfo fileInfo(mDownloads[i]); // we dont update the data itself but only a copy of it.... int old_size = fileInfo.peers.size() ; rsFiles->FileDetails(*it, RS_FILE_HINTS_DOWNLOAD, fileInfo); @@ -572,30 +646,37 @@ public: { beginInsertRows(index(i,0), old_size, new_size-1); insertRows(old_size, new_size - old_size,index(i,0)); - std::cerr << "called insert rows ( " << old_size << ", " << new_size - old_size << ")" << std::endl; +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "called insert rows ( " << old_size << ", " << new_size - old_size << ",index(" << index(i,0)<< "))" << std::endl; +#endif endInsertRows(); } else if(new_size < old_size) { beginRemoveRows(index(i,0), new_size, old_size-1); removeRows(new_size, old_size - new_size,index(i,0)); - std::cerr << "called remove rows ( " << old_size << ", " << old_size - new_size << ")" << std::endl; +#ifdef DEBUG_DOWNLOADLIST + std::cerr << "called remove rows ( " << old_size << ", " << old_size - new_size << ",index(" << index(i,0)<< "))" << std::endl; +#endif endRemoveRows(); } - } -// endResetModel(); + mDownloads[i] = fileInfo ; // ... because insertRows() calls rowCount() which needs to be consistent with the *old* number of rows. - if(!mDownloads.empty()) - { - QModelIndex topLeft = createIndex(0,0), bottomRight = createIndex(mDownloads.size()-1, COLUMN_COUNT-1); - emit dataChanged(topLeft, bottomRight); + // This is apparently not needed. + // + // if(!mDownloads.empty()) + // { + // QModelIndex topLeft = createIndex(0,0), bottomRight = createIndex(mDownloads.size()-1, COLUMN_COUNT-1); + // emit dataChanged(topLeft, bottomRight); + // mDownloads[i] = fileInfo ; + // } } } private: - static const uint32_t TRANSFERS_NB_DOWNLOADS_BITS_32BITS = 22 ; // Means 2^22 simultaneous transfers - static const uint32_t TRANSFERS_NB_DOWNLOADS_BIT_MASK_32BITS = 0x003fffff ; // actual bit mask corresponding to previous number of bits - static const uint32_t TRANSFERS_NB_SOURCES_BITS_32BITS = 10 ; // Means 2^10 simultaneous sources + static const uint32_t TRANSFERS_NB_DOWNLOADS_BITS_32BITS = 22 ; // Means 2^22 simultaneous transfers + static const uint32_t TRANSFERS_NB_SOURCES_BITS_32BITS = 10 ; // Means 2^10 simultaneous sources + static const uint32_t TRANSFERS_NB_SOURCES_BIT_MASK_32BITS = (1 << TRANSFERS_NB_SOURCES_BITS_32BITS)-1 ;// actual bit mask corresponding to previous number of bits static bool convertTabEntryToRefPointer(uint32_t entry,int source_id,void *& ref) { @@ -606,30 +687,33 @@ private: } // the pointer is formed the following way: // - // [ 10 bits | 22 bits ] + // [ 22 bits | 10 bits ] // - // This means that the whoel software has the following build-in limitation: - // * 1023 sources + // This means that the whole software has the following build-in limitation: // * 4M simultaenous file transfers + // * 1023 sources - if(uint32_t(source_id+1) >= (1u<= (1u<< TRANSFERS_NB_DOWNLOADS_BITS_32BITS)) + if(uint32_t(source_id+1) >= (1u<= (1u<< TRANSFERS_NB_DOWNLOADS_BITS_32BITS)) { std::cerr << "(EE) cannot convert download index " << entry << " and source " << source_id << " to pointer." << std::endl; return false ; } - ref = reinterpret_cast( ( uint32_t(1+source_id) << TRANSFERS_NB_DOWNLOADS_BITS_32BITS ) + ( (entry+1) & TRANSFERS_NB_DOWNLOADS_BIT_MASK_32BITS)) ; + ref = reinterpret_cast( ( uint32_t(1+entry) << TRANSFERS_NB_SOURCES_BITS_32BITS ) + ( (source_id+1) & TRANSFERS_NB_SOURCES_BIT_MASK_32BITS)) ; + assert(ref != NULL) ; return true; } static bool convertRefPointerToTabEntry(void *ref,uint32_t& entry,int& source_id) { + assert(ref != NULL) ; + // we pack the couple (id of DL, id of source) into a single 32-bits pointer that is required by the AbstractItemModel class. #pragma GCC diagnostic ignored "-Wstrict-aliasing" - uint32_t ntr = uint32_t( *reinterpret_cast(&ref) & TRANSFERS_NB_DOWNLOADS_BIT_MASK_32BITS ) ; - uint32_t src = ( *reinterpret_cast(&ref)) >> TRANSFERS_NB_DOWNLOADS_BITS_32BITS ; + uint32_t src = uint32_t( *reinterpret_cast(&ref) & TRANSFERS_NB_SOURCES_BIT_MASK_32BITS ) ; + uint32_t ntr = ( *reinterpret_cast(&ref)) >> TRANSFERS_NB_SOURCES_BITS_32BITS ; #pragma GCC diagnostic pop if(ntr == 0) From dcb3f79e4cb2a2cd45585c315fe6a697aa93a017 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 3 Mar 2018 15:55:52 +0100 Subject: [PATCH 027/161] removed some debug info in RSLink --- retroshare-gui/src/gui/RetroShareLink.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/RetroShareLink.cpp b/retroshare-gui/src/gui/RetroShareLink.cpp index f4af64017..5b45ff584 100644 --- a/retroshare-gui/src/gui/RetroShareLink.cpp +++ b/retroshare-gui/src/gui/RetroShareLink.cpp @@ -1213,7 +1213,6 @@ bool RetroShareLink::checkRadix64(const QString& s) return false; } } - std::cerr << "Radix check: passed" << std::endl; return true ; } @@ -1968,7 +1967,9 @@ void RSLinkClipboard::parseText(QString text, QList &links,Retro { links.clear(); +#ifdef DEBUG_RSLINK std::cerr << "Parsing text:" << text.toStdString() << std::endl ; +#endif QRegExp rx(QString("retroshare://(%1)[^\r\n]+").arg(HOST_REGEXP)); From 919417a137819eccbc5f14d974177f0034da56bb Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 3 Mar 2018 19:04:54 +0100 Subject: [PATCH 028/161] make sure tor executable from config path can be reached --- retroshare-gui/src/TorControl/TorManager.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/retroshare-gui/src/TorControl/TorManager.cpp b/retroshare-gui/src/TorControl/TorManager.cpp index 85ff58f7a..96c45b88b 100644 --- a/retroshare-gui/src/TorControl/TorManager.cpp +++ b/retroshare-gui/src/TorControl/TorManager.cpp @@ -446,7 +446,8 @@ QString TorManagerPrivate::torExecutablePath() const { SettingsObject settings(QStringLiteral("tor")); QString path = settings.read("executablePath").toString(); - if (!path.isEmpty()) + + if (!path.isEmpty() && QFile::exists(path)) return path; #ifdef Q_OS_WIN @@ -456,6 +457,7 @@ QString TorManagerPrivate::torExecutablePath() const #endif path = qApp->applicationDirPath(); + if (QFile::exists(path + filename)) return path + filename; From 593e57b14cb97771d1f8d9662dd69149b985147b Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 3 Mar 2018 20:51:25 +0100 Subject: [PATCH 029/161] updated changelog --- build_scripts/Debian+Ubuntu/changelog | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/build_scripts/Debian+Ubuntu/changelog b/build_scripts/Debian+Ubuntu/changelog index 9610bcc43..57ed5bd3e 100644 --- a/build_scripts/Debian+Ubuntu/changelog +++ b/build_scripts/Debian+Ubuntu/changelog @@ -1,5 +1,21 @@ retroshare (ZZZZZZ-1.XXXXXX~YYYYYY) YYYYYY; urgency=low + 919417a csoler Sat, 3 Mar 2018 19:04:54 +0100 make sure tor executable from config path can be reached + b587ac8 csoler Sat, 3 Mar 2018 16:06:49 +0100 Merge pull request #1192 from PhenomRetroShare/Add_WarningMaxFlatViewFile + 9bcff07 csoler Sat, 3 Mar 2018 15:58:11 +0100 Merge pull request #1203 from PhenomRetroShare/Fix_SharedFileDialogShowColumn + ead2aea csoler Sat, 3 Mar 2018 15:56:51 +0100 Merge pull request #1204 from csoler/v0.6-FT + dcb3f79 csoler Sat, 3 Mar 2018 15:55:52 +0100 removed some debug info in RSLink + 1a1dc64 csoler Sat, 3 Mar 2018 15:49:45 +0100 fixed transfers list bug causing ghost entries to appear + 58d418d Phenom Sat, 3 Mar 2018 12:08:21 +0100 Fix SharedFilesDialog show old hidden column. + ab45a97 Phenom Sat, 3 Mar 2018 12:05:13 +0100 Revert "Fix SharedFilesDialog show old hidden column." + 57f87a6 csoler Fri, 2 Mar 2018 22:58:50 +0100 Merge pull request #1201 from csoler/v0.6-SecurityFixes + 9386bb9 csoler Fri, 2 Mar 2018 22:57:14 +0100 tried to fix FT list bug. + 952ff03 csoler Fri, 2 Mar 2018 21:28:09 +0100 Merge pull request #1200 from csoler/v0.6-SecurityFixes + e88eebf csoler Fri, 2 Mar 2018 21:26:38 +0100 attempt to fix wrong number of rows in DL list + f2a737e csoler Fri, 2 Mar 2018 18:13:09 +0100 fixed font size compilation error + ca402c2 csoler Fri, 2 Mar 2018 18:05:11 +0100 fixed column sizing in FT dialog + efa5827 csoler Fri, 2 Mar 2018 18:01:56 +0100 fixed missing peer availability maps in FT dialog + e5edea3 csoler Thu, 1 Mar 2018 14:10:30 +0100 removed precise and zesty which are obsolete 3d7be85 csoler Thu, 1 Mar 2018 11:46:47 +0100 Merge pull request #1199 from csoler/v0.6-SecurityFixes 026951f csoler Thu, 1 Mar 2018 11:45:54 +0100 added consistency check in getGroupMeta so that ADMIN/PUBLISH flags always correspond to what the key set reflects 028a246 csoler Thu, 1 Mar 2018 09:44:59 +0100 Merge pull request #1197 from csoler/v0.6-SecurityFixes From a89ab8ffa3f491b43fcf01a627f744122b59df7e Mon Sep 17 00:00:00 2001 From: Phenom Date: Sat, 24 Feb 2018 14:07:32 +0100 Subject: [PATCH 030/161] Add ShowEmpty sub menu in Tree Remote SharedFilesDialog. --- .../gui/FileTransfer/SharedFilesDialog.cpp | 111 ++++++----- .../src/gui/FileTransfer/SharedFilesDialog.h | 2 +- retroshare-gui/src/gui/RemoteDirModel.cpp | 185 +++++++++++------- retroshare-gui/src/gui/RemoteDirModel.h | 156 ++++++++------- 4 files changed, 263 insertions(+), 191 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index 30da45723..ae6377120 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -146,7 +146,7 @@ public: /** Constructor */ SharedFilesDialog::SharedFilesDialog(RetroshareDirModel *_tree_model,RetroshareDirModel *_flat_model,QWidget *parent) -: RsAutoUpdatePage(1000,parent),model(NULL) + : RsAutoUpdatePage(1000,parent), model(NULL) { /* Invoke the Qt Designer generated object setup routine */ ui.setupUi(this); @@ -410,7 +410,7 @@ void SharedFilesDialog::changeCurrentViewModel(int viewTypeIndex) ui.dirTreeView->header()->headerDataChanged(Qt::Horizontal, COLUMN_NAME, COLUMN_WN_VISU_DIR) ; // recursRestoreExpandedItems(ui.dirTreeView->rootIndex(),expanded_indexes); - FilterItems(); + FilterItems(); } void LocalSharedFilesDialog::showProperColumns() @@ -491,53 +491,63 @@ void RemoteSharedFilesDialog::spawnCustomPopupMenu( QPoint point ) { if (!rsPeers) return; /* not ready yet! */ + QMenu *contextMenu = new QMenu(this); + QModelIndex idx = ui.dirTreeView->indexAt(point) ; - if (!idx.isValid()) return; - - QModelIndex midx = proxyModel->mapToSource(idx) ; - if (!midx.isValid()) return; - - currentFile = model->data(midx, RetroshareDirModel::FileNameRole).toString() ; - int type = model->getType(midx) ; - if (type != DIR_TYPE_DIR && type != DIR_TYPE_FILE) return; - - - QMenu contextMnu( this ) ; - - collCreateAct->setEnabled(true); - collOpenAct->setEnabled(true); - - QMenu collectionMenu(tr("Collection"), this); - collectionMenu.setIcon(QIcon(IMAGE_LIBRARY)); - collectionMenu.addAction(collCreateAct); - collectionMenu.addAction(collOpenAct); - - QModelIndexList list = ui.dirTreeView->selectionModel()->selectedRows() ; - - if(type == DIR_TYPE_DIR || list.size() > 1) + if (idx.isValid()) { - QAction *downloadActI = new QAction(QIcon(IMAGE_DOWNLOAD), tr( "Download..." ), &contextMnu ) ; - connect( downloadActI , SIGNAL( triggered() ), this, SLOT( downloadRemoteSelectedInteractive() ) ) ; - contextMnu.addAction( downloadActI) ; - } - else - { - QAction *downloadAct = new QAction(QIcon(IMAGE_DOWNLOAD), tr( "Download" ), &contextMnu ) ; - connect( downloadAct , SIGNAL( triggered() ), this, SLOT( downloadRemoteSelected() ) ) ; - contextMnu.addAction( downloadAct) ; + + QModelIndex midx = proxyModel->mapToSource(idx) ; + if (midx.isValid()) + { + + currentFile = model->data(midx, RetroshareDirModel::FileNameRole).toString() ; + int type = model->getType(midx) ; + if ( (type == DIR_TYPE_DIR) || (type == DIR_TYPE_FILE) ) + { + collCreateAct->setEnabled(true); + collOpenAct->setEnabled(true); + + QModelIndexList list = ui.dirTreeView->selectionModel()->selectedRows() ; + + if(type == DIR_TYPE_DIR || list.size() > 1) + { + QAction *downloadActI = new QAction(QIcon(IMAGE_DOWNLOAD), tr( "Download..." ), contextMenu ) ; + connect( downloadActI , SIGNAL( triggered() ), this, SLOT( downloadRemoteSelectedInteractive() ) ) ; + contextMenu->addAction( downloadActI) ; + } + else + { + QAction *downloadAct = new QAction(QIcon(IMAGE_DOWNLOAD), tr( "Download" ), contextMenu ) ; + connect( downloadAct , SIGNAL( triggered() ), this, SLOT( downloadRemoteSelected() ) ) ; + contextMenu->addAction( downloadAct) ; + } + + contextMenu->addSeparator() ;//------------------------------------ + contextMenu->addAction( copylinkAct) ; + contextMenu->addAction( sendlinkAct) ; + contextMenu->addSeparator() ;//------------------------------------ + contextMenu->addAction(QIcon(IMAGE_MSG), tr("Recommend in a message to..."), this, SLOT(recommendFilesToMsg())) ; + + contextMenu->addSeparator() ;//------------------------------------ + + QMenu collectionMenu(tr("Collection"), this); + collectionMenu.setIcon(QIcon(IMAGE_LIBRARY)); + collectionMenu.addAction(collCreateAct); + collectionMenu.addAction(collOpenAct); + contextMenu->addMenu(&collectionMenu) ; + + } + + } } - contextMnu.addSeparator() ;//------------------------------------ - contextMnu.addAction( copylinkAct) ; - contextMnu.addAction( sendlinkAct) ; - contextMnu.addSeparator() ;//------------------------------------ - contextMnu.addAction(QIcon(IMAGE_MSG), tr("Recommend in a message to..."), this, SLOT(recommendFilesToMsg())) ; + contextMenu = model->getContextMenu(contextMenu); + if (!contextMenu->children().isEmpty()) + contextMenu->exec(QCursor::pos()) ; - contextMnu.addSeparator() ;//------------------------------------ - contextMnu.addMenu(&collectionMenu) ; - - contextMnu.exec(QCursor::pos()) ; + delete contextMenu; } QModelIndexList SharedFilesDialog::getSelected() @@ -908,7 +918,7 @@ void SharedFilesDialog::restoreExpandedPathsAndSelection(const std::setmodel()->index(row,0).data(Qt::DisplayRole).toString().toStdString(); recursRestoreExpandedItems(ui.dirTreeView->model()->index(row,0),path,expanded_indexes,hidden_indexes,selected_indexes); } - QItemSelection selection ; + //QItemSelection selection ; ui.dirTreeView->blockSignals(false) ; } @@ -997,12 +1007,12 @@ void SharedFilesDialog::postModDirectories(bool local) flat_model->postMods(); ui.dirTreeView->update() ; - if (ui.filterPatternLineEdit->text().isEmpty() == false) + if (ui.filterPatternLineEdit->text().isEmpty() == false) FilterItems(); - ui.dirTreeView->setSortingEnabled(true); + ui.dirTreeView->setSortingEnabled(true); - restoreExpandedPathsAndSelection(expanded_indexes,hidden_indexes,selected_indexes) ; + restoreExpandedPathsAndSelection(expanded_indexes,hidden_indexes,selected_indexes) ; #ifdef DEBUG_SHARED_FILES_DIALOG std::cerr << "****** updated directories! Re-enabling sorting ******" << std::endl; @@ -1320,10 +1330,10 @@ void SharedFilesDialog::clearFilter() /* clear Filter */ void SharedFilesDialog::startFilter() { - ui.filterStartButton->hide(); - lastFilterString = ui.filterPatternLineEdit->text(); + ui.filterStartButton->hide(); + lastFilterString = ui.filterPatternLineEdit->text(); - FilterItems(); + FilterItems(); } void SharedFilesDialog::updateDirTreeView() @@ -1603,4 +1613,3 @@ bool SharedFilesDialog::tree_FilterItem(const QModelIndex &index, const QString return (visible || visibleChildCount); } - diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h index 6c48ea4c0..982abd275 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h @@ -195,7 +195,7 @@ class RemoteSharedFilesDialog : public SharedFilesDialog private slots: void downloadRemoteSelected(); void downloadRemoteSelectedInteractive(); - void expanded(const QModelIndex& indx); + void expanded(const QModelIndex& indx); }; #endif diff --git a/retroshare-gui/src/gui/RemoteDirModel.cpp b/retroshare-gui/src/gui/RemoteDirModel.cpp index 94cf7c6bf..f6d0df88f 100644 --- a/retroshare-gui/src/gui/RemoteDirModel.cpp +++ b/retroshare-gui/src/gui/RemoteDirModel.cpp @@ -50,21 +50,40 @@ static const size_t FLAT_VIEW_MAX_REFS_TABLE_SIZE = 10000 ; // static const uint32_t FLAT_VIEW_MIN_DELAY_BETWEEN_UPDATES = 120 ; // dont rebuild ref list more than every 2 mins. RetroshareDirModel::RetroshareDirModel(bool mode, QObject *parent) - : QAbstractItemModel(parent), - ageIndicator(IND_ALWAYS), - RemoteMode(mode), nIndex(1), indexSet(1) /* ass zero index cant be used */ + : QAbstractItemModel(parent), _visible(false) + , ageIndicator(IND_ALWAYS) + , RemoteMode(mode)//, nIndex(1), indexSet(1) /* ass zero index cant be used */ + , mLastRemote(false), mLastReq(0), mUpdating(false) { - _visible = false ; #if QT_VERSION < QT_VERSION_CHECK (5, 0, 0) setSupportedDragActions(Qt::CopyAction); #endif treeStyle(); - mDirDetails.ref = (void*)intptr_t(0xffffffff) ; - mLastRemote = false ; - mUpdating = false; + mDirDetails.ref = (void*)intptr_t(0xffffffff) ; } +TreeStyle_RDM::TreeStyle_RDM(bool mode) + : RetroshareDirModel(mode), _showEmpty(false) +{ + _showEmptyAct = new QAction(QIcon(), tr("Show Empty"), this); + _showEmptyAct->setCheckable(true); + _showEmptyAct->setChecked(_showEmpty); + connect(_showEmptyAct, SIGNAL(toggled(bool)), this, SLOT(showEmpty(bool))); +} + +FlatStyle_RDM::FlatStyle_RDM(bool mode) + : RetroshareDirModel(mode), _ref_mutex("Flat file list") +{ + _needs_update = true ; + + { + RS_STACK_MUTEX(_ref_mutex) ; + _last_update = 0 ; + } +} + + // QAbstractItemModel::setSupportedDragActions() was replaced by virtual QAbstractItemModel::supportedDragActions() #if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0) Qt::DropActions RetroshareDirModel::supportedDragActions() const @@ -78,7 +97,6 @@ static bool isNewerThanEpoque(uint32_t ts) return ts > 0 ; // this should be conservative enough } - void RetroshareDirModel::treeStyle() { categoryIcon.addPixmap(QPixmap(":/images/folder16.png"), @@ -97,6 +115,21 @@ void TreeStyle_RDM::updateRef(const QModelIndex& indx) const rsFiles->requestDirUpdate(indx.internalPointer()) ; } +QMenu* TreeStyle_RDM::getContextMenu(QMenu* contextMenu) +{ + if (!contextMenu){ + contextMenu = new QMenu(); + } else { + if (RemoteMode) + contextMenu->addSeparator(); + } + + if (RemoteMode) + contextMenu->addAction( _showEmptyAct) ; + + return contextMenu; +} + bool TreeStyle_RDM::hasChildren(const QModelIndex &parent) const { @@ -171,9 +204,13 @@ int TreeStyle_RDM::rowCount(const QModelIndex &parent) const void *ref = (parent.isValid())? parent.internalPointer() : NULL ; - DirDetails details ; + if ((!ref) && RemoteMode) + _parentRow.clear(); //Only clear it when asking root child number and in remote mode. - if (! requestDirDetails(ref, RemoteMode,details)) + + DirDetails details ; + + if (! requestDirDetails(ref, RemoteMode,details)) { #ifdef RDM_DEBUG std::cerr << "lookup failed -> 0"; @@ -181,7 +218,7 @@ int TreeStyle_RDM::rowCount(const QModelIndex &parent) const #endif return 0; } - if (details.type == DIR_TYPE_FILE) + if (details.type == DIR_TYPE_FILE) { #ifdef RDM_DEBUG std::cerr << "lookup FILE: 0"; @@ -195,9 +232,21 @@ int TreeStyle_RDM::rowCount(const QModelIndex &parent) const std::cerr << "lookup PER/DIR #" << details->count; std::cerr << std::endl; #endif - return details.count; + if ((details.type == DIR_TYPE_ROOT) && !_showEmpty && RemoteMode) + { + DirDetails childDetails; + //Scan all children to know if they are empty. + //And save their real row index + //Prefer do like that than modify requestDirDetails with a new flag (rsFiles->RequestDirDetails) + for(uint64_t i = 0; i < details.count; ++i) + { + if (requestDirDetails(details.children[i].ref, RemoteMode,childDetails) && (childDetails.count > 0)) + _parentRow.push_back(i); + } + return _parentRow.size(); + } + return details.count; } - int FlatStyle_RDM::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); @@ -210,6 +259,7 @@ int FlatStyle_RDM::rowCount(const QModelIndex &parent) const return _ref_entries.size() ; } + int TreeStyle_RDM::columnCount(const QModelIndex &/*parent*/) const { return COLUMN_COUNT; @@ -218,6 +268,7 @@ int FlatStyle_RDM::columnCount(const QModelIndex &/*parent*/) const { return COLUMN_COUNT; } + QString RetroshareDirModel::getFlagsString(FileStorageFlags flags) { char str[11] = "- - -" ; @@ -253,7 +304,6 @@ QString RetroshareDirModel::getGroupsString(FileStorageFlags flags,const std::li return groups_str ; } - QString RetroshareDirModel::getAgeIndicatorString(const DirDetails &details) const { QString ret(""); @@ -496,17 +546,6 @@ QVariant TreeStyle_RDM::displayRole(const DirDetails& details,int coln) const return QVariant(); } /* end of DisplayRole */ -FlatStyle_RDM::FlatStyle_RDM(bool mode) - : RetroshareDirModel(mode), _ref_mutex("Flat file list") -{ - _needs_update = true ; - - { - RS_STACK_MUTEX(_ref_mutex) ; - _last_update = 0 ; - } -} - void FlatStyle_RDM::update() { if(_needs_update) @@ -650,7 +689,6 @@ QVariant TreeStyle_RDM::sortRole(const QModelIndex& /*index*/,const DirDetails& } return QVariant(); } - QVariant FlatStyle_RDM::sortRole(const QModelIndex& /*index*/,const DirDetails& details,int coln) const { /* @@ -678,9 +716,6 @@ QVariant FlatStyle_RDM::sortRole(const QModelIndex& /*index*/,const DirDetails& return QVariant(); } /* end of SortRole */ - - - QVariant RetroshareDirModel::data(const QModelIndex &index, int role) const { #ifdef RDM_DEBUG @@ -771,6 +806,7 @@ QVariant RetroshareDirModel::data(const QModelIndex &index, int role) const return QVariant(); } +/**** //void RetroshareDirModel::getAgeIndicatorRec(const DirDetails &details, QString &ret) const { // if (details.type == DIR_TYPE_FILE) { // ret = getAgeIndicatorString(details); @@ -786,6 +822,7 @@ QVariant RetroshareDirModel::data(const QModelIndex &index, int role) const // } // } //} +****/ QVariant TreeStyle_RDM::headerData(int section, Qt::Orientation orientation, int role) const { @@ -895,8 +932,8 @@ QModelIndex TreeStyle_RDM::index(int row, int column, const QModelIndex & parent std::cerr << ": row:" << row << " col:" << column << " "; #endif - // This function is used extensively. There's no way we can use requestDirDetails() in it, which would - // cause far too much overhead. So we use a dedicated function that only grabs the required information. + // This function is used extensively. There's no way we can use requestDirDetails() in it, which would + // cause far too much overhead. So we use a dedicated function that only grabs the required information. if(row < 0) return QModelIndex() ; @@ -910,15 +947,19 @@ QModelIndex TreeStyle_RDM::index(int row, int column, const QModelIndex & parent } ********/ + //If on root and don't show empty child, get real parent row + if ((!ref) && (!_showEmpty) && RemoteMode && ((size_t)row >= _parentRow.size())) + return QModelIndex(); - void *result ; + int parentRow = ((!ref) && (!_showEmpty) && RemoteMode) ? _parentRow[row] : row ; - if(rsFiles->findChildPointer(ref, row, result, ((RemoteMode) ? RS_FILE_HINTS_REMOTE : RS_FILE_HINTS_LOCAL))) - return createIndex(row, column, result) ; - else + void *result ; + + if(rsFiles->findChildPointer(ref, parentRow, result, ((RemoteMode) ? RS_FILE_HINTS_REMOTE : RS_FILE_HINTS_LOCAL))) + return createIndex(row, column, result) ; + else return QModelIndex(); } - QModelIndex FlatStyle_RDM::index(int row, int column, const QModelIndex & parent) const { Q_UNUSED(parent); @@ -964,10 +1005,10 @@ QModelIndex TreeStyle_RDM::parent( const QModelIndex & index ) const } void *ref = index.internalPointer(); - DirDetails details ; + DirDetails details ; - if (! requestDirDetails(ref, RemoteMode,details)) - { + if (! requestDirDetails(ref, RemoteMode,details)) + { #ifdef RDM_DEBUG std::cerr << "Failed Lookup -> invalid"; std::cerr << std::endl; @@ -975,7 +1016,7 @@ QModelIndex TreeStyle_RDM::parent( const QModelIndex & index ) const return QModelIndex(); } - if (!(details.parent)) + if (!(details.parent)) { #ifdef RDM_DEBUG std::cerr << "success. parent is Root/NULL --> invalid"; @@ -988,9 +1029,10 @@ QModelIndex TreeStyle_RDM::parent( const QModelIndex & index ) const std::cerr << "success index(" << details->prow << ",0," << details->parent << ")"; std::cerr << std::endl; - std::cerr << "Creating index 3 row=" << details.prow << ", column=" << 0 << ", ref=" << (void*)details.parent << std::endl; + std::cerr << "Creating index 3 row=" << details.prow << ", column=" << 0 << ", ref=" << (void*)details.parent << std::endl; #endif - return createIndex(details.prow, COLUMN_NAME, details.parent); + + return createIndex(details.prow, COLUMN_NAME, details.parent); } QModelIndex FlatStyle_RDM::parent( const QModelIndex & index ) const { @@ -1039,7 +1081,7 @@ Qt::ItemFlags RetroshareDirModel::flags( const QModelIndex & index ) const -/* Callback from */ +/* Callback from Core*/ void RetroshareDirModel::preMods() { emit layoutAboutToBeChanged(); @@ -1056,7 +1098,7 @@ void RetroshareDirModel::preMods() #endif } -/* Callback from */ +/* Callback from Core*/ void RetroshareDirModel::postMods() { // emit layoutAboutToBeChanged(); @@ -1075,6 +1117,30 @@ void RetroshareDirModel::postMods() emit layoutChanged(); } +void FlatStyle_RDM::postMods() +{ + time_t now = time(NULL); + + if(_last_update + FLAT_VIEW_MIN_DELAY_BETWEEN_UPDATES > now) + return ; + + if(visible()) + { + emit layoutAboutToBeChanged(); + + { + RS_STACK_MUTEX(_ref_mutex) ; + _ref_stack.clear() ; + _ref_stack.push_back(NULL) ; // init the stack with the topmost parent directory + _ref_entries.clear(); + _last_update = now; + } + QTimer::singleShot(100,this,SLOT(updateRefs())) ; + } + else + _needs_update = true ; +} + bool RetroshareDirModel::requestDirDetails(void *ref, bool remote,DirDetails& d) const { #ifdef RDM_DEBUG @@ -1475,35 +1541,11 @@ int RetroshareDirModel::getType ( const QModelIndex & index ) const return rsFiles->getType(index.internalPointer(),flags); } -FlatStyle_RDM::~FlatStyle_RDM() -{ -} - TreeStyle_RDM::~TreeStyle_RDM() { } -void FlatStyle_RDM::postMods() +FlatStyle_RDM::~FlatStyle_RDM() { - time_t now = time(NULL); - - if(_last_update + FLAT_VIEW_MIN_DELAY_BETWEEN_UPDATES > now) - return ; - - if(visible()) - { - emit layoutAboutToBeChanged(); - - { - RS_STACK_MUTEX(_ref_mutex) ; - _ref_stack.clear() ; - _ref_stack.push_back(NULL) ; // init the stack with the topmost parent directory - _ref_entries.clear(); - _last_update = now; - } - QTimer::singleShot(100,this,SLOT(updateRefs())) ; - } - else - _needs_update = true ; } void FlatStyle_RDM::updateRefs() @@ -1563,3 +1605,8 @@ void FlatStyle_RDM::updateRefs() RetroshareDirModel::postMods() ; } +void TreeStyle_RDM::showEmpty(const bool value) +{ + _showEmpty = value; + update(); +} diff --git a/retroshare-gui/src/gui/RemoteDirModel.h b/retroshare-gui/src/gui/RemoteDirModel.h index fb0272ae3..6b4fe8967 100644 --- a/retroshare-gui/src/gui/RemoteDirModel.h +++ b/retroshare-gui/src/gui/RemoteDirModel.h @@ -25,7 +25,9 @@ #include #include +#include #include +#include #include #include @@ -63,7 +65,7 @@ class RetroshareDirModel : public QAbstractItemModel RetroshareDirModel(bool mode, QObject *parent = 0); virtual ~RetroshareDirModel() {} - Qt::ItemFlags flags ( const QModelIndex & index ) const; + virtual Qt::ItemFlags flags ( const QModelIndex & index ) const; /* Callback from Core */ virtual void preMods(); @@ -82,18 +84,21 @@ class RetroshareDirModel : public QAbstractItemModel void getFileInfoFromIndexList(const QModelIndexList& list, std::list& files_info) ; void openSelected(const QModelIndexList &list); void getFilePaths(const QModelIndexList &list, std::list &fullpaths); - void getFilePath(const QModelIndex& index, std::string& fullpath); - void changeAgeIndicator(uint32_t indicator) { ageIndicator = indicator; } + void getFilePath(const QModelIndex& index, std::string& fullpath); + void changeAgeIndicator(uint32_t indicator) { ageIndicator = indicator; } + + bool requestDirDetails(void *ref, bool remote,DirDetails& d) const; - bool requestDirDetails(void *ref, bool remote,DirDetails& d) const; virtual void update() {} + virtual void updateRef(const QModelIndex&) const =0; - virtual void updateRef(const QModelIndex&) const =0; + virtual QMenu* getContextMenu(QMenu* contextMenu) {return contextMenu;} public: - virtual QMimeData * mimeData ( const QModelIndexList & indexes ) const; + //Overloaded from QAbstractItemModel + virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; virtual QStringList mimeTypes () const; - virtual QVariant data(const QModelIndex &index, int role) const; + virtual QMimeData * mimeData ( const QModelIndexList & indexes ) const; #if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0) virtual Qt::DropActions supportedDragActions() const; #endif @@ -104,10 +109,10 @@ class RetroshareDirModel : public QAbstractItemModel void treeStyle(); void downloadDirectory(const DirDetails & details, int prefixLen); static QString getFlagsString(FileStorageFlags f) ; - static QString getGroupsString(FileStorageFlags flags, const std::list &) ; + static QString getGroupsString(FileStorageFlags flags, const std::list &) ; QString getAgeIndicatorString(const DirDetails &) const; // void getAgeIndicatorRec(const DirDetails &details, QString &ret) const; - static const QIcon& getFlagsIcon(FileStorageFlags flags) ; + static const QIcon& getFlagsIcon(FileStorageFlags flags) ; virtual QVariant displayRole(const DirDetails&,int) const = 0 ; virtual QVariant sortRole(const QModelIndex&,const DirDetails&,int) const =0; @@ -119,46 +124,46 @@ class RetroshareDirModel : public QAbstractItemModel QIcon categoryIcon; QIcon peerIcon; - class RemoteIndex - { - public: - RemoteIndex() {} - RemoteIndex(std::string in_person, - std::string in_path, - int in_idx, - int in_row, - int in_column, - std::string in_name, - int in_size, - int in_type, - int in_ts, int in_rank) - :id(in_person), path(in_path), parent(in_idx), - row(in_row), column(in_column), - name(in_name), size(in_size), - type(in_type), timestamp(in_ts), rank(in_rank) - { - return; - } - - std::string id; - std::string path; - int parent; - int row; - int column; - - /* display info */ - std::string name; - int size; - int type; - int timestamp; - int rank; - - }; + //class RemoteIndex + //{ + // public: + // RemoteIndex() {} + // RemoteIndex(std::string in_person, + // std::string in_path, + // int in_idx, + // int in_row, + // int in_column, + // std::string in_name, + // int in_size, + // int in_type, + // int in_ts, int in_rank) + // :id(in_person), path(in_path), parent(in_idx), + // row(in_row), column(in_column), + // name(in_name), size(in_size), + // type(in_type), timestamp(in_ts), rank(in_rank) + // { + // return; + // } + // + // std::string id; + // std::string path; + // int parent; + // int row; + // int column; + // + // /* display info */ + // std::string name; + // int size; + // int type; + // int timestamp; + // int rank; + // + //}; bool RemoteMode; - mutable int nIndex; - mutable std::vector indexSet; + //mutable int nIndex; + //mutable std::vector indexSet; // This material attempts to keep last request in cache, with no search cost. @@ -178,28 +183,36 @@ class TreeStyle_RDM: public RetroshareDirModel Q_OBJECT public: - TreeStyle_RDM(bool mode) - : RetroshareDirModel(mode) - { - } - + TreeStyle_RDM(bool mode); virtual ~TreeStyle_RDM() ; protected: - virtual void updateRef(const QModelIndex&) const ; + + //Overloaded from RetroshareDirModel virtual void update() ; - - /* These are all overloaded Virtual Functions */ - virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; - virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; - - virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + virtual void updateRef(const QModelIndex&) const ; + virtual QMenu* getContextMenu(QMenu* contextMenu) ; virtual QVariant displayRole(const DirDetails&,int) const ; virtual QVariant sortRole(const QModelIndex&,const DirDetails&,int) const ; + //Overloaded from QAbstractItemModel virtual QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex() ) const; virtual QModelIndex parent ( const QModelIndex & index ) const; + + virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; + virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; virtual bool hasChildren(const QModelIndex & parent = QModelIndex()) const; + + virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + + private slots: + void showEmpty(const bool value); + + private: + QAction *_showEmptyAct; + bool _showEmpty; + protected: + mutable std::vector _parentRow ; // used to store the real parent row for non empty child }; // This class shows a flat list of all shared files @@ -212,9 +225,9 @@ class FlatStyle_RDM: public RetroshareDirModel public: FlatStyle_RDM(bool mode); - virtual ~FlatStyle_RDM() ; + //Overloaded from RetroshareDirModel virtual void update() ; bool isMaxRefsTableSize(size_t* maxSize = NULL); @@ -223,27 +236,30 @@ class FlatStyle_RDM: public RetroshareDirModel void updateRefs() ; protected: - virtual void updateRef(const QModelIndex&) const {} - virtual void postMods(); - - virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; - virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; - - virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + //Overloaded from RetroshareDirModel + virtual void postMods();/* Callback from Core */ + virtual void updateRef(const QModelIndex&) const {} virtual QVariant displayRole(const DirDetails&,int) const ; virtual QVariant sortRole(const QModelIndex&,const DirDetails&,int) const ; + //Overloaded from QAbstractItemModel virtual QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex() ) const; virtual QModelIndex parent ( const QModelIndex & index ) const; + + virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; + virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; virtual bool hasChildren(const QModelIndex & parent = QModelIndex()) const; + virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + + QString computeDirectoryPath(const DirDetails& details) const ; - mutable RsMutex _ref_mutex ; - std::vector _ref_entries ;// used to store the refs to display - std::vector _ref_stack ; // used to store the refs to update + mutable RsMutex _ref_mutex ; + std::vector _ref_entries ; // used to store the refs to display + std::vector _ref_stack ; // used to store the refs to update bool _needs_update ; - time_t _last_update ; + time_t _last_update ; }; From 4e6f2ae702ccbb5d3fa2db7f33ef5b426d54d424 Mon Sep 17 00:00:00 2001 From: Phenom Date: Sun, 4 Mar 2018 14:31:16 +0100 Subject: [PATCH 031/161] Fix Icon for About page. --- retroshare-gui/src/gui/settings/AboutPage.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/settings/AboutPage.h b/retroshare-gui/src/gui/settings/AboutPage.h index 3f41750e0..f9d84e5e5 100644 --- a/retroshare-gui/src/gui/settings/AboutPage.h +++ b/retroshare-gui/src/gui/settings/AboutPage.h @@ -39,7 +39,7 @@ public: /** Loads the settings for this page */ virtual void load(); - virtual QPixmap iconPixmap() const { return QPixmap(":/icons/settings/sound.svg") ; } + virtual QPixmap iconPixmap() const { return QPixmap(":/icons/svg/info.svg") ; } virtual QString pageName() const { return tr("About") ; } virtual QString helpText() const { return ""; } From 51cc94bfa7c62a05ae5179223892e6e8184838be Mon Sep 17 00:00:00 2001 From: Phenom Date: Sun, 4 Mar 2018 17:31:40 +0100 Subject: [PATCH 032/161] Fix ChatWidget current text edit color when changing appearance style. Don't change already typed text, who wants to change style while typping? --- retroshare-gui/src/gui/chat/ChatWidget.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index a8e95dabf..2859527cd 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -474,6 +474,7 @@ uint32_t ChatWidget::maxMessageSize() bool ChatWidget::eventFilter(QObject *obj, QEvent *event) { + //QEvent::Type type = event->type(); if (obj == ui->textBrowser || obj == ui->textBrowser->viewport() || obj == ui->leSearch || obj == ui->chatTextEdit) { if (event->type() == QEvent::KeyPress) { @@ -673,6 +674,17 @@ bool ChatWidget::eventFilter(QObject *obj, QEvent *event) } } } + if (event->type() == QEvent::StyleChange) + { + QString colorName = currentColor.name(); + qreal desiredContrast = Settings->valueFromGroup("Chat", "MinimumContrast", 4.5).toDouble(); + QColor backgroundColor = ui->chatTextEdit->palette().base().color(); + RsHtml::findBestColor(colorName, backgroundColor, desiredContrast); + + currentColor = QColor(colorName); + ui->chatTextEdit->setTextColor(currentColor); + colorChanged(); + } } else if (obj == ui->leSearch) { if (event->type() == QEvent::KeyPress) { From 6877589baf7fffafe38b0c833e9f8715d99259ca Mon Sep 17 00:00:00 2001 From: Phenom Date: Sun, 4 Mar 2018 18:19:18 +0100 Subject: [PATCH 033/161] Add description in GroupTreeWidget tooltip. --- retroshare-gui/src/gui/common/GroupTreeWidget.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/common/GroupTreeWidget.cpp b/retroshare-gui/src/gui/common/GroupTreeWidget.cpp index 46e083673..fc1cfae19 100644 --- a/retroshare-gui/src/gui/common/GroupTreeWidget.cpp +++ b/retroshare-gui/src/gui/common/GroupTreeWidget.cpp @@ -481,12 +481,14 @@ void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList< tooltip += "\n" + QString::number(itemInfo.max_visible_posts) + " messages available" ; // if(itemInfo.max_visible_posts) // wtf? this=0 when there are some posts definitely exist - lastpost is recent if(itemInfo.lastpost == QDateTime::fromTime_t(0)) - tooltip += "\n" + tr("Last Post") + ": " + tr("Never") ; + tooltip += "\n" + tr("Last Post") + ": " + tr("Never") ; else tooltip += "\n" + tr("Last Post") + ": " + DateTime::formatLongDateTime(itemInfo.lastpost) ; if(!IS_GROUP_SUBSCRIBED(itemInfo.subscribeFlags)) tooltip += "\n" + tr("Subscribe to download and read messages") ; + tooltip += "\n" + tr("Description") + ": " + itemInfo.description; + item->setToolTip(COLUMN_NAME, tooltip); item->setToolTip(COLUMN_UNREAD, tooltip); item->setToolTip(COLUMN_POPULARITY, tooltip); From 8b42873968bdc6bd5cdd92949c51f4d3de697d6c Mon Sep 17 00:00:00 2001 From: Phenom Date: Sun, 4 Mar 2018 22:45:11 +0100 Subject: [PATCH 034/161] Fix Friend Avatar status overlay no depends size scale. --- retroshare-gui/src/gui/common/FriendList.cpp | 23 ++++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/retroshare-gui/src/gui/common/FriendList.cpp b/retroshare-gui/src/gui/common/FriendList.cpp index 726bf61ea..82f416909 100644 --- a/retroshare-gui/src/gui/common/FriendList.cpp +++ b/retroshare-gui/src/gui/common/FriendList.cpp @@ -496,22 +496,21 @@ void FriendList::groupsChanged() static QIcon createAvatar(const QPixmap &avatar, const QPixmap &overlay) { - int avatarWidth = avatar.width(); - int avatarHeight = avatar.height(); + int avatarWidth = avatar.width(); + int avatarHeight = avatar.height(); - QPixmap pixmap(avatar); + QPixmap pixmap(avatar); - int overlayWidth = avatarWidth / 2.5; - int overlayHeight = avatarHeight / 2.5; - int overlayX = avatarWidth - overlayWidth; - int overlayY = avatarHeight - overlayHeight; + int overlaySize = (avatarWidth > avatarHeight) ? (avatarWidth/2.5) : (avatarHeight/2.5); + int overlayX = avatarWidth - overlaySize; + int overlayY = avatarHeight - overlaySize; - QPainter painter(&pixmap); - painter.drawPixmap(overlayX, overlayY, overlayWidth, overlayHeight, overlay); + QPainter painter(&pixmap); + painter.drawPixmap(overlayX, overlayY, overlaySize, overlaySize, overlay); - QIcon icon; - icon.addPixmap(pixmap); - return icon; + QIcon icon; + icon.addPixmap(pixmap); + return icon; } static void getNameWidget(QTreeWidget *treeWidget, QTreeWidgetItem *item, ElidedLabel *&nameLabel, ElidedLabel *&textLabel) From c92b86017486dc676f4d738c402ef29fa5168ebf Mon Sep 17 00:00:00 2001 From: thunder2 Date: Sun, 4 Mar 2018 20:08:15 +0100 Subject: [PATCH 035/161] Windows build environment: - Updated external libraries - Added compile of plugins to build.bat - Added copy of Qt style DLL to pack.bat and Windows Installer - Removed "CONFIG=console" for release build --- build_scripts/Windows/build-libs/Makefile | 23 +++++++++--------- .../build-libs/libxslt-1.1.28-fix.tar.gz | Bin 3009 -> 0 bytes build_scripts/Windows/build/build.bat | 2 +- build_scripts/Windows/build/pack.bat | 6 +++++ .../Windows/env/tools/prepare-tools.bat | 6 ++--- .../Windows/installer/retroshare-Qt5.nsi | 8 ++++-- plugins/VOIP/VOIP.pro | 4 +-- retroshare-gui/src/retroshare-gui.pro | 2 ++ 8 files changed, 31 insertions(+), 20 deletions(-) delete mode 100755 build_scripts/Windows/build-libs/libxslt-1.1.28-fix.tar.gz diff --git a/build_scripts/Windows/build-libs/Makefile b/build_scripts/Windows/build-libs/Makefile index 11b60f77d..4d7339149 100755 --- a/build_scripts/Windows/build-libs/Makefile +++ b/build_scripts/Windows/build-libs/Makefile @@ -1,17 +1,17 @@ ZLIB_VERSION=1.2.3 BZIP2_VERSION=1.0.6 MINIUPNPC_VERSION=2.0 -OPENSSL_VERSION=1.0.2k +OPENSSL_VERSION=1.0.2n SPEEX_VERSION=1.2.0 SPEEXDSP_VERSION=1.2rc3 -OPENCV_VERSION=3.2.0 -LIBXML2_VERSION=2.9.4 -LIBXSLT_VERSION=1.1.28 -CURL_VERSION=7.53.1 +OPENCV_VERSION=3.4.1 +LIBXML2_VERSION=2.9.7 +LIBXSLT_VERSION=1.1.32 +CURL_VERSION=7.58.0 TCL_VERSION=8.6.2 SQLCIPHER_VERSION=2.2.1 -LIBMICROHTTPD_VERSION=0.9.52 -FFMPEG_VERSION=3.2.4 +LIBMICROHTTPD_VERSION=0.9.59 +FFMPEG_VERSION=3.4 MAKEFILE_PATH=$(dir $(MAKEFILE_LIST)) LIBS_PATH?=$(MAKEFILE_PATH)../../../../libs @@ -137,7 +137,7 @@ libs/openssl-$(OPENSSL_VERSION): $(DOWNLOAD_PATH)/openssl-$(OPENSSL_VERSION).tar speex: libs/speex-$(SPEEX_VERSION) $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz: - wget http://downloads.xiph.org/releases/speex/speex-$(SPEEX_VERSION).tar.gz -O $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz + wget --no-check-certificate http://downloads.xiph.org/releases/speex/speex-$(SPEEX_VERSION).tar.gz -O $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz libs/speex-$(SPEEX_VERSION): $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz # prepare @@ -158,7 +158,7 @@ libs/speex-$(SPEEX_VERSION): $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz speexdsp: libs/speexdsp-$(SPEEXDSP_VERSION) $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz: - wget http://downloads.xiph.org/releases/speex/speexdsp-$(SPEEXDSP_VERSION).tar.gz -O $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz + wget --no-check-certificate http://downloads.xiph.org/releases/speex/speexdsp-$(SPEEXDSP_VERSION).tar.gz -O $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz libs/speexdsp-$(SPEEXDSP_VERSION): $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz # prepare @@ -187,7 +187,7 @@ libs/opencv-$(OPENCV_VERSION): $(DOWNLOAD_PATH)/opencv-$(OPENCV_VERSION).tar.gz # build mkdir -p opencv-$(OPENCV_VERSION)/build #cd opencv-$(OPENCV_VERSION)/build && cmake .. -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX="`pwd`/../../libs" - cd opencv-$(OPENCV_VERSION)/build && cmake .. -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX="`pwd`/install" + cd opencv-$(OPENCV_VERSION)/build && cmake .. -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DBUILD_SHARED_LIBS=OFF -DENABLE_CXX11=ON -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -DSTRSAFE_NO_DEPRECATE" -DCMAKE_INSTALL_PREFIX="`pwd`/install" cd opencv-$(OPENCV_VERSION)/build && make install # copy files mkdir -p libs/opencv-$(OPENCV_VERSION).tmp/include @@ -228,7 +228,6 @@ libs/libxslt-$(LIBXSLT_VERSION): $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar rm -r -f libs/libxslt-* tar xvf $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz tar xvf $(DOWNLOAD_PATH)/libxslt-$(LIBXSLT_VERSION).tar.gz - tar xvf $(MAKEFILE_PATH)libxslt-$(LIBXSLT_VERSION)-fix.tar.gz # build cd libxslt-$(LIBXSLT_VERSION) && ./configure --with-libxml-src=../libxml2-$(LIBXML2_VERSION) -enable-shared=no CFLAGS=-DLIBXML_STATIC cd libxslt-$(LIBXSLT_VERSION) && make @@ -245,7 +244,7 @@ libs/libxslt-$(LIBXSLT_VERSION): $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar curl: libs/curl-$(CURL_VERSION) $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz: - wget --no-check-certificate http://curl.haxx.se/$(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz -O $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz + wget --no-check-certificate http://curl.haxx.se/download/curl-$(CURL_VERSION).tar.gz -O $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz libs/curl-$(CURL_VERSION): $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz # prepare diff --git a/build_scripts/Windows/build-libs/libxslt-1.1.28-fix.tar.gz b/build_scripts/Windows/build-libs/libxslt-1.1.28-fix.tar.gz deleted file mode 100755 index af1bada8f0069b853007b5cff8a0c0818ca040fc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3009 zcmV;y3qJH8iwFQJxh_-y1MM1XQ`<CUG4Y$~MWSZns>* z8U+=y*zDnHa*|fe8;8vcz-jn;{F$pFB=D|_#gg(|6jM?>=yq2Ga$$R9-RiR>EG9HZ+7>k|94rOw41x_H*2K%0((CS|L6X1Y?sM4iQIia`m?!3XVmsJ-*jx^ z7{tG(yl6AQ>nRLH=fc43ws&HADw(cyrc}>H{{rZDXozwpDwGVgLQ*xHnRKo%x!@V7BeG0a~vPF#h_*{D1D)En|eqKZ|ytKC<*TtRX4L@z^r2UDL+kjY5Axp1lBk>8nj)b;K)o!$_aYWj(3(ZihgQ$X

i~45**?dI7&u_Wt1zHGq0LB`F0ie3vaAQ$q`q${mrx<>@I_14%5D7FSG$iN6m-U-0mz1-ltK2%KUd@QO z(2*{BJAB=T^6|lPt{@dERk02j;MGbNb(KJpooN8U;m*X+ASLu)?G4zehGc;$8okw8uhXjTZ*pd@; z7i=#$d_Me=%Nm;BYMhw?2E8%NZ2!u!!GKl3l}(V(RQB0Ct`BMrVs4G+>4)!-r83o*fqZoPiq9G73PRu88vCd3vZR%kn#L0oTtS9HD_? z?+xcv%x*iKz_IZ(N9@>?DqNgrk1V%${k!M+oV=R?DIjS!zb^9SFv@R|rVb3}cYY!| zvaE@t3ygGgIyDVAGsXFQJw6UV(W z^&f72gNd=s{>^3f=UV)Q9ncIN@G_8b^&zjSe#qqqo5pbTu(cTvl)2_#i)g}M(bsGk z6mAVCJhsU0FFiQ_LcL*>crB?BFf9ay=LGJQF|8P|102yICaGVq*Q>0${&IFIui?k% z$L9xN^OK?ly zmh{}*y6MuX@3{9dx!NFC*nt#j`S%TR6(|!jZq16CC+OB{`S-k}{C+c<^hdCD$Vog` z6fSkopUfPcR#nL&sbUg|9|}f`6|yY1f5y4|0)$>Rg_IsCxB?|BaF|Mv$!`8NH7h1! zEMc@^NQ+ot2oMRG=SFr!%sF-`LBe^gE4P*WB9yIjCQK6|f~%xla5fo!s_XC@PEHlS z;go%9gWZg3KY3sW1H0(2_C~Z-qUD4E#uGsr0}U{7Ozy3B_tA;MQ_ zeN*`!VdrYy*cP34n}3%I+3ODVuxQaclR2JUd4Znn42si$AZzen9`$3H^}FRKUum#x zr{;Mzres>nm7E)TH*$egIK#^-g>*RIPTbtaMN7s41Vf2OgM6hrG0NUNU8Sjnx=@Cs zjb=B17~dT>$&^Ym_!-O<3s+Y;&@UfdBXM)bR(>_(tvIn>4o?Lmm-3+l=YcDpc0QF8 zDK9mq5yv#e8G?_s5mAjDfGJF49)7KzX*ZO~#YYpDTIS5O@eTe70!$M(RD^*2H8j7; zNTO*N4kN&n7lUYDJSvK-H01AEy^{Urm*UQA;SY?tYFWq z%V8hMV5diFIC~4rO05FgN^GR4;zm(nX4p{%%hnb$;a}pAi;Tns#R|F0(>4)~#0h+y z3Hb+Uk-@A!b3xU3XXj^36Odu&Et`3dunaN1K-iJ*>cPAcY4{N8Sz#m|7z#ot6#tJu z%B2b??ENRutjhb&@ub@sjfsL@BjM@>4t^U|HU+FYW<^r4I<&bNJ^Z`*+tJZhV+$5T z4=8V2zpWo_{o34$)3lN1V5?eHzAG-TOd;(*8+w#3*1ufRxdFjJY1LTC~d8L!BnaPcGD@klqpVbRleuVVT8gImft5hc4 z2_v7RnqgKtCL4w5iz^L0kBmEFqf*+=CAH&;Zug+%Pl_VlYEr#sNVWVMl60&2mQI7$ z_$ljIg(6lnU!p+piyU@&!L)m+D)A%hvHB_PuT_o*Lcf={_fxC# zkN)H00tzUgfC36Apnw7jD4>7>3Mim}0tzUgfC36Apnw7jD4>7>{xtX>GilDK08jt` DKW^3- diff --git a/build_scripts/Windows/build/build.bat b/build_scripts/Windows/build/build.bat index 5367efa97..d4f2ab7ba 100644 --- a/build_scripts/Windows/build/build.bat +++ b/build_scripts/Windows/build/build.bat @@ -52,7 +52,7 @@ echo. title Build - %SourceName%-%RsBuildConfig% [qmake] -qmake "%SourcePath%\RetroShare.pro" -r "CONFIG+=%RsBuildConfig% version_detail_bash_script rs_autologin" +qmake "%SourcePath%\RetroShare.pro" -r "CONFIG+=%RsBuildConfig% version_detail_bash_script rs_autologin retroshare_plugins" if errorlevel 1 goto error echo. diff --git a/build_scripts/Windows/build/pack.bat b/build_scripts/Windows/build/pack.bat index d52377d9f..49dff68b3 100644 --- a/build_scripts/Windows/build/pack.bat +++ b/build_scripts/Windows/build/pack.bat @@ -128,6 +128,12 @@ if "%QtMainVersion%"=="5" ( copy "%QtPath%\..\plugins\audio\qtaudio_windows.dll" "%RsDeployPath%\audio" %Quite% ) +if exist "%QtPath%\..\plugins\styles\qwindowsvistastyle.dll" ( + echo Copy styles + mkdir "%RsDeployPath%\styles" %Quite% + copy "%QtPath%\..\plugins\styles\qwindowsvistastyle.dll" "%RsDeployPath%\styles" %Quite% +) + copy "%QtPath%\..\plugins\imageformats\*.dll" "%RsDeployPath%\imageformats" %Quite% del /Q "%RsDeployPath%\imageformats\*d?.dll" %Quite% diff --git a/build_scripts/Windows/env/tools/prepare-tools.bat b/build_scripts/Windows/env/tools/prepare-tools.bat index 94ad1ae8b..2295d0ca3 100644 --- a/build_scripts/Windows/env/tools/prepare-tools.bat +++ b/build_scripts/Windows/env/tools/prepare-tools.bat @@ -8,7 +8,7 @@ set SevenZipUrl=http://7-zip.org/a/7z1602.msi set SevenZipInstall=7z1602.msi ::set CurlUrl=https://bintray.com/artifact/download/vszakats/generic/curl-7.50.1-win32-mingw.7z ::set CurlInstall=curl-7.50.1-win32-mingw.7z -set WgetUrl=https://eternallybored.org/misc/wget/current/wget.exe +set WgetUrl=https://eternallybored.org/misc/wget/1.19.4/32/wget.exe set WgetInstall=wget.exe set JomUrl=http://download.qt.io/official_releases/jom/jom.zip set JomInstall=jom.zip @@ -118,7 +118,7 @@ if not exist "%EnvToolsPath%\cut.exe" ( %cecho% info "Download Unix Tools installation" if not exist "%EnvDownloadPath%\%UnixToolsInstall%" call "%ToolsPath%\winhttpjs.bat" %UnixToolsUrl% -saveTo "%EnvDownloadPath%\%UnixToolsInstall%" - if not exist "%EnvDownloadPath%\%UnixToolsInstall%" %cecho% error ""Cannot download Unix Tools installation" & goto error + if not exist "%EnvDownloadPath%\%UnixToolsInstall%" %cecho% error "Cannot download Unix Tools installation" & goto error %cecho% info "Unpack Unix Tools" "%EnvSevenZipExe%" x -o"%EnvTempPath%" "%EnvDownloadPath%\%UnixToolsInstall%" @@ -134,7 +134,7 @@ if not exist "%EnvToolsPath%\sed.exe" ( %cecho% info "Download Unix Tools installation" if not exist "%EnvDownloadPath%\%UnixToolsInstall%" call "%ToolsPath%\winhttpjs.bat" %UnixToolsUrl% -saveTo "%EnvDownloadPath%\%UnixToolsInstall%" - if not exist "%EnvDownloadPath%\%UnixToolsInstall%" %cecho% error ""Cannot download Unix Tools installation" & goto error + if not exist "%EnvDownloadPath%\%UnixToolsInstall%" %cecho% error "Cannot download Unix Tools installation" & goto error %cecho% info "Unpack Unix Tools" "%EnvSevenZipExe%" x -o"%EnvTempPath%" "%EnvDownloadPath%\%UnixToolsInstall%" diff --git a/build_scripts/Windows/installer/retroshare-Qt5.nsi b/build_scripts/Windows/installer/retroshare-Qt5.nsi index d847cc3ae..94dae3603 100644 --- a/build_scripts/Windows/installer/retroshare-Qt5.nsi +++ b/build_scripts/Windows/installer/retroshare-Qt5.nsi @@ -195,11 +195,15 @@ Section $(Section_Main) Section_Main ; Qt audio SetOutPath "$INSTDIR\audio" - File /r "${QTDIR}\plugins\audio\qtaudio_windows.dll" + File "${QTDIR}\plugins\audio\qtaudio_windows.dll" ; Qt platforms SetOutPath "$INSTDIR\platforms" - File /r "${QTDIR}\plugins\platforms\qwindows.dll" + File "${QTDIR}\plugins\platforms\qwindows.dll" + + ; Qt styles + SetOutPath "$INSTDIR\styles" + File /NONFATAL "${QTDIR}\plugins\styles\qwindowsvistastyle.dll" ; MinGW binaries SetOutPath "$INSTDIR" diff --git a/plugins/VOIP/VOIP.pro b/plugins/VOIP/VOIP.pro index c178332a0..ccf7f00f9 100644 --- a/plugins/VOIP/VOIP.pro +++ b/plugins/VOIP/VOIP.pro @@ -35,7 +35,7 @@ win32 { DEPENDPATH += . $$INC_DIR INCLUDEPATH += . $$INC_DIR - OPENCV_VERSION = "320" + OPENCV_VERSION = "341" USE_PRECOMPILED_LIBS = for(lib, LIB_DIR) { #message(Scanning $$lib) @@ -76,7 +76,7 @@ win32 { message(Use system opencv libraries.) LIBS += -lopencv_core -lopencv_highgui -lopencv_imgproc } - LIBS += -lz -lole32 -loleaut32 -luuid -lvfw32 -llibjpeg -llibtiff -llibpng -llibjasper -lIlmImf + LIBS += -lzlib -lole32 -loleaut32 -luuid -lvfw32 -llibjpeg -llibtiff -llibpng -llibjasper -lIlmImf LIBS += -lavifil32 -lavicap32 -lavcodec -lavutil -lswresample } diff --git a/retroshare-gui/src/retroshare-gui.pro b/retroshare-gui/src/retroshare-gui.pro index 2fa5aafca..7a6d8d2a9 100644 --- a/retroshare-gui/src/retroshare-gui.pro +++ b/retroshare-gui/src/retroshare-gui.pro @@ -169,6 +169,8 @@ win32 { CONFIG(debug, debug|release) { # show console output CONFIG += console + } else { + CONFIG -= console } # Switch on extra warnings From f5a3b26199e7024c326d35f2d872e25e101c8177 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Fri, 9 Mar 2018 20:26:29 +0100 Subject: [PATCH 036/161] More omogeneous variable naming in android build tools --- README-Android.asciidoc | 4 ++-- android-prepare-toolchain.sh | 18 +++++++++--------- retroshare.pri | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README-Android.asciidoc b/README-Android.asciidoc index 0422b8796..6f1f783e9 100644 --- a/README-Android.asciidoc +++ b/README-Android.asciidoc @@ -31,7 +31,7 @@ export ANDROID_NDK_PATH="/opt/android-ndk/" ## The path where your fresh compiled toolchain will be installed, make sure ## the parent exists -export NDK_TOOLCHAIN_PATH="${HOME}/Builds/android-toolchains/retroshare-android/" +export NATIVE_LIBS_TOOLCHAIN_PATH="${HOME}/Builds/android-toolchains/retroshare-android/" ## The CPU architecture of the Android device you want to target export ANDROID_NDK_ARCH="arm" @@ -66,7 +66,7 @@ _Qt Creator left pane -> Projects -> Build and Run -> Android SOMESTUFF kit -> Build Environement -> Add Variable: +NATIVE_LIBS_TOOLCHAIN_PATH+ -Value: +Same value as NDK_TOOLCHAIN_PATH in Preparing The Environement step+ +Value: +Same value as NATIVE_LIBS_TOOLCHAIN_PATH in Preparing The Environement step+ Some of RetroShare modules like +retroshare-gui+ and +WebUI+ are not available on Android so to be able to compile RetroShare without errors you will have to diff --git a/android-prepare-toolchain.sh b/android-prepare-toolchain.sh index 4c6745c1d..3863a65ae 100755 --- a/android-prepare-toolchain.sh +++ b/android-prepare-toolchain.sh @@ -5,7 +5,7 @@ [ -z ${ANDROID_NDK_ARCH+x} ] && export ANDROID_NDK_ARCH="arm" [ -z ${ANDROID_NDK_ABI_VER+x} ] && export ANDROID_NDK_ABI_VER="4.9" [ -z ${ANDROID_PLATFORM_VER+x} ] && export ANDROID_PLATFORM_VER="18" -[ -z ${NDK_TOOLCHAIN_PATH+x} ] && export NDK_TOOLCHAIN_PATH="${HOME}/Builds/android-toolchains/retroshare-android-${ANDROID_PLATFORM_VER}-${ANDROID_NDK_ARCH}-abi${ANDROID_NDK_ABI_VER}/" +[ -z ${NATIVE_LIBS_TOOLCHAIN_PATH+x} ] && export NATIVE_LIBS_TOOLCHAIN_PATH="${HOME}/Builds/android-toolchains/retroshare-android-${ANDROID_PLATFORM_VER}-${ANDROID_NDK_ARCH}-abi${ANDROID_NDK_ABI_VER}/" [ -z ${HOST_NUM_CPU+x} ] && export HOST_NUM_CPU=$(grep "^processor" /proc/cpuinfo | wc -l) [ -z ${BZIP2_SOURCE_VERSION+x} ] && export BZIP2_SOURCE_VERSION="1.0.6" [ -z ${OPENSSL_SOURCE_VERSION+x} ] && export OPENSSL_SOURCE_VERSION="1.0.2n" @@ -23,21 +23,21 @@ else cArch="${ANDROID_NDK_ARCH}" eABI="eabi" fi -export SYSROOT="${NDK_TOOLCHAIN_PATH}/sysroot" +export SYSROOT="${NATIVE_LIBS_TOOLCHAIN_PATH}/sysroot" export PREFIX="${SYSROOT}" -export CC="${NDK_TOOLCHAIN_PATH}/bin/${cArch}-linux-android${eABI}-gcc" -export CXX="${NDK_TOOLCHAIN_PATH}/bin/${cArch}-linux-android${eABI}-g++" -export AR="${NDK_TOOLCHAIN_PATH}/bin/${cArch}-linux-android${eABI}-ar" -export RANLIB="${NDK_TOOLCHAIN_PATH}/bin/${cArch}-linux-android${eABI}-ranlib" +export CC="${NATIVE_LIBS_TOOLCHAIN_PATH}/bin/${cArch}-linux-android${eABI}-gcc" +export CXX="${NATIVE_LIBS_TOOLCHAIN_PATH}/bin/${cArch}-linux-android${eABI}-g++" +export AR="${NATIVE_LIBS_TOOLCHAIN_PATH}/bin/${cArch}-linux-android${eABI}-ar" +export RANLIB="${NATIVE_LIBS_TOOLCHAIN_PATH}/bin/${cArch}-linux-android${eABI}-ranlib" export ANDROID_DEV="${ANDROID_NDK_PATH}/platforms/android-${ANDROID_PLATFORM_VER}/arch-${ANDROID_NDK_ARCH}/usr" ## More information available at https://android.googlesource.com/platform/ndk/+/ics-mr0/docs/STANDALONE-TOOLCHAIN.html build_toolchain() { - rm -rf ${NDK_TOOLCHAIN_PATH} + rm -rf ${NATIVE_LIBS_TOOLCHAIN_PATH} [ "${ANDROID_NDK_ARCH}" == "x86" ] && toolchainName="${ANDROID_NDK_ARCH}-${ANDROID_NDK_ABI_VER}" || toolchainName="${ANDROID_NDK_ARCH}-linux-androideabi-${ANDROID_NDK_ABI_VER}" - ${ANDROID_NDK_PATH}/build/tools/make-standalone-toolchain.sh --ndk-dir=${ANDROID_NDK_PATH} --arch=${ANDROID_NDK_ARCH} --install-dir=${NDK_TOOLCHAIN_PATH} --platform=android-${ANDROID_PLATFORM_VER} --toolchain=${toolchainName} --verbose + ${ANDROID_NDK_PATH}/build/tools/make-standalone-toolchain.sh --ndk-dir=${ANDROID_NDK_PATH} --arch=${ANDROID_NDK_ARCH} --install-dir=${NATIVE_LIBS_TOOLCHAIN_PATH} --platform=android-${ANDROID_PLATFORM_VER} --toolchain=${toolchainName} --verbose } ## More information available at retroshare://file?name=Android%20Native%20Development%20Kit%20Cookbook.pdf&size=29214468&hash=0123361c1b14366ce36118e82b90faf7c7b1b136 @@ -164,4 +164,4 @@ build_sqlite build_sqlcipher build_libupnp -echo NDK_TOOLCHAIN_PATH=${NDK_TOOLCHAIN_PATH} +echo NATIVE_LIBS_TOOLCHAIN_PATH=${NATIVE_LIBS_TOOLCHAIN_PATH} diff --git a/retroshare.pri b/retroshare.pri index 54fd2da11..446c1bd61 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -129,7 +129,7 @@ android-* { CONFIG -= libresapihttpserver upnp_miniupnpc QT *= androidextras INCLUDEPATH += $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/include - LIBS *= -L$$NDK_TOOLCHAIN_PATH/sysroot/usr/lib/ + LIBS *= -L$$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/ } win32 { From 2b300eb9b0eb54d712a2cf5227ed3e165add78c9 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 10 Mar 2018 00:44:24 +0100 Subject: [PATCH 037/161] Update version in Android Manifest --- retroshare-qml-app/src/android/AndroidManifest.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-qml-app/src/android/AndroidManifest.xml b/retroshare-qml-app/src/android/AndroidManifest.xml index 0a247cce7..79771eb2f 100644 --- a/retroshare-qml-app/src/android/AndroidManifest.xml +++ b/retroshare-qml-app/src/android/AndroidManifest.xml @@ -1,7 +1,7 @@ Date: Sat, 10 Mar 2018 13:15:53 +0100 Subject: [PATCH 038/161] Remove android JNI .class from source --- .../android/qml_app/jni/NativeCalls.class | Bin 281 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 retroshare-qml-app/src/android/src/org/retroshare/android/qml_app/jni/NativeCalls.class diff --git a/retroshare-qml-app/src/android/src/org/retroshare/android/qml_app/jni/NativeCalls.class b/retroshare-qml-app/src/android/src/org/retroshare/android/qml_app/jni/NativeCalls.class deleted file mode 100644 index debcf7718071208df46a03095009ecf9604e2419..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 281 zcmYjMu};H441G?UgoZ*{Ix?^ohTg&kVyT2wsVYN(6K{nPUbrB&P;UMZpLxEk1kTe z*!fy{6TI8JXm6CZm3LgJ*H!zZ*K5^SR{x}8^tbD-Y|Jv?42e7vBY_-_R4|k7R5D3U ZFFru;QV?>%Y)=lUhXeHXY$2H7@D~>lKvMt! From 13b18e3e9025547b6658ee2adfe26eeb6dd6c8e2 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 10 Mar 2018 16:05:52 +0100 Subject: [PATCH 039/161] added missing decoration icon on transfer sources --- retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp index d6e9dc067..bbcb29e38 100644 --- a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp @@ -602,7 +602,7 @@ public: QString iconName,tooltip; TransfersDialog::getPeerName(fileInfo.peers[source_id].peerId, iconName, tooltip); - return QVariant(iconName); + return QVariant(QIcon(iconName)); } } else From 514b31e20f981bf29e1d52769b0ec4c8435e22b6 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sun, 11 Mar 2018 20:46:07 +0100 Subject: [PATCH 040/161] Add Qt installation in android preparation script Needed for F-Droid --- android-prepare-toolchain.sh | 85 +++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/android-prepare-toolchain.sh b/android-prepare-toolchain.sh index 3863a65ae..abaa0e214 100755 --- a/android-prepare-toolchain.sh +++ b/android-prepare-toolchain.sh @@ -13,7 +13,8 @@ [ -z ${SQLITE_SOURCE_VERSION+x} ] && export SQLITE_SOURCE_VERSION="3220000" [ -z ${SQLCIPHER_SOURCE_VERSION+x} ] && export SQLCIPHER_SOURCE_VERSION="3.4.2" [ -z ${LIBUPNP_SOURCE_VERSION+x} ] && export LIBUPNP_SOURCE_VERSION="1.6.24" - +[ -z ${QT_VERSION+x} ] && export QT_VERSION="5.9.4" +[ -z ${INSTALL_QT_ANDROID+x} ] && export INSTALL_QT_ANDROID="false" ## You should not edit the following variables if [ "${ANDROID_NDK_ARCH}" == "x86" ]; then @@ -40,6 +41,87 @@ build_toolchain() ${ANDROID_NDK_PATH}/build/tools/make-standalone-toolchain.sh --ndk-dir=${ANDROID_NDK_PATH} --arch=${ANDROID_NDK_ARCH} --install-dir=${NATIVE_LIBS_TOOLCHAIN_PATH} --platform=android-${ANDROID_PLATFORM_VER} --toolchain=${toolchainName} --verbose } +## More information available at https://gitlab.com/relan/provisioners/merge_requests/1 and http://stackoverflow.com/a/34032216 +install_qt_android() +{ + QT_VERSION_CODE=$(echo $QT_VERSION | tr -d .) + QT_INSTALL_PATH=${NATIVE_LIBS_TOOLCHAIN_PATH}/Qt + QT_INSTALLER="qt-unified-linux-x64-3.0.2-online.run" + + [ -f ${QT_INSTALLER} ] || wget http://master.qt.io/archive/online_installers/3.0/${QT_INSTALLER} + chmod a+x ${QT_INSTALLER} + + QT_INSTALLER_SCRIPT=$(mktemp) + cat << EOF > "${QT_INSTALLER_SCRIPT}" +function Controller() { + installer.autoRejectMessageBoxes(); + installer.installationFinished.connect(function() { + gui.clickButton(buttons.NextButton); + }); + + var welcomePage = gui.pageWidgetByObjectName("WelcomePage"); + welcomePage.completeChanged.connect(function() { + if (gui.currentPageWidget().objectName == welcomePage.objectName) + gui.clickButton(buttons.NextButton); + }); +} + +Controller.prototype.WelcomePageCallback = function() { + gui.clickButton(buttons.NextButton); +} + +Controller.prototype.CredentialsPageCallback = function() { + gui.clickButton(buttons.NextButton); +} + +Controller.prototype.IntroductionPageCallback = function() { + gui.clickButton(buttons.NextButton); +} + +Controller.prototype.TargetDirectoryPageCallback = function() { + gui.currentPageWidget().TargetDirectoryLineEdit.setText("$QT_INSTALL_PATH"); + gui.clickButton(buttons.NextButton); +} + +Controller.prototype.ComponentSelectionPageCallback = function() { + var widget = gui.currentPageWidget(); + + // You can get these component names by running the installer with the + // --verbose flag. It will then print out a resource tree. + + widget.deselectComponent("qt.tools.qtcreator"); + widget.deselectComponent("qt.tools.doc"); + widget.deselectComponent("qt.tools.examples"); + + widget.selectComponent("qt.$QT_VERSION_CODE.android_armv7"); + + gui.clickButton(buttons.NextButton); +} + +Controller.prototype.LicenseAgreementPageCallback = function() { + gui.currentPageWidget().AcceptLicenseRadioButton.setChecked(true); + gui.clickButton(buttons.NextButton); +} + +Controller.prototype.StartMenuDirectoryPageCallback = function() { + gui.clickButton(buttons.NextButton); +} + +Controller.prototype.ReadyForInstallationPageCallback = function() { + gui.clickButton(buttons.NextButton); +} + +Controller.prototype.FinishedPageCallback = function() { + var checkBoxForm = gui.currentPageWidget().LaunchQtCreatorCheckBoxForm; + if (checkBoxForm && checkBoxForm.launchQtCreatorCheckBox) + checkBoxForm.launchQtCreatorCheckBox.checked = false; + gui.clickButton(buttons.FinishButton); +} +EOF + +QT_QPA_PLATFORM=minimal ./${QT_INSTALLER} --script ${QT_INSTALLER_SCRIPT} +} + ## More information available at retroshare://file?name=Android%20Native%20Development%20Kit%20Cookbook.pdf&size=29214468&hash=0123361c1b14366ce36118e82b90faf7c7b1b136 build_bzlib() { @@ -158,6 +240,7 @@ build_libmicrohttpd() } build_toolchain +[ "${INSTALL_QT_ANDROID}X" == "trueX" ] && install_qt_android build_bzlib build_openssl build_sqlite From e7078b5256d5b3aaa01216eb9eb9066353cf4f27 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 12 Mar 2018 14:27:59 +0100 Subject: [PATCH 041/161] Update Android documentation --- README-Android.asciidoc | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/README-Android.asciidoc b/README-Android.asciidoc index 6f1f783e9..da4149cda 100644 --- a/README-Android.asciidoc +++ b/README-Android.asciidoc @@ -91,6 +91,8 @@ pointing to your SDK installation path, like == Quircks +=== Protected Apps + On some Android devices like +Huawei ALE-L21+ background applications are killed when screen is turned off unless they are in the _protected app_ list. At moment seems apps developers don't have a way to have the application @@ -106,6 +108,69 @@ To enable enable _protection_: +Android menu -> Settings -> Privacy & security Other devices may offer similar _features_ please report them. +=== APK signature mismatch + +If you try to install a RetroShare APK that comes from a different source +(eg: if you try to upgrade from F-Droid when you originally installed an APK +build by yourself) Android will prevent that from happening. In that case the +only solution is to uninstall the app and then install the new APK but if you do +it also the application data and your precious cryptographic keys, friend list +etc. will be lost forever. +To avoid that you can attempt to manually backup and then restore from the +command-line (+adb backup+ seems not working either) to change the app source +without erasing the appliation data. + +CAUTION: Following steps require root access on your Android device + +.Backup RetroShare Android application data +[source,bash] +-------------------------------------------------------------------------------- +export ORIG_DIR="/data/data/org.retroshare.android.qml_app" +export BACKUP_DIR="org.retroshare.android.qml_app.backup" + +adb root + +adb shell am force-stop org.retroshare.android.qml_app +sleep 1s + +mkdir ${BACKUP_DIR} + +# Avoid adb pull failing +adb shell rm ${ORIG_DIR}/files/.retroshare/libresapi.sock +adb pull ${ORIG_DIR}/files/ ${BACKUP_DIR}/files/ +-------------------------------------------------------------------------------- + +After this you should be able to uninstall the old APK with your preferred +method, as example from the command-line. + +.Uninstall RetroShare Android from the command-line +[source,bash] +-------------------------------------------------------------------------------- +adb uninstall org.retroshare.android.qml_app +-------------------------------------------------------------------------------- + +Now you can install a different signature APK and then restore the application +data with the following commands. + +[source,bash] +-------------------------------------------------------------------------------- +export ORIG_DIR="/data/data/org.retroshare.android.qml_app" +export BACKUP_DIR="org.retroshare.android.qml_app.backup" + +adb root + +adb shell am force-stop org.retroshare.android.qml_app +sleep 1s + +APP_OWNER="$(adb shell busybox ls -lnd ${ORIG_DIR} | awk '{print $3":"$4}')" +adb shell rm -rf ${ORIG_DIR}/files +adb push ${BACKUP_DIR}/files/ ${ORIG_DIR}/files/ +adb shell busybox chown -R ${APP_OWNER} ${ORIG_DIR}/files/ +-------------------------------------------------------------------------------- + +Opening RetroShare android app now should show your old profile. + + == Debugging with GDB QtCreator actually support debugging only for the foreground activity, so to From ce56a201c483ed6e4ae5fd50455fddf48589e6bb Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 12 Mar 2018 16:22:42 +0100 Subject: [PATCH 042/161] Added sources verification in android toolchain Reasonable and needed for F-Droid inclusion --- android-prepare-toolchain.sh | 122 ++++++++++++++++++++++++++++------- 1 file changed, 98 insertions(+), 24 deletions(-) diff --git a/android-prepare-toolchain.sh b/android-prepare-toolchain.sh index abaa0e214..7d8337add 100755 --- a/android-prepare-toolchain.sh +++ b/android-prepare-toolchain.sh @@ -1,22 +1,78 @@ #!/bin/bash -## You are supposed to provide the following variables according to your system setup -[ -z ${ANDROID_NDK_PATH+x} ] && export ANDROID_NDK_PATH="/opt/android-ndk/" -[ -z ${ANDROID_NDK_ARCH+x} ] && export ANDROID_NDK_ARCH="arm" -[ -z ${ANDROID_NDK_ABI_VER+x} ] && export ANDROID_NDK_ABI_VER="4.9" -[ -z ${ANDROID_PLATFORM_VER+x} ] && export ANDROID_PLATFORM_VER="18" -[ -z ${NATIVE_LIBS_TOOLCHAIN_PATH+x} ] && export NATIVE_LIBS_TOOLCHAIN_PATH="${HOME}/Builds/android-toolchains/retroshare-android-${ANDROID_PLATFORM_VER}-${ANDROID_NDK_ARCH}-abi${ANDROID_NDK_ABI_VER}/" -[ -z ${HOST_NUM_CPU+x} ] && export HOST_NUM_CPU=$(grep "^processor" /proc/cpuinfo | wc -l) -[ -z ${BZIP2_SOURCE_VERSION+x} ] && export BZIP2_SOURCE_VERSION="1.0.6" -[ -z ${OPENSSL_SOURCE_VERSION+x} ] && export OPENSSL_SOURCE_VERSION="1.0.2n" -[ -z ${SQLITE_SOURCE_YEAR+x} ] && export SQLITE_SOURCE_YEAR="2018" -[ -z ${SQLITE_SOURCE_VERSION+x} ] && export SQLITE_SOURCE_VERSION="3220000" -[ -z ${SQLCIPHER_SOURCE_VERSION+x} ] && export SQLCIPHER_SOURCE_VERSION="3.4.2" -[ -z ${LIBUPNP_SOURCE_VERSION+x} ] && export LIBUPNP_SOURCE_VERSION="1.6.24" -[ -z ${QT_VERSION+x} ] && export QT_VERSION="5.9.4" -[ -z ${INSTALL_QT_ANDROID+x} ] && export INSTALL_QT_ANDROID="false" +## Define default value for variable, take two arguments, $1 variable name, +## $2 default variable value, if the variable is not already define define it +## with default value. +function define_default_value() +{ + VAR_NAME="${1}" + DEFAULT_VALUE="${2}" + + [ -z "${!VAR_NAME}" ] && export ${VAR_NAME}="${DEFAULT_VALUE}" +} + +## You are supposed to provide the following variables according to your system setup +define_default_value ANDROID_NDK_PATH "/opt/android-ndk/" +define_default_value ANDROID_NDK_ARCH "arm" +define_default_value ANDROID_NDK_ABI_VER "4.9" +define_default_value ANDROID_PLATFORM_VER "18" +define_default_value NATIVE_LIBS_TOOLCHAIN_PATH "${HOME}/Builds/android-toolchains/retroshare-android-${ANDROID_PLATFORM_VER}-${ANDROID_NDK_ARCH}-abi${ANDROID_NDK_ABI_VER}/" +define_default_value HOST_NUM_CPU $(nproc) + +define_default_value BZIP2_SOURCE_VERSION "1.0.6" +define_default_value BZIP2_SOURCE_SHA256 a2848f34fcd5d6cf47def00461fcb528a0484d8edef8208d6d2e2909dc61d9cd + +define_default_value OPENSSL_SOURCE_VERSION "1.0.2n" +define_default_value OPENSSL_SOURCE_SHA256 370babb75f278c39e0c50e8c4e7493bc0f18db6867478341a832a982fd15a8fe + +define_default_value SQLITE_SOURCE_YEAR "2018" +define_default_value SQLITE_SOURCE_VERSION "3220000" +define_default_value SQLITE_SOURCE_SHA256 2824ab1238b706bc66127320afbdffb096361130e23291f26928a027b885c612 + +define_default_value SQLCIPHER_SOURCE_VERSION "3.4.2" +define_default_value SQLCIPHER_SOURCE_SHA256 69897a5167f34e8a84c7069f1b283aba88cdfa8ec183165c4a5da2c816cfaadb + +define_default_value LIBUPNP_SOURCE_VERSION "1.6.24" +define_default_value LIBUPNP_SOURCE_SHA256 7d83d79af3bb4062e5c3a58bf2e90d2da5b8b99e2b2d57c23b5b6f766288cf96 + +define_default_value INSTALL_QT_ANDROID "false" +define_default_value QT_VERSION "5.9.4" +define_default_value QT_ANDROID_INSTALLER_SHA256 a214084e2295c9a9f8727e8a0131c37255bf724bfc69e80f7012ba3abeb1f763 + + +## $1 filename, $2 sha256 hash +function check_sha256() +{ + echo ${2} "${1}" | sha256sum -c &> /dev/null +} + +## $1 filename, $2 sha256 hash, $3 url +function verified_download() +{ + FILENAME="$1" + SHA256="$2" + URL="$3" + + check_sha256 "${FILENAME}" "${SHA256}" || + { + rm -rf "${FILENAME}" + + wget -O "${FILENAME}" "$URL" || + { + echo "Failed downloading ${FILENAME} from $URL" + exit 1 + } + + check_sha256 "${FILENAME}" "${SHA256}" || + { + echo "SHA256 mismatch for ${FILENAME} from ${URL} expected sha256 ${SHA256} got $(sha256sum ${FILENAME} | awk '{print $1}')" + exit 1 + } + } +} + + -## You should not edit the following variables if [ "${ANDROID_NDK_ARCH}" == "x86" ]; then cArch="i686" eABI="" @@ -48,10 +104,12 @@ install_qt_android() QT_INSTALL_PATH=${NATIVE_LIBS_TOOLCHAIN_PATH}/Qt QT_INSTALLER="qt-unified-linux-x64-3.0.2-online.run" - [ -f ${QT_INSTALLER} ] || wget http://master.qt.io/archive/online_installers/3.0/${QT_INSTALLER} + verified_download $QT_INSTALLER $QT_ANDROID_INSTALLER_SHA256 \ + http://master.qt.io/archive/online_installers/3.0/${QT_INSTALLER} + chmod a+x ${QT_INSTALLER} - QT_INSTALLER_SCRIPT=$(mktemp) + QT_INSTALLER_SCRIPT="qt_installer_script.js" cat << EOF > "${QT_INSTALLER_SCRIPT}" function Controller() { installer.autoRejectMessageBoxes(); @@ -127,7 +185,10 @@ build_bzlib() { B_dir="bzip2-${BZIP2_SOURCE_VERSION}" rm -rf $B_dir - [ -f $B_dir.tar.gz ] || wget http://www.bzip.org/${BZIP2_SOURCE_VERSION}/bzip2-${BZIP2_SOURCE_VERSION}.tar.gz + + verified_download $B_dir.tar.gz $BZIP2_SOURCE_SHA256 \ + http://www.bzip.org/${BZIP2_SOURCE_VERSION}/bzip2-${BZIP2_SOURCE_VERSION}.tar.gz + tar -xf $B_dir.tar.gz cd $B_dir sed -i "/^CC=.*/d" Makefile @@ -148,7 +209,10 @@ build_openssl() { B_dir="openssl-${OPENSSL_SOURCE_VERSION}" rm -rf $B_dir - [ -f $B_dir.tar.gz ] || wget https://www.openssl.org/source/$B_dir.tar.gz + + verified_download $B_dir.tar.gz $OPENSSL_SOURCE_SHA256 \ + https://www.openssl.org/source/$B_dir.tar.gz + tar -xf $B_dir.tar.gz cd $B_dir if [ "${ANDROID_NDK_ARCH}" == "arm" ]; then @@ -174,7 +238,10 @@ build_openssl() build_sqlite() { B_dir="sqlite-autoconf-${SQLITE_SOURCE_VERSION}" - [ -f $B_dir.tar.gz ] || wget https://www.sqlite.org/${SQLITE_SOURCE_YEAR}/$B_dir.tar.gz + + verified_download $B_dir.tar.gz $SQLITE_SOURCE_SHA256 \ + https://www.sqlite.org/${SQLITE_SOURCE_YEAR}/$B_dir.tar.gz + tar -xf $B_dir.tar.gz cd $B_dir ./configure --prefix="${SYSROOT}/usr" --host=${ANDROID_NDK_ARCH}-linux @@ -189,9 +256,13 @@ build_sqlite() build_sqlcipher() { B_dir="sqlcipher-${SQLCIPHER_SOURCE_VERSION}" - T_file="${B_dir}.tar.gz" - [ -f $T_file ] || wget -O $T_file https://github.com/sqlcipher/sqlcipher/archive/v${SQLCIPHER_SOURCE_VERSION}.tar.gz rm -rf $B_dir + + T_file="${B_dir}.tar.gz" + + verified_download $T_file $SQLCIPHER_SOURCE_SHA256 \ + https://github.com/sqlcipher/sqlcipher/archive/v${SQLCIPHER_SOURCE_VERSION}.tar.gz + tar -xf $T_file cd $B_dir ./configure --build=$(sh ./config.guess) \ @@ -209,7 +280,10 @@ build_libupnp() { B_dir="libupnp-${LIBUPNP_SOURCE_VERSION}" rm -rf $B_dir - [ -f $B_dir.tar.bz2 ] || wget https://sourceforge.net/projects/pupnp/files/pupnp/libUPnP%20${LIBUPNP_SOURCE_VERSION}/$B_dir.tar.bz2 + + verified_download $B_dir.tar.bz2 $LIBUPNP_SOURCE_SHA256 \ + https://sourceforge.net/projects/pupnp/files/pupnp/libUPnP%20${LIBUPNP_SOURCE_VERSION}/$B_dir.tar.bz2 + tar -xf $B_dir.tar.bz2 cd $B_dir ## liupnp must be configured as static library because if not the linker will From e9b49e122f146b28b711f374d8cedc15b57cf59a Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 12 Mar 2018 17:06:01 +0100 Subject: [PATCH 043/161] Update Android quirks documentation --- README-Android.asciidoc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README-Android.asciidoc b/README-Android.asciidoc index da4149cda..40efe9c12 100644 --- a/README-Android.asciidoc +++ b/README-Android.asciidoc @@ -159,9 +159,14 @@ export BACKUP_DIR="org.retroshare.android.qml_app.backup" adb root +## Launch the app to make sure the parent directory exists and has proper owner +adb shell monkey -p org.retroshare.android.qml_app -c android.intent.category.LAUNCHER 1 +sleep 1s + adb shell am force-stop org.retroshare.android.qml_app sleep 1s + APP_OWNER="$(adb shell busybox ls -lnd ${ORIG_DIR} | awk '{print $3":"$4}')" adb shell rm -rf ${ORIG_DIR}/files adb push ${BACKUP_DIR}/files/ ${ORIG_DIR}/files/ From 91634ba6c1ba2eab58ce7d98e73a25f716db5a9d Mon Sep 17 00:00:00 2001 From: thunder2 Date: Thu, 8 Mar 2018 17:45:24 +0100 Subject: [PATCH 044/161] Added build of Retrotor to Windows build environment --- build_scripts/Windows/build-retrotor.bat | 27 +++++++++++++++++++ build_scripts/Windows/build.bat | 4 +-- .../Windows/build/build-installer.bat | 2 +- build_scripts/Windows/build/build.bat | 13 +++++---- build_scripts/Windows/build/clean.bat | 2 +- build_scripts/Windows/build/env.bat | 19 +++++++++++-- build_scripts/Windows/build/git-log.bat | 12 ++++----- build_scripts/Windows/build/pack.bat | 26 ++++++++++++++---- 8 files changed, 83 insertions(+), 22 deletions(-) create mode 100644 build_scripts/Windows/build-retrotor.bat diff --git a/build_scripts/Windows/build-retrotor.bat b/build_scripts/Windows/build-retrotor.bat new file mode 100644 index 000000000..cbf21e1c0 --- /dev/null +++ b/build_scripts/Windows/build-retrotor.bat @@ -0,0 +1,27 @@ +@echo off + +setlocal + +:: Initialize environment +call "%~dp0env.bat" +if errorlevel 1 goto error_env +call "%EnvPath%\env.bat" +if errorlevel 1 goto error_env + +%cecho% info "Build libraries" +call "%~dp0build-libs\build-libs.bat" auto-copy +if errorlevel 1 %cecho% error "Failed to build libraries." & exit /B %ERRORLEVEL% + +%cecho% info "Build %SourceName%" +call "%~dp0build\build.bat" retrotor +if errorlevel 1 %cecho% error "Failed to build %SourceName%." & exit /B %ERRORLEVEL% + +%cecho% info "Pack %SourceName%" +call "%~dp0build\pack.bat" retrotor +if errorlevel 1 %cecho% error "Failed to pack %SourceName%." & exit /B %ERRORLEVEL% + +exit /B 0 + +:error_env +echo Failed to initialize environment. +exit /B 1 diff --git a/build_scripts/Windows/build.bat b/build_scripts/Windows/build.bat index 48b6be7bb..22052a3e1 100644 --- a/build_scripts/Windows/build.bat +++ b/build_scripts/Windows/build.bat @@ -13,11 +13,11 @@ call "%~dp0build-libs\build-libs.bat" auto-copy if errorlevel 1 %cecho% error "Failed to build libraries." & exit /B %ERRORLEVEL% %cecho% info "Build %SourceName%" -call "%~dp0build\build.bat" +call "%~dp0build\build.bat" standard if errorlevel 1 %cecho% error "Failed to build %SourceName%." & exit /B %ERRORLEVEL% %cecho% info "Pack %SourceName%" -call "%~dp0build\pack.bat" +call "%~dp0build\pack.bat" standard if errorlevel 1 %cecho% error "Failed to pack %SourceName%." & exit /B %ERRORLEVEL% %cecho% info "Build installer" diff --git a/build_scripts/Windows/build/build-installer.bat b/build_scripts/Windows/build/build-installer.bat index 9d3aa837e..40c408c62 100644 --- a/build_scripts/Windows/build/build-installer.bat +++ b/build_scripts/Windows/build/build-installer.bat @@ -21,7 +21,7 @@ set /P LibsGCCVersion=<"%RootPath%\libs\gcc-version" if "%LibsGCCVersion%" NEQ "%GCCVersion%" echo Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%).& exit /B 1 :: Initialize environment -call "%~dp0env.bat" +call "%~dp0env.bat" standard if errorlevel 1 goto error_env :: Build defines for script diff --git a/build_scripts/Windows/build/build.bat b/build_scripts/Windows/build/build.bat index d4f2ab7ba..68e00235b 100644 --- a/build_scripts/Windows/build/build.bat +++ b/build_scripts/Windows/build/build.bat @@ -21,7 +21,7 @@ set /P LibsGCCVersion=<"%RootPath%\libs\gcc-version" if "%LibsGCCVersion%" NEQ "%GCCVersion%" echo Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%).& exit /B 1 :: Initialize environment -call "%~dp0env.bat" +call "%~dp0env.bat" %* if errorlevel 1 goto error_env :: Check git executable @@ -36,7 +36,7 @@ echo. echo === Version echo. -title Build - %SourceName%-%RsBuildConfig% [Version] +title Build - %SourceName%%RsType%-%RsBuildConfig% [Version] pushd "%SourcePath%\retroshare-gui\src\gui\images" :: Touch resource file @@ -50,16 +50,19 @@ echo. echo === qmake echo. -title Build - %SourceName%-%RsBuildConfig% [qmake] +title Build - %SourceName%%RsType%-%RsBuildConfig% [qmake] -qmake "%SourcePath%\RetroShare.pro" -r "CONFIG+=%RsBuildConfig% version_detail_bash_script rs_autologin retroshare_plugins" +set RS_QMAKE_CONFIG=%RsBuildConfig% version_detail_bash_script rs_autologin retroshare_plugins +if "%RsRetroTor%"=="1" set RS_QMAKE_CONFIG=%RS_QMAKE_CONFIG% retrotor + +qmake "%SourcePath%\RetroShare.pro" -r "CONFIG+=%RS_QMAKE_CONFIG%" if errorlevel 1 goto error echo. echo === make echo. -title Build - %SourceName%-%RsBuildConfig% [make] +title Build - %SourceName%%RsType%-%RsBuildConfig% [make] if exist "%EnvJomExe%" ( "%EnvJomExe%" diff --git a/build_scripts/Windows/build/clean.bat b/build_scripts/Windows/build/clean.bat index 0797e7681..d8b725535 100644 --- a/build_scripts/Windows/build/clean.bat +++ b/build_scripts/Windows/build/clean.bat @@ -7,7 +7,7 @@ call "%~dp0..\env.bat" if errorlevel 1 goto error_env call "%EnvPath%\env.bat" if errorlevel 1 goto error_env -call "%~dp0env.bat" +call "%~dp0env.bat" %* if errorlevel 1 goto error_env if not exist "%RsBuildPath%" exit /B 0 diff --git a/build_scripts/Windows/build/env.bat b/build_scripts/Windows/build/env.bat index cf25d543b..094b1b686 100644 --- a/build_scripts/Windows/build/env.bat +++ b/build_scripts/Windows/build/env.bat @@ -1,3 +1,18 @@ +if "%~1"=="standard" ( + set RsRetroTor= + set RsType= +) else ( + if "%~1"=="retrotor" ( + set RsRetroTor=1 + set RsType=-tor + ) else ( + echo. + echo Usage: standard^|retrotor + echo. + exit /B 1 + ) +) + set BuildPath=%EnvRootPath%\builds set DeployPath=%EnvRootPath%\deploy @@ -19,8 +34,8 @@ call "%ToolsPath%\get-qt-version.bat" QtVersion if "%QtVersion%"=="" echo Cannot get Qt version.& exit /B 1 set RsBuildConfig=release -set RsBuildPath=%BuildPath%\Qt-%QtVersion%-%RsBuildConfig% -set RsDeployPath=%DeployPath%\Qt-%QtVersion%-%RsBuildConfig% +set RsBuildPath=%BuildPath%\Qt-%QtVersion%%RsType%-%RsBuildConfig% +set RsDeployPath=%DeployPath%\Qt-%QtVersion%%RsType%-%RsBuildConfig% set RsPackPath=%DeployPath% set RsArchiveAdd= diff --git a/build_scripts/Windows/build/git-log.bat b/build_scripts/Windows/build/git-log.bat index 8a36c7537..5923397f1 100644 --- a/build_scripts/Windows/build/git-log.bat +++ b/build_scripts/Windows/build/git-log.bat @@ -1,19 +1,19 @@ :: Usage: -:: call git-log.bat [no-ask] +:: call git-log.bat standard|retrotor [no-ask] @echo off setlocal set NoAsk= -if "%~1"=="no-ask" set NoAsk=1 +if "%~2"=="no-ask" set NoAsk=1 :: Initialize environment call "%~dp0..\env.bat" if errorlevel 1 goto error_env call "%EnvPath%\env.bat" if errorlevel 1 goto error_env -call "%~dp0env.bat" +call "%~dp0env.bat" %1 if errorlevel 1 goto error_env :: Check git executable @@ -58,7 +58,7 @@ for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set RsDate=%RsDate:~0,4%%RsDate:~4,2%%RsDate:~6,2% :: Get last revision -set RsLastRefFile=%BuildPath%\Qt-%QtVersion%-%RsBuildConfig%-LastRef.txt +set RsLastRefFile=%BuildPath%\Qt-%QtVersion%%RsType%-%RsBuildConfig%-LastRef.txt set RsLastRef= if exist "%RsLastRefFile%" set /P RsLastRef=<"%RsLastRefFile%" @@ -86,9 +86,9 @@ if %errorlevel%==2 exit /B 1 :no_confirm if "%RsBuildConfig%" NEQ "release" ( - set RsGitLog=%DeployPath%\RetroShare-%RsVersion%-Windows-Portable-%RsDate%-%RsRevision%-Qt-%QtVersion%%RsArchiveAdd%-%RsBuildConfig%.txt + set RsGitLog=%DeployPath%\RetroShare-%RsVersion%-Windows-Portable-%RsDate%-%RsRevision%-Qt-%QtVersion%%RsType%%RsArchiveAdd%-%RsBuildConfig%.txt ) else ( - set RsGitLog=%DeployPath%\RetroShare-%RsVersion%-Windows-Portable-%RsDate%-%RsRevision%-Qt-%QtVersion%%RsArchiveAdd%.txt + set RsGitLog=%DeployPath%\RetroShare-%RsVersion%-Windows-Portable-%RsDate%-%RsRevision%-Qt-%QtVersion%%RsType%%RsArchiveAdd%.txt ) title %SourceName%-%RsBuildConfig% [git log] diff --git a/build_scripts/Windows/build/pack.bat b/build_scripts/Windows/build/pack.bat index 49dff68b3..215b3c402 100644 --- a/build_scripts/Windows/build/pack.bat +++ b/build_scripts/Windows/build/pack.bat @@ -23,7 +23,7 @@ set /P LibsGCCVersion=<"%RootPath%\libs\gcc-version" if "%LibsGCCVersion%" NEQ "%GCCVersion%" echo Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%).& exit /B 1 :: Initialize environment -call "%~dp0env.bat" +call "%~dp0env.bat" %* if errorlevel 1 goto error_env :: Remove deploy path @@ -65,6 +65,17 @@ set RsDate= for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set RsDate=%%I set RsDate=%RsDate:~0,4%%RsDate:~4,2%%RsDate:~6,2% +if "%RsRetroTor%"=="1" ( + :: Check Retrotor + if not exist "%EnvDownloadPath%\tor\Tor\tor.exe" ( + echo Tor binary not found. Please download Tor Expert Bundle from + echo https://www.torproject.org/download/download.html.en + echo and unpack to + echo %EnvDownloadPath%\tor + goto error + ) +) + set QtMainVersion=%QtVersion:~0,1% rem Qt 4 = QtSvg4.dll @@ -75,9 +86,9 @@ if "%QtMainVersion%"=="4" set QtMainVersion2=4 if "%QtMainVersion%"=="5" set QtMainVersion1=5 if "%RsBuildConfig%" NEQ "release" ( - set Archive=%RsPackPath%\RetroShare-%RsVersion%-Windows-Portable-%RsDate%-%RsRevision%-Qt-%QtVersion%%RsArchiveAdd%-%RsBuildConfig%.7z + set Archive=%RsPackPath%\RetroShare-%RsVersion%-Windows-Portable-%RsDate%-%RsRevision%-Qt-%QtVersion%%RsType%%RsArchiveAdd%-%RsBuildConfig%.7z ) else ( - set Archive=%RsPackPath%\RetroShare-%RsVersion%-Windows-Portable-%RsDate%-%RsRevision%-Qt-%QtVersion%%RsArchiveAdd%.7z + set Archive=%RsPackPath%\RetroShare-%RsVersion%-Windows-Portable-%RsDate%-%RsRevision%-Qt-%QtVersion%%RsType%%RsArchiveAdd%.7z ) if exist "%Archive%" del /Q "%Archive%" @@ -85,7 +96,7 @@ if exist "%Archive%" del /Q "%Archive%" :: Create deploy path mkdir "%RsDeployPath%" -title Pack - %SourceName%-%RsBuildConfig% [copy files] +title Pack - %SourceName%%RsType%-%RsBuildConfig% [copy files] set ExtensionsFile=%SourcePath%\libretroshare\src\rsserver\rsinit.cc set Extensions= @@ -172,8 +183,13 @@ if exist "%SourcePath%\libresapi\src\webui" ( xcopy /S "%SourcePath%\libresapi\src\webui" "%RsDeployPath%\webui" %Quite% ) +if "%RsRetroTor%"=="1" ( + echo copy tor + echo n | copy /-y "%EnvDownloadPath%\tor\Tor\*.*" "%RsDeployPath%" %Quite% +) + rem pack files -title Pack - %SourceName%-%RsBuildConfig% [pack files] +title Pack - %SourceName%%RsType%-%RsBuildConfig% [pack files] "%EnvSevenZipExe%" a -mx=9 -t7z "%Archive%" "%RsDeployPath%\*" From 0e6d27ad7a0ab3a4370db6e7057d6d651a7e10c3 Mon Sep 17 00:00:00 2001 From: csoler Date: Tue, 13 Mar 2018 20:25:38 +0100 Subject: [PATCH 045/161] fixed bug in FT causing transfer lines to only update when the mouse moves over the widget --- retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp index bbcb29e38..0ecc55ac2 100644 --- a/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/TransfersDialog.cpp @@ -661,8 +661,16 @@ public: endRemoveRows(); } + uint32_t old_status = mDownloads[i].downloadStatus ; + mDownloads[i] = fileInfo ; // ... because insertRows() calls rowCount() which needs to be consistent with the *old* number of rows. + if(fileInfo.downloadStatus == FT_STATE_DOWNLOADING || old_status != fileInfo.downloadStatus) + { + QModelIndex topLeft = createIndex(i,0), bottomRight = createIndex(i, COLUMN_COUNT-1); + emit dataChanged(topLeft, bottomRight); + } + // This is apparently not needed. // // if(!mDownloads.empty()) From b3653d1283101c9e55d9f3caceed23b039803fd8 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 15 Mar 2018 11:32:55 +0100 Subject: [PATCH 046/161] enabled aggressive re-request of pending slices at end of transfer, thus fixing the long delay to finish files with mixed fast/slow sources --- libretroshare/src/ft/ftchunkmap.cc | 52 ++++++++++++++++++++++----- libretroshare/src/ft/ftchunkmap.h | 44 ++++++++++++++--------- libretroshare/src/ft/ftfilecreator.cc | 11 +++++- 3 files changed, 82 insertions(+), 25 deletions(-) diff --git a/libretroshare/src/ft/ftchunkmap.cc b/libretroshare/src/ft/ftchunkmap.cc index 3fd64892b..b83c98769 100644 --- a/libretroshare/src/ft/ftchunkmap.cc +++ b/libretroshare/src/ft/ftchunkmap.cc @@ -39,6 +39,7 @@ static const uint32_t SOURCE_CHUNK_MAP_UPDATE_PERIOD = 60 ; //! TTL for chunkmap info static const uint32_t INACTIVE_CHUNK_TIME_LAPSE = 3600 ; //! TTL for an inactive chunk static const uint32_t FT_CHUNKMAP_MAX_CHUNK_JUMP = 50 ; //! Maximum chunk jump in progressive DL mode +static const uint32_t FT_CHUNKMAP_MAX_SLICE_REASK_DELAY = 10 ; //! Maximum chunk jump in progressive DL mode std::ostream& operator<<(std::ostream& o,const ftChunk& c) { @@ -120,7 +121,7 @@ void ChunkMap::setAvailabilityMap(const CompressedChunkMap& map) } } -void ChunkMap::dataReceived(const ftChunk::ChunkId& cid) +void ChunkMap::dataReceived(const ftChunk::OffsetInFile& cid) { // 1 - find which chunk contains the received data. // @@ -139,7 +140,7 @@ void ChunkMap::dataReceived(const ftChunk::ChunkId& cid) return ; } - std::map::iterator it(itc->second._slices.find(cid)) ; + std::map::iterator it(itc->second._slices.find(cid)) ; if(it == itc->second._slices.end()) { @@ -150,8 +151,8 @@ void ChunkMap::dataReceived(const ftChunk::ChunkId& cid) return ; } - _total_downloaded += it->second ; - itc->second._remains -= it->second ; + _total_downloaded += it->second.size ; + itc->second._remains -= it->second.size ; itc->second._slices.erase(it) ; itc->second._last_data_received = time(NULL) ; // update time stamp @@ -256,6 +257,36 @@ void ChunkMap::setChunkCheckingResult(uint32_t chunk_number,bool check_succeeded } } +bool ChunkMap::reAskPendingChunk(const RsPeerId& peer_id,uint32_t size_hint,uint64_t& offset,uint32_t& size) +{ + // make sure that we're at the end of the file. No need to be too greedy in the middle of it. + + for(uint32_t i=0;i<_map.size();++i) + if(_map[i] == FileChunksInfo::CHUNK_OUTSTANDING) + return false ; + + time_t now = time(NULL); + + for(std::map::iterator it(_slices_to_download.begin());it!=_slices_to_download.end();++it) + for(std::map::iterator it2(it->second._slices.begin());it2!=it->second._slices.end();++it2) + if(it2->second.request_time + FT_CHUNKMAP_MAX_SLICE_REASK_DELAY < now && it2->second.peers.end()==it2->second.peers.find(peer_id)) + { + offset = it2->first; + size = it2->second.size ; + +#ifdef DEBUG_FTCHUNK + std::cerr << "*** ChunkMap::reAskPendingChunk: re-asking slice (" << offset << ", " << size << ") to peer " << peer_id << std::endl; +#endif + + it2->second.request_time = now ; + it2->second.peers.insert(peer_id) ; + + return true ; + } + + return false ; +} + // Warning: a chunk may be empty, but still being downloaded, so asking new slices from it // will produce slices of size 0. This happens at the end of each chunk. // --> I need to get slices from the next chunk, in such a case. @@ -295,7 +326,7 @@ bool ChunkMap::getDataChunk(const RsPeerId& peer_id,uint32_t size_hint,ftChunk& ChunkDownloadInfo& cdi(_slices_to_download[c]) ; falsafe_it = pit ; // let's keep this one just in case. - if(cdi._slices.rbegin() != cdi._slices.rend() && cdi._slices.rbegin()->second*0.7 <= (float)size_hint) + if(cdi._slices.rbegin() != cdi._slices.rend() && cdi._slices.rbegin()->second.size*0.7 <= (float)size_hint) { it = pit ; #ifdef DEBUG_FTCHUNK @@ -348,9 +379,14 @@ bool ChunkMap::getDataChunk(const RsPeerId& peer_id,uint32_t size_hint,ftChunk& // Get the first slice of the chunk, that is at most of length size // it->second.getSlice(size_hint,chunk) ; - _slices_to_download[chunk.offset/_chunk_size]._slices[chunk.id] = chunk.size ; _slices_to_download[chunk.offset/_chunk_size]._last_data_received = time(NULL) ; + ChunkDownloadInfo::SliceRequestInfo& r(_slices_to_download[chunk.offset/_chunk_size]._slices[chunk.id]); + + r.size = chunk.size ; + r.request_time = time(NULL); + r.peers.insert(peer_id); + chunk.peer_id = peer_id ; #ifdef DEBUG_FTCHUNK @@ -362,7 +398,7 @@ bool ChunkMap::getDataChunk(const RsPeerId& peer_id,uint32_t size_hint,ftChunk& return true ; } -void ChunkMap::removeInactiveChunks(std::vector& to_remove) +void ChunkMap::removeInactiveChunks(std::vector& to_remove) { to_remove.clear() ; time_t now = time(NULL) ; @@ -377,7 +413,7 @@ void ChunkMap::removeInactiveChunks(std::vector& to_remove) // std::map::iterator tmp(it) ; - for(std::map::const_iterator it2(it->second._slices.begin());it2!=it->second._slices.end();++it2) + for(std::map::const_iterator it2(it->second._slices.begin());it2!=it->second._slices.end();++it2) to_remove.push_back(it2->first) ; _map[it->first] = FileChunksInfo::CHUNK_OUTSTANDING ; // reset the chunk diff --git a/libretroshare/src/ft/ftchunkmap.h b/libretroshare/src/ft/ftchunkmap.h index 3ee4a1234..f7c696b9a 100644 --- a/libretroshare/src/ft/ftchunkmap.h +++ b/libretroshare/src/ft/ftchunkmap.h @@ -32,7 +32,7 @@ class ftController ; class ftChunk { public: - typedef uint64_t ChunkId ; + typedef uint64_t OffsetInFile ; ftChunk():offset(0), size(0), id(0), ts(0),ref_cnt(NULL) {} @@ -40,7 +40,7 @@ class ftChunk uint64_t offset; // current offset of the slice uint64_t size; // size remaining to download - ChunkId id ; // id of the chunk. Equal to the starting offset of the chunk + OffsetInFile id ; // id of the chunk. Equal to the starting offset of the chunk time_t ts; // time of last data received int *ref_cnt; // shared counter of number of sub-blocks. Used when a slice gets split. RsPeerId peer_id ; @@ -70,10 +70,17 @@ class Chunk uint64_t _end ; // const }; -class ChunkDownloadInfo +struct ChunkDownloadInfo { public: - std::map _slices ; + struct SliceRequestInfo + { + uint32_t size ; // size of the slice + time_t request_time ; // last request time + std::set peers ; // peers the slice was requested to. Normally only one, except at the end of the file. + }; + + std::map _slices ; uint32_t _remains ; time_t _last_data_received ; }; @@ -109,24 +116,29 @@ class ChunkMap /// destructor virtual ~ChunkMap() {} - /// Returns an slice of data to be asked to the peer within a chunk. + /// Returns an slice of data to be asked to the peer within a chunk. /// If a chunk is already been downloaded by this peer, take a slice at /// the beginning of this chunk, or at least where it starts. - /// If not, randomly/streamly select a new chunk depending on the strategy. - /// adds an entry in the chunk_ids map, and sets up 1 interval for it. - /// the chunk should be available from the designated peer. + /// If not, randomly/streamly select a new chunk depending on the strategy. + /// adds an entry in the chunk_ids map, and sets up 1 interval for it. + /// the chunk should be available from the designated peer. /// On return: /// - source_chunk_map_needed = true if the source map should be asked virtual bool getDataChunk(const RsPeerId& peer_id,uint32_t size_hint,ftChunk& chunk,bool& source_chunk_map_needed) ; - /// Notify received a slice of data. This needs to - /// - carve in the map of chunks what is received, what is not. - /// - tell which chunks are finished. For this, each interval must know what chunk number it has been attributed - /// when the interval is split in the middle, the number of intervals for the chunk is increased. If the interval is - /// completely covered by the data, the interval number is decreased. + /// Returns an already pending slice that was being downloaded but hasn't arrived yet. This is mostly used at the end of the file + /// in order to re-ask pendign slices to active peers while slow peers take a lot of time to send their remaining slices. + /// + bool reAskPendingChunk(const RsPeerId& peer_id,uint32_t size_hint,uint64_t& offset,uint32_t& size); - virtual void dataReceived(const ftChunk::ChunkId& c_id) ; + /// Notify received a slice of data. This needs to + /// - carve in the map of chunks what is received, what is not. + /// - tell which chunks are finished. For this, each interval must know what chunk number it has been attributed + /// when the interval is split in the middle, the number of intervals for the chunk is increased. If the interval is + /// completely covered by the data, the interval number is decreased. + + virtual void dataReceived(const ftChunk::OffsetInFile& c_id) ; /// Decides how chunks are selected. /// STREAMING: the 1st chunk is always returned @@ -163,7 +175,7 @@ class ChunkMap /// Remove active chunks that have not received any data for the last 60 seconds, and return /// the list of slice numbers that should be canceled. - void removeInactiveChunks(std::vector& to_remove) ; + void removeInactiveChunks(std::vector& to_remove) ; /// Updates the peer's availablility map // @@ -214,7 +226,7 @@ class ChunkMap uint32_t _chunk_size ; //! Size of chunks. Common to all chunks. FileChunksInfo::ChunkStrategy _strategy ; //! how do we allocate new chunks std::map _active_chunks_feed ; //! vector of chunks being downloaded. Exactly 1 chunk per peer. - std::map _slices_to_download ; //! list of (slice id,slice size) + std::map _slices_to_download ; //! list of (slice offset,slice size) currently being downloaded std::vector _map ; //! vector of chunk state over the whole file std::map _peers_chunks_availability ; //! what does each source peer have uint64_t _total_downloaded ; //! completion for the file diff --git a/libretroshare/src/ft/ftfilecreator.cc b/libretroshare/src/ft/ftfilecreator.cc index c47cc1c4c..56bf78bf7 100644 --- a/libretroshare/src/ft/ftfilecreator.cc +++ b/libretroshare/src/ft/ftfilecreator.cc @@ -240,7 +240,7 @@ void ftFileCreator::removeInactiveChunks() #ifdef FILE_DEBUG std::cerr << "ftFileCreator::removeInactiveChunks(): looking for old chunks." << std::endl ; #endif - std::vector to_remove ; + std::vector to_remove ; chunkMap.removeInactiveChunks(to_remove) ; @@ -421,7 +421,9 @@ int ftFileCreator::locked_notifyReceived(uint64_t offset, uint32_t chunk_size) if(!found) { +#ifdef FILE_DEBUG std::cerr << "ftFileCreator::locked_notifyReceived(): failed to find an active slice for " << offset << "+" << chunk_size << ", hash = " << hash << ": dropping data." << std::endl; +#endif return 0; /* ignoring */ } } @@ -531,7 +533,14 @@ bool ftFileCreator::getMissingChunk(const RsPeerId& peer_id,uint32_t size_hint,u ftChunk chunk ; if(!chunkMap.getDataChunk(peer_id,size_hint,chunk,source_chunk_map_needed)) + { + // No chunks are available. We brutally re-ask an ongoing chunk to another peer. + + if(chunkMap.reAskPendingChunk(peer_id,size_hint,offset,size)) + return true ; + return false ; + } #ifdef FILE_DEBUG std::cerr << "ffc::getMissingChunk() Retrieved new chunk: " << chunk << std::endl ; From e1ad21c357c28fb521485f2fc9fdaad4e2211caa Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 15 Mar 2018 13:11:19 +0100 Subject: [PATCH 047/161] fixed wrong file count in RsCollectionDialog when downloading files --- libretroshare/src/ft/ftchunkmap.cc | 2 +- .../src/gui/common/RsCollectionDialog.cpp | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libretroshare/src/ft/ftchunkmap.cc b/libretroshare/src/ft/ftchunkmap.cc index b83c98769..64411ac58 100644 --- a/libretroshare/src/ft/ftchunkmap.cc +++ b/libretroshare/src/ft/ftchunkmap.cc @@ -39,7 +39,7 @@ static const uint32_t SOURCE_CHUNK_MAP_UPDATE_PERIOD = 60 ; //! TTL for chunkmap info static const uint32_t INACTIVE_CHUNK_TIME_LAPSE = 3600 ; //! TTL for an inactive chunk static const uint32_t FT_CHUNKMAP_MAX_CHUNK_JUMP = 50 ; //! Maximum chunk jump in progressive DL mode -static const uint32_t FT_CHUNKMAP_MAX_SLICE_REASK_DELAY = 10 ; //! Maximum chunk jump in progressive DL mode +static const uint32_t FT_CHUNKMAP_MAX_SLICE_REASK_DELAY = 10 ; //! Maximum time to re-ask a slice to another peer at end of transfer std::ostream& operator<<(std::ostream& o,const ftChunk& c) { diff --git a/retroshare-gui/src/gui/common/RsCollectionDialog.cpp b/retroshare-gui/src/gui/common/RsCollectionDialog.cpp index 26e609289..36e0eb202 100644 --- a/retroshare-gui/src/gui/common/RsCollectionDialog.cpp +++ b/retroshare-gui/src/gui/common/RsCollectionDialog.cpp @@ -639,8 +639,17 @@ void RsCollectionDialog::directoryLoaded(QString dirLoaded) */ void RsCollectionDialog::updateSizes() { - ui._selectedFiles_TL->setText(QString::number(getRootItem()->data(COLUMN_FILEC,ROLE_SELFILEC).toULongLong())) ; - ui._totalSize_TL->setText(misc::friendlyUnit(getRootItem()->data(COLUMN_SIZE,ROLE_SELSIZE).toULongLong())) ; + uint64_t total_size = 0 ; + uint32_t total_count = 0 ; + + for(uint32_t i=0;itopLevelItemCount();++i) + { + total_size += ui._fileEntriesTW->topLevelItem(i)->data(COLUMN_SIZE ,ROLE_SELSIZE ).toULongLong(); + total_count += ui._fileEntriesTW->topLevelItem(i)->data(COLUMN_FILEC,ROLE_SELFILEC).toULongLong(); + } + + ui._selectedFiles_TL->setText(QString::number(total_count)); + ui._totalSize_TL->setText(misc::friendlyUnit(total_size)); } /** From 311b190f67bff3f1d43fa3947ef680feafef5716 Mon Sep 17 00:00:00 2001 From: Phenom Date: Sun, 4 Mar 2018 20:06:33 +0100 Subject: [PATCH 048/161] Add Background to xprogressbar text for more readability. --- retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp b/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp index 5a3f2a8ef..9ab743f87 100644 --- a/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp +++ b/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp @@ -320,9 +320,14 @@ void xProgressBar::paint() // paint text? if (displayText) { - QLocale locale; + QRect bounding = painter->boundingRect(rect, Qt::AlignCenter, QLocale().toString(_pinfo.progress, 'f', 2) + "%"); + QColor color (255-textColor.red(), 255-textColor.green(), 255-textColor.blue(), 125); + painter->setPen(color); + painter->setBrush(QBrush(color)); + painter->drawRect(bounding.adjusted(2,2,-4,-4)); + painter->setPen(textColor); - painter->drawText(rect, Qt::AlignCenter, locale.toString(_pinfo.progress, 'f', 2) + "%"); + painter->drawText(rect, Qt::AlignCenter, QLocale().toString(_pinfo.progress, 'f', 2) + "%"); } backgroundColor.setRgb(255, 255, 255); From 7da73b35a94abb18a9a78a96fac45afaa6417558 Mon Sep 17 00:00:00 2001 From: Phenom Date: Mon, 5 Mar 2018 20:31:39 +0100 Subject: [PATCH 049/161] Add differents views depends ProgressBar width Less than text width: only show progress bar. Text width to 1.5 times: only show text. More than 1.5 times: show chunk and text. --- retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp b/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp index 9ab743f87..003904b30 100644 --- a/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp +++ b/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp @@ -233,7 +233,9 @@ void xProgressBar::paint() uint32_t ss = _pinfo.nb_chunks ; - if(ss > 1) // for small files we use a more progressive display + QRect bounding = painter->boundingRect(rect, Qt::AlignCenter, QLocale().toString(_pinfo.progress, 'f', 2) + "%"); + + if((ss > 1) && (rect.width() > (1.5*bounding.width()))) // for small files we use a more progressive display { if(!_pinfo.cmap._map.empty()) { @@ -303,7 +305,7 @@ void xProgressBar::paint() overPaintSelectedChunks( _pinfo.chunks_in_progress , QColor(170, 20,9), QColor(223,121,123), width,ss) ; overPaintSelectedChunks( _pinfo.chunks_in_checking , QColor(186,143,0), QColor(223,196, 61), width,ss) ; } - else + else if ((rect.width() < bounding.width()) || !displayText) { // calculate progress value int preWidth = static_cast((rect.width() - 1 - hSpan)*(_pinfo.progress/100.0f)); @@ -318,9 +320,8 @@ void xProgressBar::paint() // paint text? - if (displayText) + if (displayText && (rect.width() >= bounding.width())) { - QRect bounding = painter->boundingRect(rect, Qt::AlignCenter, QLocale().toString(_pinfo.progress, 'f', 2) + "%"); QColor color (255-textColor.red(), 255-textColor.green(), 255-textColor.blue(), 125); painter->setPen(color); painter->setBrush(QBrush(color)); From e1482dd5e4777114aca818b21199e04b0ac90dca Mon Sep 17 00:00:00 2001 From: Phenom Date: Sat, 17 Mar 2018 00:00:05 +0100 Subject: [PATCH 050/161] Add Rounded and Gradient Background to xprogressbar text. --- .../src/gui/FileTransfer/xprogressbar.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp b/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp index 003904b30..f1eb7f3f2 100644 --- a/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp +++ b/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp @@ -322,10 +322,15 @@ void xProgressBar::paint() // paint text? if (displayText && (rect.width() >= bounding.width())) { - QColor color (255-textColor.red(), 255-textColor.green(), 255-textColor.blue(), 125); - painter->setPen(color); - painter->setBrush(QBrush(color)); - painter->drawRect(bounding.adjusted(2,2,-4,-4)); + QColor colorInt (255-textColor.red(), 255-textColor.green(), 255-textColor.blue(), 127); + QColor colorBor (255-textColor.red(), 255-textColor.green(), 255-textColor.blue(), 63); + QRadialGradient radialGrad(bounding.x()+(bounding.width()/2), bounding.y()+(bounding.height()/2),bounding.width()/2); + radialGrad.setColorAt(0.0, colorInt); + radialGrad.setColorAt(1.0, colorBor); + radialGrad.setSpread(QGradient::ReflectSpread); + painter->setPen(colorBor); + painter->setBrush(radialGrad); + painter->drawRoundedRect(bounding.adjusted(-2,2,2,-3),4.0,4.0); painter->setPen(textColor); painter->drawText(rect, Qt::AlignCenter, QLocale().toString(_pinfo.progress, 'f', 2) + "%"); From 599c3d4c0fd1ed2f3572cfb4b2d29a5cb62304d1 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 23 Mar 2018 23:00:27 -0500 Subject: [PATCH 051/161] fixed clickjacking attack with x-frame-options --- libresapi/src/api/ApiServerMHD.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libresapi/src/api/ApiServerMHD.cpp b/libresapi/src/api/ApiServerMHD.cpp index 273010d8b..6b77db59a 100644 --- a/libresapi/src/api/ApiServerMHD.cpp +++ b/libresapi/src/api/ApiServerMHD.cpp @@ -369,6 +369,9 @@ static void secure_queue_response(MHD_Connection *connection, unsigned int statu // tell Internet Explorer to not do content sniffing MHD_add_response_header(response, "X-Content-Type-Options", "nosniff"); + // Prevent clickjacking attacks (also prevented by CSP, but not in all browsers, including FireFox) + MHD_add_response_header(response, "X-Frame-Options", "SAMEORIGIN"); + // Content security policy header, its a new technology and not implemented everywhere // get own host name as the browser sees it From 8d1f1da242675a03a05ccecfd7ef8596d60e0afe Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Wed, 28 Mar 2018 16:30:35 +0200 Subject: [PATCH 052/161] Extend libresapi with minimal support for channels The code is not elegant as this version of the API will be soon obsolete but it offer a bunch of channels functionalities, comments and votes are not implemented yet /channels/list_channels get all visibile channels /channels/get_channel get content of a subscribed channel /channels/toggle_subscribe subscribe/unsubscribe to a channel /channels/toggle_auto_download set/unset auto-download for files attached to posts in a channel /channels/toggle_read mark a post as read /channels/create_channel create a new channel /channels/create_post create a new post in given channel, group_id paramenter renamed to channel_id for consistence mChannels use reference instead of pointer as it must be valid for the whole lifetime of the object RsGxsCommentService and derivatives use proper types for parameter, avoid reference when unneeded --- libresapi/src/api/ApiServer.cpp | 2 +- libresapi/src/api/ChannelsHandler.cpp | 405 ++++++++++++++++++-- libresapi/src/api/ChannelsHandler.h | 32 +- libretroshare/src/retroshare/rsgxscommon.h | 29 +- libretroshare/src/services/p3gxschannels.cc | 10 +- libretroshare/src/services/p3gxschannels.h | 17 +- libretroshare/src/services/p3posted.h | 31 +- 7 files changed, 449 insertions(+), 77 deletions(-) diff --git a/libresapi/src/api/ApiServer.cpp b/libresapi/src/api/ApiServer.cpp index db8b75d16..b1f6629fd 100644 --- a/libresapi/src/api/ApiServer.cpp +++ b/libresapi/src/api/ApiServer.cpp @@ -240,7 +240,7 @@ public: mTransfersHandler(sts, ifaces.mFiles, ifaces.mPeers, *ifaces.mNotify), mChatHandler(sts, ifaces.mNotify, ifaces.mMsgs, ifaces.mPeers, ifaces.mIdentity, &mPeersHandler), mApiPluginHandler(sts, ifaces), - mChannelsHandler(ifaces.mGxsChannels), + mChannelsHandler(*ifaces.mGxsChannels), mStatsHandler() #ifdef LIBRESAPI_QT ,mSettingsHandler(sts) diff --git a/libresapi/src/api/ChannelsHandler.cpp b/libresapi/src/api/ChannelsHandler.cpp index 27cc02e56..d1812dc3d 100644 --- a/libresapi/src/api/ChannelsHandler.cpp +++ b/libresapi/src/api/ChannelsHandler.cpp @@ -1,7 +1,26 @@ +/* + * RetroShare JSON API + * Copyright (C) 2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + #include "ChannelsHandler.h" #include #include +#include #include #include "Operators.h" @@ -21,22 +40,347 @@ StreamBase& operator << (StreamBase& left, RsGxsFile& file) { double size = 0; left << makeKeyValueReference("size", size); - file.mSize = size; + file.mSize = size; } return left; } -ChannelsHandler::ChannelsHandler(RsGxsChannels *channels): - mChannels(channels) +ChannelsHandler::ChannelsHandler(RsGxsChannels& channels): mChannels(channels) { - addResourceHandler("create_post", this, &ChannelsHandler::handleCreatePost); + addResourceHandler("list_channels", this, + &ChannelsHandler::handleListChannels); + addResourceHandler("get_channel", this, &ChannelsHandler::handleGetChannel); + addResourceHandler("toggle_subscribe", this, &ChannelsHandler::handleToggleSubscription); + addResourceHandler("toggle_auto_download", this, &ChannelsHandler::handleToggleAutoDownload); + addResourceHandler("toggle_read", this, &ChannelsHandler::handleTogglePostRead); + addResourceHandler("create_channel", this, &ChannelsHandler::handleCreateChannel); + addResourceHandler("create_post", this, &ChannelsHandler::handleCreatePost); } -ResponseTask* ChannelsHandler::handleCreatePost(Request &req, Response &resp) +void ChannelsHandler::handleListChannels(Request& /*req*/, Response& resp) +{ + RsTokReqOptions opts; + opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA; + uint32_t token; + + RsTokenService& tChannels = *mChannels.getTokenService(); + + tChannels.requestGroupInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts); + + time_t start = time(NULL); + while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + std::vector grps; + if( tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE + && mChannels.getGroupData(token, grps) ) + { + for( std::vector::iterator vit = grps.begin(); + vit != grps.end(); ++vit ) + { + RsGxsChannelGroup& grp = *vit; + KeyValueReference id("id", grp.mMeta.mGroupId); + KeyValueReference vis_msg("visible_msg_count", grp.mMeta.mVisibleMsgCount); + bool own = (grp.mMeta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN); + bool subscribed = IS_GROUP_SUBSCRIBED(grp.mMeta.mSubscribeFlags); + std::string lastPostTsStr = std::to_string(grp.mMeta.mLastPost); + std::string publishTsStr = std::to_string(grp.mMeta.mPublishTs); + std::string thumbnail_base64; + Radix64::encode(grp.mImage.mData, grp.mImage.mSize, thumbnail_base64); + resp.mDataStream.getStreamToMember() + << id + << makeKeyValueReference("name", grp.mMeta.mGroupName) + << makeKeyValueReference("last_post_ts", lastPostTsStr) + << makeKeyValueReference("popularity", grp.mMeta.mPop) + << makeKeyValueReference("publish_ts", publishTsStr) + << vis_msg + << makeKeyValueReference("group_status", grp.mMeta.mGroupStatus) + << makeKeyValueReference("author_id", grp.mMeta.mAuthorId) + << makeKeyValueReference("parent_grp_id", grp.mMeta.mParentGrpId) + << makeKeyValueReference("description", grp.mDescription) + << makeKeyValueReference("own", own) + << makeKeyValueReference("subscribed", subscribed) + << makeKeyValueReference("thumbnail_base64_png", thumbnail_base64) + << makeKeyValueReference("auto_download", grp.mAutoDownload); + } + + resp.setOk(); + } + else resp.setFail("Cant get data from GXS!"); +} + +void ChannelsHandler::handleGetChannel(Request& req, Response& resp) +{ + std::string chanIdStr; + req.mStream << makeKeyValueReference("channel_id", chanIdStr); + if(chanIdStr.empty()) + { + resp.setFail("channel_id required!"); + return; + + } + + RsGxsGroupId chanId(chanIdStr); + if(chanId.isNull()) + { + resp.setFail("Invalid channel_id:" + chanIdStr); + return; + } + + std::list groupIds; groupIds.push_back(chanId); + uint32_t token; + RsTokReqOptions opts; + opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; + + if(! mChannels.getTokenService()-> + requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, groupIds) ) + { + resp.setFail("Unknown GXS error!"); + return; + } + + time_t start = time(NULL); + while((mChannels.getTokenService()->requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(mChannels.getTokenService()->requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + std::vector posts; + std::vector comments; + if( mChannels.getTokenService()->requestStatus(token) == + RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE && + mChannels.getPostData(token, posts, comments) ) + { + for( std::vector::iterator vit = posts.begin(); + vit != posts.end(); ++vit ) + { + RsGxsChannelPost& post = *vit; + RsMsgMetaData& pMeta = post.mMeta; + resp.mDataStream.getStreamToMember() + << makeKeyValueReference("channel_id", pMeta.mGroupId) + << makeKeyValueReference("name", pMeta.mMsgName) + << makeKeyValueReference("id", pMeta.mMsgId) + << makeKeyValueReference("parent_id", pMeta.mParentId) + << makeKeyValueReference("author_id", pMeta.mAuthorId) + << makeKeyValueReference("orig_msg_id", pMeta.mOrigMsgId) + << makeKeyValueReference("thread_id", pMeta.mThreadId) + << makeKeyValueReference("message", post.mMsg); + } + + for( std::vector::iterator vit = comments.begin(); + vit != comments.end(); ++vit ) + { + RsGxsComment& comment = *vit; + RsMsgMetaData& cMeta = comment.mMeta; + std::string scoreStr = std::to_string(comment.mScore); + resp.mDataStream.getStreamToMember() + << makeKeyValueReference("channel_id", cMeta.mGroupId) + << makeKeyValueReference("name", cMeta.mMsgName) + << makeKeyValueReference("id", cMeta.mMsgId) + << makeKeyValueReference("parent_id", cMeta.mParentId) + << makeKeyValueReference("author_id", cMeta.mAuthorId) + << makeKeyValueReference("orig_msg_id", cMeta.mOrigMsgId) + << makeKeyValueReference("thread_id", cMeta.mThreadId) + << makeKeyValueReference("score", scoreStr) + << makeKeyValueReference("message", comment.mComment); + } + + resp.setOk(); + } + else resp.setFail("Cant get data from GXS!"); +} + +void ChannelsHandler::handleToggleSubscription(Request& req, Response& resp) +{ + std::string chanIdStr; + bool subscribe = true; + + req.mStream << makeKeyValueReference("channel_id", chanIdStr) + << makeKeyValueReference("subscribe", subscribe); + + if(chanIdStr.empty()) + { + resp.setFail("channel_id required!"); + return; + } + + RsGxsGroupId chanId(chanIdStr); + if(chanId.isNull()) + { + resp.setFail("Invalid channel_id:" + chanIdStr); + return; + } + + uint32_t token; + if(mChannels.subscribeToGroup(token, chanId, subscribe)) + { + RsTokenService& tChannels = *mChannels.getTokenService(); + + time_t start = time(NULL); + while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + if(tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + resp.setOk(); + else resp.setFail("Unknown GXS error!"); + } + else resp.setFail("Unknown GXS error!"); +} + +void ChannelsHandler::handleCreateChannel(Request& req, Response& resp) +{ + RsGxsChannelGroup chan; + RsGroupMetaData& cMeta = chan.mMeta; + std::string authorIdStr; + std::string thumbnail_base64; + + req.mStream << makeKeyValueReference("author_id", authorIdStr) + << makeKeyValueReference("name", cMeta.mGroupName) + << makeKeyValueReference("description", chan.mDescription) + << makeKeyValueReference("thumbnail_base64_png", thumbnail_base64); + + if(cMeta.mGroupName.empty()) + { + resp.setFail("Channel name required!"); + return; + } + + if(thumbnail_base64.empty()) chan.mImage.clear(); + else + { + std::vector png_data = Radix64::decode(thumbnail_base64); + if(!png_data.empty()) + { + if(png_data.size() < 8) + { + resp.setFail("Decoded thumbnail_base64_png is smaller than 8 byte. This can't be a valid png file!"); + return; + } + uint8_t png_magic_number[] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a}; + if(!std::equal(&png_magic_number[0],&png_magic_number[8],png_data.begin())) + { + resp.setFail("Decoded thumbnail_base64_png does not seem to be a png file. (Header is missing magic number)"); + return; + } + chan.mImage.copy(png_data.data(), png_data.size()); + } + } + + if(!authorIdStr.empty()) cMeta.mAuthorId = RsGxsId(authorIdStr); + + // ATM supports creating only public channels + cMeta.mGroupFlags = GXS_SERV::FLAG_PRIVACY_PUBLIC; + + // I am not sure about those flags I have reversed them with the debugger + // that gives 520 as value of this member when a channel with default + // options is created from Qt Gui + cMeta.mSignFlags = GXS_SERV::MSG_AUTHEN_CHILD_AUTHOR_SIGN | + GXS_SERV::FLAG_AUTHOR_AUTHENTICATION_REQUIRED; + + uint32_t token; + if(mChannels.createGroup(token, chan)) + { + RsTokenService& tChannels = *mChannels.getTokenService(); + + time_t start = time(NULL); + while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + if(tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + resp.setOk(); + else resp.setFail("Unknown GXS error!"); + } + else resp.setFail("Unkown GXS error!"); +} + +void ChannelsHandler::handleToggleAutoDownload(Request& req, Response& resp) +{ + + std::string chanIdStr; + bool autoDownload = true; + + req.mStream << makeKeyValueReference("channel_id", chanIdStr) + << makeKeyValueReference("auto_download", autoDownload); + + if(chanIdStr.empty()) + { + resp.setFail("channel_id required!"); + return; + } + + RsGxsGroupId chanId(chanIdStr); + if(chanId.isNull()) + { + resp.setFail("Invalid channel_id:" + chanIdStr); + return; + } + + if(mChannels.setChannelAutoDownload(chanId, autoDownload)) + resp.setOk(); + else resp.setFail(); +} + +void ChannelsHandler::handleTogglePostRead(Request& req, Response& resp) +{ + std::string chanIdStr; + std::string postIdStr; + bool read = true; + + req.mStream << makeKeyValueReference("channel_id", chanIdStr) + << makeKeyValueReference("post_id", postIdStr) + << makeKeyValueReference("read", read); + + if(chanIdStr.empty()) + { + resp.setFail("channel_id required!"); + return; + } + + RsGxsGroupId chanId(chanIdStr); + if(chanId.isNull()) + { + resp.setFail("Invalid channel_id:" + chanIdStr); + return; + } + + if(postIdStr.empty()) + { + resp.setFail("post_id required!"); + return; + } + + RsGxsMessageId postId(postIdStr); + if(postId.isNull()) + { + resp.setFail("Invalid post_id:" + postIdStr); + return; + } + + std::cerr << __PRETTY_FUNCTION__ << " " << chanIdStr << " " << postIdStr + << " " << read << std::endl; + + uint32_t token; + mChannels.setMessageReadStatus(token, std::make_pair(chanId,postId), read); + + RsTokenService& tChannels = *mChannels.getTokenService(); + + time_t start = time(NULL); + while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + if(tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + resp.setOk(); + else resp.setFail("Unknown GXS error!"); +} + +void ChannelsHandler::handleCreatePost(Request &req, Response &resp) { RsGxsChannelPost post; - req.mStream << makeKeyValueReference("group_id", post.mMeta.mGroupId); + req.mStream << makeKeyValueReference("channel_id", post.mMeta.mGroupId); req.mStream << makeKeyValueReference("subject", post.mMeta.mMsgName); req.mStream << makeKeyValueReference("message", post.mMsg); @@ -53,36 +397,36 @@ ResponseTask* ChannelsHandler::handleCreatePost(Request &req, Response &resp) if(post.mMeta.mGroupId.isNull()) { - resp.setFail("groupd_id is null"); - return 0; + resp.setFail("groupd_id is null"); + return; } if(post.mMeta.mMsgName.empty()) { - resp.setFail("subject is empty"); - return 0; + resp.setFail("subject is empty"); + return; } if(post.mMsg.empty()) { - resp.setFail("msg text is empty"); - return 0; + resp.setFail("msg text is empty"); + return; } // empty file list is ok, but files have to be valid for(std::list::iterator lit = post.mFiles.begin(); lit != post.mFiles.end(); ++lit) { if(lit->mHash.isNull()) { - resp.setFail("at least one file hash is empty"); - return 0; + resp.setFail("at least one file hash is empty"); + return; } if(lit->mName.empty()) { - resp.setFail("at leats one file name is empty"); - return 0; + resp.setFail("at leats one file name is empty"); + return; } if(lit->mSize == 0) { - resp.setFail("at least one file size is empty"); - return 0; + resp.setFail("at least one file size is empty"); + return; } } @@ -91,22 +435,33 @@ ResponseTask* ChannelsHandler::handleCreatePost(Request &req, Response &resp) { if(png_data.size() < 8) { - resp.setFail("Decoded thumbnail_base64_png is smaller than 8 byte. This can't be a valid png file!"); - return 0; + resp.setFail("Decoded thumbnail_base64_png is smaller than 8 byte. This can't be a valid png file!"); + return; } uint8_t png_magic_number[] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a}; if(!std::equal(&png_magic_number[0],&png_magic_number[8],png_data.begin())) { - resp.setFail("Decoded thumbnail_base64_png does not seem to be a png file. (Header is missing magic number)"); - return 0; + resp.setFail("Decoded thumbnail_base64_png does not seem to be a png file. (Header is missing magic number)"); + return; } post.mThumbnail.copy(png_data.data(), png_data.size()); } uint32_t token; - mChannels->createPost(token, post); - // TODO: grp creation acknowledge - return 0; + if(mChannels.createPost(token, post)) + { + RsTokenService& tChannels = *mChannels.getTokenService(); + + time_t start = time(NULL); + while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + if(tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + resp.setOk(); + else resp.setFail("Unknown GXS error!"); + } + else resp.setFail("Unknown GXS error!"); } } // namespace resource_api diff --git a/libresapi/src/api/ChannelsHandler.h b/libresapi/src/api/ChannelsHandler.h index 1d8559f52..b40f23b73 100644 --- a/libresapi/src/api/ChannelsHandler.h +++ b/libresapi/src/api/ChannelsHandler.h @@ -1,4 +1,21 @@ #pragma once +/* + * RetroShare JSON API + * Copyright (C) 2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ #include "ResourceRouter.h" @@ -7,15 +24,20 @@ class RsGxsChannels; namespace resource_api { -class ChannelsHandler : public ResourceRouter +struct ChannelsHandler : ResourceRouter { -public: - ChannelsHandler(RsGxsChannels* channels); + ChannelsHandler(RsGxsChannels& channels); private: - ResponseTask* handleCreatePost(Request& req, Response& resp); + void handleListChannels(Request& req, Response& resp); + void handleGetChannel(Request& req, Response& resp); + void handleToggleSubscription(Request& req, Response& resp); + void handleCreateChannel(Request& req, Response& resp); + void handleToggleAutoDownload(Request& req, Response& resp); + void handleTogglePostRead(Request& req, Response& resp); + void handleCreatePost(Request& req, Response& resp); - RsGxsChannels* mChannels; + RsGxsChannels& mChannels; }; } // namespace resource_api diff --git a/libretroshare/src/retroshare/rsgxscommon.h b/libretroshare/src/retroshare/rsgxscommon.h index 6d4b1655e..b1579e406 100644 --- a/libretroshare/src/retroshare/rsgxscommon.h +++ b/libretroshare/src/retroshare/rsgxscommon.h @@ -124,24 +124,27 @@ class RsGxsComment }; -class RsGxsCommentService +struct RsGxsCommentService { - public: + RsGxsCommentService() {} + virtual ~RsGxsCommentService() {} - RsGxsCommentService() { return; } -virtual ~RsGxsCommentService() { return; } + /** Get previously requested comment data with token */ + virtual bool getCommentData( uint32_t token, + std::vector &comments ) = 0; + virtual bool getRelatedComments( uint32_t token, + std::vector &comments ) = 0; -virtual bool getCommentData(const uint32_t &token, std::vector &comments) = 0; -virtual bool getRelatedComments(const uint32_t &token, std::vector &comments) = 0; + virtual bool createComment(uint32_t &token, RsGxsComment &comment) = 0; + virtual bool createVote(uint32_t &token, RsGxsVote &vote) = 0; -//virtual bool getDetailedCommentData(const uint32_t &token, std::vector &comments); - -virtual bool createComment(uint32_t &token, RsGxsComment &comment) = 0; -virtual bool createVote(uint32_t &token, RsGxsVote &vote) = 0; - -virtual bool acknowledgeComment(const uint32_t& token, std::pair& msgId) = 0; -virtual bool acknowledgeVote(const uint32_t& token, std::pair& msgId) = 0; + virtual bool acknowledgeComment( + uint32_t token, + std::pair& msgId ) = 0; + virtual bool acknowledgeVote( + uint32_t token, + std::pair& msgId ) = 0; }; diff --git a/libretroshare/src/services/p3gxschannels.cc b/libretroshare/src/services/p3gxschannels.cc index d2578d470..2e412034f 100644 --- a/libretroshare/src/services/p3gxschannels.cc +++ b/libretroshare/src/services/p3gxschannels.cc @@ -1179,7 +1179,9 @@ void p3GxsChannels::setMessageProcessedStatus(uint32_t& token, const RsGxsGrpMsg setMsgStatusFlags(token, msgId, status, mask); } -void p3GxsChannels::setMessageReadStatus(uint32_t& token, const RsGxsGrpMsgIdPair& msgId, bool read) +void p3GxsChannels::setMessageReadStatus( uint32_t& token, + const RsGxsGrpMsgIdPair& msgId, + bool read ) { #ifdef GXSCHANNELS_DEBUG std::cerr << "p3GxsChannels::setMessageReadStatus()"; @@ -1189,10 +1191,8 @@ void p3GxsChannels::setMessageReadStatus(uint32_t& token, const RsGxsGrpMsgIdPai /* Always remove status unprocessed */ uint32_t mask = GXS_SERV::GXS_MSG_STATUS_GUI_NEW | GXS_SERV::GXS_MSG_STATUS_GUI_UNREAD; uint32_t status = GXS_SERV::GXS_MSG_STATUS_GUI_UNREAD; - if (read) - { - status = 0; - } + if (read) status = 0; + setMsgStatusFlags(token, msgId, status, mask); } diff --git a/libretroshare/src/services/p3gxschannels.h b/libretroshare/src/services/p3gxschannels.h index 4eb5f5fba..0e6156f48 100644 --- a/libretroshare/src/services/p3gxschannels.h +++ b/libretroshare/src/services/p3gxschannels.h @@ -108,15 +108,12 @@ virtual bool setChannelDownloadDirectory(const RsGxsGroupId &groupId, const std: virtual bool getChannelDownloadDirectory(const RsGxsGroupId &groupId, std::string& directory); /* Comment service - Provide RsGxsCommentService - redirect to p3GxsCommentService */ -virtual bool getCommentData(const uint32_t &token, std::vector &msgs) - { - return mCommentService->getGxsCommentData(token, msgs); - } + virtual bool getCommentData(uint32_t token, std::vector &msgs) + { return mCommentService->getGxsCommentData(token, msgs); } -virtual bool getRelatedComments(const uint32_t &token, std::vector &msgs) - { - return mCommentService->getGxsRelatedComments(token, msgs); - } + virtual bool getRelatedComments( uint32_t token, + std::vector &msgs ) + { return mCommentService->getGxsRelatedComments(token, msgs); } virtual bool createComment(uint32_t &token, RsGxsComment &msg) { @@ -128,13 +125,13 @@ virtual bool createVote(uint32_t &token, RsGxsVote &msg) return mCommentService->createGxsVote(token, msg); } -virtual bool acknowledgeComment(const uint32_t& token, std::pair& msgId) +virtual bool acknowledgeComment(uint32_t token, std::pair& msgId) { return acknowledgeMsg(token, msgId); } -virtual bool acknowledgeVote(const uint32_t& token, std::pair& msgId) +virtual bool acknowledgeVote(uint32_t token, std::pair& msgId) { if (mCommentService->acknowledgeVote(token, msgId)) { diff --git a/libretroshare/src/services/p3posted.h b/libretroshare/src/services/p3posted.h index 0cb2bcd74..aa4bbc8ee 100644 --- a/libretroshare/src/services/p3posted.h +++ b/libretroshare/src/services/p3posted.h @@ -83,16 +83,14 @@ virtual void setMessageReadStatus(uint32_t& token, const RsGxsGrpMsgIdPair& msgI } - /* Comment service - Provide RsGxsCommentService - redirect to p3GxsCommentService */ -virtual bool getCommentData(const uint32_t &token, std::vector &msgs) - { - return mCommentService->getGxsCommentData(token, msgs); - } + /** Comment service - Provide RsGxsCommentService - + * redirect to p3GxsCommentService */ + virtual bool getCommentData(uint32_t token, std::vector &msgs) + { return mCommentService->getGxsCommentData(token, msgs); } -virtual bool getRelatedComments(const uint32_t &token, std::vector &msgs) - { - return mCommentService->getGxsRelatedComments(token, msgs); - } + virtual bool getRelatedComments( uint32_t token, + std::vector &msgs ) + { return mCommentService->getGxsRelatedComments(token, msgs); } virtual bool createComment(uint32_t &token, RsGxsComment &msg) { @@ -104,17 +102,14 @@ virtual bool createVote(uint32_t &token, RsGxsVote &msg) return mCommentService->createGxsVote(token, msg); } -virtual bool acknowledgeComment(const uint32_t& token, std::pair& msgId) - { - return acknowledgeMsg(token, msgId); - } + virtual bool acknowledgeComment( + uint32_t token, std::pair& msgId ) + { return acknowledgeMsg(token, msgId); } -virtual bool acknowledgeVote(const uint32_t& token, std::pair& msgId) + virtual bool acknowledgeVote( + uint32_t token, std::pair& msgId ) { - if (mCommentService->acknowledgeVote(token, msgId)) - { - return true; - } + if (mCommentService->acknowledgeVote(token, msgId)) return true; return acknowledgeMsg(token, msgId); } }; From 82fa54a735903549b5e961dfee9ff41d0141bfae Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Thu, 29 Mar 2018 11:28:23 +0200 Subject: [PATCH 053/161] Avoid using id as JSON field as it may interfere with QML --- libresapi/src/api/ChannelsHandler.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libresapi/src/api/ChannelsHandler.cpp b/libresapi/src/api/ChannelsHandler.cpp index d1812dc3d..52a3527ac 100644 --- a/libresapi/src/api/ChannelsHandler.cpp +++ b/libresapi/src/api/ChannelsHandler.cpp @@ -80,7 +80,7 @@ void ChannelsHandler::handleListChannels(Request& /*req*/, Response& resp) vit != grps.end(); ++vit ) { RsGxsChannelGroup& grp = *vit; - KeyValueReference id("id", grp.mMeta.mGroupId); + KeyValueReference id("channel_id", grp.mMeta.mGroupId); KeyValueReference vis_msg("visible_msg_count", grp.mMeta.mVisibleMsgCount); bool own = (grp.mMeta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN); bool subscribed = IS_GROUP_SUBSCRIBED(grp.mMeta.mSubscribeFlags); @@ -159,7 +159,7 @@ void ChannelsHandler::handleGetChannel(Request& req, Response& resp) resp.mDataStream.getStreamToMember() << makeKeyValueReference("channel_id", pMeta.mGroupId) << makeKeyValueReference("name", pMeta.mMsgName) - << makeKeyValueReference("id", pMeta.mMsgId) + << makeKeyValueReference("post_id", pMeta.mMsgId) << makeKeyValueReference("parent_id", pMeta.mParentId) << makeKeyValueReference("author_id", pMeta.mAuthorId) << makeKeyValueReference("orig_msg_id", pMeta.mOrigMsgId) @@ -176,7 +176,7 @@ void ChannelsHandler::handleGetChannel(Request& req, Response& resp) resp.mDataStream.getStreamToMember() << makeKeyValueReference("channel_id", cMeta.mGroupId) << makeKeyValueReference("name", cMeta.mMsgName) - << makeKeyValueReference("id", cMeta.mMsgId) + << makeKeyValueReference("comment_id", cMeta.mMsgId) << makeKeyValueReference("parent_id", cMeta.mParentId) << makeKeyValueReference("author_id", cMeta.mAuthorId) << makeKeyValueReference("orig_msg_id", cMeta.mOrigMsgId) From c821c179ef74a0b1ffe3979a3c56e998410765a0 Mon Sep 17 00:00:00 2001 From: hunbernd Date: Sat, 31 Mar 2018 22:39:58 +0200 Subject: [PATCH 054/161] Fix private chat icon not changing back to idle icon from new message icon when window activated Broken in add9c1e72c895a8774709ba676034ea59fbbdc23 --- retroshare-gui/src/gui/chat/ChatWidget.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index 2859527cd..bb0763bd3 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -743,6 +743,13 @@ bool ChatWidget::eventFilter(QObject *obj, QEvent *event) } } + } else { + if (event->type() == QEvent::WindowActivate) { + if (isVisible() && (window() == NULL || window()->isActiveWindow())) { + newMessages = false; + emit infoChanged(this); + } + } } // pass the event on to the parent class return QWidget::eventFilter(obj, event); From 6964ba41652e4f52aad8321f475ec946d57c4ac7 Mon Sep 17 00:00:00 2001 From: RetroPooh Date: Sun, 1 Apr 2018 14:13:21 +0300 Subject: [PATCH 055/161] fix tab index on serverpage --- retroshare-gui/src/gui/settings/ServerPage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/settings/ServerPage.cpp b/retroshare-gui/src/gui/settings/ServerPage.cpp index 6a019990a..448514006 100755 --- a/retroshare-gui/src/gui/settings/ServerPage.cpp +++ b/retroshare-gui/src/gui/settings/ServerPage.cpp @@ -1005,7 +1005,7 @@ void ServerPage::saveRates() void ServerPage::tabChanged(int page) { - if(page == 2) + if(page == TAB_HIDDEN_SERVICE) updateOutProxyIndicator(); } From e17edbfa1f6696e9176d0c50ef0334b02408e252 Mon Sep 17 00:00:00 2001 From: RetroPooh Date: Sun, 1 Apr 2018 14:13:42 +0300 Subject: [PATCH 056/161] fix default tab on serverpage --- retroshare-gui/src/gui/settings/ServerPage.ui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/settings/ServerPage.ui b/retroshare-gui/src/gui/settings/ServerPage.ui index 36e114a8f..f8f55423f 100755 --- a/retroshare-gui/src/gui/settings/ServerPage.ui +++ b/retroshare-gui/src/gui/settings/ServerPage.ui @@ -26,7 +26,7 @@ - 2 + 0 From 0a943ea9ee85213aed0c103716d57a0da35fd178 Mon Sep 17 00:00:00 2001 From: RetroPooh Date: Sun, 1 Apr 2018 17:26:48 +0300 Subject: [PATCH 057/161] log shut up --- libretroshare/src/pqi/p3linkmgr.cc | 8 +++++++- libretroshare/src/pqi/p3peermgr.cc | 4 +++- libretroshare/src/pqi/pqipersongrp.cc | 3 +++ libretroshare/src/pqi/pqissl.cc | 11 +++++++++++ libretroshare/src/pqi/pqisslproxy.cc | 7 +++++-- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/libretroshare/src/pqi/p3linkmgr.cc b/libretroshare/src/pqi/p3linkmgr.cc index c84b8ea4f..6c839d268 100644 --- a/libretroshare/src/pqi/p3linkmgr.cc +++ b/libretroshare/src/pqi/p3linkmgr.cc @@ -56,6 +56,7 @@ static struct RsLog::logInfo p3connectzoneInfo = {RsLog::Default, "p3connect"}; /**** * #define LINKMGR_DEBUG 1 + * #define LINKMGR_DEBUG_LOG 1 * #define LINKMGR_DEBUG_CONNFAIL 1 * #define LINKMGR_DEBUG_ACTIONS 1 * #define LINKMGR_DEBUG_LINKTYPE 1 @@ -620,7 +621,9 @@ bool p3LinkMgrIMPL::connectAttempt(const RsPeerId &id, struct sockaddr_storage & } +#ifdef LINKMGR_DEBUG_LOG rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::connectAttempt() called id: " + id.toStdString()); +#endif it->second.lastattempt = time(NULL); it->second.inConnAttempt = true; @@ -823,7 +826,9 @@ bool p3LinkMgrIMPL::connectResult(const RsPeerId &id, bool success, bool isIncom out += " FAILED ATTEMPT (Not Connected)"; } } +#ifdef LINKMGR_DEBUG_LOG rslog(RSL_WARNING, p3connectzone, out); +#endif } @@ -2057,8 +2062,9 @@ bool p3LinkMgrIMPL::locked_ConnectAttempt_Complete(peerConnectState *peer) int p3LinkMgrIMPL::addFriend(const RsPeerId &id, bool isVisible) { +#ifdef LINKMGR_DEBUG_LOG rslog(RSL_WARNING, p3connectzone, "p3LinkMgr::addFriend() id: " + id.toStdString()); - +#endif { RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index 9862838aa..0d1562b41 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -69,6 +69,7 @@ static struct RsLog::logInfo p3peermgrzoneInfo = {RsLog::Default, "p3peermgr"}; /**** * #define PEER_DEBUG 1 + * #define PEER_DEBUG_LOG 1 ***/ #define MAX_AVAIL_PERIOD 230 //times a peer stay in available state when not connected @@ -910,8 +911,9 @@ bool p3PeerMgrIMPL::addFriend(const RsPeerId& input_id, const RsPgpId& input_gpg RsPeerId id = input_id ; RsPgpId gpg_id = input_gpg_id ; +#ifdef PEER_DEBUG_LOG rslog(RSL_WARNING, p3peermgrzone, "p3PeerMgr::addFriend() id: " + id.toStdString()); - +#endif { RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ diff --git a/libretroshare/src/pqi/pqipersongrp.cc b/libretroshare/src/pqi/pqipersongrp.cc index a8afa2b84..3df48fdb3 100644 --- a/libretroshare/src/pqi/pqipersongrp.cc +++ b/libretroshare/src/pqi/pqipersongrp.cc @@ -45,6 +45,7 @@ static std::list waitingIds; /**** *#define PGRP_DEBUG 1 + *#define PGRP_DEBUG_LOG 1 ****/ #define DEFAULT_DOWNLOAD_KB_RATE (200.0) @@ -420,7 +421,9 @@ int pqipersongrp::addPeer(const RsPeerId& id) sm -> pqi = pqip; // reset it to start it working. +#ifdef PGRP_DEBUG_LOG pqioutput(PQL_WARNING, pqipersongrpzone, "pqipersongrp::addPeer() => reset() called to initialise new person"); +#endif pqip -> reset(); pqip -> listen(); diff --git a/libretroshare/src/pqi/pqissl.cc b/libretroshare/src/pqi/pqissl.cc index 898080262..41c8a523e 100644 --- a/libretroshare/src/pqi/pqissl.cc +++ b/libretroshare/src/pqi/pqissl.cc @@ -61,6 +61,7 @@ static struct RsLog::logInfo pqisslzoneInfo = {RsLog::Default, "pqisslzone"}; #define PQISSL_DEBUG 1 #define PQISSL_LOG_DEBUG 1 +#define PQISSL_LOG_DEBUG2 1 const int PQISSL_LOCAL_FLAG = 0x01; const int PQISSL_REMOTE_FLAG = 0x02; @@ -269,7 +270,9 @@ int pqissl::reset_locked() #endif } +#ifdef PQISSL_LOG_DEBUG2 rslog(RSL_ALERT, pqisslzone, outLog); +#endif // notify people of problem! // but only if we really shut something down. @@ -678,7 +681,9 @@ int pqissl::Initiate_Connection() std::string out; rs_sprintf(out, "pqissl::Initiate_Connection() Connecting To: %s via: ", PeerId().toStdString().c_str()); out += sockaddr_storage_tostring(addr); +#ifdef PQISSL_LOG_DEBUG2 rslog(RSL_WARNING, pqisslzone, out); +#endif } if (sockaddr_storage_isnull(addr)) @@ -832,10 +837,14 @@ bool pqissl::CheckConnectionTimeout() std::string out; rs_sprintf(out, "pqissl::Basic_Connection_Complete() Connection Timed Out. Peer: %s Period: %lu", PeerId().toStdString().c_str(), mConnectTimeout); +#ifdef PQISSL_LOG_DEBUG2 rslog(RSL_WARNING, pqisslzone, out); +#endif /* as sockfd is valid, this should close it all up */ +#ifdef PQISSL_LOG_DEBUG2 rslog(RSL_ALERT, pqisslzone, "pqissl::Basic_Connection_Complete() -> calling reset()"); +#endif reset_locked(); return true; } @@ -974,7 +983,9 @@ int pqissl::Basic_Connection_Complete() { std::string out; rs_sprintf(out, "pqissl::Basic_Connection_Complete() TCP Connection Complete: cert: %s on osock: ", PeerId().toStdString().c_str(), sockfd); +#ifdef PQISSL_LOG_DEBUG2 rslog(RSL_WARNING, pqisslzone, out); +#endif } return 1; } diff --git a/libretroshare/src/pqi/pqisslproxy.cc b/libretroshare/src/pqi/pqisslproxy.cc index 90444bcf9..500c6a7d8 100644 --- a/libretroshare/src/pqi/pqisslproxy.cc +++ b/libretroshare/src/pqi/pqisslproxy.cc @@ -42,6 +42,7 @@ static struct RsLog::logInfo pqisslproxyzoneInfo = {RsLog::Default, "pqisslproxy #define pqisslproxyzone &pqisslproxyzoneInfo // #define PROXY_DEBUG 1 +// #define PROXY_DEBUG_LOG 1 #define PROXY_STATE_FAILED 0 #define PROXY_STATE_INIT 1 @@ -593,8 +594,9 @@ bool pqisslproxy::connect_parameter(uint32_t type, const std::string &value) { std::string out; rs_sprintf(out, "pqisslproxy::connect_parameter() Peer: %s DOMAIN_ADDRESS: %s", PeerId().toStdString().c_str(), value.c_str()); +#ifdef PROXY_DEBUG_LOG rslog(RSL_WARNING, pqisslproxyzone, out); - +#endif mDomainAddress = value; #ifdef PROXY_DEBUG std::cerr << out << std::endl; @@ -615,8 +617,9 @@ bool pqisslproxy::connect_parameter(uint32_t type, uint32_t value) { std::string out; rs_sprintf(out, "pqisslproxy::connect_parameter() Peer: %s REMOTE_PORT: %lu", PeerId().toStdString().c_str(), value); +#ifdef PROXY_DEBUG_LOG rslog(RSL_WARNING, pqisslproxyzone, out); - +#endif mRemotePort = value; #ifdef PROXY_DEBUG std::cerr << out << std::endl; From fdb8dc568c8676f78dbf643d0b0d9a133d4ca5ea Mon Sep 17 00:00:00 2001 From: RetroPooh Date: Mon, 2 Apr 2018 19:38:56 +0300 Subject: [PATCH 058/161] fix unicode display for chat status --- retroshare-gui/src/gui/chat/ChatWidget.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index 2859527cd..00b258d83 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -1791,7 +1791,7 @@ void ChatWidget::updatePeersCustomStateString(const QString& /*peer_id*/, const void ChatWidget::updateStatusString(const QString &statusMask, const QString &statusString, bool permanent) { - ui->typingLabel->setText(QString(statusMask).arg(tr(statusString.toUtf8()))); // displays info for 5 secs. + ui->typingLabel->setText(QString(statusMask).arg(trUtf8(statusString.toUtf8()))); // displays info for 5 secs. ui->typingPixmapLabel->setPixmap(QPixmap(":images/typing.png") ); if (statusString == "is typing...") { From 234b576f382ca49ae12ba0a032ef6a39f3f3f5f7 Mon Sep 17 00:00:00 2001 From: RetroPooh Date: Mon, 2 Apr 2018 19:43:21 +0300 Subject: [PATCH 059/161] change distant chat status indicator color to differ closed and pending --- retroshare-gui/src/gui/chat/PopupDistantChatDialog.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/chat/PopupDistantChatDialog.cpp b/retroshare-gui/src/gui/chat/PopupDistantChatDialog.cpp index f457e11d9..da662fcb6 100644 --- a/retroshare-gui/src/gui/chat/PopupDistantChatDialog.cpp +++ b/retroshare-gui/src/gui/chat/PopupDistantChatDialog.cpp @@ -135,7 +135,7 @@ void PopupDistantChatDialog::updateDisplay() break ; case RS_DISTANT_CHAT_STATUS_TUNNEL_DN: //std::cerr << "Tunnel asked. Waiting for reponse. " << std::endl; - _status_label->setIcon(QIcon(IMAGE_RED_LED)); + _status_label->setIcon(QIcon(IMAGE_YEL_LED)); msg = QObject::tr( "Tunnel is pending... Messages will be delivered as" " soon as possible" ); _status_label->setToolTip(msg); From cc091cc2c8872c555c09d30b3c741390f699713d Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 7 Apr 2018 12:48:01 +0200 Subject: [PATCH 060/161] Fixed hidden nodes listening failure In case of hidden node the listen address was not properly converted to ipv4 mapped format causing bind to fail Use sockaddr_storage_copy instead of = as sockaddr_storage is not guaranted to be copyable --- libretroshare/src/pqi/pqissl.cc | 17 ++++++++------- libretroshare/src/pqi/pqissllistener.cc | 28 +++++++++++-------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/libretroshare/src/pqi/pqissl.cc b/libretroshare/src/pqi/pqissl.cc index 48186b755..58c7fc856 100644 --- a/libretroshare/src/pqi/pqissl.cc +++ b/libretroshare/src/pqi/pqissl.cc @@ -589,7 +589,10 @@ int pqissl::Initiate_Connection() #endif int err; - sockaddr_storage addr = remote_addr; + sockaddr_storage addr; sockaddr_storage_copy(remote_addr, addr); + + std::cerr << __PRETTY_FUNCTION__ << " " << sockaddr_storage_tostring(addr) + << std::endl; if(waiting != WAITING_DELAY) @@ -638,13 +641,6 @@ int pqissl::Initiate_Connection() return -1; } - { - std::string out; - rs_sprintf(out, "pqissl::Initiate_Connection() Connecting To: %s via: ", PeerId().toStdString().c_str()); - out += sockaddr_storage_tostring(addr); - rslog(RSL_WARNING, pqisslzone, out); - } - if (sockaddr_storage_isnull(addr)) { rslog(RSL_WARNING, pqisslzone, "pqissl::Initiate_Connection() Invalid (0.0.0.0) Remote Address, Aborting Connect."); @@ -721,6 +717,11 @@ int pqissl::Initiate_Connection() //std::cerr << "Setting Connect Timeout " << mConnectTimeout << " Seconds into Future " << std::endl; sockaddr_storage_ipv4_to_ipv6(addr); + + std::cerr << __PRETTY_FUNCTION__ << " Connecting To: " + << PeerId().toStdString() <<" via: " + << sockaddr_storage_tostring(addr) << std::endl; + if (0 != (err = unix_connect(osock, addr))) { switch (errno) diff --git a/libretroshare/src/pqi/pqissllistener.cc b/libretroshare/src/pqi/pqissllistener.cc index 15c754888..b93eb1e97 100644 --- a/libretroshare/src/pqi/pqissllistener.cc +++ b/libretroshare/src/pqi/pqissllistener.cc @@ -60,8 +60,10 @@ static struct RsLog::logInfo pqissllistenzoneInfo = {RsLog::Default, "p3peermgr" pqissllistenbase::pqissllistenbase(const sockaddr_storage &addr, p3PeerMgr *pm) - : laddr(addr), mPeerMgr(pm), active(false) + : mPeerMgr(pm), active(false) { + sockaddr_storage_copy(addr, laddr); + if (!(AuthSSL::getAuthSSL()-> active())) { pqioutput(PQL_ALERT, pqissllistenzone, @@ -208,26 +210,20 @@ int pqissllistenbase::setuplisten() } } -#ifdef OPEN_UNIVERSAL_PORT - struct sockaddr_storage tmpaddr = laddr; - if (!mPeerMgr->isHidden()) - { - tmpaddr.ss_family = PF_INET6; - sockaddr_storage_zeroip(tmpaddr); - } + struct sockaddr_storage tmpaddr; + sockaddr_storage_copy(laddr, tmpaddr); + sockaddr_storage_ipv4_to_ipv6(tmpaddr); + if (!mPeerMgr->isHidden()) sockaddr_storage_zeroip(tmpaddr); + if (0 != (err = rs_bind(lsock, tmpaddr))) -#else - if (0 != (err = universal_bind(lsock, laddr))) -#endif { std::string out = "pqissllistenbase::setuplisten() Cannot Bind to Local Address!\n"; showSocketError(out); pqioutput(PQL_ALERT, pqissllistenzone, out); - std::cerr << out << std::endl; - std::cerr << "laddr: " << sockaddr_storage_tostring(laddr) << std::endl; -#ifdef OPEN_UNIVERSAL_PORT - if (!mPeerMgr->isHidden()) std::cerr << "Zeroed tmpaddr: " << sockaddr_storage_tostring(tmpaddr) << std::endl; -#endif + std::cerr << out << std::endl + << "tmpaddr: " << sockaddr_storage_tostring(tmpaddr) + << std::endl; + print_stacktrace(); return -1; } From e2b0e27205e75d2e0065a654249a49e09572c174 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 7 Apr 2018 00:56:07 +0200 Subject: [PATCH 061/161] fixed costly polling in RsGenExchange --- libretroshare/src/gxs/rsgenexchange.cc | 30 +++++++++++++++----------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 14a2471e1..11137f8b0 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -64,7 +64,7 @@ static const uint32_t INDEX_AUTHEN_ADMIN = 0x00000040; // admin key #define GXS_MASK "GXS_MASK_HACK" -//#define GEN_EXCH_DEBUG 1 +#define GEN_EXCH_DEBUG 1 static const uint32_t MSG_CLEANUP_PERIOD = 60*59; // 59 minutes static const uint32_t INTEGRITY_CHECK_PERIOD = 60*31; // 31 minutes @@ -2860,8 +2860,10 @@ void RsGenExchange::processRecvdMessages() time_t now = time(NULL); + if(mMsgPendingValidate.empty()) + return ; #ifdef GEN_EXCH_DEBUG - if(!mMsgPendingValidate.empty()) + else std::cerr << "processing received messages" << std::endl; #endif // 1 - First, make sure items metadata is deserialised, clean old failed items, and collect the groups Ids we have to check @@ -2904,9 +2906,11 @@ void RsGenExchange::processRecvdMessages() } } - // 2 - Retrieve the metadata for the associated groups. + // 2 - Retrieve the metadata for the associated groups. The test is here to avoid the default behavior to + // retrieve all groups when the list is empty - mDataStore->retrieveGxsGrpMetaData(grpMetas); + if(!grpMetas.empty()) + mDataStore->retrieveGxsGrpMetaData(grpMetas); GxsMsgReq msgIds; RsNxsMsgDataTemporaryList msgs_to_store; @@ -2934,7 +2938,7 @@ void RsGenExchange::processRecvdMessages() // } #ifdef GEN_EXCH_DEBUG - std::cerr << " deserialised info: grp id=" << meta->mGroupId << ", msg id=" << meta->mMsgId ; + std::cerr << " deserialised info: grp id=" << msg->grpId << ", msg id=" << msg->msgId ; #endif std::map::iterator mit = grpMetas.find(msg->grpId); @@ -2977,8 +2981,8 @@ void RsGenExchange::processRecvdMessages() msg->metaData->recvTS = time(NULL); #ifdef GEN_EXCH_DEBUG - std::cerr << " new status flags: " << meta->mMsgStatus << std::endl; - std::cerr << " computed hash: " << meta->mHash << std::endl; + std::cerr << " new status flags: " << msg->metaData->mMsgStatus << std::endl; + std::cerr << " computed hash: " << msg->metaData->mHash << std::endl; std::cerr << "Message received. Identity=" << msg->metaData->mAuthorId << ", from peer " << msg->PeerId() << std::endl; #endif @@ -3062,9 +3066,6 @@ void RsGenExchange::processRecvdGroups() GxsPendingItem& gpsi = vit->second; RsNxsGrp* grp = gpsi.mItem; -#ifdef GEN_EXCH_DEBUG - std::cerr << " processing validation for group " << meta->mGroupId << ", original attempt time: " << time(NULL) - gpsi.mFirstTryTS << " seconds ago" << std::endl; -#endif if(grp->metaData == NULL) { RsGxsGrpMetaData* meta = new RsGxsGrpMetaData(); @@ -3074,6 +3075,9 @@ void RsGenExchange::processRecvdGroups() else delete meta ; } +#ifdef GEN_EXCH_DEBUG + std::cerr << " processing validation for group " << grp->metaData->mGroupId << ", original attempt time: " << time(NULL) - gpsi.mFirstTryTS << " seconds ago" << std::endl; +#endif // early deletion of group from the pending list if it's malformed, not accepted, or has been tried unsuccessfully for too long @@ -3102,7 +3106,7 @@ void RsGenExchange::processRecvdGroups() if(!grp->metaData->mAuthorId.isNull()) { #ifdef GEN_EXCH_DEBUG - std::cerr << "Group routage info: Identity=" << meta->mAuthorId << " from " << grp->PeerId() << std::endl; + std::cerr << "Group routage info: Identity=" << grp->metaData->mAuthorId << " from " << grp->PeerId() << std::endl; #endif mRoutingClues[grp->metaData->mAuthorId].insert(grp->PeerId()) ; } @@ -3349,7 +3353,7 @@ void RsGenExchange::removeDeleteExistingMessages( std::list& msgs, Gx const RsGxsMessageId::std_vector& msgIds = msgIdReq[(*cit2)->metaData->mGroupId]; #ifdef GEN_EXCH_DEBUG - std::cerr << " grpid=" << cit2->second->mGroupId << ", msgid=" << cit2->second->mMsgId ; + std::cerr << " grpid=" << (*cit2)->grpId << ", msgid=" << (*cit2)->msgId ; #endif // Avoid storing messages that are already in the database, as well as messages that are too old (or generally do not pass the database storage test) @@ -3369,7 +3373,7 @@ void RsGenExchange::removeDeleteExistingMessages( std::list& msgs, Gx } } #ifdef GEN_EXCH_DEBUG - std::cerr << " discarding " << cit2->second->mMsgId << std::endl; + std::cerr << " discarding " << (*cit2)->msgId << std::endl; #endif delete *cit2; From 27824943ffd582ed713b7c2bf05f646a0ad26661 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 7 Apr 2018 14:29:23 +0200 Subject: [PATCH 062/161] removed debug info --- libretroshare/src/gxs/rsgenexchange.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 11137f8b0..15d0aa9e9 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -64,7 +64,7 @@ static const uint32_t INDEX_AUTHEN_ADMIN = 0x00000040; // admin key #define GXS_MASK "GXS_MASK_HACK" -#define GEN_EXCH_DEBUG 1 +//#define GEN_EXCH_DEBUG 1 static const uint32_t MSG_CLEANUP_PERIOD = 60*59; // 59 minutes static const uint32_t INTEGRITY_CHECK_PERIOD = 60*31; // 31 minutes From c19919962d1f2173da79d5f81f539412c9a69c97 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sun, 8 Apr 2018 12:37:41 +0200 Subject: [PATCH 063/161] pqissl silence extra debug message --- libretroshare/src/pqi/pqissl.cc | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/libretroshare/src/pqi/pqissl.cc b/libretroshare/src/pqi/pqissl.cc index 58c7fc856..57b4f5e18 100644 --- a/libretroshare/src/pqi/pqissl.cc +++ b/libretroshare/src/pqi/pqissl.cc @@ -585,16 +585,13 @@ int pqissl::Delay_Connection() int pqissl::Initiate_Connection() { #ifdef PQISSL_DEBUG - std::cout << __PRETTY_FUNCTION__ << std::endl; + std::cerr << __PRETTY_FUNCTION__ << " " + << sockaddr_storage_tostring(remote_addr) << std::endl; #endif int err; sockaddr_storage addr; sockaddr_storage_copy(remote_addr, addr); - std::cerr << __PRETTY_FUNCTION__ << " " << sockaddr_storage_tostring(addr) - << std::endl; - - if(waiting != WAITING_DELAY) { std::cerr << __PRETTY_FUNCTION__ << " Already Attempt in Progress!" From eccff5358af0eaf9c900262221e592b5cfe773ff Mon Sep 17 00:00:00 2001 From: Phenom Date: Sun, 4 Mar 2018 16:45:51 +0100 Subject: [PATCH 064/161] Add a timer in SharedFilesDialog filter to not so often triggs search in files. --- .../gui/FileTransfer/SharedFilesDialog.cpp | 62 +++++++++++++------ .../src/gui/FileTransfer/SharedFilesDialog.h | 9 ++- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index ae6377120..e423472e2 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -186,7 +186,14 @@ SharedFilesDialog::SharedFilesDialog(RetroshareDirModel *_tree_model,RetroshareD connect(ui.filterClearButton, SIGNAL(clicked()), this, SLOT(clearFilter())); connect(ui.filterStartButton, SIGNAL(clicked()), this, SLOT(startFilter())); connect(ui.filterPatternLineEdit, SIGNAL(returnPressed()), this, SLOT(startFilter())); - connect(ui.filterPatternLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(filterRegExpChanged())); + connect(ui.filterPatternLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(onFilterTextEdited())); + //Hidden by default, shown on onFilterTextEdited + ui.filterClearButton->hide(); + ui.filterStartButton->hide(); + + mFilterTimer = new RsProtectedTimer( this ); + mFilterTimer->setSingleShot( true ); // Ensure the timer will fire only once after it was started + connect(mFilterTimer, SIGNAL(timeout()), this, SLOT(filterRegExpChanged())); /* Set header resize modes and initial section sizes */ QHeaderView * header = ui.dirTreeView->header () ; @@ -1246,34 +1253,40 @@ void SharedFilesDialog::indicatorChanged(int index) updateDisplay() ; } -void SharedFilesDialog::filterRegExpChanged() +void SharedFilesDialog::onFilterTextEdited() { - QString text = ui.filterPatternLineEdit->text(); + QString text = ui.filterPatternLineEdit->text(); - if (text.isEmpty()) { - ui.filterClearButton->hide(); - } else { - ui.filterClearButton->show(); - } + if (text.isEmpty()) { + ui.filterClearButton->hide(); + } else { + ui.filterClearButton->show(); + } - if (text == lastFilterString) { - ui.filterStartButton->hide(); - } else { - ui.filterStartButton->show(); - } - - //bool valid = false ; - //QColor color ; + if (text == lastFilterString) { + ui.filterStartButton->hide(); + } else { + ui.filterStartButton->show(); + } if(text.length() > 0 && text.length() < 3) { - //valid = false; - ui.filterStartButton->setEnabled(false) ; ui.filterPatternFrame->setToolTip(tr("Search string should be at least 3 characters long.")) ; return ; } + ui.filterStartButton->setEnabled(true) ; + ui.filterPatternFrame->setToolTip(QString()); + + mFilterTimer->start( 500 ); // This will fire filterRegExpChanged after 500 ms. + // If the user types something before it fires, the timer restarts counting +} + +void SharedFilesDialog::filterRegExpChanged() +{ + QString text = ui.filterPatternLineEdit->text(); + if(text.length() > 0 && proxyModel == tree_proxyModel) { std::list result_list ; @@ -1497,8 +1510,19 @@ void SharedFilesDialog::FilterItems() std::cerr << "Found " << result_list.size() << " results" << std::endl; #endif - if(result_list.size() > MAX_SEARCH_RESULTS) + size_t resSize = result_list.size(); + if(resSize == 0) + { + ui.filterPatternFrame->setToolTip(tr("No result.")) ; return ; + } + if(resSize > MAX_SEARCH_RESULTS) + { + ui.filterPatternFrame->setToolTip(tr("More than %1 results. Add more/longer search words to select less.").arg(MAX_SEARCH_RESULTS)) ; + return ; + } + ui.filterPatternFrame->setToolTip(tr("Found %1 results.").arg(resSize)) ; + #ifdef DEBUG_SHARED_FILES_DIALOG std::cerr << "Found this result: " << std::endl; #endif diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h index 982abd275..63a57683c 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h @@ -22,10 +22,13 @@ #ifndef _SHAREDFILESDIALOG_H #define _SHAREDFILESDIALOG_H -#include +#include "ui_SharedFilesDialog.h" + #include "RsAutoUpdatePage.h" #include "gui/RetroShareLink.h" -#include "ui_SharedFilesDialog.h" +#include "util/RsProtectedTimer.h" + +#include class RetroshareDirModel; class QSortFilterProxyModel; @@ -73,6 +76,7 @@ private slots: void indicatorChanged(int index); + void onFilterTextEdited(); void filterRegExpChanged(); void clearFilter(); void startFilter(); @@ -137,6 +141,7 @@ protected: QString lastFilterString; QString mLastFilterText ; + RsProtectedTimer* mFilterTimer; QList mHiddenIndexes; }; From d91964acb3cbfabef8a397f3e396e4e7129274fc Mon Sep 17 00:00:00 2001 From: Phenom Date: Sat, 14 Apr 2018 12:30:55 +0200 Subject: [PATCH 065/161] Disable Search in SharedFilesDialog while typping. --- retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index e423472e2..2fce386ae 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -81,6 +81,8 @@ #define MAX_SEARCH_RESULTS 3000 +#define DISABLE_SEARCH_WHILE_TYPING 1 + // Define to avoid using the search in treeview, because it is really slow for now. // //#define DONT_USE_SEARCH_IN_TREE_VIEW 1 @@ -1279,8 +1281,10 @@ void SharedFilesDialog::onFilterTextEdited() ui.filterStartButton->setEnabled(true) ; ui.filterPatternFrame->setToolTip(QString()); +#ifndef DISABLE_SEARCH_WHILE_TYPING mFilterTimer->start( 500 ); // This will fire filterRegExpChanged after 500 ms. // If the user types something before it fires, the timer restarts counting +#endif } void SharedFilesDialog::filterRegExpChanged() From f20705b36d074f5c8f8ffc457645ab9cb948e807 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sun, 15 Apr 2018 12:32:39 +0200 Subject: [PATCH 066/161] Workaround for systems that miss IPV6_V6ONLY This should improve compatibility with old systems that miss IPV6_V6ONLY and in particular for Windows XP --- libretroshare/src/util/rsnet.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libretroshare/src/util/rsnet.h b/libretroshare/src/util/rsnet.h index 1d028e991..a5381702c 100644 --- a/libretroshare/src/util/rsnet.h +++ b/libretroshare/src/util/rsnet.h @@ -58,6 +58,15 @@ int inet_aton(const char *name, struct in_addr *addr); #endif /********************************** WINDOWS/UNIX SPECIFIC PART ******************/ +/** + * Workaround for binary compatibility between Windows XP (which miss + * IPV6_V6ONLY define), and newer Windows that has it. + * @see http://lua-users.org/lists/lua-l/2013-04/msg00191.html + */ +#ifndef IPV6_V6ONLY +# define IPV6_V6ONLY 27 +#endif + /* 64 bit conversions */ #ifndef ntohll uint64_t ntohll(uint64_t x); From b36cb1ef17e4bad9632405f7c5dc9b96ef509827 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sun, 15 Apr 2018 12:41:54 +0200 Subject: [PATCH 067/161] Fix retroshare GUI building with no_libresapihttpserver --- retroshare-gui/src/gui/MainWindow.cpp | 5 ++++- retroshare-gui/src/gui/settings/rsettingswin.cpp | 5 ++++- retroshare-gui/src/main.cpp | 9 ++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/retroshare-gui/src/gui/MainWindow.cpp b/retroshare-gui/src/gui/MainWindow.cpp index 6219575b5..116b9637f 100644 --- a/retroshare-gui/src/gui/MainWindow.cpp +++ b/retroshare-gui/src/gui/MainWindow.cpp @@ -107,10 +107,13 @@ #include "gui/common/RsCollection.h" #include "settings/rsettingswin.h" #include "settings/rsharesettings.h" -#include "settings/WebuiPage.h" #include "common/StatusDefs.h" #include "gui/notifyqt.h" +#ifdef ENABLE_WEBUI +# include "settings/WebuiPage.h" +#endif + #include #include diff --git a/retroshare-gui/src/gui/settings/rsettingswin.cpp b/retroshare-gui/src/gui/settings/rsettingswin.cpp index 3a7df34c9..0a2b348f0 100644 --- a/retroshare-gui/src/gui/settings/rsettingswin.cpp +++ b/retroshare-gui/src/gui/settings/rsettingswin.cpp @@ -41,12 +41,15 @@ #include "PostedPage.h" #include "PluginsPage.h" #include "ServicePermissionsPage.h" -#include "WebuiPage.h" #include "rsharesettings.h" #include "gui/notifyqt.h" #include "gui/common/FloatingHelpBrowser.h" #include "gui/common/RSElidedItemDelegate.h" +#ifdef ENABLE_WEBUI +# include "WebuiPage.h" +#endif + #define IMAGE_GENERAL ":/images/kcmsystem24.png" #define ITEM_SPACING 2 diff --git a/retroshare-gui/src/main.cpp b/retroshare-gui/src/main.cpp index 54979f983..acd56d9ef 100644 --- a/retroshare-gui/src/main.cpp +++ b/retroshare-gui/src/main.cpp @@ -41,16 +41,19 @@ #include "gui/FileTransfer/TransfersDialog.h" #include "gui/settings/RsharePeerSettings.h" #include "gui/settings/rsharesettings.h" -#include "gui/settings/WebuiPage.h" #include "idle/idle.h" #include "lang/languagesupport.h" #include "util/RsGxsUpdateBroadcast.h" #include "util/rsdir.h" #include "util/rstime.h" +#ifdef ENABLE_WEBUI +# include "gui/settings/WebuiPage.h" +#endif + #ifdef RETROTOR -#include "TorControl/TorManager.h" -#include "TorControl/TorControlWindow.h" +# include "TorControl/TorManager.h" +# include "TorControl/TorControlWindow.h" #endif #include "retroshare/rsidentity.h" From 4876a0ea3b87abfe7c404f4bc5fbb43c44577820 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sun, 15 Apr 2018 13:41:52 +0200 Subject: [PATCH 068/161] Multiple improvements to build process Restructure and document retroshare.pri variables and helper functions Link sqlite statically like other libraries on Android qmake has multiple win32-* specs match them correctly Move a buch of generic thing to retroshare.pri instead of having them replocated accross project specific .pro Fix retroshare-gui too many symbols exported on windows liking error by adding QMAKE_LFLAGS+=-Wl,--exclude-libs,ALL Rename retroshare-gui/src/util/win32.h because the name is too prone to confusion and build conflicts libreasapi avoid usage of cretar_prl link_prl that seems unreliable on some platforms libreasapi rename LIBRESAPI_QT to more understendable LIBRESAPI_SETTINGS Use use_*.pri instead of copy pasting error prone qmake snippets around Expose bitdht option to retroshare.pri Add RS_THREAD_LIB qmake variable for better crossplatform support Move debug and profiling generic options to retroshare.pri (avoid copy/pasting) Remove Qt script module in qmake as it is not available anymore --- RetroShare.pro | 3 +- android-prepare-toolchain.sh | 4 +- libbitdht/src/libbitdht.pro | 17 +- libbitdht/src/use_libbitdht.pri | 5 + libresapi/src/api/ApiServer.cpp | 8 +- libresapi/src/libresapi.pro | 60 +-- libresapi/src/use_libresapi.pri | 27 + libretroshare/src/libretroshare.pro | 77 +-- libretroshare/src/use_libretroshare.pri | 33 ++ openpgpsdk/src/openpgpsdk.pro | 27 +- openpgpsdk/src/use_openpgpsdk.pri | 20 + .../src/retroshare-android-service.pro | 39 +- .../src/gui/settings/rsharesettings.cpp | 2 +- retroshare-gui/src/retroshare-gui.pro | 98 +--- retroshare-gui/src/rshare.h | 2 +- .../util/{win32.cpp => retroshareWin32.cpp} | 2 +- .../src/util/{win32.h => retroshareWin32.h} | 14 +- retroshare-nogui/src/retroshare-nogui.pro | 63 +-- retroshare-qml-app/src/retroshare-qml-app.pro | 1 - retroshare.pri | 464 ++++++++++++------ tests/unittests/unittests.pro | 2 +- 21 files changed, 502 insertions(+), 466 deletions(-) create mode 100644 libbitdht/src/use_libbitdht.pri create mode 100644 libresapi/src/use_libresapi.pri create mode 100644 libretroshare/src/use_libretroshare.pri create mode 100644 openpgpsdk/src/use_openpgpsdk.pri rename retroshare-gui/src/util/{win32.cpp => retroshareWin32.cpp} (99%) rename retroshare-gui/src/util/{win32.h => retroshareWin32.h} (97%) diff --git a/RetroShare.pro b/RetroShare.pro index 9a6f24d5f..d124ce94d 100644 --- a/RetroShare.pro +++ b/RetroShare.pro @@ -1,13 +1,12 @@ !include("retroshare.pri"): error("Could not include file retroshare.pri") TEMPLATE = subdirs -#CONFIG += tests SUBDIRS += openpgpsdk openpgpsdk.file = openpgpsdk/src/openpgpsdk.pro retrotor { - libretroshare.depends = openpgpsdk + libretroshare.depends = openpgpsdk } else { SUBDIRS += libbitdht libbitdht.file = libbitdht/src/libbitdht.pro diff --git a/android-prepare-toolchain.sh b/android-prepare-toolchain.sh index 7d8337add..da01cc8dc 100755 --- a/android-prepare-toolchain.sh +++ b/android-prepare-toolchain.sh @@ -248,8 +248,8 @@ build_sqlite() make -j${HOST_NUM_CPU} make install rm -f ${SYSROOT}/usr/lib/libsqlite3.so* - ${CC} -shared -o libsqlite3.so -fPIC sqlite3.o -ldl - cp libsqlite3.so "${SYSROOT}/usr/lib" +# ${CC} -shared -o libsqlite3.so -fPIC sqlite3.o -ldl +# cp libsqlite3.so "${SYSROOT}/usr/lib" cd .. } diff --git a/libbitdht/src/libbitdht.pro b/libbitdht/src/libbitdht.pro index ad6986f32..d9e5816c4 100644 --- a/libbitdht/src/libbitdht.pro +++ b/libbitdht/src/libbitdht.pro @@ -6,23 +6,10 @@ CONFIG -= qt TARGET = bitdht DESTDIR = lib +!include("use_libbitdht.pri"):error("Including") + QMAKE_CXXFLAGS *= -Wall -DBE_DEBUG -profiling { - QMAKE_CXXFLAGS -= -fomit-frame-pointer - QMAKE_CXXFLAGS *= -pg -g -fno-omit-frame-pointer -} - -release { - # not much here yet. -} - -#CONFIG += debug -debug { - QMAKE_CXXFLAGS -= -O2 -fomit-frame-pointer - QMAKE_CXXFLAGS *= -g -fno-omit-frame-pointer -} - # treat warnings as error for better removing #QMAKE_CFLAGS += -Werror #QMAKE_CXXFLAGS += -Werror diff --git a/libbitdht/src/use_libbitdht.pri b/libbitdht/src/use_libbitdht.pri new file mode 100644 index 000000000..bbaf1d4d5 --- /dev/null +++ b/libbitdht/src/use_libbitdht.pri @@ -0,0 +1,5 @@ +DEPENDPATH *= $$system_path($$clean_path($${PWD}/../../libbitdht/src)) +INCLUDEPATH *= $$system_path($$clean_path($${PWD}/../../libbitdht/src)) +LIBS *= -L$$system_path($$clean_path($${OUT_PWD}/../../libbitdht/src/lib/)) -lbitdht + +!equals(TARGET, bitdht):PRE_TARGETDEPS *= $$system_path($$clean_path($${OUT_PWD}/../../libbitdht/src/lib/libbitdht.a)) diff --git a/libresapi/src/api/ApiServer.cpp b/libresapi/src/api/ApiServer.cpp index db8b75d16..f08671114 100644 --- a/libresapi/src/api/ApiServer.cpp +++ b/libresapi/src/api/ApiServer.cpp @@ -17,7 +17,7 @@ #include "ChannelsHandler.h" #include "StatsHandler.h" -#ifdef LIBRESAPI_QT +#ifdef LIBRESAPI_SETTINGS #include "SettingsHandler.h" #endif @@ -242,7 +242,7 @@ public: mApiPluginHandler(sts, ifaces), mChannelsHandler(ifaces.mGxsChannels), mStatsHandler() -#ifdef LIBRESAPI_QT +#ifdef LIBRESAPI_SETTINGS ,mSettingsHandler(sts) #endif { @@ -272,7 +272,7 @@ public: &ChannelsHandler::handleRequest); router.addResourceHandler("stats", dynamic_cast(&mStatsHandler), &StatsHandler::handleRequest); -#ifdef LIBRESAPI_QT +#ifdef LIBRESAPI_SETTINGS router.addResourceHandler("settings", dynamic_cast(&mSettingsHandler), &SettingsHandler::handleRequest); #endif @@ -290,7 +290,7 @@ public: ChannelsHandler mChannelsHandler; StatsHandler mStatsHandler; -#ifdef LIBRESAPI_QT +#ifdef LIBRESAPI_SETTINGS SettingsHandler mSettingsHandler; #endif }; diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index ea2daa082..f3124dc77 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -2,50 +2,29 @@ TEMPLATE = lib CONFIG += staticlib -CONFIG += create_prl CONFIG -= qt TARGET = resapi TARGET_PRL = libresapi DESTDIR = lib -DEPENDPATH += ../../libretroshare/src/ -INCLUDEPATH += ../../libretroshare/src +!include(use_libresapi.pri):error("Including") +libresapilocalserver { + CONFIG *= qt + QT *= network + SOURCES *= api/ApiServerLocal.cpp + HEADERS *= api/ApiServerLocal.h +} -retroshare_android_service { - win32 { - OBJECTS_DIR = temp/obj +libresapi_settings { + CONFIG *= qt + QT *= core - LIBS_DIR = $$PWD/../../libs/lib - LIBS += $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a - LIBS += $$OUT_PWD/../../openpgpsdk/src/lib/libops.a - - for(lib, LIB_DIR):LIBS += -L"$$lib" - for(bin, BIN_DIR):LIBS += -L"$$bin" - - - LIBS += -lssl -lcrypto -lpthread -lminiupnpc -lz -lws2_32 - LIBS += -luuid -lole32 -liphlpapi -lcrypt32 -lgdi32 - LIBS += -lwinmm - - DEFINES *= WINDOWS_SYS WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T - - DEPENDPATH += . $$INC_DIR - INCLUDEPATH += . $$INC_DIR - - greaterThan(QT_MAJOR_VERSION, 4) { - # Qt 5 - RC_INCLUDEPATH += $$_PRO_FILE_PWD_/../../libretroshare/src - } else { - # Qt 4 - QMAKE_RC += --include-dir=$$_PRO_FILE_PWD_/../../libretroshare/src - } - } + SOURCES += api/SettingsHandler.cpp + HEADERS += api/SettingsHandler.h } libresapihttpserver { - CONFIG += libmicrohttpd - unix { webui_files.path = "$${DATA_DIR}/webui" @@ -211,18 +190,3 @@ HEADERS += \ api/ChannelsHandler.h \ api/StatsHandler.h \ api/FileSharingHandler.h - -libresapilocalserver { - CONFIG *= qt - QT *= network - SOURCES *= api/ApiServerLocal.cpp - HEADERS *= api/ApiServerLocal.h -} - -qt_dependencies { - CONFIG *= qt - QT *= core - - SOURCES += api/SettingsHandler.cpp - HEADERS += api/SettingsHandler.h -} diff --git a/libresapi/src/use_libresapi.pri b/libresapi/src/use_libresapi.pri new file mode 100644 index 000000000..e9b1753a2 --- /dev/null +++ b/libresapi/src/use_libresapi.pri @@ -0,0 +1,27 @@ +DEPENDPATH *= $$system_path($$clean_path($$PWD/../../libresapi/src)) +INCLUDEPATH *= $$system_path($$clean_path($${PWD}/../../libresapi/src)) +LIBS *= -L$$system_path($$clean_path($${OUT_PWD}/../../libresapi/src/lib/)) -lresapi + +!equals(TARGET, resapi):PRE_TARGETDEPS *= $$system_path($$clean_path($${OUT_PWD}/../../libresapi/src/lib/libresapi.a)) + +!include("../../libretroshare/src/use_libretroshare.pri"):error("Including") + +sLibs = +mLibs = +dLibs = + +libresapihttpserver { + mLibs *= microhttpd +} + +static { + sLibs *= $$mLibs +} else { + dLibs *= $$mLibs +} + +LIBS += $$linkStaticLibs(sLibs) +PRE_TARGETDEPS += $$pretargetStaticLibs(sLibs) + +LIBS += $$linkDynamicLibs(dLibs) + diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index c2604fa4a..fa9491c63 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -2,30 +2,17 @@ TEMPLATE = lib CONFIG += staticlib -CONFIG += create_prl CONFIG -= qt TARGET = retroshare TARGET_PRL = libretroshare DESTDIR = lib -#CONFIG += dsdv - -retrotor { - DEFINES *= RETROTOR - CONFIG -= bitdht -} else { - CONFIG += bitdht -} +!include("use_libretroshare.pri"):error("Including") # the dht stunner is used to obtain RS external ip addr. when it is natted # this system is unreliable and rs supports a newer and better one (asking connected peers) # CONFIG += useDhtStunner -profiling { - QMAKE_CXXFLAGS -= -fomit-frame-pointer - QMAKE_CXXFLAGS *= -pg -g -fno-omit-frame-pointer -} - # treat warnings as error for better removing #QMAKE_CFLAGS += -Werror #QMAKE_CXXFLAGS += -Werror @@ -203,21 +190,11 @@ linux-* { PKGCONFIG *= libssl libupnp PKGCONFIG *= libcrypto zlib - LIBS *= -lpthread -ldl -} + no_sqlcipher:PKGCONFIG *= sqlite3 + LIBS *= -ldl -linux-* { DEFINES *= PLUGIN_DIR=\"\\\"$${PLUGIN_DIR}\\\"\" DEFINES *= DATA_DIR=\"\\\"$${DATA_DIR}\\\"\" - - ## where to put the librarys interface - #include_rsiface.path = "$${INC_DIR}" - #include_rsiface.files = $$PUBLIC_HEADERS - #INSTALLS += include_rsiface - - ## where to put the shared library itself - #target.path = "$$LIB_DIR" - #INSTALLS *= target } linux-g++ { @@ -234,7 +211,7 @@ version_detail_bash_script { PRE_TARGETDEPS = write_version_detail write_version_detail.commands = $$PWD/version_detail.sh } - win32 { + win32-* { QMAKE_EXTRA_TARGETS += write_version_detail PRE_TARGETDEPS = write_version_detail write_version_detail.commands = $$PWD/version_detail.bat @@ -263,13 +240,11 @@ win32-x-g++ { } ################################# Windows ########################################## -win32 { +win32-g++ { QMAKE_CC = $${QMAKE_CXX} OBJECTS_DIR = temp/obj MOC_DIR = temp/moc - DEFINES *= WINDOWS_SYS WIN32 STATICLIB MINGW WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T - # This defines the platform to be WinXP or later and is needed for getaddrinfo (_WIN32_WINNT_WINXP) - DEFINES *= WINVER=0x0501 + DEFINES *= STATICLIB # Switch on extra warnings QMAKE_CFLAGS += -Wextra @@ -289,15 +264,8 @@ win32 { CONFIG += upnp_miniupnpc - no_sqlcipher { - PKGCONFIG *= sqlite3 - LIBS += -lsqlite3 - } else { - LIBS += -lsqlcipher - } - - DEPENDPATH += . $$INC_DIR - INCLUDEPATH += . $$INC_DIR + wLibs = ws2_32 gdi32 uuid iphlpapi crypt32 ole32 winmm + LIBS += $$linkDynamicLibs(wLibs) } ################################# MacOSX ########################################## @@ -697,13 +665,16 @@ SOURCES += util/folderiterator.cc \ util/rsrecogn.cc \ util/rstime.cc - -upnp_miniupnpc { - HEADERS += upnp/upnputil.h upnp/upnphandler_miniupnp.h - SOURCES += upnp/upnputil.c upnp/upnphandler_miniupnp.cc +## Added for retrocompatibility remove ASAP +isEmpty(RS_UPNP_LIB) { + upnp_miniupnpc:RS_UPNP_LIB=miniupnpc + upnp_libupnp:RS_UPNP_LIB="upnp ixml threadutil" } -upnp_libupnp { +equals(RS_UPNP_LIB, miniupnpc) { + HEADERS += upnp/upnputil.h upnp/upnphandler_miniupnp.h + SOURCES += upnp/upnputil.c upnp/upnphandler_miniupnp.cc +} else { HEADERS += upnp/UPnPBase.h upnp/upnphandler_linux.h SOURCES += upnp/UPnPBase.cpp upnp/upnphandler_linux.cc DEFINES *= RS_USE_LIBUPNP @@ -934,19 +905,13 @@ android-* { DEFINES *= "fopen64=fopen" DEFINES *= "fseeko64=fseeko" DEFINES *= "ftello64=ftello" - LIBS *= -lbz2 -lupnp -lixml -lthreadutil -lsqlite3 -## Static library are verysensible to order in command line, has to be in the -## end of file for this reason +## Static library are very susceptible to order in command line + sLibs = bz2 $$RS_UPNP_LIB $$RS_SQL_LIB ssl crypto - LIBS += -L$$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/ -lsqlcipher - PRE_TARGETDEPS += $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/libsqlcipher.a - - LIBS += -L$$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/ -lssl - PRE_TARGETDEPS += $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/libssl.a - - LIBS += -L$$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/ -lcrypto - PRE_TARGETDEPS += $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/libcrypto.a + LIBS += $$linkStaticLibs(sLibs) + PRE_TARGETDEPS += $$pretargetStaticLibs(sLibs) HEADERS += util/androiddebug.h } + diff --git a/libretroshare/src/use_libretroshare.pri b/libretroshare/src/use_libretroshare.pri new file mode 100644 index 000000000..d17e36739 --- /dev/null +++ b/libretroshare/src/use_libretroshare.pri @@ -0,0 +1,33 @@ +DEPENDPATH *= $$system_path($$clean_path($${PWD}/../../libretroshare/src/)) +INCLUDEPATH *= $$system_path($$clean_path($${PWD}/../../libretroshare/src)) +LIBS *= -L$$system_path($$clean_path($${OUT_PWD}/../../libretroshare/src/lib/)) -lretroshare + +equals(TARGET, retroshare):equals(TEMPLATE, lib){ +} else { + PRE_TARGETDEPS *= $$system_path($$clean_path($$OUT_PWD/../../libretroshare/src/lib/libretroshare.a)) +} + +!include("../../openpgpsdk/src/use_openpgpsdk.pri"):error("Including") + +bitdht { + !include("../../libbitdht/src/use_libbitdht.pri"):error("Including") +} + +sLibs = +mLibs = $$RS_SQL_LIB ssl crypto $$RS_THREAD_LIB $$RS_UPNP_LIB +dLibs = + +linux-* { + mLibs += dl +} + +static { + sLibs *= $$mLibs +} else { + dLibs *= $$mLibs +} + +LIBS += $$linkStaticLibs(sLibs) +PRE_TARGETDEPS += $$pretargetStaticLibs(sLibs) + +LIBS += $$linkDynamicLibs(dLibs) diff --git a/openpgpsdk/src/openpgpsdk.pro b/openpgpsdk/src/openpgpsdk.pro index eaf4ef2e6..f667a0d23 100644 --- a/openpgpsdk/src/openpgpsdk.pro +++ b/openpgpsdk/src/openpgpsdk.pro @@ -9,8 +9,8 @@ QMAKE_CXXFLAGS *= -Wall -Werror -W TARGET = ops DESTDIR = lib -DEPENDPATH += . $$INC_DIR -INCLUDEPATH += . $$INC_DIR + +!include(use_openpgpsdk.pri):error("Including") #################################### Windows ##################################### @@ -18,7 +18,11 @@ linux-* { OBJECTS_DIR = temp/linux/obj } -win32 { +win32-g++ { + + HEADERS += openpgpsdk/opsstring.h + SOURCES += openpgpsdk/opsstring.c + DEFINES *= WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T # Switch off optimization for release version @@ -27,9 +31,13 @@ win32 { QMAKE_CFLAGS_RELEASE -= -O2 QMAKE_CFLAGS_RELEASE += -O0 - # Switch on optimization for debug version - #QMAKE_CXXFLAGS_DEBUG += -O2 - #QMAKE_CFLAGS_DEBUG += -O2 + mLibs = bz2 z ssl crypto + static { + LIBS += $$linkStaticLibs(mLibs) + PRE_TARGETDEPS += $$pretargetStaticLibs(mLibs) + } else { + LIBS += $$linkDynamicLibs(mLibs) + } } @@ -74,9 +82,6 @@ HEADERS += openpgpsdk/writer.h \ openpgpsdk/parse_local.h \ openpgpsdk/keyring_local.h \ openpgpsdk/opsdir.h -win32{ -HEADERS += openpgpsdk/opsstring.h -} SOURCES += openpgpsdk/accumulate.c \ openpgpsdk/compress.c \ @@ -116,9 +121,7 @@ SOURCES += openpgpsdk/accumulate.c \ openpgpsdk/writer_skey_checksum.c \ openpgpsdk/writer_stream_encrypt_se_ip.c \ openpgpsdk/opsdir.c -win32{ -SOURCES += openpgpsdk/opsstring.c -} + ################################# Android ##################################### diff --git a/openpgpsdk/src/use_openpgpsdk.pri b/openpgpsdk/src/use_openpgpsdk.pri new file mode 100644 index 000000000..adf10f1ca --- /dev/null +++ b/openpgpsdk/src/use_openpgpsdk.pri @@ -0,0 +1,20 @@ +DEPENDPATH *= $$system_path($$clean_path($${PWD}/../../openpgpsdk/src)) +INCLUDEPATH *= $$system_path($$clean_path($${PWD}/../../openpgpsdk/src)) +LIBS *= -L$$system_path($$clean_path($${OUT_PWD}/../../openpgpsdk/src/lib/)) -lops + +!equals(TARGET, ops):PRE_TARGETDEPS *= $$system_path($$clean_path($${OUT_PWD}/../../openpgpsdk/src/lib/libops.a)) + +sLibs = +mLibs = ssl crypto z bz2 +dLibs = + +static { + sLibs *= $$mLibs +} else { + dLibs *= $$mLibs +} + +LIBS += $$linkStaticLibs(sLibs) +PRE_TARGETDEPS += $$pretargetStaticLibs(sLibs) + +LIBS += $$linkDynamicLibs(dLibs) diff --git a/retroshare-android-service/src/retroshare-android-service.pro b/retroshare-android-service/src/retroshare-android-service.pro index bcafa7725..f83392456 100644 --- a/retroshare-android-service/src/retroshare-android-service.pro +++ b/retroshare-android-service/src/retroshare-android-service.pro @@ -11,43 +11,8 @@ android-*:CONFIG += dll android-*:TEMPLATE = lib !android-*:TEMPLATE = app -DEPENDPATH *= ../../libresapi/src -INCLUDEPATH *= ../../libresapi/src -PRE_TARGETDEPS *= ../../libresapi/src/lib/libresapi.a -LIBS *= ../../libresapi/src/lib/libresapi.a +!include("../../libresapi/src/use_libresapi.pri"):error("Including") -DEPENDPATH *= ../../libretroshare/src -INCLUDEPATH *= ../../libretroshare/src -PRE_TARGETDEPS *= ../../libretroshare/src/lib/libretroshare.a -LIBS *= ../../libretroshare/src/lib/libretroshare.a - -win32 { - OBJECTS_DIR = temp/obj - - LIBS_DIR = $$PWD/../../libs/lib - LIBS += $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a - LIBS += $$OUT_PWD/../../openpgpsdk/src/lib/libops.a - - for(lib, LIB_DIR):LIBS += -L"$$lib" - for(bin, BIN_DIR):LIBS += -L"$$bin" - - - LIBS += -lssl -lcrypto -lpthread -lminiupnpc -lz -lws2_32 - LIBS += -luuid -lole32 -liphlpapi -lcrypt32 -lgdi32 - LIBS += -lwinmm - - DEFINES *= WINDOWS_SYS WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T - - DEPENDPATH += . $$INC_DIR - INCLUDEPATH += . $$INC_DIR - - greaterThan(QT_MAJOR_VERSION, 4) { - # Qt 5 - RC_INCLUDEPATH += $$_PRO_FILE_PWD_/../../libretroshare/src - } else { - # Qt 4 - QMAKE_RC += --include-dir=$$_PRO_FILE_PWD_/../../libretroshare/src - } -} +!include("../../libretroshare/src/use_libretroshare.pri"):error("Including") SOURCES += service.cpp diff --git a/retroshare-gui/src/gui/settings/rsharesettings.cpp b/retroshare-gui/src/gui/settings/rsharesettings.cpp index 5955cd421..91b306cc0 100644 --- a/retroshare-gui/src/gui/settings/rsharesettings.cpp +++ b/retroshare-gui/src/gui/settings/rsharesettings.cpp @@ -35,7 +35,7 @@ #include #if defined(Q_OS_WIN) -#include +#include #endif /* Retroshare's Settings */ diff --git a/retroshare-gui/src/retroshare-gui.pro b/retroshare-gui/src/retroshare-gui.pro index 7a6d8d2a9..9cf15c4f1 100644 --- a/retroshare-gui/src/retroshare-gui.pro +++ b/retroshare-gui/src/retroshare-gui.pro @@ -2,28 +2,24 @@ TEMPLATE = app QT += network xml -CONFIG += qt gui uic qrc resources idle bitdht -CONFIG += link_prl +CONFIG += qt gui uic qrc resources idle CONFIG += console TARGET = retroshare DEFINES += TARGET=\\\"$${TARGET}\\\" -# Plz never commit the .pro with these flags enabled. -# Use this flag when developping new features only. -# -#CONFIG += unfinished -#CONFIG += debug -#DEFINES *= SIGFPE_DEBUG +DEPENDPATH *= $${PWD} $${RS_INCLUDE_DIR} retroshare-gui +INCLUDEPATH *= $${PWD} $${RS_INCLUDE_DIR} retroshare-gui -profiling { - QMAKE_CXXFLAGS -= -fomit-frame-pointer - QMAKE_CXXFLAGS *= -pg -g -fno-omit-frame-pointer - QMAKE_LFLAGS *= -pg +libresapihttpserver { + !include("../../libresapi/src/use_libresapi.pri"):error("Including") + HEADERS *= gui/settings/WebuiPage.h + SOURCES *= gui/settings/WebuiPage.cpp + FORMS *= gui/settings/WebuiPage.ui } -retrotor { - DEFINES *= RETROTOR +!include("../../libretroshare/src/use_libretroshare.pri"):error("Including") +retrotor { FORMS += TorControl/TorControlWindow.ui SOURCES += TorControl/TorControlWindow.cpp HEADERS += TorControl/TorControlWindow.h @@ -59,22 +55,6 @@ RCC_DIR = temp/qrc UI_DIR = temp/ui MOC_DIR = temp/moc -#CONFIG += debug -debug { - QMAKE_CFLAGS += -g - QMAKE_CXXFLAGS -= -O2 - QMAKE_CXXFLAGS += -O0 - QMAKE_CFLAGS -= -O2 - QMAKE_CFLAGS += -O0 -} - -DEPENDPATH *= retroshare-gui -INCLUDEPATH *= retroshare-gui - -# treat warnings as error for better removing -#QMAKE_CFLAGS += -Werror -#QMAKE_CXXFLAGS += -Werror - ################################# Linux ########################################## # Put lib dir in QMAKE_LFLAGS so it appears before -L/usr/lib linux-* { @@ -133,7 +113,7 @@ version_detail_bash_script { PRE_TARGETDEPS = write_version_detail write_version_detail.commands = $$PWD/version_detail.sh } - win32 { + win32-* { QMAKE_EXTRA_TARGETS += write_version_detail PRE_TARGETDEPS = write_version_detail write_version_detail.commands = $$PWD/version_detail.bat @@ -165,7 +145,7 @@ win32-x-g++ { #################################### Windows ##################################### -win32 { +win32-g++ { CONFIG(debug, debug|release) { # show console output CONFIG += console @@ -185,8 +165,9 @@ win32 { QMAKE_LFLAGS += -Wl,-nxcompat } - # solve linker warnings because of the order of the libraries - QMAKE_LFLAGS += -Wl,--start-group + # Fix linking error (ld.exe: Error: export ordinal too large) due to too + # many exported symbols. + QMAKE_LFLAGS+=-Wl,--exclude-libs,ALL # Switch off optimization for release version QMAKE_CXXFLAGS_RELEASE -= -O2 @@ -199,15 +180,10 @@ win32 { #QMAKE_CFLAGS_DEBUG += -O2 OBJECTS_DIR = temp/obj - #LIBS += -L"D/Qt/2009.03/qt/plugins/imageformats" - #QTPLUGIN += qjpeg - for(lib, LIB_DIR):LIBS += -L"$$lib" - for(bin, BIN_DIR):LIBS += -L"$$bin" + dLib = ws2_32 gdi32 uuid ole32 iphlpapi crypt32 winmm + LIBS *= $$linkDynamicLibs(dLib) - LIBS += -lssl -lcrypto -lpthread -lminiupnpc -lz -lws2_32 - LIBS += -luuid -lole32 -liphlpapi -lcrypt32 -lgdi32 - LIBS += -lwinmm RC_FILE = gui/images/retroshare_win.rc # export symbols for the plugins @@ -220,11 +196,6 @@ win32 { QMAKE_PRE_LINK = $(CHK_DIR_EXISTS) lib || $(MKDIR) lib } - DEFINES *= WINDOWS_SYS WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T - - DEPENDPATH += . $$INC_DIR - INCLUDEPATH += . $$INC_DIR - greaterThan(QT_MAJOR_VERSION, 4) { # Qt 5 RC_INCLUDEPATH += $$_PRO_FILE_PWD_/../../libretroshare/src @@ -309,32 +280,11 @@ openbsd-* { LIBS *= -rdynamic } - - -############################## Common stuff ###################################### - -# On Linux systems that alredy have libssl and libcrypto it is advisable -# to rename the patched version of SSL to something like libsslxpgp.a and libcryptoxpg.a - -# ########################################### - -DEPENDPATH += . $$PWD/../../libretroshare/src/ -INCLUDEPATH += $$PWD/../../libretroshare/src/ - -PRE_TARGETDEPS *= $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a -LIBS *= $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a - wikipoos { PRE_TARGETDEPS *= $$OUT_PWD/../../supportlibs/pegmarkdown/lib/libpegmarkdown.a LIBS *= $$OUT_PWD/../../supportlibs/pegmarkdown/lib/libpegmarkdown.a } -# webinterface -DEPENDPATH += $$PWD/../../libresapi/src -INCLUDEPATH += $$PWD/../../libresapi/src -PRE_TARGETDEPS *= $$OUT_PWD/../../libresapi/src/lib/libresapi.a -LIBS += $$OUT_PWD/../../libresapi/src/lib/libresapi.a - retrotor { HEADERS += TorControl/AddOnionCommand.h \ TorControl/AuthenticateCommand.h \ @@ -439,7 +389,6 @@ HEADERS += rshare.h \ util/stringutil.h \ util/RsNetUtil.h \ util/DateTime.h \ - util/win32.h \ util/RetroStyleLabel.h \ util/dllexport.h \ util/NonCopyable.h \ @@ -619,7 +568,8 @@ HEADERS += rshare.h \ util/imageutil.h \ gui/NetworkDialog/pgpid_item_model.h \ gui/NetworkDialog/pgpid_item_proxy.h \ - gui/common/RsCollection.h + gui/common/RsCollection.h \ + util/retroshareWin32.h # gui/ForumsDialog.h \ # gui/forums/ForumDetails.h \ # gui/forums/EditForumDetails.h \ @@ -803,7 +753,6 @@ SOURCES += main.cpp \ util/stringutil.cpp \ util/RsNetUtil.cpp \ util/DateTime.cpp \ - util/win32.cpp \ util/RetroStyleLabel.cpp \ util/WidgetBackgroundImage.cpp \ util/NonCopyable.cpp \ @@ -981,7 +930,8 @@ SOURCES += main.cpp \ util/imageutil.cpp \ gui/NetworkDialog/pgpid_item_model.cpp \ gui/NetworkDialog/pgpid_item_proxy.cpp \ - gui/common/RsCollection.cpp + gui/common/RsCollection.cpp \ + util/retroshareWin32.cpp # gui/ForumsDialog.cpp \ # gui/forums/ForumDetails.cpp \ # gui/forums/EditForumDetails.cpp \ @@ -1419,9 +1369,3 @@ gxsgui { } - -libresapihttpserver { - HEADERS *= gui/settings/WebuiPage.h - SOURCES *= gui/settings/WebuiPage.cpp - FORMS *= gui/settings/WebuiPage.ui -} diff --git a/retroshare-gui/src/rshare.h b/retroshare-gui/src/rshare.h index fd17b0f13..2d154a361 100644 --- a/retroshare-gui/src/rshare.h +++ b/retroshare-gui/src/rshare.h @@ -27,7 +27,7 @@ #if defined(Q_OS_WIN) #include -#include +#include "util/retroshareWin32.h" #endif #include diff --git a/retroshare-gui/src/util/win32.cpp b/retroshare-gui/src/util/retroshareWin32.cpp similarity index 99% rename from retroshare-gui/src/util/win32.cpp rename to retroshare-gui/src/util/retroshareWin32.cpp index b928653fb..5a7f66b8c 100644 --- a/retroshare-gui/src/util/win32.cpp +++ b/retroshare-gui/src/util/retroshareWin32.cpp @@ -33,7 +33,7 @@ #endif #include -#include "win32.h" +#include "retroshareWin32.h" /** Finds the location of the "special" Windows folder using the given CSIDL diff --git a/retroshare-gui/src/util/win32.h b/retroshare-gui/src/util/retroshareWin32.h similarity index 97% rename from retroshare-gui/src/util/win32.h rename to retroshare-gui/src/util/retroshareWin32.h index 5b0dcdca3..8dbb351f3 100644 --- a/retroshare-gui/src/util/win32.h +++ b/retroshare-gui/src/util/retroshareWin32.h @@ -1,7 +1,8 @@ +#pragma once /**************************************************************** - * This file is distributed under the following license: - * - * Copyright (c) 2006-2007, crypton + * This file is distributed under the following license: + * + * Copyright (c) 2006-2007, crypton * Copyright (c) 2006, Matt Edman, Justin Hipple * * This program is free software; you can redistribute it and/or @@ -20,10 +21,6 @@ * Boston, MA 02110-1301, USA. ****************************************************************/ - -#ifndef _WIN32_H -#define _WIN32_H - #include #include @@ -41,6 +38,3 @@ void win32_registry_set_key_value(QString keyLocation, QString keyName, QString /** Removes the key from the registry if it exists */ void win32_registry_remove_key(QString keyLocation, QString keyName); - -#endif - diff --git a/retroshare-nogui/src/retroshare-nogui.pro b/retroshare-nogui/src/retroshare-nogui.pro index 1fdb27b71..8e731a9d1 100644 --- a/retroshare-nogui/src/retroshare-nogui.pro +++ b/retroshare-nogui/src/retroshare-nogui.pro @@ -2,22 +2,20 @@ TEMPLATE = app TARGET = retroshare-nogui -CONFIG += bitdht -#CONFIG += introserver CONFIG -= qt xml gui -CONFIG += link_prl -#CONFIG += debug -debug { - QMAKE_CFLAGS -= -O2 - QMAKE_CFLAGS += -O0 - QMAKE_CFLAGS += -g +DEPENDPATH *= $${PWD} $${RS_INCLUDE_DIR} +INCLUDEPATH *= $${PWD} $${RS_INCLUDE_DIR} - QMAKE_CXXFLAGS -= -O2 - QMAKE_CXXFLAGS += -O0 - QMAKE_CXXFLAGS += -g +libresapihttpserver { + !include("../../libresapi/src/use_libresapi.pri"):error("Including") + + HEADERS += TerminalApiClient.h + SOURCES += TerminalApiClient.cpp } +!include("../../libretroshare/src/use_libretroshare.pri"):error("Including") + ################################# Linux ########################################## linux-* { #CONFIG += version_detail_bash_script @@ -53,42 +51,32 @@ win32-x-g++ { LIBS += -lole32 -lwinmm RC_FILE = gui/images/retroshare_win.rc - - DEFINES *= WIN32 } #################################### Windows ##################################### -win32 { +win32-g++ { CONFIG += console OBJECTS_DIR = temp/obj RCC_DIR = temp/qrc UI_DIR = temp/ui MOC_DIR = temp/moc - # solve linker warnings because of the order of the libraries - QMAKE_LFLAGS += -Wl,--start-group + ## solve linker warnings because of the order of the libraries + #QMAKE_LFLAGS += -Wl,--start-group - CONFIG(debug, debug|release) { - } else { + CONFIG(debug, debug|release) { + } else { # Tell linker to use ASLR protection QMAKE_LFLAGS += -Wl,-dynamicbase # Tell linker to use DEP protection QMAKE_LFLAGS += -Wl,-nxcompat } - for(lib, LIB_DIR):LIBS += -L"$$lib" - LIBS += -lssl -lcrypto -lpthread -lminiupnpc -lz - LIBS += -lcrypto -lws2_32 -lgdi32 - LIBS += -luuid -lole32 -liphlpapi -lcrypt32 - LIBS += -lole32 -lwinmm + dLib = ws2_32 gdi32 uuid ole32 iphlpapi crypt32 winmm + LIBS *= $$linkDynamicLibs(dLib) RC_FILE = resources/retroshare_win.rc - - DEFINES *= WINDOWS_SYS _USE_32BIT_TIME_T - - DEPENDPATH += . $$INC_DIR - INCLUDEPATH += . $$INC_DIR } ##################################### MacOS ###################################### @@ -156,11 +144,6 @@ haiku-* { ############################## Common stuff ###################################### -DEPENDPATH += . $$PWD/../../libretroshare/src -INCLUDEPATH += . $$PWD/../../libretroshare/src - -PRE_TARGETDEPS *= $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a -LIBS *= $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a # Input HEADERS += notifytxt.h @@ -168,19 +151,9 @@ SOURCES += notifytxt.cc \ retroshare.cc introserver { +## Introserver is broken (doesn't compile) should be either fixed or removed + HEADERS += introserver.h SOURCES += introserver.cc DEFINES *= RS_INTRO_SERVER } - -libresapihttpserver { - DEFINES *= ENABLE_WEBUI - PRE_TARGETDEPS *= $$OUT_PWD/../../libresapi/src/lib/libresapi.a - LIBS += $$OUT_PWD/../../libresapi/src/lib/libresapi.a - DEPENDPATH += $$PWD/../../libresapi/src - INCLUDEPATH += $$PWD/../../libresapi/src - HEADERS += \ - TerminalApiClient.h - SOURCES += \ - TerminalApiClient.cpp -} diff --git a/retroshare-qml-app/src/retroshare-qml-app.pro b/retroshare-qml-app/src/retroshare-qml-app.pro index 15054102c..04b8d12ec 100644 --- a/retroshare-qml-app/src/retroshare-qml-app.pro +++ b/retroshare-qml-app/src/retroshare-qml-app.pro @@ -22,7 +22,6 @@ android-* { HEADERS += NativeCalls.h androidplatforminteracions.h SOURCES += NativeCalls.cpp androidplatforminteracions.cpp - ANDROID_EXTRA_LIBS *= $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/libsqlite3.so ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android diff --git a/retroshare.pri b/retroshare.pri index 446c1bd61..b1eccbb09 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -1,14 +1,15 @@ +################################################################################ +## Documented build options (CONFIG) goes here as all the rest depend on them ## +## CONFIG must not be edited in other .pro files, aka if CONFIG need do be ##### +## programatically modified depending on platform or from CONFIG itself it ##### +## can be done ONLY inside this file (retroshare.pri) ########################## +################################################################################ + # To disable RetroShare-gui append the following # assignation to qmake command line "CONFIG+=no_retroshare_gui" CONFIG *= retroshare_gui no_retroshare_gui:CONFIG -= retroshare_gui -# To build the RetroTor executable, just uncomment the following option. -# RetroTor is a version of RS that automatically configures Tor for its own usage -# using only hidden nodes. It will not start if Tor is not working. - -# CONFIG *= retrotor - # To disable RetroShare-nogui append the following # assignation to qmake command line "CONFIG+=no_retroshare_nogui" CONFIG *= retroshare_nogui @@ -41,10 +42,10 @@ retroshare_qml_app:CONFIG -= no_retroshare_qml_app CONFIG *= no_libresapilocalserver libresapilocalserver:CONFIG -= no_libresapilocalserver -# To enable Qt dependencies in libresapi append the following -# assignation to qmake command line "CONFIG+=qt_dependencies" -CONFIG *= no_qt_dependencies -qt_dependencies:CONFIG -= no_qt_dependencies +# To enable libresapi settings handler in libresapi append the following +# assignation to qmake command line "CONFIG+=libresapi_settings" +CONFIG *= no_libresapi_settings +libresapi_settings:CONFIG -= no_libresapi_settings # To disable libresapi via HTTP (based on libmicrohttpd) append the following # assignation to qmake command line "CONFIG+=no_libresapihttpserver" @@ -62,6 +63,15 @@ no_sqlcipher:CONFIG -= sqlcipher CONFIG *= no_rs_autologin rs_autologin:CONFIG -= no_rs_autologin +# To build RetroShare Tor only version with automatic hidden node setup append +# the following assignation to qmake command line "CONFIG+=retrotor" +CONFIG *= no_retrotor +retrotor { + CONFIG -= no_retrotor + CONFIG *= rs_onlyhiddennode + DEFINES *= RETROTOR +} + # To have only hidden node generation append the following assignation # to qmake command line "CONFIG+=rs_onlyhiddennode" CONFIG *= no_rs_onlyhiddennode @@ -92,6 +102,11 @@ CONFIG *= rs_gxs_trans CONFIG *= no_rs_async_chat rs_async_chat:CONFIG -= no_rs_async_chat +# To disable bitdht append the following assignation to qmake command line +# "CONFIG+=no_bitdht" +CONFIG *= bitdht +no_bitdht:CONFIG -= bitdht + # To select your MacOsX version append the following assignation to qmake # command line "CONFIG+=rs_macos10.11" where 10.11 depends your version macx:CONFIG *= rs_macos10.11 @@ -100,20 +115,242 @@ rs_macos10.9:CONFIG -= rs_macos10.11 rs_macos10.10:CONFIG -= rs_macos10.11 rs_macos10.12:CONFIG -= rs_macos10.11 +########################################################################################################################################################### +# +# V07_NON_BACKWARD_COMPATIBLE_CHANGE_001: +# +# What: Computes the node id by performing a sha256 hash of the certificate's PGP signature, instead of simply picking up the last 20 bytes of it. +# +# Why: There is no real risk in forging a certificate with the same ID as the authentication is performed over the PGP signature of the certificate +# which hashes the full SSL certificate (i.e. the full serialized CERT_INFO structure). However the possibility to +# create two certificates with the same IDs is a problem, as it can be used to cause disturbance in the software. +# +# Backward compat: connexions impossible with non patched peers older than Nov 2017, probably because the SSL id that is computed is not the same on both side, +# and in particular unpatched peers see a cerficate with ID different (because computed with the old method) than the ID that was +# submitted when making friends. +# +# Note: the advantage of basing the ID on the signature rather than the public key is not very clear, given that the signature is based on a hash +# of the public key (and the rest of the certificate info). +# +# V07_NON_BACKWARD_COMPATIBLE_CHANGE_002: +# +# What: Use RSA+SHA256 instead of RSA+SHA1 for PGP certificate signatures +# +# Why: Sha1 is likely to be prone to primary collisions anytime soon, so it is urgent to turn to a more secure solution. +# +# Backward compat: unpatched peers after Nov 2017 are able to verify signatures since openpgp-sdk already handle it. +# +# V07_NON_BACKWARD_COMPATIBLE_CHANGE_003: +# +# What: Do not hash PGP certificate twice when signing +# +# Why: hasing twice is not per se a security issue, but it makes it harder to change the settings for hashing. +# +# Backward compat: patched peers cannot connect to non patched peers older than Nov 2017. +########################################################################################################################################################### + +#CONFIG += rs_v07_changes +rs_v07_changes { + DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_001 + DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_002 + DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_003 +} + +################################################################################ +## RetroShare qmake functions goes here as all the rest may use them ########### +################################################################################ + +## This function is useful to look for the location of a file in a list of paths +## like the which command on linux, first paramether is the file name, +## second parameter is the name of a variable containing the list of folders +## where to look for. First match is returned. +defineReplace(findFileInPath) { + fileName=$$1 + pathList=$$2 + + for(mDir, $$pathList) { + attempt = $$clean_path($$mDir/$$fileName) + exists($$attempt) { + return($$system_path($$attempt)) + } + } + return() +} + +## This function return linker option to link statically the libraries contained +## in the variable given as paramether. +## Be carefull static library are very susceptible to order +defineReplace(linkStaticLibs) { + libsVarName = $$1 + retSlib = + + for(mLib, $$libsVarName) { + attemptPath=$$findFileInPath(lib$${mLib}.a, QMAKE_LIBDIR) + isEmpty(attemptPath):error(lib$${mLib}.a not found in [$${QMAKE_LIBDIR}]) + + retSlib += -L$$dirname(attemptPath) -l$$mLib + } + + return($$retSlib) +} + +## This function return pretarget deps for the static the libraries contained in +## the variable given as paramether. +defineReplace(pretargetStaticLibs) { + libsVarName = $$1 + + retPreTarget = + + for(mLib, $$libsVarName) { + attemptPath=$$findFileInPath(lib$${mLib}.a, QMAKE_LIBDIR) + isEmpty(attemptPath):error(lib$${mLib}.a not found in [$${QMAKE_LIBDIR}]) + + retPreTarget += $$attemptPath + } + + return($$retPreTarget) +} + +## This function return linker option to link dynamically the libraries +## contained in the variable given as paramether. +defineReplace(linkDynamicLibs) { + libsVarName = $$1 + retDlib = + + for(mLib, $$libsVarName) { + retDlib += -l$$mLib + } + + return($$retDlib) +} + + +################################################################################ +## Statements and variables that depends on build options (CONFIG)goes here #### +################################################################################ +## +## Defining the following variables may be needed depending on platform and +## build options (CONFIG) +## +## PREFIX String variable containing the directory considered as prefix set +## with = operator. +## QMAKE_LIBDIR, INCLUDEPATH Lists variables where qmake will look for includes +## and libraries. Add values using *= operator. +## RS_BIN_DIR, RS_LIB_DIR, RS_INCLUDE_DIR, RS_DATA_DIR, RS_PLUGIN_DIR String +## variables of directories where RetroShare components will be installed, on +## most platforms they are automatically calculated from PREFIX or in other +## ways. +## RS_SQL_LIB String viariable containing the name of the SQL library to use +## ("sqlcipher sqlite3", sqlite3) it is usually precalculated depending on +## CONFIG. +## RS_UPNP_LIB String viariable containing the name of the UPNP library to use +## (miniupnpc, "upnp ixml threadutil") it usually depend on platform. +## RS_THREAD_LIB String viariable containing the name of the multi threading +## library to use (pthread, "") it usually depend on platform. + +wikipoos:DEFINES *= RS_USE_WIKI +rs_gxs:DEFINES *= RS_ENABLE_GXS +libresapilocalserver:DEFINES *= LIBRESAPI_LOCAL_SERVER +libresapi_settings:DEFINES *= LIBRESAPI_SETTINGS +libresapihttpserver:DEFINES *= ENABLE_WEBUI +RS_THREAD_LIB=pthread +RS_UPNP_LIB = upnp ixml threadutil + +sqlcipher { + DEFINES -= NO_SQLCIPHER + RS_SQL_LIB = sqlcipher sqlite3 +} +no_sqlcipher { + DEFINES *= NO_SQLCIPHER + RS_SQL_LIB = sqlite3 +} + +rs_autologin { + DEFINES *= RS_AUTOLOGIN + warning("You have enabled RetroShare auto-login, this is discouraged. The usage of auto-login on some linux distributions may allow someone having access to your session to steal the SSL keys of your node location and therefore compromise your security") +} + +rs_onlyhiddennode { + DEFINES *= RS_ONLYHIDDENNODE + CONFIG -= bitdht + CONFIG *= no_bitdht + message("QMAKE: You have enabled only hidden node.") +} + +no_rs_deprecatedwarning { + QMAKE_CXXFLAGS += -Wno-deprecated + QMAKE_CXXFLAGS += -Wno-deprecated-declarations + DEFINES *= RS_NO_WARN_DEPRECATED + message("QMAKE: You have disabled deprecated warnings.") +} + +no_rs_cppwarning { + QMAKE_CXXFLAGS += -Wno-cpp + DEFINES *= RS_NO_WARN_CPP + message("QMAKE: You have disabled C preprocessor warnings.") +} + +rs_gxs_trans { + DEFINES *= RS_GXS_TRANS + greaterThan(QT_MAJOR_VERSION, 4) { + CONFIG += c++11 + } else { + QMAKE_CXXFLAGS += -std=c++0x + } +} + +rs_async_chat { + DEFINES *= RS_ASYNC_CHAT +} + +rs_chatserver { + DEFINES *= RS_CHATSERVER +} + +debug { + QMAKE_CXXFLAGS -= -O2 -fomit-frame-pointer + QMAKE_CFLAGS -= -O2 -fomit-frame-pointer + + QMAKE_CXXFLAGS *= -O0 -g -fno-omit-frame-pointer + QMAKE_CFLAGS *= -O0 -g -fno-omit-frame-pointer +} + +profiling { + QMAKE_CXXFLAGS -= -fomit-frame-pointer + QMAKE_CFLAGS -= -fomit-frame-pointer + + QMAKE_CXXFLAGS *= -pg -g -fno-omit-frame-pointer + QMAKE_CFLAGS *= -pg -g -fno-omit-frame-pointer + + QMAKE_LFLAGS *= -pg +} + +## Retrocompatibility assignations, get rid of this ASAP +isEmpty(BIN_DIR) : BIN_DIR = $${RS_BIN_DIR} +isEmpty(INC_DIR) : INC_DIR = $${RS_INCLUDE_DIR} +isEmpty(LIBDIR) : LIBDIR = $${QMAKE_LIBDIR} +isEmpty(DATA_DIR) : DATA_DIR = $${RS_DATA_DIR} +isEmpty(PLUGIN_DIR): PLUGIN_DIR= $${RS_PLUGIN_DIR} + + +################################################################################ +## Last goes platform specific statements common to all RetroShare subprojects # +################################################################################ linux-* { - isEmpty(PREFIX) { PREFIX = "/usr" } - isEmpty(BIN_DIR) { BIN_DIR = "$${PREFIX}/bin" } - isEmpty(INC_DIR) { INC_DIR = "$${PREFIX}/include/retroshare" } - isEmpty(LIB_DIR) { LIB_DIR = "$${PREFIX}/lib" } - isEmpty(DATA_DIR) { DATA_DIR = "$${PREFIX}/share/retroshare" } - isEmpty(PLUGIN_DIR) { PLUGIN_DIR = "$${LIB_DIR}/retroshare/extensions6" } + isEmpty(PREFIX) : PREFIX = "/usr" + isEmpty(RS_BIN_DIR) : RS_BIN_DIR = "$${PREFIX}/bin" + isEmpty(RS_INCLUDE_DIR): RS_INCLUDE_DIR = "$${PREFIX}/include" + isEmpty(RS_LIB_DIR) : RS_LIB_DIR = "$${PREFIX}/lib" + isEmpty(RS_DATA_DIR) : RS_DATA_DIR = "$${PREFIX}/share/retroshare" + isEmpty(RS_PLUGIN_DIR) : RS_PLUGIN_DIR = "$${RS_LIB_DIR}/retroshare/extensions6" + + INCLUDEPATH *= "$$RS_INCLUDE_DIR" + QMAKE_LIBDIR *= "$$RS_LIB_DIR" rs_autologin { - !macx { - DEFINES *= HAS_GNOME_KEYRING - PKGCONFIG *= gnome-keyring-1 - } + DEFINES *= HAS_GNOME_KEYRING + PKGCONFIG *= gnome-keyring-1 } } @@ -125,46 +362,66 @@ android-* { CONFIG -= no_retroshare_android_notify_service CONFIG *= retroshare_android_notify_service } - CONFIG *= no_libresapihttpserver upnp_libupnp - CONFIG -= libresapihttpserver upnp_miniupnpc + CONFIG *= no_libresapihttpserver + CONFIG -= libresapihttpserver QT *= androidextras - INCLUDEPATH += $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/include - LIBS *= -L$$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/ + INCLUDEPATH *= $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/include + QMAKE_LIBDIR *= "$$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/" + + # The android libc, bionic, provides built-in support for pthreads, + # additional linking (-lpthreads) break linking. + # See https://stackoverflow.com/a/31277163 + RS_THREAD_LIB = } -win32 { - message(***retroshare.pri:Win32) - exists($$PWD/../libs) { - message(Get pre-compiled libraries.) - isEmpty(PREFIX) { PREFIX = "$$PWD/../libs" } - isEmpty(BIN_DIR) { BIN_DIR = "$${PREFIX}/bin" } - isEmpty(INC_DIR) { INC_DIR = "$${PREFIX}/include" } - isEmpty(LIB_DIR) { LIB_DIR = "$${PREFIX}/lib" } - } +win32-g++ { + PREFIX_MSYS2 = $$(MINGW_PREFIX) + isEmpty(PREFIX_MSYS2) { + message("MINGW_PREFIX is not set, attempting MSYS2 autodiscovery.") - # Check for msys2 - PREFIX_MSYS2 = $$(MINGW_PREFIX) - isEmpty(PREFIX_MSYS2) { - exists(C:/msys32/mingw32/include) { - message(MINGW_PREFIX is empty. Set it in your environment variables.) - message(Found it here:C:\msys32\mingw32) - PREFIX_MSYS2 = "C:\msys32\mingw32" - } - exists(C:/msys64/mingw32/include) { - message(MINGW_PREFIX is empty. Set it in your environment variables.) - message(Found it here:C:\msys64\mingw32) - PREFIX_MSYS2 = "C:\msys64\mingw32" - } - } - !isEmpty(PREFIX_MSYS2) { - message(msys2 is installed.) - BIN_DIR += "$${PREFIX_MSYS2}/bin" - INC_DIR += "$${PREFIX_MSYS2}/include" - LIB_DIR += "$${PREFIX_MSYS2}/lib" - } + TEMPTATIVE_MSYS2=$$system_path(C:\\msys32\\mingw32) + exists($$clean_path($${TEMPTATIVE_MSYS2}/include)) { + PREFIX_MSYS2=$${TEMPTATIVE_MSYS2} + } + + TEMPTATIVE_MSYS2=$$system_path(C:\\msys64\\mingw32) + exists($$clean_path($${TEMPTATIVE_MSYS2}/include)) { + PREFIX_MSYS2=$${TEMPTATIVE_MSYS2} + } + + isEmpty(PREFIX_MSYS2) { + error(Cannot find MSYS2 please set MINGW_PREFIX) + } else { + message(Found MSYS2: $${PREFIX_MSYS2}) + } + } + + isEmpty(PREFIX) { + PREFIX = $$system_path($${PREFIX_MSYS2}) + } + + INCLUDEPATH *= $$system_path($${PREFIX}/include) + INCLUDEPATH *= $$system_path($${PREFIX_MSYS2}/include) + + QMAKE_LIBDIR *= $$system_path($${PREFIX}/lib) + QMAKE_LIBDIR *= $$system_path($${PREFIX_MSYS2}/lib) + + RS_BIN_DIR = $$system_path($${PREFIX}/bin) + RS_INCLUDE_DIR = $$system_path($${PREFIX}/include) + RS_LIB_DIR = $$system_path($${PREFIX}/lib) + + RS_UPNP_LIB = miniupnpc + + DEFINES *= NOGDI WIN32 WIN32_LEAN_AND_MEAN WINDOWS_SYS _USE_32BIT_TIME_T + + # This defines the platform to be WinXP or later and is needed for + # getaddrinfo (_WIN32_WINNT_WINXP) + DEFINES *= WINVER=0x0501 + + message(***retroshare.pri:Win32 PREFIX $$PREFIX INCLUDEPATH $$INCLUDEPATH QMAKE_LIBDIR $$QMAKE_LIBDIR DEFINES $$DEFINES) } -macx { +macx-* { rs_macos10.8 { message(***retroshare.pri: Set Target and SDK to MacOS 10.8 ) QMAKE_MACOSX_DEPLOYMENT_TARGET=10.8 @@ -206,102 +463,3 @@ macx { LIB_DIR += "/opt/local/lib" CONFIG += c++11 } - -unfinished { - CONFIG += gxscircles - CONFIG += gxsthewire - CONFIG += gxsphotoshare - CONFIG += wikipoos -} - -wikipoos:DEFINES *= RS_USE_WIKI -rs_gxs:DEFINES *= RS_ENABLE_GXS -libresapilocalserver:DEFINES *= LIBRESAPI_LOCAL_SERVER -qt_dependencies:DEFINES *= LIBRESAPI_QT -libresapihttpserver:DEFINES *= ENABLE_WEBUI -sqlcipher:DEFINES -= NO_SQLCIPHER -no_sqlcipher:DEFINES *= NO_SQLCIPHER -rs_autologin { - DEFINES *= RS_AUTOLOGIN - warning("You have enabled RetroShare auto-login, this is discouraged. The usage of auto-login on some linux distributions may allow someone having access to your session to steal the SSL keys of your node location and therefore compromise your security") -} - -retrotor { - CONFIG *= rs_onlyhiddennode -} - -rs_onlyhiddennode { - DEFINES *= RS_ONLYHIDDENNODE - warning("QMAKE: You have enabled only hidden node.") -} - -no_rs_deprecatedwarning { - QMAKE_CXXFLAGS += -Wno-deprecated - QMAKE_CXXFLAGS += -Wno-deprecated-declarations - DEFINES *= RS_NO_WARN_DEPRECATED - warning("QMAKE: You have disabled deprecated warnings.") -} - -no_rs_cppwarning { - QMAKE_CXXFLAGS += -Wno-cpp - DEFINES *= RS_NO_WARN_CPP - warning("QMAKE: You have disabled C preprocessor warnings.") -} - -rs_gxs_trans { - DEFINES *= RS_GXS_TRANS - greaterThan(QT_MAJOR_VERSION, 4) { - CONFIG += c++11 - } else { - QMAKE_CXXFLAGS += -std=c++0x - } -} - -rs_async_chat { - DEFINES *= RS_ASYNC_CHAT -} - -rs_chatserver { - DEFINES *= RS_CHATSERVER -} - -########################################################################################################################################################### -# -# V07_NON_BACKWARD_COMPATIBLE_CHANGE_001: -# -# What: Computes the node id by performing a sha256 hash of the certificate's PGP signature, instead of simply picking up the last 20 bytes of it. -# -# Why: There is no real risk in forging a certificate with the same ID as the authentication is performed over the PGP signature of the certificate -# which hashes the full SSL certificate (i.e. the full serialized CERT_INFO structure). However the possibility to -# create two certificates with the same IDs is a problem, as it can be used to cause disturbance in the software. -# -# Backward compat: connexions impossible with non patched peers older than Nov 2017, probably because the SSL id that is computed is not the same on both side, -# and in particular unpatched peers see a cerficate with ID different (because computed with the old method) than the ID that was -# submitted when making friends. -# -# Note: the advantage of basing the ID on the signature rather than the public key is not very clear, given that the signature is based on a hash -# of the public key (and the rest of the certificate info). -# -# V07_NON_BACKWARD_COMPATIBLE_CHANGE_002: -# -# What: Use RSA+SHA256 instead of RSA+SHA1 for PGP certificate signatures -# -# Why: Sha1 is likely to be prone to primary collisions anytime soon, so it is urgent to turn to a more secure solution. -# -# Backward compat: unpatched peers after Nov 2017 are able to verify signatures since openpgp-sdk already handle it. -# -# V07_NON_BACKWARD_COMPATIBLE_CHANGE_003: -# -# What: Do not hash PGP certificate twice when signing -# -# Why: hasing twice is not per se a security issue, but it makes it harder to change the settings for hashing. -# -# Backward compat: patched peers cannot connect to non patched peers older than Nov 2017. -########################################################################################################################################################### - -#CONFIG += rs_v07_changes -rs_v07_changes { - DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_001 - DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_002 - DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_003 -} diff --git a/tests/unittests/unittests.pro b/tests/unittests/unittests.pro index 790ab1c6d..c17be5b77 100644 --- a/tests/unittests/unittests.pro +++ b/tests/unittests/unittests.pro @@ -1,6 +1,6 @@ !include("../../retroshare.pri"): error("Could not include file ../../retroshare.pri") -QT += network xml script +QT += network xml CONFIG += bitdht CONFIG += gxs debug From c39173c3db8c0087dfcfb2919c2161b8cb0c1579 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Thu, 19 Apr 2018 14:30:50 +0200 Subject: [PATCH 069/161] Fix TravisCI --- .travis.yml | 31 +++++++------------------------ retroshare.pri | 1 + 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/.travis.yml b/.travis.yml index 90a8f8514..8e8d95b05 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,28 +13,11 @@ matrix: before_install: - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get update; fi - - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get install -y build-essential checkinstall cmake libavutil-dev libavcodec-dev libavformat-dev libbz2-dev libcurl4-openssl-dev libcv-dev libopencv-highgui-dev libhighgui-dev libgnome-keyring-dev libgstreamer-plugins-base0.10-dev libgstreamer0.10-dev libjasper-dev libjpeg-dev libmicrohttpd-dev libopencv-dev libprotobuf-dev libqt4-dev libspeex-dev libspeexdsp-dev libsqlite3-dev libssl-dev libswscale-dev libtbb-dev libtiff4-dev libupnp-dev libv4l-dev libxine-dev libxslt1-dev libxss-dev pkg-config protobuf-compiler python-dev qtmobility-dev gdb ; fi + - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get install -y build-essential libssl-dev libsqlcipher-dev libbz2-dev libmicrohttpd-dev libsqlite3-dev libupnp-dev pkg-config qt5-default libxss-dev qtmultimedia5-dev libqt5x11extras5-dev libqt5designer5 qttools5-dev; fi - -# - if [ $TRAVIS_OS_NAME == osx ]; then xcode-select --install ; fi - if [ $TRAVIS_OS_NAME == osx ]; then brew update ; fi -# - if [ $TRAVIS_OS_NAME == osx ]; then brew install qt55 openssl miniupnpc libmicrohttpd speex homebrew/science/opencv ffmpeg sqlcipher ; fi - - if [ $TRAVIS_OS_NAME == osx ]; then brew install qt55 openssl miniupnpc libmicrohttpd speex speexdsp ffmpeg sqlcipher ; fi + - if [ $TRAVIS_OS_NAME == osx ]; then brew install qt55 openssl miniupnpc libmicrohttpd sqlcipher; fi - if [ $TRAVIS_OS_NAME == osx ]; then brew link --force qt55 ; fi -#Fix for opencv and numpy already installed by system - - if [ $TRAVIS_OS_NAME == osx ]; then rm '/usr/local/bin/f2py'; fi - - if [ $TRAVIS_OS_NAME == osx ]; then rm -r '/usr/local/lib/python2.7/site-packages/numpy'; fi - - if [ $TRAVIS_OS_NAME == osx ]; then brew install opencv; fi - -# FIX: Now openssl is not linked in /usr/local/include and lib - - if [ $TRAVIS_OS_NAME == osx ]; then ln -s /usr/local/opt/openssl/include/* /usr/local/include/; fi - - if [ $TRAVIS_OS_NAME == osx ]; then ln -s /usr/local/opt/openssl/lib/*.a /usr/local/lib/; fi - - if [ $TRAVIS_OS_NAME == osx ]; then ln -s /usr/local/opt/openssl/lib/*.dylib /usr/local/lib/; fi - -# - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get update && sudo apt-get install -y llvm-3.4 llvm-3.4-dev; fi -# - rvm use $RVM --install --binary --fuzzy -# - gem update --system -# - gem --version env: global: @@ -48,16 +31,16 @@ addons: name: "RetroShare/RetroShare" description: "RetroShare Build submitted via Travis CI" build_command_prepend: "qmake CONFIG+=no_sqlcipher; make clean" - build_command: "make -j 4" + build_command: "make -j4" branch_pattern: coverity_scan before_script: - - if [ $TRAVIS_OS_NAME == linux ]; then qmake QMAKE_CC=$CC QMAKE_CXX=$CXX CONFIG+=no_sqlcipher CONFIG+=tests ; fi - - if [ $TRAVIS_OS_NAME == osx ]; then qmake QMAKE_CC=$CC QMAKE_CXX=$CXX CONFIG+=no_sqlcipher CONFIG+=tests CONFIG+=rs_macos10.12 ; fi + - if [ $TRAVIS_OS_NAME == linux ]; then qmake QMAKE_CC=$CC QMAKE_CXX=$CXX; fi + - if [ $TRAVIS_OS_NAME == osx ]; then qmake QMAKE_CC=$CC QMAKE_CXX=$CXX CONFIG+=rs_macos10.12 INCLUDEPATH+=/usr/local/opt/openssl/include/ QMAKE_LIBDIR+=/usr/local/opt/openssl/lib/; fi script: - - if [ $TRAVIS_OS_NAME == linux ] && [ "${COVERITY_SCAN_BRANCH}" != 1 ]; then make && tests/unittests/unittests >/dev/null 2>&1 ; fi - - if [ $TRAVIS_OS_NAME == osx ] && [ "${COVERITY_SCAN_BRANCH}" != 1 ]; then make -j 4 ; fi + - if [ $TRAVIS_OS_NAME == linux ] && [ "${COVERITY_SCAN_BRANCH}" != 1 ]; then make -j2; fi + - if [ $TRAVIS_OS_NAME == osx ] && [ "${COVERITY_SCAN_BRANCH}" != 1 ]; then make -j2; fi #after_success: diff --git a/retroshare.pri b/retroshare.pri index b1eccbb09..b0b07f043 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -462,4 +462,5 @@ macx-* { LIB_DIR += "/usr/local/lib" LIB_DIR += "/opt/local/lib" CONFIG += c++11 + RS_UPNP_LIB = miniupnpc } From c599b5a627d055bd9e713220d8a81b249b3cbfb5 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 22 Apr 2018 17:13:25 +0200 Subject: [PATCH 070/161] removed debug output in pqissl and pqissllistenner --- libretroshare/src/pqi/pqissl.cc | 5 ++++- libretroshare/src/pqi/pqissllistener.cc | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/libretroshare/src/pqi/pqissl.cc b/libretroshare/src/pqi/pqissl.cc index b8d432fb4..47da19b93 100644 --- a/libretroshare/src/pqi/pqissl.cc +++ b/libretroshare/src/pqi/pqissl.cc @@ -716,10 +716,11 @@ int pqissl::Initiate_Connection() //std::cerr << "Setting Connect Timeout " << mConnectTimeout << " Seconds into Future " << std::endl; sockaddr_storage_ipv4_to_ipv6(addr); - +#ifdef PQISSL_DEBUG std::cerr << __PRETTY_FUNCTION__ << " Connecting To: " << PeerId().toStdString() <<" via: " << sockaddr_storage_tostring(addr) << std::endl; +#endif if (0 != (err = unix_connect(osock, addr))) { @@ -730,11 +731,13 @@ int pqissl::Initiate_Connection() sockfd = osock; return 0; default: +#ifdef PQISSL_DEBUG std::cerr << __PRETTY_FUNCTION__ << " Failure connect " << sockaddr_storage_tostring(addr) << " returns: " << err << " -> errno: " << errno << " " << socket_errorType(errno) << std::endl; +#endif net_internal_close(osock); osock = -1; diff --git a/libretroshare/src/pqi/pqissllistener.cc b/libretroshare/src/pqi/pqissllistener.cc index b93eb1e97..c81c96cd2 100644 --- a/libretroshare/src/pqi/pqissllistener.cc +++ b/libretroshare/src/pqi/pqissllistener.cc @@ -119,8 +119,10 @@ int pqissllistenbase::setuplisten() reinterpret_cast(&no), sizeof(no)); if (err) std::cerr << __PRETTY_FUNCTION__ << ": Error setting IPv6 socket dual stack" << std::endl; +#ifdef DEBUG_LISTENNER else std::cerr << __PRETTY_FUNCTION__ << ": Success setting IPv6 socket dual stack" << std::endl; +#endif #endif // IPV6_V6ONLY /********************************** WINDOWS/UNIX SPECIFIC PART ******************/ From 63359e0801bf6505760dc9e7d8cde81dbad30ba3 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 22 Apr 2018 17:14:08 +0200 Subject: [PATCH 071/161] using additional const ref in rsexpr.h --- libretroshare/src/retroshare/rsexpr.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libretroshare/src/retroshare/rsexpr.h b/libretroshare/src/retroshare/rsexpr.h index 06aba7e20..72c63d463 100644 --- a/libretroshare/src/retroshare/rsexpr.h +++ b/libretroshare/src/retroshare/rsexpr.h @@ -182,7 +182,7 @@ private: class StringExpression: public Expression { public: - StringExpression(enum StringOperator op, std::list &t, bool ic): Op(op),terms(t), IgnoreCase(ic){} + StringExpression(enum StringOperator op, const std::list &t, bool ic): Op(op),terms(t), IgnoreCase(ic){} virtual void linearize(LinearizedExpression& e) const ; virtual std::string toStdString(const std::string& varstr) const; @@ -275,7 +275,7 @@ Some implementations of StringExpressions. class NameExpression: public StringExpression { public: - NameExpression(enum StringOperator op, std::list &t, bool ic): + NameExpression(enum StringOperator op, const std::list &t, bool ic): StringExpression(op,t,ic) {} bool eval(const ExpFileEntry& file); @@ -290,7 +290,7 @@ public: class PathExpression: public StringExpression { public: - PathExpression(enum StringOperator op, std::list &t, bool ic): + PathExpression(enum StringOperator op, const std::list &t, bool ic): StringExpression(op,t,ic) {} bool eval(const ExpFileEntry& file); @@ -305,7 +305,7 @@ public: class ExtExpression: public StringExpression { public: - ExtExpression(enum StringOperator op, std::list &t, bool ic): + ExtExpression(enum StringOperator op, const std::list &t, bool ic): StringExpression(op,t,ic) {} bool eval(const ExpFileEntry& file); @@ -320,7 +320,7 @@ public: class HashExpression: public StringExpression { public: - HashExpression(enum StringOperator op, std::list &t): + HashExpression(enum StringOperator op, const std::list &t): StringExpression(op,t, true) {} bool eval(const ExpFileEntry& file); From 1e6e9dfd1295f2c2bfe83ca5fca4faa1b4836159 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 22 Apr 2018 17:15:40 +0200 Subject: [PATCH 072/161] fixed crazy cost of search in file list tree/flat mode using filterProxyModel instrinsic filter system --- .../gui/FileTransfer/SharedFilesDialog.cpp | 219 ++++++++---------- .../src/gui/FileTransfer/SharedFilesDialog.h | 8 +- retroshare-gui/src/gui/RemoteDirModel.cpp | 103 +++++++- retroshare-gui/src/gui/RemoteDirModel.h | 9 +- 4 files changed, 198 insertions(+), 141 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index 2fce386ae..d56df8834 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -36,6 +36,8 @@ #include "gui/settings/rsharesettings.h" #include "util/QtVersion.h" #include "util/RsAction.h" +#include "util/misc.h" +#include "util/rstime.h" #include #include @@ -167,17 +169,24 @@ SharedFilesDialog::SharedFilesDialog(RetroshareDirModel *_tree_model,RetroshareD flat_model = _flat_model ; connect(flat_model, SIGNAL(layoutChanged()), this, SLOT(updateDirTreeView()) ); + // For filtering items we use a trick: the underlying model will use this FilterRole role to highlight selected items + // while the filterProxyModel will select them using the pre-chosen string "filtered". + tree_proxyModel = new SFDSortFilterProxyModel(tree_model, this); tree_proxyModel->setSourceModel(tree_model); tree_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive); tree_proxyModel->setSortRole(RetroshareDirModel::SortRole); tree_proxyModel->sort(COLUMN_NAME); + tree_proxyModel->setFilterRole(RetroshareDirModel::FilterRole); + tree_proxyModel->setFilterRegExp(QRegExp(QString(RETROSHARE_DIR_MODEL_FILTER_STRING))) ; flat_proxyModel = new SFDSortFilterProxyModel(flat_model, this); flat_proxyModel->setSourceModel(flat_model); flat_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive); flat_proxyModel->setSortRole(RetroshareDirModel::SortRole); flat_proxyModel->sort(COLUMN_NAME); + flat_proxyModel->setFilterRole(RetroshareDirModel::FilterRole); + flat_proxyModel->setFilterRegExp(QRegExp(QString(RETROSHARE_DIR_MODEL_FILTER_STRING))) ; // Mr.Alice: I removed this because it causes a crash for some obscur reason. Apparently when the model is changed, the proxy model cannot // deal with the change by itself. Should I call something specific? I've no idea. Removing this does not seem to cause any harm either. @@ -193,9 +202,9 @@ SharedFilesDialog::SharedFilesDialog(RetroshareDirModel *_tree_model,RetroshareD ui.filterClearButton->hide(); ui.filterStartButton->hide(); - mFilterTimer = new RsProtectedTimer( this ); - mFilterTimer->setSingleShot( true ); // Ensure the timer will fire only once after it was started - connect(mFilterTimer, SIGNAL(timeout()), this, SLOT(filterRegExpChanged())); +// mFilterTimer = new RsProtectedTimer( this ); +// mFilterTimer->setSingleShot( true ); // Ensure the timer will fire only once after it was started +// connect(mFilterTimer, SIGNAL(timeout()), this, SLOT(filterRegExpChanged())); /* Set header resize modes and initial section sizes */ QHeaderView * header = ui.dirTreeView->header () ; @@ -932,6 +941,48 @@ void SharedFilesDialog::restoreExpandedPathsAndSelection(const std::setblockSignals(false) ; } +void SharedFilesDialog::expandAll() +{ + if(ui.dirTreeView->model() == NULL) + return ; + + ui.dirTreeView->blockSignals(true) ; + +#ifdef DEBUG_SHARED_FILES_DIALOG + std::cerr << "Restoring expanded items. " << std::endl; +#endif + for(int row = 0; row < ui.dirTreeView->model()->rowCount(); ++row) + { + std::string path = ui.dirTreeView->model()->index(row,0).data(Qt::DisplayRole).toString().toStdString(); + recursExpandAll(ui.dirTreeView->model()->index(row,0)); + } + //QItemSelection selection ; + + ui.dirTreeView->blockSignals(false) ; + +} + +void SharedFilesDialog::recursExpandAll(const QModelIndex& index) +{ + ui.dirTreeView->setExpanded(index,true) ; + + for(int row=0;rowmodel()->rowCount(index);++row) + { + QModelIndex idx(index.child(row,0)) ; + + if(ui.dirTreeView->model()->rowCount(idx) > 0) + recursExpandAll(idx) ; + +// QModelIndex midx = proxyModel->mapToSource(idx) ; +// +// if (!midx.isValid()) +// continue ; +// +// if (model->getType(midx) != DIR_TYPE_FILE) +// recursExpandAll(idx) ; + } +} + void SharedFilesDialog::recursSaveExpandedItems(const QModelIndex& index,const std::string& path,std::set& exp, std::set& vis, std::set& sel @@ -983,8 +1034,8 @@ void SharedFilesDialog::recursRestoreExpandedItems(const QModelIndex& index, con bool invisible = vis.find(local_path) != vis.end(); ui.dirTreeView->setRowHidden(index.row(),index.parent(),invisible ) ; - if(invisible) - mHiddenIndexes.push_back(proxyModel->mapToSource(index)); +// if(invisible) +// mHiddenIndexes.push_back(proxyModel->mapToSource(index)); if(!invisible && exp.find(local_path) != exp.end()) { @@ -1287,6 +1338,7 @@ void SharedFilesDialog::onFilterTextEdited() #endif } +#ifdef DEPRECATED_CODE void SharedFilesDialog::filterRegExpChanged() { QString text = ui.filterPatternLineEdit->text(); @@ -1323,17 +1375,8 @@ void SharedFilesDialog::filterRegExpChanged() ui.filterStartButton->setEnabled(true) ; ui.filterPatternFrame->setToolTip(QString()); - - /* unpolish widget to clear the stylesheet's palette cache */ - // ui.filterPatternFrame->style()->unpolish(ui.filterPatternFrame); - - // QPalette palette = ui.filterPatternLineEdit->palette(); - // palette.setColor(ui.filterPatternLineEdit->backgroundRole(), color); - // ui.filterPatternLineEdit->setPalette(palette); - - // //ui.searchLineFrame->setProperty("valid", valid); - // Rshare::refreshStyleSheet(ui.filterPatternFrame, false); } +#endif /* clear Filter */ void SharedFilesDialog::clearFilter() @@ -1368,13 +1411,14 @@ void SharedFilesDialog::updateDirTreeView() ui.dirTreeView->setToolTip(""); } +//#define DEBUG_SHARED_FILES_DIALOG + +#ifdef DEPRECATED_CODE // This macro make the search expand all items that contain the searched text. // A bug however, makes RS expand everything when nothing is selected, which is a pain. #define EXPAND_WHILE_SEARCHING 1 -//#define DEBUG_SHARED_FILES_DIALOG - void recursMakeVisible(QTreeView *tree,const QSortFilterProxyModel *proxyModel,const QModelIndex& indx,uint32_t depth,const std::vector >& pointers,QList& hidden_list) { #ifdef DEBUG_SHARED_FILES_DIALOG @@ -1430,6 +1474,8 @@ void recursMakeVisible(QTreeView *tree,const QSortFilterProxyModel *proxyModel,c void SharedFilesDialog::restoreInvisibleItems() { + std::cerr << "Restoring " << mHiddenIndexes.size() << " invisible indexes" << std::endl; + for(QList::const_iterator it(mHiddenIndexes.begin());it!=mHiddenIndexes.end();++it) { QModelIndex indx = proxyModel->mapFromSource(*it); @@ -1440,6 +1486,7 @@ void SharedFilesDialog::restoreInvisibleItems() mHiddenIndexes.clear(); } +#endif class QCursorContextBlocker { @@ -1476,122 +1523,49 @@ void SharedFilesDialog::FilterItems() return ; } - std::cerr << "New last text. Performing the filter" << std::endl; + std::cerr << "New last text. Performing the filter on string \"" << text.toStdString() << "\"" << std::endl; mLastFilterText = text ; model->update() ; - restoreInvisibleItems(); QCursorContextBlocker q(ui.dirTreeView) ; - if(proxyModel == tree_proxyModel) + QCoreApplication::processEvents() ; + + std::list result_list ; + uint32_t found = 0 ; + + if(text == "") { - QCoreApplication::processEvents() ; - - std::list keywords ; - std::list result_list ; - - if(text == "") - return ; - - if(text.length() < 3) - return ; - - FileSearchFlags flags = isRemote()?RS_FILE_HINTS_REMOTE:RS_FILE_HINTS_LOCAL; - QStringList lst = text.split(" ",QString::SkipEmptyParts) ; - - for(auto it(lst.begin());it!=lst.end();++it) - keywords.push_back((*it).toStdString()); - - if(keywords.size() > 1) - { - RsRegularExpression::NameExpression exp(RsRegularExpression::ContainsAllStrings,keywords,true); - rsFiles->SearchBoolExp(&exp,result_list, flags) ; - } - else - rsFiles->SearchKeywords(keywords,result_list, flags) ; - -#ifdef DEBUG_SHARED_FILES_DIALOG - std::cerr << "Found " << result_list.size() << " results" << std::endl; -#endif - - size_t resSize = result_list.size(); - if(resSize == 0) - { - ui.filterPatternFrame->setToolTip(tr("No result.")) ; - return ; - } - if(resSize > MAX_SEARCH_RESULTS) - { - ui.filterPatternFrame->setToolTip(tr("More than %1 results. Add more/longer search words to select less.").arg(MAX_SEARCH_RESULTS)) ; - return ; - } - ui.filterPatternFrame->setToolTip(tr("Found %1 results.").arg(resSize)) ; - -#ifdef DEBUG_SHARED_FILES_DIALOG - std::cerr << "Found this result: " << std::endl; -#endif - std::vector > pointers(2,std::set()); // at least two levels need to be here. - - // Then show only the ones we need - for(auto it(result_list.begin());it!=result_list.end();++it) - { -#ifdef DEBUG_SHARED_FILES_DIALOG - std::cerr << (void*)(*it).ref << " parents: " ; -#endif - - DirDetails& det(*it) ; - void *p = NULL; - std::list lst ; - - lst.push_back(det.ref) ; - - while(det.type == DIR_TYPE_FILE || det.type == DIR_TYPE_DIR) - { - p = det.parent ; - rsFiles->RequestDirDetails( p, det, flags); - -#ifdef DEBUG_SHARED_FILES_DIALOG - std::cerr << " " << (void*)p << "(" << (int)det.type << ")"; -#endif - - lst.push_front(p) ; - } - -#ifdef DEBUG_SHARED_FILES_DIALOG - std::cerr << std::endl; -#endif - - uint32_t u=0; - for(auto it2(lst.begin());it2!=lst.end();++it2,++u) - { - if(pointers.size() <= u) - pointers.resize(u+5) ; - - pointers[u].insert(*it2) ; - } - } - - int rowCount = ui.dirTreeView->model()->rowCount(); - for (int row = 0; row < rowCount; ++row) - recursMakeVisible(ui.dirTreeView,proxyModel,ui.dirTreeView->model()->index(row, COLUMN_NAME),0,pointers,mHiddenIndexes); + model->filterItems(std::list(),found) ; + return ; } + + if(text.length() < 3) + return ; + + FileSearchFlags flags = isRemote()?RS_FILE_HINTS_REMOTE:RS_FILE_HINTS_LOCAL; + QStringList lst = text.split(" ",QString::SkipEmptyParts) ; + std::list keywords ; + + for(auto it(lst.begin());it!=lst.end();++it) + keywords.push_back((*it).toStdString()); + + model->filterItems(keywords,found) ; + + if(found > 0) + expandAll(); + + if(found == 0) + ui.filterPatternFrame->setToolTip(tr("No result.")) ; + else if(found > MAX_SEARCH_RESULTS) + ui.filterPatternFrame->setToolTip(tr("More than %1 results. Add more/longer search words to select less.").arg(MAX_SEARCH_RESULTS)) ; else - { - int rowCount = ui.dirTreeView->model()->rowCount(); - for (int row = 0; row < rowCount; ++row) - flat_FilterItem(ui.dirTreeView->model()->index(row, COLUMN_NAME), text, 0); - } + ui.filterPatternFrame->setToolTip(tr("Found %1 results.").arg(found)) ; -#ifdef DEPRECATED_CODE - int rowCount = ui.dirTreeView->model()->rowCount(); - for (int row = 0; row < rowCount; ++row) - if(proxyModel == tree_proxyModel) - tree_FilterItem(ui.dirTreeView->model()->index(row, COLUMN_NAME), text, 0); - else - flat_FilterItem(ui.dirTreeView->model()->index(row, COLUMN_NAME), text, 0); -#endif + std::cerr << found << " results found by search." << std::endl; } +#ifdef DEPRECATED_CODE bool SharedFilesDialog::flat_FilterItem(const QModelIndex &index, const QString &text, int /*level*/) { if(index.data(RetroshareDirModel::FileNameRole).toString().contains(text, Qt::CaseInsensitive)) @@ -1641,3 +1615,4 @@ bool SharedFilesDialog::tree_FilterItem(const QModelIndex &index, const QString return (visible || visibleChildCount); } +#endif diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h index 63a57683c..0d35260ee 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h @@ -77,7 +77,7 @@ private slots: void indicatorChanged(int index); void onFilterTextEdited(); - void filterRegExpChanged(); + //void filterRegExpChanged(); void clearFilter(); void startFilter(); @@ -97,6 +97,8 @@ protected: void recursSaveExpandedItems(const QModelIndex& index, const std::string &path, std::set &exp,std::set& vis, std::set& sel); void saveExpandedPathsAndSelection(std::set& paths,std::set& visible_indexes, std::set& selected_indexes) ; void restoreExpandedPathsAndSelection(const std::set& paths,const std::set& visible_indexes, const std::set& selected_indexes) ; + void recursExpandAll(const QModelIndex& index); + void expandAll(); protected: //now context menu are created again every time theu are called ( in some @@ -113,8 +115,6 @@ protected: bool tree_FilterItem(const QModelIndex &index, const QString &text, int level); bool flat_FilterItem(const QModelIndex &index, const QString &text, int level); - void restoreInvisibleItems(); - QModelIndexList getSelected(); /** Defines the actions for the context menu for QTreeWidget */ @@ -142,8 +142,6 @@ protected: QString lastFilterString; QString mLastFilterText ; RsProtectedTimer* mFilterTimer; - - QList mHiddenIndexes; }; class LocalSharedFilesDialog : public SharedFilesDialog diff --git a/retroshare-gui/src/gui/RemoteDirModel.cpp b/retroshare-gui/src/gui/RemoteDirModel.cpp index f6d0df88f..5bb189486 100644 --- a/retroshare-gui/src/gui/RemoteDirModel.cpp +++ b/retroshare-gui/src/gui/RemoteDirModel.cpp @@ -30,6 +30,7 @@ #include "retroshare/rsfiles.h" #include "retroshare/rspeers.h" #include "util/misc.h" +#include "retroshare/rsexpr.h" #include #include @@ -44,6 +45,7 @@ /***** * #define RDM_DEBUG ****/ +#define RDM_SEARCH_DEBUG static const uint32_t FLAT_VIEW_MAX_REFS_PER_SECOND = 2000 ; static const size_t FLAT_VIEW_MAX_REFS_TABLE_SIZE = 10000 ; // @@ -170,7 +172,7 @@ bool TreeStyle_RDM::hasChildren(const QModelIndex &parent) const } /* PERSON/DIR*/ #ifdef RDM_DEBUG - std::cerr << "lookup PER/DIR #" << details->count; + std::cerr << "lookup PER/DIR #" << details.count; std::cerr << std::endl; #endif return (details.count > 0); /* do we have children? */ @@ -229,7 +231,7 @@ int TreeStyle_RDM::rowCount(const QModelIndex &parent) const /* else PERSON/DIR*/ #ifdef RDM_DEBUG - std::cerr << "lookup PER/DIR #" << details->count; + std::cerr << "lookup PER/DIR #" << details.count; std::cerr << std::endl; #endif if ((details.type == DIR_TYPE_ROOT) && !_showEmpty && RemoteMode) @@ -367,11 +369,19 @@ const QIcon& RetroshareDirModel::getFlagsIcon(FileStorageFlags flags) static_icons[n] = new QIcon(pix); - std::cerr << "Generated icon for flags " << std::hex << flags << std::endl; + std::cerr << "Generated icon for flags " << std::hex << flags << std::dec << std::endl; } return *static_icons[n] ; } +QVariant RetroshareDirModel::filterRole(const DirDetails& details,int coln) const +{ + if(mFilteredPointers.empty() || mFilteredPointers.find(details.ref) != mFilteredPointers.end()) + return QString(RETROSHARE_DIR_MODEL_FILTER_STRING); + else + return QVariant(); +} + QVariant RetroshareDirModel::decorationRole(const DirDetails& details,int coln) const { if(coln == COLUMN_FRIEND_ACCESS) @@ -803,9 +813,14 @@ QVariant RetroshareDirModel::data(const QModelIndex &index, int role) const if (role == SortRole) return sortRole(index,details,coln) ; + if (role == FilterRole) + return filterRole(details,coln) ; + return QVariant(); } + + /**** //void RetroshareDirModel::getAgeIndicatorRec(const DirDetails &details, QString &ret) const { // if (details.type == DIR_TYPE_FILE) { @@ -1026,7 +1041,7 @@ QModelIndex TreeStyle_RDM::parent( const QModelIndex & index ) const } #ifdef RDM_DEBUG - std::cerr << "success index(" << details->prow << ",0," << details->parent << ")"; + std::cerr << "success index(" << details.prow << ",0," << details.parent << ")"; std::cerr << std::endl; std::cerr << "Creating index 3 row=" << details.prow << ", column=" << 0 << ", ref=" << (void*)details.parent << std::endl; @@ -1339,10 +1354,10 @@ void RetroshareDirModel::getFileInfoFromIndexList(const QModelIndexList& list, s #ifdef RDM_DEBUG std::cerr << "::::::::::::FileRecommend:::: " << std::endl; - std::cerr << "Name: " << details->name << std::endl; - std::cerr << "Hash: " << details->hash << std::endl; - std::cerr << "Size: " << details->count << std::endl; - std::cerr << "Path: " << details->path << std::endl; + std::cerr << "Name: " << details.name << std::endl; + std::cerr << "Hash: " << details.hash << std::endl; + std::cerr << "Size: " << details.count << std::endl; + std::cerr << "Path: " << details.path << std::endl; #endif // Note: for directories, the returned hash, is the peer id, so if we collect // dirs, we need to be a bit more conservative for the @@ -1449,6 +1464,70 @@ void RetroshareDirModel::getFilePaths(const QModelIndexList &list, std::list& keywords,uint32_t& found) +{ + FileSearchFlags flags = RemoteMode?RS_FILE_HINTS_REMOTE:RS_FILE_HINTS_LOCAL; + + std::list result_list ; + found = 0 ; + + if(keywords.empty()) + { + mFilteredPointers.clear(); + return ; + } + else if(keywords.size() > 1) + { + RsRegularExpression::NameExpression exp(RsRegularExpression::ContainsAllStrings,keywords,true); + rsFiles->SearchBoolExp(&exp,result_list, flags) ; + } + else + rsFiles->SearchKeywords(keywords,result_list, flags) ; + +#ifdef RDM_SEARCH_DEBUG + std::cerr << "Found " << result_list.size() << " results" << std::endl; +#endif + + if(result_list.empty()) // in this case we dont clear the list of filtered items, so that we can keep the old filter list + return ; + + mFilteredPointers.clear(); + +#ifdef RDM_SEARCH_DEBUG + std::cerr << "Found this result: " << std::endl; +#endif + + // Then show only the ones we need + + for(auto it(result_list.begin());it!=result_list.end();++it) + { + DirDetails& det(*it) ; +#ifdef RDM_SEARCH_DEBUG + std::cerr << (void*)(*it).ref << " name=\"" << det.name << "\" parents: " ; +#endif + void *p = det.ref ; + mFilteredPointers.insert(p) ; + ++found ; + + while(det.type == DIR_TYPE_FILE || det.type == DIR_TYPE_DIR) + { + p = det.parent ; + rsFiles->RequestDirDetails( p, det, flags); + +#ifdef RDM_SEARCH_DEBUG + std::cerr << " " << (void*)p << "(" << (int)det.type << ")"; +#endif + mFilteredPointers.insert(p) ; + } + +#ifdef RDM_SEARCH_DEBUG + std::cerr << std::endl; +#endif + } + std::cerr << mFilteredPointers.size() << " pointers in filter set." << std::endl; +} + + /* Drag and Drop Functionality */ QMimeData * RetroshareDirModel::mimeData ( const QModelIndexList & indexes ) const { @@ -1468,10 +1547,10 @@ QMimeData * RetroshareDirModel::mimeData ( const QModelIndexList & indexes ) con #ifdef RDM_DEBUG std::cerr << "::::::::::::FileDrag:::: " << std::endl; - std::cerr << "Name: " << details->name << std::endl; - std::cerr << "Hash: " << details->hash << std::endl; - std::cerr << "Size: " << details->count << std::endl; - std::cerr << "Path: " << details->path << std::endl; + std::cerr << "Name: " << details.name << std::endl; + std::cerr << "Hash: " << details.hash << std::endl; + std::cerr << "Size: " << details.count << std::endl; + std::cerr << "Path: " << details.path << std::endl; #endif if (details.type != DIR_TYPE_FILE) diff --git a/retroshare-gui/src/gui/RemoteDirModel.h b/retroshare-gui/src/gui/RemoteDirModel.h index 6b4fe8967..b7ff971dc 100644 --- a/retroshare-gui/src/gui/RemoteDirModel.h +++ b/retroshare-gui/src/gui/RemoteDirModel.h @@ -39,6 +39,7 @@ #define COLUMN_FRIEND_ACCESS 4 #define COLUMN_WN_VISU_DIR 5 #define COLUMN_COUNT 6 +#define RETROSHARE_DIR_MODEL_FILTER_STRING "filtered" class DirDetails; @@ -60,7 +61,7 @@ class RetroshareDirModel : public QAbstractItemModel Q_OBJECT public: - enum Roles{ FileNameRole = Qt::UserRole+1, SortRole = Qt::UserRole+2 }; + enum Roles{ FileNameRole = Qt::UserRole+1, SortRole = Qt::UserRole+2, FilterRole = Qt::UserRole+3 }; RetroshareDirModel(bool mode, QObject *parent = 0); virtual ~RetroshareDirModel() {} @@ -94,7 +95,8 @@ class RetroshareDirModel : public QAbstractItemModel virtual QMenu* getContextMenu(QMenu* contextMenu) {return contextMenu;} - public: + void filterItems(const std::list& keywords, uint32_t& found) ; + //Overloaded from QAbstractItemModel virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; virtual QStringList mimeTypes () const; @@ -118,6 +120,7 @@ class RetroshareDirModel : public QAbstractItemModel virtual QVariant sortRole(const QModelIndex&,const DirDetails&,int) const =0; QVariant decorationRole(const DirDetails&,int) const ; + QVariant filterRole(const DirDetails& details,int coln) const; uint32_t ageIndicator; @@ -172,6 +175,8 @@ class RetroshareDirModel : public QAbstractItemModel mutable time_t mLastReq; bool mUpdating ; + + std::set mFilteredPointers ; }; // This class shows the classical hierarchical directory view of shared files From 849ed79cf2dc2f1fa33db767986c347d18ef1c9a Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 22 Apr 2018 17:38:14 +0200 Subject: [PATCH 073/161] fixed missing update after filtering --- retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp | 3 ++- retroshare-gui/src/gui/RemoteDirModel.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index d56df8834..ee75f29aa 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -1525,7 +1525,6 @@ void SharedFilesDialog::FilterItems() std::cerr << "New last text. Performing the filter on string \"" << text.toStdString() << "\"" << std::endl; mLastFilterText = text ; - model->update() ; QCursorContextBlocker q(ui.dirTreeView) ; @@ -1537,6 +1536,7 @@ void SharedFilesDialog::FilterItems() if(text == "") { model->filterItems(std::list(),found) ; + model->update() ; return ; } @@ -1551,6 +1551,7 @@ void SharedFilesDialog::FilterItems() keywords.push_back((*it).toStdString()); model->filterItems(keywords,found) ; + model->update() ; if(found > 0) expandAll(); diff --git a/retroshare-gui/src/gui/RemoteDirModel.cpp b/retroshare-gui/src/gui/RemoteDirModel.cpp index 5bb189486..fddfbb404 100644 --- a/retroshare-gui/src/gui/RemoteDirModel.cpp +++ b/retroshare-gui/src/gui/RemoteDirModel.cpp @@ -44,8 +44,8 @@ /***** * #define RDM_DEBUG + * #define RDM_SEARCH_DEBUG ****/ -#define RDM_SEARCH_DEBUG static const uint32_t FLAT_VIEW_MAX_REFS_PER_SECOND = 2000 ; static const size_t FLAT_VIEW_MAX_REFS_TABLE_SIZE = 10000 ; // From 236b0ce2b4bc73efc2d45c28ca6de18a513d5816 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 22 Apr 2018 17:57:14 +0200 Subject: [PATCH 074/161] re-enabled search while typing, now that it is fast enough --- .../src/gui/FileTransfer/SharedFilesDialog.cpp | 18 ++++++++---------- retroshare-gui/src/gui/RemoteDirModel.cpp | 2 ++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index ee75f29aa..a1df23308 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -943,7 +943,7 @@ void SharedFilesDialog::restoreExpandedPathsAndSelection(const std::setmodel() == NULL) + if(ui.dirTreeView->model() == NULL || ui.dirTreeView->model() == flat_proxyModel) // this method causes infinite loops on flat models return ; ui.dirTreeView->blockSignals(true) ; @@ -956,7 +956,6 @@ void SharedFilesDialog::expandAll() std::string path = ui.dirTreeView->model()->index(row,0).data(Qt::DisplayRole).toString().toStdString(); recursExpandAll(ui.dirTreeView->model()->index(row,0)); } - //QItemSelection selection ; ui.dirTreeView->blockSignals(false) ; @@ -972,14 +971,6 @@ void SharedFilesDialog::recursExpandAll(const QModelIndex& index) if(ui.dirTreeView->model()->rowCount(idx) > 0) recursExpandAll(idx) ; - -// QModelIndex midx = proxyModel->mapToSource(idx) ; -// -// if (!midx.isValid()) -// continue ; -// -// if (model->getType(midx) != DIR_TYPE_FILE) -// recursExpandAll(idx) ; } } @@ -1332,6 +1323,7 @@ void SharedFilesDialog::onFilterTextEdited() ui.filterStartButton->setEnabled(true) ; ui.filterPatternFrame->setToolTip(QString()); + FilterItems(); #ifndef DISABLE_SEARCH_WHILE_TYPING mFilterTimer->start( 500 ); // This will fire filterRegExpChanged after 500 ms. // If the user types something before it fires, the timer restarts counting @@ -1519,11 +1511,15 @@ void SharedFilesDialog::FilterItems() if(mLastFilterText == text) // do not filter again if we already did. This is an optimization { +#ifdef DEBUG_SHARED_FILES_DIALOG std::cerr << "Last text is equal to text. skipping" << std::endl; +#endif return ; } +#ifdef DEBUG_SHARED_FILES_DIALOG std::cerr << "New last text. Performing the filter on string \"" << text.toStdString() << "\"" << std::endl; +#endif mLastFilterText = text ; QCursorContextBlocker q(ui.dirTreeView) ; @@ -1563,7 +1559,9 @@ void SharedFilesDialog::FilterItems() else ui.filterPatternFrame->setToolTip(tr("Found %1 results.").arg(found)) ; +#ifdef DEBUG_SHARED_FILES_DIALOG std::cerr << found << " results found by search." << std::endl; +#endif } #ifdef DEPRECATED_CODE diff --git a/retroshare-gui/src/gui/RemoteDirModel.cpp b/retroshare-gui/src/gui/RemoteDirModel.cpp index fddfbb404..6f7608e77 100644 --- a/retroshare-gui/src/gui/RemoteDirModel.cpp +++ b/retroshare-gui/src/gui/RemoteDirModel.cpp @@ -1524,7 +1524,9 @@ void RetroshareDirModel::filterItems(const std::list& keywords,uint std::cerr << std::endl; #endif } +#ifdef RDM_SEARCH_DEBUG std::cerr << mFilteredPointers.size() << " pointers in filter set." << std::endl; +#endif } From e5d2f88fab295f989cb703f6d5115af0d2b4efcb Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 22 Apr 2018 18:31:05 +0200 Subject: [PATCH 075/161] removed "search while typing" because it is really too painful --- retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index a1df23308..973df48b8 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -197,7 +197,7 @@ SharedFilesDialog::SharedFilesDialog(RetroshareDirModel *_tree_model,RetroshareD connect(ui.filterClearButton, SIGNAL(clicked()), this, SLOT(clearFilter())); connect(ui.filterStartButton, SIGNAL(clicked()), this, SLOT(startFilter())); connect(ui.filterPatternLineEdit, SIGNAL(returnPressed()), this, SLOT(startFilter())); - connect(ui.filterPatternLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(onFilterTextEdited())); +// connect(ui.filterPatternLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(onFilterTextEdited())); //Hidden by default, shown on onFilterTextEdited ui.filterClearButton->hide(); ui.filterStartButton->hide(); From 2294f735394538838e6c644387b7d32cb96bf517 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 22 Apr 2018 21:38:12 +0200 Subject: [PATCH 076/161] fixed search button missing due to previous commit --- retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index 973df48b8..240839a2e 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -197,7 +197,7 @@ SharedFilesDialog::SharedFilesDialog(RetroshareDirModel *_tree_model,RetroshareD connect(ui.filterClearButton, SIGNAL(clicked()), this, SLOT(clearFilter())); connect(ui.filterStartButton, SIGNAL(clicked()), this, SLOT(startFilter())); connect(ui.filterPatternLineEdit, SIGNAL(returnPressed()), this, SLOT(startFilter())); -// connect(ui.filterPatternLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(onFilterTextEdited())); + connect(ui.filterPatternLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(onFilterTextEdited())); //Hidden by default, shown on onFilterTextEdited ui.filterClearButton->hide(); ui.filterStartButton->hide(); @@ -1323,7 +1323,7 @@ void SharedFilesDialog::onFilterTextEdited() ui.filterStartButton->setEnabled(true) ; ui.filterPatternFrame->setToolTip(QString()); - FilterItems(); + //FilterItems(); #ifndef DISABLE_SEARCH_WHILE_TYPING mFilterTimer->start( 500 ); // This will fire filterRegExpChanged after 500 ms. // If the user types something before it fires, the timer restarts counting From 678ee31a14ee159449afcb3a72c4f1d1b99cba8d Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 23 Apr 2018 20:58:40 +0200 Subject: [PATCH 077/161] Fix compilation with GCC 6 Adding default include path to INCLUDEPATH break compilation with GCC 6 With many errors similar to this x86_64-pc-linux-gnu-gcc -c -march=native -mtune=native -pipe -O0 -g -fno-omit-frame-pointer -fPIC -Wall -W -D_REENTRANT -DRS_ENABLE_GXS -DENABLE_WEBUI -DRS_NO_WARN_DEPRECATED -DRS_NO_WARN_CPP -DRS_GXS_TRANS -DOPENSSL_NO_IDEA -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -isystem /usr/include/libdrm -I/usr/lib64/qt5/mkspecs/linux-g++ -o temp/linux/obj/accumulate.o openpgpsdk/accumulate.c In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/include/g++-v6/ext/string_conversions.h:41:0, from /usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/include/g++-v6/bits/basic_string.h:5417, from /usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/include/g++-v6/string:52, from /usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/include/g++-v6/bits/locale_classes.h:40, from /usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/include/g++-v6/bits/ios_base.h:41, from /usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/include/g++-v6/ios:42, from /usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/include/g++-v6/ostream:38, from /usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/include/g++-v6/iostream:39, from ./bitdht/bdobj.h:32, from ./bitdht/bdmsgs.h:34, from bitdht/bdmsgs.cc:30: /usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/include/g++-v6/cstdlib:75:25: fatal error: stdlib.h: No such file or directory #include_next ^ @see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70129 --- retroshare.pri | 1 - 1 file changed, 1 deletion(-) diff --git a/retroshare.pri b/retroshare.pri index b0b07f043..c4f47aedf 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -345,7 +345,6 @@ linux-* { isEmpty(RS_DATA_DIR) : RS_DATA_DIR = "$${PREFIX}/share/retroshare" isEmpty(RS_PLUGIN_DIR) : RS_PLUGIN_DIR = "$${RS_LIB_DIR}/retroshare/extensions6" - INCLUDEPATH *= "$$RS_INCLUDE_DIR" QMAKE_LIBDIR *= "$$RS_LIB_DIR" rs_autologin { From 056285075832fe9b74a19de6eaf784527b9e0157 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 23 Apr 2018 21:11:41 +0200 Subject: [PATCH 078/161] Fix retroshare-gui compilation with GCC 6 same as 678ee31a14ee159449afcb3a72c4f1d1b99cba8d for retroshare-gui --- retroshare-gui/src/retroshare-gui.pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/retroshare-gui.pro b/retroshare-gui/src/retroshare-gui.pro index 9cf15c4f1..30b2af946 100644 --- a/retroshare-gui/src/retroshare-gui.pro +++ b/retroshare-gui/src/retroshare-gui.pro @@ -8,7 +8,7 @@ TARGET = retroshare DEFINES += TARGET=\\\"$${TARGET}\\\" DEPENDPATH *= $${PWD} $${RS_INCLUDE_DIR} retroshare-gui -INCLUDEPATH *= $${PWD} $${RS_INCLUDE_DIR} retroshare-gui +INCLUDEPATH *= $${PWD} retroshare-gui libresapihttpserver { !include("../../libresapi/src/use_libresapi.pri"):error("Including") From b4ccfe91f8226a30453a157822856663382e10f5 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 23 Apr 2018 22:53:55 +0200 Subject: [PATCH 079/161] Fix retroshare-nogui compilation with GCC 6 same as 678ee31a14ee159449afcb3a72c4f1d1b99cba8d for retroshare-nogui --- retroshare-nogui/src/retroshare-nogui.pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-nogui/src/retroshare-nogui.pro b/retroshare-nogui/src/retroshare-nogui.pro index 8e731a9d1..6be700be5 100644 --- a/retroshare-nogui/src/retroshare-nogui.pro +++ b/retroshare-nogui/src/retroshare-nogui.pro @@ -5,7 +5,7 @@ TARGET = retroshare-nogui CONFIG -= qt xml gui DEPENDPATH *= $${PWD} $${RS_INCLUDE_DIR} -INCLUDEPATH *= $${PWD} $${RS_INCLUDE_DIR} +INCLUDEPATH *= $${PWD} libresapihttpserver { !include("../../libresapi/src/use_libresapi.pri"):error("Including") From f4e110ed0e2646c6966bd22d08c02bc5dc558bbb Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Thu, 26 Apr 2018 11:04:05 +0200 Subject: [PATCH 080/161] p3Banlist fail gracefully if IPv6 address get into Translate IPv4 mapped to plain IPv4 before processing Fail gracefully if the address is IPv6 --- libretroshare/src/services/p3banlist.cc | 90 ++++++++++++++++++------- libretroshare/src/services/p3banlist.h | 33 +++++---- 2 files changed, 86 insertions(+), 37 deletions(-) diff --git a/libretroshare/src/services/p3banlist.cc b/libretroshare/src/services/p3banlist.cc index 6a061500c..9281307fd 100644 --- a/libretroshare/src/services/p3banlist.cc +++ b/libretroshare/src/services/p3banlist.cc @@ -306,16 +306,14 @@ bool p3BanList::acceptedBanRanges_locked(const BanListPeer& blp) } return false ; } -bool p3BanList::isAddressAccepted(const sockaddr_storage &addr, uint32_t checking_flags,uint32_t *check_result) +bool p3BanList::isAddressAccepted(const sockaddr_storage &dAddr, uint32_t checking_flags,uint32_t *check_result) { - if(check_result != NULL) - *check_result = RSBANLIST_CHECK_RESULT_NOCHECK ; + sockaddr_storage addr; sockaddr_storage_copy(dAddr, addr); - if(sockaddr_storage_isLoopbackNet(addr)) - return true ; - - if(!mIPFilteringEnabled) - return true ; + if(!mIPFilteringEnabled) return true; + if(check_result != NULL) *check_result = RSBANLIST_CHECK_RESULT_NOCHECK; + if(!sockaddr_storage_ipv6_to_ipv4(addr)) return true; + if(sockaddr_storage_isLoopbackNet(addr)) return true; #ifdef DEBUG_BANLIST std::cerr << "isAddressAccepted(): tested addr=" << sockaddr_storage_iptostring(addr) << ", checking flags=" << checking_flags ; @@ -453,9 +451,20 @@ void p3BanList::getBannedIps(std::list &lst) lst.push_back(it->second) ; } -bool p3BanList::removeIpRange(const struct sockaddr_storage& addr,int masked_bytes,uint32_t list_type) +bool p3BanList::removeIpRange( const struct sockaddr_storage& dAddr, + int masked_bytes, uint32_t list_type ) { - RS_STACK_MUTEX(mBanMtx) ; + sockaddr_storage addr; sockaddr_storage_copy(dAddr, addr); + if(!sockaddr_storage_ipv6_to_ipv4(addr)) + { + std::cerr << __PRETTY_FUNCTION__ << " Cannot handle " + << sockaddr_storage_tostring(dAddr) + << " IPv6 not implemented yet!" + << std::endl; + return false; + } + + RS_STACK_MUTEX(mBanMtx); bool changed = false; std::map::iterator it ; @@ -485,9 +494,20 @@ bool p3BanList::removeIpRange(const struct sockaddr_storage& addr,int masked_byt return changed; } -bool p3BanList::addIpRange(const sockaddr_storage &addr, int masked_bytes,uint32_t list_type,const std::string& comment) +bool p3BanList::addIpRange( const sockaddr_storage &dAddr, int masked_bytes, + uint32_t list_type, const std::string& comment ) { - RS_STACK_MUTEX(mBanMtx) ; + sockaddr_storage addr; sockaddr_storage_copy(dAddr, addr); + if(!sockaddr_storage_ipv6_to_ipv4(addr)) + { + std::cerr << __PRETTY_FUNCTION__ << " Cannot handle " + << sockaddr_storage_tostring(dAddr) + << " IPv6 not implemented yet!" + << std::endl; + return false; + } + + RS_STACK_MUTEX(mBanMtx); if(getBitRange(addr) > uint32_t(masked_bytes)) { @@ -668,20 +688,31 @@ bool p3BanList::recvBanItem(RsBanListItem *item) } /* overloaded from pqiNetAssistSharePeer */ -void p3BanList::updatePeer(const RsPeerId& /*id*/, const struct sockaddr_storage &addr, int /*type*/, int /*reason*/, int time_stamp) +void p3BanList::updatePeer( const RsPeerId& /*id*/, + const sockaddr_storage &dAddr, + int /*type*/, int /*reason*/, int time_stamp ) { - RsPeerId ownId = mServiceCtrl->getOwnId(); + sockaddr_storage addr; sockaddr_storage_copy(dAddr, addr); + if(!sockaddr_storage_ipv6_to_ipv4(addr)) + { + std::cerr << __PRETTY_FUNCTION__ << " Cannot handle " + << sockaddr_storage_tostring(dAddr) + << " IPv6 not implemented yet!" + << std::endl; + return; + } - int int_reason = RSBANLIST_REASON_DHT; + RsPeerId ownId = mServiceCtrl->getOwnId(); - addBanEntry(ownId, addr, RSBANLIST_ORIGIN_SELF, int_reason, time_stamp); + int int_reason = RSBANLIST_REASON_DHT; - /* process */ - { - RsStackMutex stack(mBanMtx); /****** LOCKED MUTEX *******/ + addBanEntry(ownId, addr, RSBANLIST_ORIGIN_SELF, int_reason, time_stamp); - condenseBanSources_locked(); - } + /* process */ + { + RS_STACK_MUTEX(mBanMtx); + condenseBanSources_locked(); + } } RsSerialiser *p3BanList::setupSerialiser() @@ -882,10 +913,21 @@ bool p3BanList::loadList(std::list& load) return true ; } -bool p3BanList::addBanEntry(const RsPeerId &peerId, const struct sockaddr_storage &addr, - int level, uint32_t reason, time_t time_stamp) +bool p3BanList::addBanEntry( const RsPeerId &peerId, + const sockaddr_storage &dAddr, + int level, uint32_t reason, time_t time_stamp ) { - RsStackMutex stack(mBanMtx); /****** LOCKED MUTEX *******/ + sockaddr_storage addr; sockaddr_storage_copy(dAddr, addr); + if(!sockaddr_storage_ipv6_to_ipv4(addr)) + { + std::cerr << __PRETTY_FUNCTION__ << " Cannot handle " + << sockaddr_storage_tostring(dAddr) + << " IPv6 not implemented yet!" + << std::endl; + return false; + } + + RS_STACK_MUTEX(mBanMtx); time_t now = time(NULL); bool updated = false; diff --git a/libretroshare/src/services/p3banlist.h b/libretroshare/src/services/p3banlist.h index 5bb251b5a..34d4bc50c 100644 --- a/libretroshare/src/services/p3banlist.h +++ b/libretroshare/src/services/p3banlist.h @@ -47,27 +47,31 @@ class BanList std::map mBanPeers; }; -//!The RS BanList service. - /** - * - * Exchange list of Banned IP addresses with peers. - */ - +/** + * The RS BanList service. + * Exchange list of Banned IPv4 addresses with peers. + * + * @warning IPv4 only, IPv6 not supported yet! + */ class p3BanList: public RsBanList, public p3Service, public pqiNetAssistPeerShare, public p3Config /*, public pqiMonitor */ { public: p3BanList(p3ServiceControl *sc, p3NetMgr *nm); virtual RsServiceInfo getServiceInfo(); - /***** overloaded from RsBanList *****/ + /***** overloaded from RsBanList *****/ - virtual bool isAddressAccepted(const struct sockaddr_storage& addr, uint32_t checking_flags,uint32_t *check_result=NULL) ; + virtual bool isAddressAccepted( const sockaddr_storage& addr, + uint32_t checking_flags, + uint32_t *check_result=NULL ); virtual void getBannedIps(std::list& list) ; virtual void getWhiteListedIps(std::list& list) ; - virtual bool addIpRange(const struct sockaddr_storage& addr,int masked_bytes,uint32_t list_type,const std::string& comment) ; - virtual bool removeIpRange(const sockaddr_storage &addr, int masked_bytes, uint32_t list_type); + virtual bool addIpRange( const sockaddr_storage& addr, int masked_bytes, + uint32_t list_type, const std::string& comment ); + virtual bool removeIpRange( const sockaddr_storage &addr, int masked_bytes, + uint32_t list_type ); virtual void enableIPFiltering(bool b) ; virtual bool ipFilteringEnabled() ; @@ -86,7 +90,8 @@ public: /***** overloaded from pqiNetAssistPeerShare *****/ - virtual void updatePeer(const RsPeerId& id, const struct sockaddr_storage &addr, int type, int reason, int time_stamp); + virtual void updatePeer( const RsPeerId& id, const sockaddr_storage &addr, + int type, int reason, int time_stamp ); /*********************** p3config ******************************/ virtual RsSerialiser *setupSerialiser(); @@ -108,8 +113,10 @@ public: int sendPackets(); bool processIncoming(); - bool recvBanItem(RsBanListItem *item); - bool addBanEntry(const RsPeerId &peerId, const struct sockaddr_storage &addr, int level, uint32_t reason, time_t time_stamp); + bool recvBanItem(RsBanListItem *item); + bool addBanEntry( const RsPeerId &peerId, + const sockaddr_storage &addr, int level, uint32_t reason, + time_t time_stamp ); void sendBanLists(); int sendBanSet(const RsPeerId& peerid); From 1d5e029a254c9fa6961362401e2feff65144c49b Mon Sep 17 00:00:00 2001 From: RetroPooh Date: Thu, 26 Apr 2018 23:01:58 +0300 Subject: [PATCH 081/161] fix discovery switch --- retroshare-gui/src/gui/settings/ServerPage.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/retroshare-gui/src/gui/settings/ServerPage.cpp b/retroshare-gui/src/gui/settings/ServerPage.cpp index 6a019990a..dcbba7953 100755 --- a/retroshare-gui/src/gui/settings/ServerPage.cpp +++ b/retroshare-gui/src/gui/settings/ServerPage.cpp @@ -197,6 +197,8 @@ ServerPage::ServerPage(QWidget * parent, Qt::WindowFlags flags) std::cerr << std::endl; #endif + + connect(ui.discComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(saveAddresses())); connect(ui.netModeComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(saveAddresses())); connect(ui.localAddress, SIGNAL(textChanged(QString)),this,SLOT(saveAddresses())); connect(ui.extAddress, SIGNAL(textChanged(QString)),this,SLOT(saveAddresses())); From 2dc69cb0007a17292ed944845c96895a106fea50 Mon Sep 17 00:00:00 2001 From: RetroPooh Date: Fri, 27 Apr 2018 16:50:00 +0300 Subject: [PATCH 082/161] embed preview for images on file attach in chat --- retroshare-gui/src/gui/chat/ChatWidget.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index 2859527cd..9aff65a64 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -1598,7 +1598,7 @@ void ChatWidget::fileHashingFinished(QList hashedFiles) QList::iterator it; for (it = hashedFiles.begin(); it != hashedFiles.end(); ++it) { HashedFile& hashedFile = *it; - //QString ext = QFileInfo(hashedFile.filename).suffix(); + QString ext = QFileInfo(hashedFile.filename).suffix().toUpper(); RetroShareLink link; @@ -1610,9 +1610,23 @@ void ChatWidget::fileHashingFinished(QList hashedFiles) message += QString("").arg(hashedFile.filepath); message+="
"; } else { - QString image = FilesDefs::getImageFromFilename(hashedFile.filename, false); - if (!image.isEmpty()) { - message += QString("").arg(image); + bool preview = false; + if(hashedFiles.size()==1 && (ext == "JPG" || ext == "PNG" || ext == "JPEG" || ext == "GIF")) + { + QString encodedImage; + uint32_t maxMessageSize = this->maxMessageSize(); + if (RsHtml::makeEmbeddedImage(hashedFile.filepath, encodedImage, 640*480, maxMessageSize - 200 - link.toHtmlSize().length())) + { QTextDocumentFragment fragment = QTextDocumentFragment::fromHtml(encodedImage); + ui->chatTextEdit->textCursor().insertFragment(fragment); + preview=true; + } + } + if(!preview) + { + QString image = FilesDefs::getImageFromFilename(hashedFile.filename, false); + if (!image.isEmpty()) { + message += QString("").arg(image); + } } } message += link.toHtmlSize(); From be75e89ad2936e3c6dab7eb0228b272404dadcb4 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Fri, 27 Apr 2018 20:55:38 +0200 Subject: [PATCH 083/161] Fix compialtion after merge --- libretroshare/src/services/p3gxschannels.cc | 8 ++++---- libretroshare/src/services/p3gxsforums.cc | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libretroshare/src/services/p3gxschannels.cc b/libretroshare/src/services/p3gxschannels.cc index d2578d470..c7db82459 100644 --- a/libretroshare/src/services/p3gxschannels.cc +++ b/libretroshare/src/services/p3gxschannels.cc @@ -132,10 +132,10 @@ struct RsGxsForumNotifyRecordsItem: public RsItem virtual ~RsGxsForumNotifyRecordsItem() {} - void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) - { - RS_REGISTER_SERIAL_MEMBER(records); - } + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) + { RS_SERIAL_PROCESS(records); } + void clear() {} std::map records; diff --git a/libretroshare/src/services/p3gxsforums.cc b/libretroshare/src/services/p3gxsforums.cc index dd98f88d1..7f1e8dfcf 100644 --- a/libretroshare/src/services/p3gxsforums.cc +++ b/libretroshare/src/services/p3gxsforums.cc @@ -107,10 +107,10 @@ struct RsGxsForumNotifyRecordsItem: public RsItem virtual ~RsGxsForumNotifyRecordsItem() {} - void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) - { - RS_REGISTER_SERIAL_MEMBER(records); - } + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) + { RS_SERIAL_PROCESS(records); } + void clear() {} std::map records; From 82a00c2496aaa45780f20e1730c5d9cf39c2f26e Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 27 Apr 2018 15:34:43 -0500 Subject: [PATCH 084/161] added rapidjson to package installation command on Debian/OpenSUSE/Arch --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8902b43b8..c726983e5 100644 --- a/README.md +++ b/README.md @@ -32,19 +32,19 @@ Compilation on Linux libqt4-dev libssl-dev libxss-dev libgnome-keyring-dev libbz2-dev \ libqt4-opengl-dev libqtmultimediakit1 qtmobility-dev libsqlcipher-dev \ libspeex-dev libspeexdsp-dev libxslt1-dev libcurl4-openssl-dev \ - libopencv-dev tcl8.5 libmicrohttpd-dev + libopencv-dev tcl8.5 libmicrohttpd-dev rapidjson-dev ``` * openSUSE ```bash sudo zypper install gcc-c++ libqt4-devel libgnome-keyring-devel \ glib2-devel speex-devel libssh-devel protobuf-devel libcurl-devel \ libxml2-devel libxslt-devel sqlcipher-devel libmicrohttpd-devel \ - opencv-devel speexdsp-devel libupnp-devel libavcodec-devel + opencv-devel speexdsp-devel libupnp-devel libavcodec-devel rapidjson ``` * Arch Linux ```bash pacman -S base-devel libgnome-keyring libmicrohttpd libupnp libxslt \ - libxss opencv qt4 speex speexdsp sqlcipher + libxss opencv qt4 speex speexdsp sqlcipher rapidjson ``` 2. Checkout the source code From 10daf3b9c692518b21d8cdc4c027214743468193 Mon Sep 17 00:00:00 2001 From: sehraf Date: Sat, 28 Apr 2018 09:06:10 +0200 Subject: [PATCH 085/161] Fix 'make install' Since DATA_DIR (and the others) are not set 'make install' will move the files to /{qss, sounds, stylesheets, usr, webui} (instead of '/usr/...') Fixes 4876a0ea3b87abfe7c404f4bc5fbb43c44577820 --- retroshare.pri | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/retroshare.pri b/retroshare.pri index c4f47aedf..ed0e3c221 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -325,14 +325,6 @@ profiling { QMAKE_LFLAGS *= -pg } -## Retrocompatibility assignations, get rid of this ASAP -isEmpty(BIN_DIR) : BIN_DIR = $${RS_BIN_DIR} -isEmpty(INC_DIR) : INC_DIR = $${RS_INCLUDE_DIR} -isEmpty(LIBDIR) : LIBDIR = $${QMAKE_LIBDIR} -isEmpty(DATA_DIR) : DATA_DIR = $${RS_DATA_DIR} -isEmpty(PLUGIN_DIR): PLUGIN_DIR= $${RS_PLUGIN_DIR} - - ################################################################################ ## Last goes platform specific statements common to all RetroShare subprojects # ################################################################################ @@ -463,3 +455,10 @@ macx-* { CONFIG += c++11 RS_UPNP_LIB = miniupnpc } + +## Retrocompatibility assignations, get rid of this ASAP +isEmpty(BIN_DIR) : BIN_DIR = $${RS_BIN_DIR} +isEmpty(INC_DIR) : INC_DIR = $${RS_INCLUDE_DIR} +isEmpty(LIBDIR) : LIBDIR = $${QMAKE_LIBDIR} +isEmpty(DATA_DIR) : DATA_DIR = $${RS_DATA_DIR} +isEmpty(PLUGIN_DIR): PLUGIN_DIR= $${RS_PLUGIN_DIR} From 8e111c2ee29fe2e3488ffc2bf045bfe920361063 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 2 May 2018 22:46:27 +0200 Subject: [PATCH 086/161] added rapidjson-1.1.0 code hard-coded in the source directory to allow everyone to compile without the need to tweak too much. When v1.1.0 is mainstream (espcially on ubuntu) we can revert back to an external dependency --- libresapi/src/libresapi.pro | 5 + libretroshare/src/libretroshare.pro | 5 + libretroshare/src/serialiser/rsserializer.h | 2 +- .../src/serialiser/rstypeserializer.cc | 4 +- .../src/serialiser/rstypeserializer.h | 2 +- rapidjson-1.1.0/license.txt | 57 + rapidjson-1.1.0/rapid_json/allocators.h | 271 ++ rapidjson-1.1.0/rapid_json/document.h | 2575 +++++++++++++++++ rapidjson-1.1.0/rapid_json/encodedstream.h | 299 ++ rapidjson-1.1.0/rapid_json/encodings.h | 716 +++++ rapidjson-1.1.0/rapid_json/error/en.h | 74 + rapidjson-1.1.0/rapid_json/error/error.h | 155 + rapidjson-1.1.0/rapid_json/filereadstream.h | 99 + rapidjson-1.1.0/rapid_json/filewritestream.h | 104 + rapidjson-1.1.0/rapid_json/fwd.h | 151 + .../rapid_json/internal/biginteger.h | 290 ++ rapidjson-1.1.0/rapid_json/internal/diyfp.h | 258 ++ rapidjson-1.1.0/rapid_json/internal/dtoa.h | 245 ++ rapidjson-1.1.0/rapid_json/internal/ieee754.h | 78 + rapidjson-1.1.0/rapid_json/internal/itoa.h | 304 ++ rapidjson-1.1.0/rapid_json/internal/meta.h | 181 ++ rapidjson-1.1.0/rapid_json/internal/pow10.h | 55 + rapidjson-1.1.0/rapid_json/internal/regex.h | 701 +++++ rapidjson-1.1.0/rapid_json/internal/stack.h | 230 ++ rapidjson-1.1.0/rapid_json/internal/strfunc.h | 55 + rapidjson-1.1.0/rapid_json/internal/strtod.h | 269 ++ rapidjson-1.1.0/rapid_json/internal/swap.h | 46 + rapidjson-1.1.0/rapid_json/istreamwrapper.h | 115 + rapidjson-1.1.0/rapid_json/memorybuffer.h | 70 + rapidjson-1.1.0/rapid_json/memorystream.h | 71 + rapidjson-1.1.0/rapid_json/ostreamwrapper.h | 81 + rapidjson-1.1.0/rapid_json/pointer.h | 1358 +++++++++ rapidjson-1.1.0/rapid_json/prettywriter.h | 255 ++ rapidjson-1.1.0/rapid_json/rapidjson.h | 615 ++++ rapidjson-1.1.0/rapid_json/reader.h | 1879 ++++++++++++ rapidjson-1.1.0/rapid_json/schema.h | 2006 +++++++++++++ rapidjson-1.1.0/rapid_json/stream.h | 179 ++ rapidjson-1.1.0/rapid_json/stringbuffer.h | 117 + rapidjson-1.1.0/rapid_json/writer.h | 610 ++++ retroshare-gui/src/retroshare-gui.pro | 5 + retroshare-nogui/src/retroshare-nogui.pro | 5 + 41 files changed, 14593 insertions(+), 4 deletions(-) create mode 100644 rapidjson-1.1.0/license.txt create mode 100644 rapidjson-1.1.0/rapid_json/allocators.h create mode 100644 rapidjson-1.1.0/rapid_json/document.h create mode 100644 rapidjson-1.1.0/rapid_json/encodedstream.h create mode 100644 rapidjson-1.1.0/rapid_json/encodings.h create mode 100644 rapidjson-1.1.0/rapid_json/error/en.h create mode 100644 rapidjson-1.1.0/rapid_json/error/error.h create mode 100644 rapidjson-1.1.0/rapid_json/filereadstream.h create mode 100644 rapidjson-1.1.0/rapid_json/filewritestream.h create mode 100644 rapidjson-1.1.0/rapid_json/fwd.h create mode 100644 rapidjson-1.1.0/rapid_json/internal/biginteger.h create mode 100644 rapidjson-1.1.0/rapid_json/internal/diyfp.h create mode 100644 rapidjson-1.1.0/rapid_json/internal/dtoa.h create mode 100644 rapidjson-1.1.0/rapid_json/internal/ieee754.h create mode 100644 rapidjson-1.1.0/rapid_json/internal/itoa.h create mode 100644 rapidjson-1.1.0/rapid_json/internal/meta.h create mode 100644 rapidjson-1.1.0/rapid_json/internal/pow10.h create mode 100644 rapidjson-1.1.0/rapid_json/internal/regex.h create mode 100644 rapidjson-1.1.0/rapid_json/internal/stack.h create mode 100644 rapidjson-1.1.0/rapid_json/internal/strfunc.h create mode 100644 rapidjson-1.1.0/rapid_json/internal/strtod.h create mode 100644 rapidjson-1.1.0/rapid_json/internal/swap.h create mode 100644 rapidjson-1.1.0/rapid_json/istreamwrapper.h create mode 100644 rapidjson-1.1.0/rapid_json/memorybuffer.h create mode 100644 rapidjson-1.1.0/rapid_json/memorystream.h create mode 100644 rapidjson-1.1.0/rapid_json/ostreamwrapper.h create mode 100644 rapidjson-1.1.0/rapid_json/pointer.h create mode 100644 rapidjson-1.1.0/rapid_json/prettywriter.h create mode 100644 rapidjson-1.1.0/rapid_json/rapidjson.h create mode 100644 rapidjson-1.1.0/rapid_json/reader.h create mode 100644 rapidjson-1.1.0/rapid_json/schema.h create mode 100644 rapidjson-1.1.0/rapid_json/stream.h create mode 100644 rapidjson-1.1.0/rapid_json/stringbuffer.h create mode 100644 rapidjson-1.1.0/rapid_json/writer.h diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index f3124dc77..15a2faf5c 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -9,6 +9,11 @@ DESTDIR = lib !include(use_libresapi.pri):error("Including") +# when rapidjson is mainstream on all distribs, we will not need the sources anymore +# in the meantime, they are part of the RS directory so that it is always possible to find them + +INCLUDEPATH += ../../rapidjson-1.1.0 + libresapilocalserver { CONFIG *= qt QT *= network diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 825ff274e..e401ed8ee 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -17,6 +17,11 @@ DESTDIR = lib #QMAKE_CFLAGS += -Werror #QMAKE_CXXFLAGS += -Werror +# when rapidjson is mainstream on all distribs, we will not need the sources anymore +# in the meantime, they are part of the RS directory so that it is always possible to find them + +INCLUDEPATH += ../../rapidjson-1.1.0 + debug { # DEFINES *= DEBUG # DEFINES *= OPENDHT_DEBUG DHT_DEBUG CONN_DEBUG DEBUG_UDP_SORTER P3DISC_DEBUG DEBUG_UDP_LAYER FT_DEBUG EXTADDRSEARCH_DEBUG diff --git a/libretroshare/src/serialiser/rsserializer.h b/libretroshare/src/serialiser/rsserializer.h index dcc4d089f..aa12feeef 100644 --- a/libretroshare/src/serialiser/rsserializer.h +++ b/libretroshare/src/serialiser/rsserializer.h @@ -154,7 +154,7 @@ #include #include #include -#include +#include #include "retroshare/rsflags.h" #include "serialiser/rsserial.h" diff --git a/libretroshare/src/serialiser/rstypeserializer.cc b/libretroshare/src/serialiser/rstypeserializer.cc index 8d7ba5daf..e7faa5b52 100644 --- a/libretroshare/src/serialiser/rstypeserializer.cc +++ b/libretroshare/src/serialiser/rstypeserializer.cc @@ -36,8 +36,8 @@ #include #include // for typeid -#include -#include +#include +#include //static const uint32_t MAX_SERIALIZED_ARRAY_SIZE = 500 ; static const uint32_t MAX_SERIALIZED_CHUNK_SIZE = 10*1024*1024 ; // 10 MB. diff --git a/libretroshare/src/serialiser/rstypeserializer.h b/libretroshare/src/serialiser/rstypeserializer.h index f08a43239..3e3c0b251 100644 --- a/libretroshare/src/serialiser/rstypeserializer.h +++ b/libretroshare/src/serialiser/rstypeserializer.h @@ -35,7 +35,7 @@ #include "serialiser/rsserializer.h" #include "serialiser/rsserializable.h" -#include +#include #include // for typeid #include #include diff --git a/rapidjson-1.1.0/license.txt b/rapidjson-1.1.0/license.txt new file mode 100644 index 000000000..7ccc161c8 --- /dev/null +++ b/rapidjson-1.1.0/license.txt @@ -0,0 +1,57 @@ +Tencent is pleased to support the open source community by making RapidJSON available. + +Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. + +If you have downloaded a copy of the RapidJSON binary from Tencent, please note that the RapidJSON binary is licensed under the MIT License. +If you have downloaded a copy of the RapidJSON source code from Tencent, please note that RapidJSON source code is licensed under the MIT License, except for the third-party components listed below which are subject to different license terms. Your integration of RapidJSON into your own projects may require compliance with the MIT License, as well as the other licenses applicable to the third-party components included within RapidJSON. To avoid the problematic JSON license in your own projects, it's sufficient to exclude the bin/jsonchecker/ directory, as it's the only code under the JSON license. +A copy of the MIT License is included in this file. + +Other dependencies and licenses: + +Open Source Software Licensed Under the BSD License: +-------------------------------------------------------------------- + +The msinttypes r29 +Copyright (c) 2006-2013 Alexander Chemeris +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +* Neither the name of copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Open Source Software Licensed Under the JSON License: +-------------------------------------------------------------------- + +json.org +Copyright (c) 2002 JSON.org +All Rights Reserved. + +JSON_checker +Copyright (c) 2002 JSON.org +All Rights Reserved. + + +Terms of the JSON License: +--------------------------------------------------- + +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 shall be used for Good, not Evil. + +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. + + +Terms of the MIT License: +-------------------------------------------------------------------- + +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. diff --git a/rapidjson-1.1.0/rapid_json/allocators.h b/rapidjson-1.1.0/rapid_json/allocators.h new file mode 100644 index 000000000..98affe03f --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/allocators.h @@ -0,0 +1,271 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ALLOCATORS_H_ +#define RAPIDJSON_ALLOCATORS_H_ + +#include "rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Allocator + +/*! \class rapidjson::Allocator + \brief Concept for allocating, resizing and freeing memory block. + + Note that Malloc() and Realloc() are non-static but Free() is static. + + So if an allocator need to support Free(), it needs to put its pointer in + the header of memory block. + +\code +concept Allocator { + static const bool kNeedFree; //!< Whether this allocator needs to call Free(). + + // Allocate a memory block. + // \param size of the memory block in bytes. + // \returns pointer to the memory block. + void* Malloc(size_t size); + + // Resize a memory block. + // \param originalPtr The pointer to current memory block. Null pointer is permitted. + // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.) + // \param newSize the new size in bytes. + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize); + + // Free a memory block. + // \param pointer to the memory block. Null pointer is permitted. + static void Free(void *ptr); +}; +\endcode +*/ + +/////////////////////////////////////////////////////////////////////////////// +// CrtAllocator + +//! C-runtime library allocator. +/*! This class is just wrapper for standard C library memory routines. + \note implements Allocator concept +*/ +class CrtAllocator { +public: + static const bool kNeedFree = true; + void* Malloc(size_t size) { + if (size) // behavior of malloc(0) is implementation defined. + return std::malloc(size); + else + return NULL; // standardize to returning NULL. + } + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { + (void)originalSize; + if (newSize == 0) { + std::free(originalPtr); + return NULL; + } + return std::realloc(originalPtr, newSize); + } + static void Free(void *ptr) { std::free(ptr); } +}; + +/////////////////////////////////////////////////////////////////////////////// +// MemoryPoolAllocator + +//! Default memory allocator used by the parser and DOM. +/*! This allocator allocate memory blocks from pre-allocated memory chunks. + + It does not free memory blocks. And Realloc() only allocate new memory. + + The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default. + + User may also supply a buffer as the first chunk. + + If the user-buffer is full then additional chunks are allocated by BaseAllocator. + + The user-buffer is not deallocated by this allocator. + + \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator. + \note implements Allocator concept +*/ +template +class MemoryPoolAllocator { +public: + static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator) + + //! Constructor with chunkSize. + /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. + \param baseAllocator The allocator for allocating memory chunks. + */ + MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : + chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0) + { + } + + //! Constructor with user-supplied buffer. + /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size. + + The user buffer will not be deallocated when this allocator is destructed. + + \param buffer User supplied buffer. + \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader). + \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. + \param baseAllocator The allocator for allocating memory chunks. + */ + MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : + chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0) + { + RAPIDJSON_ASSERT(buffer != 0); + RAPIDJSON_ASSERT(size > sizeof(ChunkHeader)); + chunkHead_ = reinterpret_cast(buffer); + chunkHead_->capacity = size - sizeof(ChunkHeader); + chunkHead_->size = 0; + chunkHead_->next = 0; + } + + //! Destructor. + /*! This deallocates all memory chunks, excluding the user-supplied buffer. + */ + ~MemoryPoolAllocator() { + Clear(); + RAPIDJSON_DELETE(ownBaseAllocator_); + } + + //! Deallocates all memory chunks, excluding the user-supplied buffer. + void Clear() { + while (chunkHead_ && chunkHead_ != userBuffer_) { + ChunkHeader* next = chunkHead_->next; + baseAllocator_->Free(chunkHead_); + chunkHead_ = next; + } + if (chunkHead_ && chunkHead_ == userBuffer_) + chunkHead_->size = 0; // Clear user buffer + } + + //! Computes the total capacity of allocated memory chunks. + /*! \return total capacity in bytes. + */ + size_t Capacity() const { + size_t capacity = 0; + for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) + capacity += c->capacity; + return capacity; + } + + //! Computes the memory blocks allocated. + /*! \return total used bytes. + */ + size_t Size() const { + size_t size = 0; + for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) + size += c->size; + return size; + } + + //! Allocates a memory block. (concept Allocator) + void* Malloc(size_t size) { + if (!size) + return NULL; + + size = RAPIDJSON_ALIGN(size); + if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity) + if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size)) + return NULL; + + void *buffer = reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size; + chunkHead_->size += size; + return buffer; + } + + //! Resizes a memory block (concept Allocator) + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { + if (originalPtr == 0) + return Malloc(newSize); + + if (newSize == 0) + return NULL; + + originalSize = RAPIDJSON_ALIGN(originalSize); + newSize = RAPIDJSON_ALIGN(newSize); + + // Do not shrink if new size is smaller than original + if (originalSize >= newSize) + return originalPtr; + + // Simply expand it if it is the last allocation and there is sufficient space + if (originalPtr == reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) { + size_t increment = static_cast(newSize - originalSize); + if (chunkHead_->size + increment <= chunkHead_->capacity) { + chunkHead_->size += increment; + return originalPtr; + } + } + + // Realloc process: allocate and copy memory, do not free original buffer. + if (void* newBuffer = Malloc(newSize)) { + if (originalSize) + std::memcpy(newBuffer, originalPtr, originalSize); + return newBuffer; + } + else + return NULL; + } + + //! Frees a memory block (concept Allocator) + static void Free(void *ptr) { (void)ptr; } // Do nothing + +private: + //! Copy constructor is not permitted. + MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */; + //! Copy assignment operator is not permitted. + MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */; + + //! Creates a new chunk. + /*! \param capacity Capacity of the chunk in bytes. + \return true if success. + */ + bool AddChunk(size_t capacity) { + if (!baseAllocator_) + ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator()); + if (ChunkHeader* chunk = reinterpret_cast(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) { + chunk->capacity = capacity; + chunk->size = 0; + chunk->next = chunkHead_; + chunkHead_ = chunk; + return true; + } + else + return false; + } + + static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity. + + //! Chunk header for perpending to each chunk. + /*! Chunks are stored as a singly linked list. + */ + struct ChunkHeader { + size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself). + size_t size; //!< Current size of allocated memory in bytes. + ChunkHeader *next; //!< Next chunk in the linked list. + }; + + ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation. + size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated. + void *userBuffer_; //!< User supplied buffer. + BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks. + BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object. +}; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ENCODINGS_H_ diff --git a/rapidjson-1.1.0/rapid_json/document.h b/rapidjson-1.1.0/rapid_json/document.h new file mode 100644 index 000000000..e3e20dfbd --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/document.h @@ -0,0 +1,2575 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_DOCUMENT_H_ +#define RAPIDJSON_DOCUMENT_H_ + +/*! \file document.h */ + +#include "reader.h" +#include "internal/meta.h" +#include "internal/strfunc.h" +#include "memorystream.h" +#include "encodedstream.h" +#include // placement new +#include + +RAPIDJSON_DIAG_PUSH +#ifdef _MSC_VER +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(switch-enum) +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_OFF(effc++) +#if __GNUC__ >= 6 +RAPIDJSON_DIAG_OFF(terminate) // ignore throwing RAPIDJSON_ASSERT in RAPIDJSON_NOEXCEPT functions +#endif +#endif // __GNUC__ + +#ifndef RAPIDJSON_NOMEMBERITERATORCLASS +#include // std::iterator, std::random_access_iterator_tag +#endif + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +#include // std::move +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +// Forward declaration. +template +class GenericValue; + +template +class GenericDocument; + +//! Name-value pair in a JSON object value. +/*! + This class was internal to GenericValue. It used to be a inner struct. + But a compiler (IBM XL C/C++ for AIX) have reported to have problem with that so it moved as a namespace scope struct. + https://code.google.com/p/rapidjson/issues/detail?id=64 +*/ +template +struct GenericMember { + GenericValue name; //!< name of member (must be a string) + GenericValue value; //!< value of member. +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericMemberIterator + +#ifndef RAPIDJSON_NOMEMBERITERATORCLASS + +//! (Constant) member iterator for a JSON object value +/*! + \tparam Const Is this a constant iterator? + \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document) + \tparam Allocator Allocator type for allocating memory of object, array and string. + + This class implements a Random Access Iterator for GenericMember elements + of a GenericValue, see ISO/IEC 14882:2003(E) C++ standard, 24.1 [lib.iterator.requirements]. + + \note This iterator implementation is mainly intended to avoid implicit + conversions from iterator values to \c NULL, + e.g. from GenericValue::FindMember. + + \note Define \c RAPIDJSON_NOMEMBERITERATORCLASS to fall back to a + pointer-based implementation, if your platform doesn't provide + the C++ header. + + \see GenericMember, GenericValue::MemberIterator, GenericValue::ConstMemberIterator + */ +template +class GenericMemberIterator + : public std::iterator >::Type> { + + friend class GenericValue; + template friend class GenericMemberIterator; + + typedef GenericMember PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + typedef std::iterator BaseType; + +public: + //! Iterator type itself + typedef GenericMemberIterator Iterator; + //! Constant iterator type + typedef GenericMemberIterator ConstIterator; + //! Non-constant iterator type + typedef GenericMemberIterator NonConstIterator; + + //! Pointer to (const) GenericMember + typedef typename BaseType::pointer Pointer; + //! Reference to (const) GenericMember + typedef typename BaseType::reference Reference; + //! Signed integer type (e.g. \c ptrdiff_t) + typedef typename BaseType::difference_type DifferenceType; + + //! Default constructor (singular value) + /*! Creates an iterator pointing to no element. + \note All operations, except for comparisons, are undefined on such values. + */ + GenericMemberIterator() : ptr_() {} + + //! Iterator conversions to more const + /*! + \param it (Non-const) iterator to copy from + + Allows the creation of an iterator from another GenericMemberIterator + that is "less const". Especially, creating a non-constant iterator + from a constant iterator are disabled: + \li const -> non-const (not ok) + \li const -> const (ok) + \li non-const -> const (ok) + \li non-const -> non-const (ok) + + \note If the \c Const template parameter is already \c false, this + constructor effectively defines a regular copy-constructor. + Otherwise, the copy constructor is implicitly defined. + */ + GenericMemberIterator(const NonConstIterator & it) : ptr_(it.ptr_) {} + Iterator& operator=(const NonConstIterator & it) { ptr_ = it.ptr_; return *this; } + + //! @name stepping + //@{ + Iterator& operator++(){ ++ptr_; return *this; } + Iterator& operator--(){ --ptr_; return *this; } + Iterator operator++(int){ Iterator old(*this); ++ptr_; return old; } + Iterator operator--(int){ Iterator old(*this); --ptr_; return old; } + //@} + + //! @name increment/decrement + //@{ + Iterator operator+(DifferenceType n) const { return Iterator(ptr_+n); } + Iterator operator-(DifferenceType n) const { return Iterator(ptr_-n); } + + Iterator& operator+=(DifferenceType n) { ptr_+=n; return *this; } + Iterator& operator-=(DifferenceType n) { ptr_-=n; return *this; } + //@} + + //! @name relations + //@{ + bool operator==(ConstIterator that) const { return ptr_ == that.ptr_; } + bool operator!=(ConstIterator that) const { return ptr_ != that.ptr_; } + bool operator<=(ConstIterator that) const { return ptr_ <= that.ptr_; } + bool operator>=(ConstIterator that) const { return ptr_ >= that.ptr_; } + bool operator< (ConstIterator that) const { return ptr_ < that.ptr_; } + bool operator> (ConstIterator that) const { return ptr_ > that.ptr_; } + //@} + + //! @name dereference + //@{ + Reference operator*() const { return *ptr_; } + Pointer operator->() const { return ptr_; } + Reference operator[](DifferenceType n) const { return ptr_[n]; } + //@} + + //! Distance + DifferenceType operator-(ConstIterator that) const { return ptr_-that.ptr_; } + +private: + //! Internal constructor from plain pointer + explicit GenericMemberIterator(Pointer p) : ptr_(p) {} + + Pointer ptr_; //!< raw pointer +}; + +#else // RAPIDJSON_NOMEMBERITERATORCLASS + +// class-based member iterator implementation disabled, use plain pointers + +template +struct GenericMemberIterator; + +//! non-const GenericMemberIterator +template +struct GenericMemberIterator { + //! use plain pointer as iterator type + typedef GenericMember* Iterator; +}; +//! const GenericMemberIterator +template +struct GenericMemberIterator { + //! use plain const pointer as iterator type + typedef const GenericMember* Iterator; +}; + +#endif // RAPIDJSON_NOMEMBERITERATORCLASS + +/////////////////////////////////////////////////////////////////////////////// +// GenericStringRef + +//! Reference to a constant string (not taking a copy) +/*! + \tparam CharType character type of the string + + This helper class is used to automatically infer constant string + references for string literals, especially from \c const \b (!) + character arrays. + + The main use is for creating JSON string values without copying the + source string via an \ref Allocator. This requires that the referenced + string pointers have a sufficient lifetime, which exceeds the lifetime + of the associated GenericValue. + + \b Example + \code + Value v("foo"); // ok, no need to copy & calculate length + const char foo[] = "foo"; + v.SetString(foo); // ok + + const char* bar = foo; + // Value x(bar); // not ok, can't rely on bar's lifetime + Value x(StringRef(bar)); // lifetime explicitly guaranteed by user + Value y(StringRef(bar, 3)); // ok, explicitly pass length + \endcode + + \see StringRef, GenericValue::SetString +*/ +template +struct GenericStringRef { + typedef CharType Ch; //!< character type of the string + + //! Create string reference from \c const character array +#ifndef __clang__ // -Wdocumentation + /*! + This constructor implicitly creates a constant string reference from + a \c const character array. It has better performance than + \ref StringRef(const CharType*) by inferring the string \ref length + from the array length, and also supports strings containing null + characters. + + \tparam N length of the string, automatically inferred + + \param str Constant character array, lifetime assumed to be longer + than the use of the string in e.g. a GenericValue + + \post \ref s == str + + \note Constant complexity. + \note There is a hidden, private overload to disallow references to + non-const character arrays to be created via this constructor. + By this, e.g. function-scope arrays used to be filled via + \c snprintf are excluded from consideration. + In such cases, the referenced string should be \b copied to the + GenericValue instead. + */ +#endif + template + GenericStringRef(const CharType (&str)[N]) RAPIDJSON_NOEXCEPT + : s(str), length(N-1) {} + + //! Explicitly create string reference from \c const character pointer +#ifndef __clang__ // -Wdocumentation + /*! + This constructor can be used to \b explicitly create a reference to + a constant string pointer. + + \see StringRef(const CharType*) + + \param str Constant character pointer, lifetime assumed to be longer + than the use of the string in e.g. a GenericValue + + \post \ref s == str + + \note There is a hidden, private overload to disallow references to + non-const character arrays to be created via this constructor. + By this, e.g. function-scope arrays used to be filled via + \c snprintf are excluded from consideration. + In such cases, the referenced string should be \b copied to the + GenericValue instead. + */ +#endif + explicit GenericStringRef(const CharType* str) + : s(str), length(internal::StrLen(str)){ RAPIDJSON_ASSERT(s != 0); } + + //! Create constant string reference from pointer and length +#ifndef __clang__ // -Wdocumentation + /*! \param str constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \param len length of the string, excluding the trailing NULL terminator + + \post \ref s == str && \ref length == len + \note Constant complexity. + */ +#endif + GenericStringRef(const CharType* str, SizeType len) + : s(str), length(len) { RAPIDJSON_ASSERT(s != 0); } + + GenericStringRef(const GenericStringRef& rhs) : s(rhs.s), length(rhs.length) {} + + GenericStringRef& operator=(const GenericStringRef& rhs) { s = rhs.s; length = rhs.length; } + + //! implicit conversion to plain CharType pointer + operator const Ch *() const { return s; } + + const Ch* const s; //!< plain CharType pointer + const SizeType length; //!< length of the string (excluding the trailing NULL terminator) + +private: + //! Disallow construction from non-const array + template + GenericStringRef(CharType (&str)[N]) /* = delete */; +}; + +//! Mark a character pointer as constant string +/*! Mark a plain character pointer as a "string literal". This function + can be used to avoid copying a character string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + \tparam CharType Character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \return GenericStringRef string reference object + \relatesalso GenericStringRef + + \see GenericValue::GenericValue(StringRefType), GenericValue::operator=(StringRefType), GenericValue::SetString(StringRefType), GenericValue::PushBack(StringRefType, Allocator&), GenericValue::AddMember +*/ +template +inline GenericStringRef StringRef(const CharType* str) { + return GenericStringRef(str, internal::StrLen(str)); +} + +//! Mark a character pointer as constant string +/*! Mark a plain character pointer as a "string literal". This function + can be used to avoid copying a character string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + + This version has better performance with supplied length, and also + supports string containing null characters. + + \tparam CharType character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \param length The length of source string. + \return GenericStringRef string reference object + \relatesalso GenericStringRef +*/ +template +inline GenericStringRef StringRef(const CharType* str, size_t length) { + return GenericStringRef(str, SizeType(length)); +} + +#if RAPIDJSON_HAS_STDSTRING +//! Mark a string object as constant string +/*! Mark a string object (e.g. \c std::string) as a "string literal". + This function can be used to avoid copying a string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + + \tparam CharType character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \return GenericStringRef string reference object + \relatesalso GenericStringRef + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. +*/ +template +inline GenericStringRef StringRef(const std::basic_string& str) { + return GenericStringRef(str.data(), SizeType(str.size())); +} +#endif + +/////////////////////////////////////////////////////////////////////////////// +// GenericValue type traits +namespace internal { + +template +struct IsGenericValueImpl : FalseType {}; + +// select candidates according to nested encoding and allocator types +template struct IsGenericValueImpl::Type, typename Void::Type> + : IsBaseOf, T>::Type {}; + +// helper to match arbitrary GenericValue instantiations, including derived classes +template struct IsGenericValue : IsGenericValueImpl::Type {}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// TypeHelper + +namespace internal { + +template +struct TypeHelper {}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsBool(); } + static bool Get(const ValueType& v) { return v.GetBool(); } + static ValueType& Set(ValueType& v, bool data) { return v.SetBool(data); } + static ValueType& Set(ValueType& v, bool data, typename ValueType::AllocatorType&) { return v.SetBool(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsInt(); } + static int Get(const ValueType& v) { return v.GetInt(); } + static ValueType& Set(ValueType& v, int data) { return v.SetInt(data); } + static ValueType& Set(ValueType& v, int data, typename ValueType::AllocatorType&) { return v.SetInt(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsUint(); } + static unsigned Get(const ValueType& v) { return v.GetUint(); } + static ValueType& Set(ValueType& v, unsigned data) { return v.SetUint(data); } + static ValueType& Set(ValueType& v, unsigned data, typename ValueType::AllocatorType&) { return v.SetUint(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsInt64(); } + static int64_t Get(const ValueType& v) { return v.GetInt64(); } + static ValueType& Set(ValueType& v, int64_t data) { return v.SetInt64(data); } + static ValueType& Set(ValueType& v, int64_t data, typename ValueType::AllocatorType&) { return v.SetInt64(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsUint64(); } + static uint64_t Get(const ValueType& v) { return v.GetUint64(); } + static ValueType& Set(ValueType& v, uint64_t data) { return v.SetUint64(data); } + static ValueType& Set(ValueType& v, uint64_t data, typename ValueType::AllocatorType&) { return v.SetUint64(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsDouble(); } + static double Get(const ValueType& v) { return v.GetDouble(); } + static ValueType& Set(ValueType& v, double data) { return v.SetDouble(data); } + static ValueType& Set(ValueType& v, double data, typename ValueType::AllocatorType&) { return v.SetDouble(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsFloat(); } + static float Get(const ValueType& v) { return v.GetFloat(); } + static ValueType& Set(ValueType& v, float data) { return v.SetFloat(data); } + static ValueType& Set(ValueType& v, float data, typename ValueType::AllocatorType&) { return v.SetFloat(data); } +}; + +template +struct TypeHelper { + typedef const typename ValueType::Ch* StringType; + static bool Is(const ValueType& v) { return v.IsString(); } + static StringType Get(const ValueType& v) { return v.GetString(); } + static ValueType& Set(ValueType& v, const StringType data) { return v.SetString(typename ValueType::StringRefType(data)); } + static ValueType& Set(ValueType& v, const StringType data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); } +}; + +#if RAPIDJSON_HAS_STDSTRING +template +struct TypeHelper > { + typedef std::basic_string StringType; + static bool Is(const ValueType& v) { return v.IsString(); } + static StringType Get(const ValueType& v) { return StringType(v.GetString(), v.GetStringLength()); } + static ValueType& Set(ValueType& v, const StringType& data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); } +}; +#endif + +template +struct TypeHelper { + typedef typename ValueType::Array ArrayType; + static bool Is(const ValueType& v) { return v.IsArray(); } + static ArrayType Get(ValueType& v) { return v.GetArray(); } + static ValueType& Set(ValueType& v, ArrayType data) { return v = data; } + static ValueType& Set(ValueType& v, ArrayType data, typename ValueType::AllocatorType&) { return v = data; } +}; + +template +struct TypeHelper { + typedef typename ValueType::ConstArray ArrayType; + static bool Is(const ValueType& v) { return v.IsArray(); } + static ArrayType Get(const ValueType& v) { return v.GetArray(); } +}; + +template +struct TypeHelper { + typedef typename ValueType::Object ObjectType; + static bool Is(const ValueType& v) { return v.IsObject(); } + static ObjectType Get(ValueType& v) { return v.GetObject(); } + static ValueType& Set(ValueType& v, ObjectType data) { return v = data; } + static ValueType& Set(ValueType& v, ObjectType data, typename ValueType::AllocatorType&) { v = data; } +}; + +template +struct TypeHelper { + typedef typename ValueType::ConstObject ObjectType; + static bool Is(const ValueType& v) { return v.IsObject(); } + static ObjectType Get(const ValueType& v) { return v.GetObject(); } +}; + +} // namespace internal + +// Forward declarations +template class GenericArray; +template class GenericObject; + +/////////////////////////////////////////////////////////////////////////////// +// GenericValue + +//! Represents a JSON value. Use Value for UTF8 encoding and default allocator. +/*! + A JSON value can be one of 7 types. This class is a variant type supporting + these types. + + Use the Value if UTF8 and default allocator + + \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document) + \tparam Allocator Allocator type for allocating memory of object, array and string. +*/ +template > +class GenericValue { +public: + //! Name-value pair in an object. + typedef GenericMember Member; + typedef Encoding EncodingType; //!< Encoding type from template parameter. + typedef Allocator AllocatorType; //!< Allocator type from template parameter. + typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. + typedef GenericStringRef StringRefType; //!< Reference to a constant string + typedef typename GenericMemberIterator::Iterator MemberIterator; //!< Member iterator for iterating in object. + typedef typename GenericMemberIterator::Iterator ConstMemberIterator; //!< Constant member iterator for iterating in object. + typedef GenericValue* ValueIterator; //!< Value iterator for iterating in array. + typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array. + typedef GenericValue ValueType; //!< Value type of itself. + typedef GenericArray Array; + typedef GenericArray ConstArray; + typedef GenericObject Object; + typedef GenericObject ConstObject; + + //!@name Constructors and destructor. + //@{ + + //! Default constructor creates a null value. + GenericValue() RAPIDJSON_NOEXCEPT : data_() { data_.f.flags = kNullFlag; } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericValue(GenericValue&& rhs) RAPIDJSON_NOEXCEPT : data_(rhs.data_) { + rhs.data_.f.flags = kNullFlag; // give up contents + } +#endif + +private: + //! Copy constructor is not permitted. + GenericValue(const GenericValue& rhs); + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Moving from a GenericDocument is not permitted. + template + GenericValue(GenericDocument&& rhs); + + //! Move assignment from a GenericDocument is not permitted. + template + GenericValue& operator=(GenericDocument&& rhs); +#endif + +public: + + //! Constructor with JSON value type. + /*! This creates a Value of specified type with default content. + \param type Type of the value. + \note Default content for number is zero. + */ + explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_() { + static const uint16_t defaultFlags[7] = { + kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag, + kNumberAnyFlag + }; + RAPIDJSON_ASSERT(type <= kNumberType); + data_.f.flags = defaultFlags[type]; + + // Use ShortString to store empty string. + if (type == kStringType) + data_.ss.SetLength(0); + } + + //! Explicit copy constructor (with allocator) + /*! Creates a copy of a Value by using the given Allocator + \tparam SourceAllocator allocator of \c rhs + \param rhs Value to copy from (read-only) + \param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator(). + \see CopyFrom() + */ + template< typename SourceAllocator > + GenericValue(const GenericValue& rhs, Allocator & allocator); + + //! Constructor for boolean value. + /*! \param b Boolean value + \note This constructor is limited to \em real boolean values and rejects + implicitly converted types like arbitrary pointers. Use an explicit cast + to \c bool, if you want to construct a boolean JSON value in such cases. + */ +#ifndef RAPIDJSON_DOXYGEN_RUNNING // hide SFINAE from Doxygen + template + explicit GenericValue(T b, RAPIDJSON_ENABLEIF((internal::IsSame))) RAPIDJSON_NOEXCEPT // See #472 +#else + explicit GenericValue(bool b) RAPIDJSON_NOEXCEPT +#endif + : data_() { + // safe-guard against failing SFINAE + RAPIDJSON_STATIC_ASSERT((internal::IsSame::Value)); + data_.f.flags = b ? kTrueFlag : kFalseFlag; + } + + //! Constructor for int value. + explicit GenericValue(int i) RAPIDJSON_NOEXCEPT : data_() { + data_.n.i64 = i; + data_.f.flags = (i >= 0) ? (kNumberIntFlag | kUintFlag | kUint64Flag) : kNumberIntFlag; + } + + //! Constructor for unsigned value. + explicit GenericValue(unsigned u) RAPIDJSON_NOEXCEPT : data_() { + data_.n.u64 = u; + data_.f.flags = (u & 0x80000000) ? kNumberUintFlag : (kNumberUintFlag | kIntFlag | kInt64Flag); + } + + //! Constructor for int64_t value. + explicit GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT : data_() { + data_.n.i64 = i64; + data_.f.flags = kNumberInt64Flag; + if (i64 >= 0) { + data_.f.flags |= kNumberUint64Flag; + if (!(static_cast(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000))) + data_.f.flags |= kUintFlag; + if (!(static_cast(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + data_.f.flags |= kIntFlag; + } + else if (i64 >= static_cast(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + data_.f.flags |= kIntFlag; + } + + //! Constructor for uint64_t value. + explicit GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT : data_() { + data_.n.u64 = u64; + data_.f.flags = kNumberUint64Flag; + if (!(u64 & RAPIDJSON_UINT64_C2(0x80000000, 0x00000000))) + data_.f.flags |= kInt64Flag; + if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000))) + data_.f.flags |= kUintFlag; + if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + data_.f.flags |= kIntFlag; + } + + //! Constructor for double value. + explicit GenericValue(double d) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = d; data_.f.flags = kNumberDoubleFlag; } + + //! Constructor for constant string (i.e. do not make a copy of string) + GenericValue(const Ch* s, SizeType length) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(StringRef(s, length)); } + + //! Constructor for constant string (i.e. do not make a copy of string) + explicit GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(s); } + + //! Constructor for copy-string (i.e. do make a copy of string) + GenericValue(const Ch* s, SizeType length, Allocator& allocator) : data_() { SetStringRaw(StringRef(s, length), allocator); } + + //! Constructor for copy-string (i.e. do make a copy of string) + GenericValue(const Ch*s, Allocator& allocator) : data_() { SetStringRaw(StringRef(s), allocator); } + +#if RAPIDJSON_HAS_STDSTRING + //! Constructor for copy-string from a string object (i.e. do make a copy of string) + /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + GenericValue(const std::basic_string& s, Allocator& allocator) : data_() { SetStringRaw(StringRef(s), allocator); } +#endif + + //! Constructor for Array. + /*! + \param a An array obtained by \c GetArray(). + \note \c Array is always pass-by-value. + \note the source array is moved into this value and the sourec array becomes empty. + */ + GenericValue(Array a) RAPIDJSON_NOEXCEPT : data_(a.value_.data_) { + a.value_.data_ = Data(); + a.value_.data_.f.flags = kArrayFlag; + } + + //! Constructor for Object. + /*! + \param o An object obtained by \c GetObject(). + \note \c Object is always pass-by-value. + \note the source object is moved into this value and the sourec object becomes empty. + */ + GenericValue(Object o) RAPIDJSON_NOEXCEPT : data_(o.value_.data_) { + o.value_.data_ = Data(); + o.value_.data_.f.flags = kObjectFlag; + } + + //! Destructor. + /*! Need to destruct elements of array, members of object, or copy-string. + */ + ~GenericValue() { + if (Allocator::kNeedFree) { // Shortcut by Allocator's trait + switch(data_.f.flags) { + case kArrayFlag: + { + GenericValue* e = GetElementsPointer(); + for (GenericValue* v = e; v != e + data_.a.size; ++v) + v->~GenericValue(); + Allocator::Free(e); + } + break; + + case kObjectFlag: + for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) + m->~Member(); + Allocator::Free(GetMembersPointer()); + break; + + case kCopyStringFlag: + Allocator::Free(const_cast(GetStringPointer())); + break; + + default: + break; // Do nothing for other types. + } + } + } + + //@} + + //!@name Assignment operators + //@{ + + //! Assignment with move semantics. + /*! \param rhs Source of the assignment. It will become a null value after assignment. + */ + GenericValue& operator=(GenericValue& rhs) RAPIDJSON_NOEXCEPT { + RAPIDJSON_ASSERT(this != &rhs); + this->~GenericValue(); + RawAssign(rhs); + return *this; + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move assignment in C++11 + GenericValue& operator=(GenericValue&& rhs) RAPIDJSON_NOEXCEPT { + return *this = rhs.Move(); + } +#endif + + //! Assignment of constant string reference (no copy) + /*! \param str Constant string reference to be assigned + \note This overload is needed to avoid clashes with the generic primitive type assignment overload below. + \see GenericStringRef, operator=(T) + */ + GenericValue& operator=(StringRefType str) RAPIDJSON_NOEXCEPT { + GenericValue s(str); + return *this = s; + } + + //! Assignment with primitive types. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param value The value to be assigned. + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref SetString(const Ch*, Allocator&) (for copying) or + \ref StringRef() (to explicitly mark the pointer as constant) instead. + All other pointer types would implicitly convert to \c bool, + use \ref SetBool() instead. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::IsPointer), (GenericValue&)) + operator=(T value) { + GenericValue v(value); + return *this = v; + } + + //! Deep-copy assignment from Value + /*! Assigns a \b copy of the Value to the current Value object + \tparam SourceAllocator Allocator type of \c rhs + \param rhs Value to copy from (read-only) + \param allocator Allocator to use for copying + */ + template + GenericValue& CopyFrom(const GenericValue& rhs, Allocator& allocator) { + RAPIDJSON_ASSERT(static_cast(this) != static_cast(&rhs)); + this->~GenericValue(); + new (this) GenericValue(rhs, allocator); + return *this; + } + + //! Exchange the contents of this value with those of other. + /*! + \param other Another value. + \note Constant complexity. + */ + GenericValue& Swap(GenericValue& other) RAPIDJSON_NOEXCEPT { + GenericValue temp; + temp.RawAssign(*this); + RawAssign(other); + other.RawAssign(temp); + return *this; + } + + //! free-standing swap function helper + /*! + Helper function to enable support for common swap implementation pattern based on \c std::swap: + \code + void swap(MyClass& a, MyClass& b) { + using std::swap; + swap(a.value, b.value); + // ... + } + \endcode + \see Swap() + */ + friend inline void swap(GenericValue& a, GenericValue& b) RAPIDJSON_NOEXCEPT { a.Swap(b); } + + //! Prepare Value for move semantics + /*! \return *this */ + GenericValue& Move() RAPIDJSON_NOEXCEPT { return *this; } + //@} + + //!@name Equal-to and not-equal-to operators + //@{ + //! Equal-to operator + /*! + \note If an object contains duplicated named member, comparing equality with any object is always \c false. + \note Linear time complexity (number of all values in the subtree and total lengths of all strings). + */ + template + bool operator==(const GenericValue& rhs) const { + typedef GenericValue RhsType; + if (GetType() != rhs.GetType()) + return false; + + switch (GetType()) { + case kObjectType: // Warning: O(n^2) inner-loop + if (data_.o.size != rhs.data_.o.size) + return false; + for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) { + typename RhsType::ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name); + if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value) + return false; + } + return true; + + case kArrayType: + if (data_.a.size != rhs.data_.a.size) + return false; + for (SizeType i = 0; i < data_.a.size; i++) + if ((*this)[i] != rhs[i]) + return false; + return true; + + case kStringType: + return StringEqual(rhs); + + case kNumberType: + if (IsDouble() || rhs.IsDouble()) { + double a = GetDouble(); // May convert from integer to double. + double b = rhs.GetDouble(); // Ditto + return a >= b && a <= b; // Prevent -Wfloat-equal + } + else + return data_.n.u64 == rhs.data_.n.u64; + + default: + return true; + } + } + + //! Equal-to operator with const C-string pointer + bool operator==(const Ch* rhs) const { return *this == GenericValue(StringRef(rhs)); } + +#if RAPIDJSON_HAS_STDSTRING + //! Equal-to operator with string object + /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + bool operator==(const std::basic_string& rhs) const { return *this == GenericValue(StringRef(rhs)); } +#endif + + //! Equal-to operator with primitive types + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c true, \c false + */ + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr,internal::IsGenericValue >), (bool)) operator==(const T& rhs) const { return *this == GenericValue(rhs); } + + //! Not-equal-to operator + /*! \return !(*this == rhs) + */ + template + bool operator!=(const GenericValue& rhs) const { return !(*this == rhs); } + + //! Not-equal-to operator with const C-string pointer + bool operator!=(const Ch* rhs) const { return !(*this == rhs); } + + //! Not-equal-to operator with arbitrary types + /*! \return !(*this == rhs) + */ + template RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& rhs) const { return !(*this == rhs); } + + //! Equal-to operator with arbitrary types (symmetric version) + /*! \return (rhs == lhs) + */ + template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator==(const T& lhs, const GenericValue& rhs) { return rhs == lhs; } + + //! Not-Equal-to operator with arbitrary types (symmetric version) + /*! \return !(rhs == lhs) + */ + template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& lhs, const GenericValue& rhs) { return !(rhs == lhs); } + //@} + + //!@name Type + //@{ + + Type GetType() const { return static_cast(data_.f.flags & kTypeMask); } + bool IsNull() const { return data_.f.flags == kNullFlag; } + bool IsFalse() const { return data_.f.flags == kFalseFlag; } + bool IsTrue() const { return data_.f.flags == kTrueFlag; } + bool IsBool() const { return (data_.f.flags & kBoolFlag) != 0; } + bool IsObject() const { return data_.f.flags == kObjectFlag; } + bool IsArray() const { return data_.f.flags == kArrayFlag; } + bool IsNumber() const { return (data_.f.flags & kNumberFlag) != 0; } + bool IsInt() const { return (data_.f.flags & kIntFlag) != 0; } + bool IsUint() const { return (data_.f.flags & kUintFlag) != 0; } + bool IsInt64() const { return (data_.f.flags & kInt64Flag) != 0; } + bool IsUint64() const { return (data_.f.flags & kUint64Flag) != 0; } + bool IsDouble() const { return (data_.f.flags & kDoubleFlag) != 0; } + bool IsString() const { return (data_.f.flags & kStringFlag) != 0; } + + // Checks whether a number can be losslessly converted to a double. + bool IsLosslessDouble() const { + if (!IsNumber()) return false; + if (IsUint64()) { + uint64_t u = GetUint64(); + volatile double d = static_cast(u); + return (d >= 0.0) + && (d < static_cast(std::numeric_limits::max())) + && (u == static_cast(d)); + } + if (IsInt64()) { + int64_t i = GetInt64(); + volatile double d = static_cast(i); + return (d >= static_cast(std::numeric_limits::min())) + && (d < static_cast(std::numeric_limits::max())) + && (i == static_cast(d)); + } + return true; // double, int, uint are always lossless + } + + // Checks whether a number is a float (possible lossy). + bool IsFloat() const { + if ((data_.f.flags & kDoubleFlag) == 0) + return false; + double d = GetDouble(); + return d >= -3.4028234e38 && d <= 3.4028234e38; + } + // Checks whether a number can be losslessly converted to a float. + bool IsLosslessFloat() const { + if (!IsNumber()) return false; + double a = GetDouble(); + if (a < static_cast(-std::numeric_limits::max()) + || a > static_cast(std::numeric_limits::max())) + return false; + double b = static_cast(static_cast(a)); + return a >= b && a <= b; // Prevent -Wfloat-equal + } + + //@} + + //!@name Null + //@{ + + GenericValue& SetNull() { this->~GenericValue(); new (this) GenericValue(); return *this; } + + //@} + + //!@name Bool + //@{ + + bool GetBool() const { RAPIDJSON_ASSERT(IsBool()); return data_.f.flags == kTrueFlag; } + //!< Set boolean value + /*! \post IsBool() == true */ + GenericValue& SetBool(bool b) { this->~GenericValue(); new (this) GenericValue(b); return *this; } + + //@} + + //!@name Object + //@{ + + //! Set this value as an empty object. + /*! \post IsObject() == true */ + GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; } + + //! Get the number of members in the object. + SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; } + + //! Check whether the object is empty. + bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; } + + //! Get a value from an object associated with the name. + /*! \pre IsObject() == true + \tparam T Either \c Ch or \c const \c Ch (template used for disambiguation with \ref operator[](SizeType)) + \note In version 0.1x, if the member is not found, this function returns a null value. This makes issue 7. + Since 0.2, if the name is not correct, it will assert. + If user is unsure whether a member exists, user should use HasMember() first. + A better approach is to use FindMember(). + \note Linear time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >),(GenericValue&)) operator[](T* name) { + GenericValue n(StringRef(name)); + return (*this)[n]; + } + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >),(const GenericValue&)) operator[](T* name) const { return const_cast(*this)[name]; } + + //! Get a value from an object associated with the name. + /*! \pre IsObject() == true + \tparam SourceAllocator Allocator of the \c name value + + \note Compared to \ref operator[](T*), this version is faster because it does not need a StrLen(). + And it can also handle strings with embedded null characters. + + \note Linear time complexity. + */ + template + GenericValue& operator[](const GenericValue& name) { + MemberIterator member = FindMember(name); + if (member != MemberEnd()) + return member->value; + else { + RAPIDJSON_ASSERT(false); // see above note + + // This will generate -Wexit-time-destructors in clang + // static GenericValue NullValue; + // return NullValue; + + // Use static buffer and placement-new to prevent destruction + static char buffer[sizeof(GenericValue)]; + return *new (buffer) GenericValue(); + } + } + template + const GenericValue& operator[](const GenericValue& name) const { return const_cast(*this)[name]; } + +#if RAPIDJSON_HAS_STDSTRING + //! Get a value from an object associated with name (string object). + GenericValue& operator[](const std::basic_string& name) { return (*this)[GenericValue(StringRef(name))]; } + const GenericValue& operator[](const std::basic_string& name) const { return (*this)[GenericValue(StringRef(name))]; } +#endif + + //! Const member iterator + /*! \pre IsObject() == true */ + ConstMemberIterator MemberBegin() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(GetMembersPointer()); } + //! Const \em past-the-end member iterator + /*! \pre IsObject() == true */ + ConstMemberIterator MemberEnd() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(GetMembersPointer() + data_.o.size); } + //! Member iterator + /*! \pre IsObject() == true */ + MemberIterator MemberBegin() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer()); } + //! \em Past-the-end member iterator + /*! \pre IsObject() == true */ + MemberIterator MemberEnd() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer() + data_.o.size); } + + //! Check whether a member exists in the object. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + bool HasMember(const Ch* name) const { return FindMember(name) != MemberEnd(); } + +#if RAPIDJSON_HAS_STDSTRING + //! Check whether a member exists in the object with string object. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + bool HasMember(const std::basic_string& name) const { return FindMember(name) != MemberEnd(); } +#endif + + //! Check whether a member exists in the object with GenericValue name. + /*! + This version is faster because it does not need a StrLen(). It can also handle string with null character. + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + template + bool HasMember(const GenericValue& name) const { return FindMember(name) != MemberEnd(); } + + //! Find member by name. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + + \note Earlier versions of Rapidjson returned a \c NULL pointer, in case + the requested member doesn't exist. For consistency with e.g. + \c std::map, this has been changed to MemberEnd() now. + \note Linear time complexity. + */ + MemberIterator FindMember(const Ch* name) { + GenericValue n(StringRef(name)); + return FindMember(n); + } + + ConstMemberIterator FindMember(const Ch* name) const { return const_cast(*this).FindMember(name); } + + //! Find member by name. + /*! + This version is faster because it does not need a StrLen(). It can also handle string with null character. + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + + \note Earlier versions of Rapidjson returned a \c NULL pointer, in case + the requested member doesn't exist. For consistency with e.g. + \c std::map, this has been changed to MemberEnd() now. + \note Linear time complexity. + */ + template + MemberIterator FindMember(const GenericValue& name) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(name.IsString()); + MemberIterator member = MemberBegin(); + for ( ; member != MemberEnd(); ++member) + if (name.StringEqual(member->name)) + break; + return member; + } + template ConstMemberIterator FindMember(const GenericValue& name) const { return const_cast(*this).FindMember(name); } + +#if RAPIDJSON_HAS_STDSTRING + //! Find member by string object name. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + */ + MemberIterator FindMember(const std::basic_string& name) { return FindMember(GenericValue(StringRef(name))); } + ConstMemberIterator FindMember(const std::basic_string& name) const { return FindMember(GenericValue(StringRef(name))); } +#endif + + //! Add a member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value Value of any type. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note The ownership of \c name and \c value will be transferred to this object on success. + \pre IsObject() && name.IsString() + \post name.IsNull() && value.IsNull() + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, GenericValue& value, Allocator& allocator) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(name.IsString()); + + ObjectData& o = data_.o; + if (o.size >= o.capacity) { + if (o.capacity == 0) { + o.capacity = kDefaultObjectCapacity; + SetMembersPointer(reinterpret_cast(allocator.Malloc(o.capacity * sizeof(Member)))); + } + else { + SizeType oldCapacity = o.capacity; + o.capacity += (oldCapacity + 1) / 2; // grow by factor 1.5 + SetMembersPointer(reinterpret_cast(allocator.Realloc(GetMembersPointer(), oldCapacity * sizeof(Member), o.capacity * sizeof(Member)))); + } + } + Member* members = GetMembersPointer(); + members[o.size].name.RawAssign(name); + members[o.size].value.RawAssign(value); + o.size++; + return *this; + } + + //! Add a constant string value as member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, StringRefType value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Add a string object as member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, std::basic_string& value, Allocator& allocator) { + GenericValue v(value, allocator); + return AddMember(name, v, allocator); + } +#endif + + //! Add any primitive value as member (name-value pair) to the object. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param name A string value as name of member. + \param value Value of primitive type \c T as value of member + \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref + AddMember(StringRefType, StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized Constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + AddMember(GenericValue& name, T value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericValue& AddMember(GenericValue&& name, GenericValue&& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(GenericValue&& name, GenericValue& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(GenericValue& name, GenericValue&& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(StringRefType name, GenericValue&& value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + + + //! Add a member (name-value pair) to the object. + /*! \param name A constant string reference as name of member. + \param value Value of any type. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note The ownership of \c value will be transferred to this object on success. + \pre IsObject() + \post value.IsNull() + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(StringRefType name, GenericValue& value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } + + //! Add a constant string value as member (name-value pair) to the object. + /*! \param name A constant string reference as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(StringRefType,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(StringRefType name, StringRefType value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + + //! Add any primitive value as member (name-value pair) to the object. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param name A constant string reference as name of member. + \param value Value of primitive type \c T as value of member + \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref + AddMember(StringRefType, StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized Constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + AddMember(StringRefType name, T value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } + + //! Remove all members in the object. + /*! This function do not deallocate memory in the object, i.e. the capacity is unchanged. + \note Linear time complexity. + */ + void RemoveAllMembers() { + RAPIDJSON_ASSERT(IsObject()); + for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) + m->~Member(); + data_.o.size = 0; + } + + //! Remove a member in object by its name. + /*! \param name Name of member to be removed. + \return Whether the member existed. + \note This function may reorder the object members. Use \ref + EraseMember(ConstMemberIterator) if you need to preserve the + relative order of the remaining members. + \note Linear time complexity. + */ + bool RemoveMember(const Ch* name) { + GenericValue n(StringRef(name)); + return RemoveMember(n); + } + +#if RAPIDJSON_HAS_STDSTRING + bool RemoveMember(const std::basic_string& name) { return RemoveMember(GenericValue(StringRef(name))); } +#endif + + template + bool RemoveMember(const GenericValue& name) { + MemberIterator m = FindMember(name); + if (m != MemberEnd()) { + RemoveMember(m); + return true; + } + else + return false; + } + + //! Remove a member in object by iterator. + /*! \param m member iterator (obtained by FindMember() or MemberBegin()). + \return the new iterator after removal. + \note This function may reorder the object members. Use \ref + EraseMember(ConstMemberIterator) if you need to preserve the + relative order of the remaining members. + \note Constant time complexity. + */ + MemberIterator RemoveMember(MemberIterator m) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(data_.o.size > 0); + RAPIDJSON_ASSERT(GetMembersPointer() != 0); + RAPIDJSON_ASSERT(m >= MemberBegin() && m < MemberEnd()); + + MemberIterator last(GetMembersPointer() + (data_.o.size - 1)); + if (data_.o.size > 1 && m != last) + *m = *last; // Move the last one to this place + else + m->~Member(); // Only one left, just destroy + --data_.o.size; + return m; + } + + //! Remove a member from an object by iterator. + /*! \param pos iterator to the member to remove + \pre IsObject() == true && \ref MemberBegin() <= \c pos < \ref MemberEnd() + \return Iterator following the removed element. + If the iterator \c pos refers to the last element, the \ref MemberEnd() iterator is returned. + \note This function preserves the relative order of the remaining object + members. If you do not need this, use the more efficient \ref RemoveMember(MemberIterator). + \note Linear time complexity. + */ + MemberIterator EraseMember(ConstMemberIterator pos) { + return EraseMember(pos, pos +1); + } + + //! Remove members in the range [first, last) from an object. + /*! \param first iterator to the first member to remove + \param last iterator following the last member to remove + \pre IsObject() == true && \ref MemberBegin() <= \c first <= \c last <= \ref MemberEnd() + \return Iterator following the last removed element. + \note This function preserves the relative order of the remaining object + members. + \note Linear time complexity. + */ + MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(data_.o.size > 0); + RAPIDJSON_ASSERT(GetMembersPointer() != 0); + RAPIDJSON_ASSERT(first >= MemberBegin()); + RAPIDJSON_ASSERT(first <= last); + RAPIDJSON_ASSERT(last <= MemberEnd()); + + MemberIterator pos = MemberBegin() + (first - MemberBegin()); + for (MemberIterator itr = pos; itr != last; ++itr) + itr->~Member(); + std::memmove(&*pos, &*last, static_cast(MemberEnd() - last) * sizeof(Member)); + data_.o.size -= static_cast(last - first); + return pos; + } + + //! Erase a member in object by its name. + /*! \param name Name of member to be removed. + \return Whether the member existed. + \note Linear time complexity. + */ + bool EraseMember(const Ch* name) { + GenericValue n(StringRef(name)); + return EraseMember(n); + } + +#if RAPIDJSON_HAS_STDSTRING + bool EraseMember(const std::basic_string& name) { return EraseMember(GenericValue(StringRef(name))); } +#endif + + template + bool EraseMember(const GenericValue& name) { + MemberIterator m = FindMember(name); + if (m != MemberEnd()) { + EraseMember(m); + return true; + } + else + return false; + } + + Object GetObject() { RAPIDJSON_ASSERT(IsObject()); return Object(*this); } + ConstObject GetObject() const { RAPIDJSON_ASSERT(IsObject()); return ConstObject(*this); } + + //@} + + //!@name Array + //@{ + + //! Set this value as an empty array. + /*! \post IsArray == true */ + GenericValue& SetArray() { this->~GenericValue(); new (this) GenericValue(kArrayType); return *this; } + + //! Get the number of elements in array. + SizeType Size() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size; } + + //! Get the capacity of array. + SizeType Capacity() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.capacity; } + + //! Check whether the array is empty. + bool Empty() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size == 0; } + + //! Remove all elements in the array. + /*! This function do not deallocate memory in the array, i.e. the capacity is unchanged. + \note Linear time complexity. + */ + void Clear() { + RAPIDJSON_ASSERT(IsArray()); + GenericValue* e = GetElementsPointer(); + for (GenericValue* v = e; v != e + data_.a.size; ++v) + v->~GenericValue(); + data_.a.size = 0; + } + + //! Get an element from array by index. + /*! \pre IsArray() == true + \param index Zero-based index of element. + \see operator[](T*) + */ + GenericValue& operator[](SizeType index) { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(index < data_.a.size); + return GetElementsPointer()[index]; + } + const GenericValue& operator[](SizeType index) const { return const_cast(*this)[index]; } + + //! Element iterator + /*! \pre IsArray() == true */ + ValueIterator Begin() { RAPIDJSON_ASSERT(IsArray()); return GetElementsPointer(); } + //! \em Past-the-end element iterator + /*! \pre IsArray() == true */ + ValueIterator End() { RAPIDJSON_ASSERT(IsArray()); return GetElementsPointer() + data_.a.size; } + //! Constant element iterator + /*! \pre IsArray() == true */ + ConstValueIterator Begin() const { return const_cast(*this).Begin(); } + //! Constant \em past-the-end element iterator + /*! \pre IsArray() == true */ + ConstValueIterator End() const { return const_cast(*this).End(); } + + //! Request the array to have enough capacity to store elements. + /*! \param newCapacity The capacity that the array at least need to have. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note Linear time complexity. + */ + GenericValue& Reserve(SizeType newCapacity, Allocator &allocator) { + RAPIDJSON_ASSERT(IsArray()); + if (newCapacity > data_.a.capacity) { + SetElementsPointer(reinterpret_cast(allocator.Realloc(GetElementsPointer(), data_.a.capacity * sizeof(GenericValue), newCapacity * sizeof(GenericValue)))); + data_.a.capacity = newCapacity; + } + return *this; + } + + //! Append a GenericValue at the end of the array. + /*! \param value Value to be appended. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \post value.IsNull() == true + \return The value itself for fluent API. + \note The ownership of \c value will be transferred to this array on success. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + \note Amortized constant time complexity. + */ + GenericValue& PushBack(GenericValue& value, Allocator& allocator) { + RAPIDJSON_ASSERT(IsArray()); + if (data_.a.size >= data_.a.capacity) + Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : (data_.a.capacity + (data_.a.capacity + 1) / 2), allocator); + GetElementsPointer()[data_.a.size++].RawAssign(value); + return *this; + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericValue& PushBack(GenericValue&& value, Allocator& allocator) { + return PushBack(value, allocator); + } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + + //! Append a constant string reference at the end of the array. + /*! \param value Constant string reference to be appended. + \param allocator Allocator for reallocating memory. It must be the same one used previously. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \return The value itself for fluent API. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + \note Amortized constant time complexity. + \see GenericStringRef + */ + GenericValue& PushBack(StringRefType value, Allocator& allocator) { + return (*this).template PushBack(value, allocator); + } + + //! Append a primitive value at the end of the array. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param value Value of primitive type T to be appended. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \return The value itself for fluent API. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref PushBack(GenericValue&, Allocator&) or \ref + PushBack(StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + PushBack(T value, Allocator& allocator) { + GenericValue v(value); + return PushBack(v, allocator); + } + + //! Remove the last element in the array. + /*! + \note Constant time complexity. + */ + GenericValue& PopBack() { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(!Empty()); + GetElementsPointer()[--data_.a.size].~GenericValue(); + return *this; + } + + //! Remove an element of array by iterator. + /*! + \param pos iterator to the element to remove + \pre IsArray() == true && \ref Begin() <= \c pos < \ref End() + \return Iterator following the removed element. If the iterator pos refers to the last element, the End() iterator is returned. + \note Linear time complexity. + */ + ValueIterator Erase(ConstValueIterator pos) { + return Erase(pos, pos + 1); + } + + //! Remove elements in the range [first, last) of the array. + /*! + \param first iterator to the first element to remove + \param last iterator following the last element to remove + \pre IsArray() == true && \ref Begin() <= \c first <= \c last <= \ref End() + \return Iterator following the last removed element. + \note Linear time complexity. + */ + ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(data_.a.size > 0); + RAPIDJSON_ASSERT(GetElementsPointer() != 0); + RAPIDJSON_ASSERT(first >= Begin()); + RAPIDJSON_ASSERT(first <= last); + RAPIDJSON_ASSERT(last <= End()); + ValueIterator pos = Begin() + (first - Begin()); + for (ValueIterator itr = pos; itr != last; ++itr) + itr->~GenericValue(); + std::memmove(pos, last, static_cast(End() - last) * sizeof(GenericValue)); + data_.a.size -= static_cast(last - first); + return pos; + } + + Array GetArray() { RAPIDJSON_ASSERT(IsArray()); return Array(*this); } + ConstArray GetArray() const { RAPIDJSON_ASSERT(IsArray()); return ConstArray(*this); } + + //@} + + //!@name Number + //@{ + + int GetInt() const { RAPIDJSON_ASSERT(data_.f.flags & kIntFlag); return data_.n.i.i; } + unsigned GetUint() const { RAPIDJSON_ASSERT(data_.f.flags & kUintFlag); return data_.n.u.u; } + int64_t GetInt64() const { RAPIDJSON_ASSERT(data_.f.flags & kInt64Flag); return data_.n.i64; } + uint64_t GetUint64() const { RAPIDJSON_ASSERT(data_.f.flags & kUint64Flag); return data_.n.u64; } + + //! Get the value as double type. + /*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessDouble() to check whether the converison is lossless. + */ + double GetDouble() const { + RAPIDJSON_ASSERT(IsNumber()); + if ((data_.f.flags & kDoubleFlag) != 0) return data_.n.d; // exact type, no conversion. + if ((data_.f.flags & kIntFlag) != 0) return data_.n.i.i; // int -> double + if ((data_.f.flags & kUintFlag) != 0) return data_.n.u.u; // unsigned -> double + if ((data_.f.flags & kInt64Flag) != 0) return static_cast(data_.n.i64); // int64_t -> double (may lose precision) + RAPIDJSON_ASSERT((data_.f.flags & kUint64Flag) != 0); return static_cast(data_.n.u64); // uint64_t -> double (may lose precision) + } + + //! Get the value as float type. + /*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessFloat() to check whether the converison is lossless. + */ + float GetFloat() const { + return static_cast(GetDouble()); + } + + GenericValue& SetInt(int i) { this->~GenericValue(); new (this) GenericValue(i); return *this; } + GenericValue& SetUint(unsigned u) { this->~GenericValue(); new (this) GenericValue(u); return *this; } + GenericValue& SetInt64(int64_t i64) { this->~GenericValue(); new (this) GenericValue(i64); return *this; } + GenericValue& SetUint64(uint64_t u64) { this->~GenericValue(); new (this) GenericValue(u64); return *this; } + GenericValue& SetDouble(double d) { this->~GenericValue(); new (this) GenericValue(d); return *this; } + GenericValue& SetFloat(float f) { this->~GenericValue(); new (this) GenericValue(f); return *this; } + + //@} + + //!@name String + //@{ + + const Ch* GetString() const { RAPIDJSON_ASSERT(IsString()); return (data_.f.flags & kInlineStrFlag) ? data_.ss.str : GetStringPointer(); } + + //! Get the length of string. + /*! Since rapidjson permits "\\u0000" in the json string, strlen(v.GetString()) may not equal to v.GetStringLength(). + */ + SizeType GetStringLength() const { RAPIDJSON_ASSERT(IsString()); return ((data_.f.flags & kInlineStrFlag) ? (data_.ss.GetLength()) : data_.s.length); } + + //! Set this value as a string without copying source string. + /*! This version has better performance with supplied length, and also support string containing null character. + \param s source string pointer. + \param length The length of source string, excluding the trailing null terminator. + \return The value itself for fluent API. + \post IsString() == true && GetString() == s && GetStringLength() == length + \see SetString(StringRefType) + */ + GenericValue& SetString(const Ch* s, SizeType length) { return SetString(StringRef(s, length)); } + + //! Set this value as a string without copying source string. + /*! \param s source string reference + \return The value itself for fluent API. + \post IsString() == true && GetString() == s && GetStringLength() == s.length + */ + GenericValue& SetString(StringRefType s) { this->~GenericValue(); SetStringRaw(s); return *this; } + + //! Set this value as a string by copying from source string. + /*! This version has better performance with supplied length, and also support string containing null character. + \param s source string. + \param length The length of source string, excluding the trailing null terminator. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length + */ + GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { this->~GenericValue(); SetStringRaw(StringRef(s, length), allocator); return *this; } + + //! Set this value as a string by copying from source string. + /*! \param s source string. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length + */ + GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(s, internal::StrLen(s), allocator); } + +#if RAPIDJSON_HAS_STDSTRING + //! Set this value as a string by copying from source string. + /*! \param s source string. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s.data() && strcmp(GetString(),s.data() == 0 && GetStringLength() == s.size() + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + GenericValue& SetString(const std::basic_string& s, Allocator& allocator) { return SetString(s.data(), SizeType(s.size()), allocator); } +#endif + + //@} + + //!@name Array + //@{ + + //! Templated version for checking whether this value is type T. + /*! + \tparam T Either \c bool, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c float, \c const \c char*, \c std::basic_string + */ + template + bool Is() const { return internal::TypeHelper::Is(*this); } + + template + T Get() const { return internal::TypeHelper::Get(*this); } + + template + T Get() { return internal::TypeHelper::Get(*this); } + + template + ValueType& Set(const T& data) { return internal::TypeHelper::Set(*this, data); } + + template + ValueType& Set(const T& data, AllocatorType& allocator) { return internal::TypeHelper::Set(*this, data, allocator); } + + //@} + + //! Generate events of this value to a Handler. + /*! This function adopts the GoF visitor pattern. + Typical usage is to output this JSON value as JSON text via Writer, which is a Handler. + It can also be used to deep clone this value via GenericDocument, which is also a Handler. + \tparam Handler type of handler. + \param handler An object implementing concept Handler. + */ + template + bool Accept(Handler& handler) const { + switch(GetType()) { + case kNullType: return handler.Null(); + case kFalseType: return handler.Bool(false); + case kTrueType: return handler.Bool(true); + + case kObjectType: + if (RAPIDJSON_UNLIKELY(!handler.StartObject())) + return false; + for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) { + RAPIDJSON_ASSERT(m->name.IsString()); // User may change the type of name by MemberIterator. + if (RAPIDJSON_UNLIKELY(!handler.Key(m->name.GetString(), m->name.GetStringLength(), (m->name.data_.f.flags & kCopyFlag) != 0))) + return false; + if (RAPIDJSON_UNLIKELY(!m->value.Accept(handler))) + return false; + } + return handler.EndObject(data_.o.size); + + case kArrayType: + if (RAPIDJSON_UNLIKELY(!handler.StartArray())) + return false; + for (const GenericValue* v = Begin(); v != End(); ++v) + if (RAPIDJSON_UNLIKELY(!v->Accept(handler))) + return false; + return handler.EndArray(data_.a.size); + + case kStringType: + return handler.String(GetString(), GetStringLength(), (data_.f.flags & kCopyFlag) != 0); + + default: + RAPIDJSON_ASSERT(GetType() == kNumberType); + if (IsDouble()) return handler.Double(data_.n.d); + else if (IsInt()) return handler.Int(data_.n.i.i); + else if (IsUint()) return handler.Uint(data_.n.u.u); + else if (IsInt64()) return handler.Int64(data_.n.i64); + else return handler.Uint64(data_.n.u64); + } + } + +private: + template friend class GenericValue; + template friend class GenericDocument; + + enum { + kBoolFlag = 0x0008, + kNumberFlag = 0x0010, + kIntFlag = 0x0020, + kUintFlag = 0x0040, + kInt64Flag = 0x0080, + kUint64Flag = 0x0100, + kDoubleFlag = 0x0200, + kStringFlag = 0x0400, + kCopyFlag = 0x0800, + kInlineStrFlag = 0x1000, + + // Initial flags of different types. + kNullFlag = kNullType, + kTrueFlag = kTrueType | kBoolFlag, + kFalseFlag = kFalseType | kBoolFlag, + kNumberIntFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag, + kNumberUintFlag = kNumberType | kNumberFlag | kUintFlag | kUint64Flag | kInt64Flag, + kNumberInt64Flag = kNumberType | kNumberFlag | kInt64Flag, + kNumberUint64Flag = kNumberType | kNumberFlag | kUint64Flag, + kNumberDoubleFlag = kNumberType | kNumberFlag | kDoubleFlag, + kNumberAnyFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag | kUintFlag | kUint64Flag | kDoubleFlag, + kConstStringFlag = kStringType | kStringFlag, + kCopyStringFlag = kStringType | kStringFlag | kCopyFlag, + kShortStringFlag = kStringType | kStringFlag | kCopyFlag | kInlineStrFlag, + kObjectFlag = kObjectType, + kArrayFlag = kArrayType, + + kTypeMask = 0x07 + }; + + static const SizeType kDefaultArrayCapacity = 16; + static const SizeType kDefaultObjectCapacity = 16; + + struct Flag { +#if RAPIDJSON_48BITPOINTER_OPTIMIZATION + char payload[sizeof(SizeType) * 2 + 6]; // 2 x SizeType + lower 48-bit pointer +#elif RAPIDJSON_64BIT + char payload[sizeof(SizeType) * 2 + sizeof(void*) + 6]; // 6 padding bytes +#else + char payload[sizeof(SizeType) * 2 + sizeof(void*) + 2]; // 2 padding bytes +#endif + uint16_t flags; + }; + + struct String { + SizeType length; + SizeType hashcode; //!< reserved + const Ch* str; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + // implementation detail: ShortString can represent zero-terminated strings up to MaxSize chars + // (excluding the terminating zero) and store a value to determine the length of the contained + // string in the last character str[LenPos] by storing "MaxSize - length" there. If the string + // to store has the maximal length of MaxSize then str[LenPos] will be 0 and therefore act as + // the string terminator as well. For getting the string length back from that value just use + // "MaxSize - str[LenPos]". + // This allows to store 13-chars strings in 32-bit mode, 21-chars strings in 64-bit mode, + // 13-chars strings for RAPIDJSON_48BITPOINTER_OPTIMIZATION=1 inline (for `UTF8`-encoded strings). + struct ShortString { + enum { MaxChars = sizeof(static_cast(0)->payload) / sizeof(Ch), MaxSize = MaxChars - 1, LenPos = MaxSize }; + Ch str[MaxChars]; + + inline static bool Usable(SizeType len) { return (MaxSize >= len); } + inline void SetLength(SizeType len) { str[LenPos] = static_cast(MaxSize - len); } + inline SizeType GetLength() const { return static_cast(MaxSize - str[LenPos]); } + }; // at most as many bytes as "String" above => 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + // By using proper binary layout, retrieval of different integer types do not need conversions. + union Number { +#if RAPIDJSON_ENDIAN == RAPIDJSON_LITTLEENDIAN + struct I { + int i; + char padding[4]; + }i; + struct U { + unsigned u; + char padding2[4]; + }u; +#else + struct I { + char padding[4]; + int i; + }i; + struct U { + char padding2[4]; + unsigned u; + }u; +#endif + int64_t i64; + uint64_t u64; + double d; + }; // 8 bytes + + struct ObjectData { + SizeType size; + SizeType capacity; + Member* members; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + struct ArrayData { + SizeType size; + SizeType capacity; + GenericValue* elements; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + union Data { + String s; + ShortString ss; + Number n; + ObjectData o; + ArrayData a; + Flag f; + }; // 16 bytes in 32-bit mode, 24 bytes in 64-bit mode, 16 bytes in 64-bit with RAPIDJSON_48BITPOINTER_OPTIMIZATION + + RAPIDJSON_FORCEINLINE const Ch* GetStringPointer() const { return RAPIDJSON_GETPOINTER(Ch, data_.s.str); } + RAPIDJSON_FORCEINLINE const Ch* SetStringPointer(const Ch* str) { return RAPIDJSON_SETPOINTER(Ch, data_.s.str, str); } + RAPIDJSON_FORCEINLINE GenericValue* GetElementsPointer() const { return RAPIDJSON_GETPOINTER(GenericValue, data_.a.elements); } + RAPIDJSON_FORCEINLINE GenericValue* SetElementsPointer(GenericValue* elements) { return RAPIDJSON_SETPOINTER(GenericValue, data_.a.elements, elements); } + RAPIDJSON_FORCEINLINE Member* GetMembersPointer() const { return RAPIDJSON_GETPOINTER(Member, data_.o.members); } + RAPIDJSON_FORCEINLINE Member* SetMembersPointer(Member* members) { return RAPIDJSON_SETPOINTER(Member, data_.o.members, members); } + + // Initialize this value as array with initial data, without calling destructor. + void SetArrayRaw(GenericValue* values, SizeType count, Allocator& allocator) { + data_.f.flags = kArrayFlag; + if (count) { + GenericValue* e = static_cast(allocator.Malloc(count * sizeof(GenericValue))); + SetElementsPointer(e); + std::memcpy(e, values, count * sizeof(GenericValue)); + } + else + SetElementsPointer(0); + data_.a.size = data_.a.capacity = count; + } + + //! Initialize this value as object with initial data, without calling destructor. + void SetObjectRaw(Member* members, SizeType count, Allocator& allocator) { + data_.f.flags = kObjectFlag; + if (count) { + Member* m = static_cast(allocator.Malloc(count * sizeof(Member))); + SetMembersPointer(m); + std::memcpy(m, members, count * sizeof(Member)); + } + else + SetMembersPointer(0); + data_.o.size = data_.o.capacity = count; + } + + //! Initialize this value as constant string, without calling destructor. + void SetStringRaw(StringRefType s) RAPIDJSON_NOEXCEPT { + data_.f.flags = kConstStringFlag; + SetStringPointer(s); + data_.s.length = s.length; + } + + //! Initialize this value as copy string with initial data, without calling destructor. + void SetStringRaw(StringRefType s, Allocator& allocator) { + Ch* str = 0; + if (ShortString::Usable(s.length)) { + data_.f.flags = kShortStringFlag; + data_.ss.SetLength(s.length); + str = data_.ss.str; + } else { + data_.f.flags = kCopyStringFlag; + data_.s.length = s.length; + str = static_cast(allocator.Malloc((s.length + 1) * sizeof(Ch))); + SetStringPointer(str); + } + std::memcpy(str, s, s.length * sizeof(Ch)); + str[s.length] = '\0'; + } + + //! Assignment without calling destructor + void RawAssign(GenericValue& rhs) RAPIDJSON_NOEXCEPT { + data_ = rhs.data_; + // data_.f.flags = rhs.data_.f.flags; + rhs.data_.f.flags = kNullFlag; + } + + template + bool StringEqual(const GenericValue& rhs) const { + RAPIDJSON_ASSERT(IsString()); + RAPIDJSON_ASSERT(rhs.IsString()); + + const SizeType len1 = GetStringLength(); + const SizeType len2 = rhs.GetStringLength(); + if(len1 != len2) { return false; } + + const Ch* const str1 = GetString(); + const Ch* const str2 = rhs.GetString(); + if(str1 == str2) { return true; } // fast path for constant string + + return (std::memcmp(str1, str2, sizeof(Ch) * len1) == 0); + } + + Data data_; +}; + +//! GenericValue with UTF8 encoding +typedef GenericValue > Value; + +/////////////////////////////////////////////////////////////////////////////// +// GenericDocument + +//! A document for parsing JSON text as DOM. +/*! + \note implements Handler concept + \tparam Encoding Encoding for both parsing and string storage. + \tparam Allocator Allocator for allocating memory for the DOM + \tparam StackAllocator Allocator for allocating memory for stack during parsing. + \warning Although GenericDocument inherits from GenericValue, the API does \b not provide any virtual functions, especially no virtual destructor. To avoid memory leaks, do not \c delete a GenericDocument object via a pointer to a GenericValue. +*/ +template , typename StackAllocator = CrtAllocator> +class GenericDocument : public GenericValue { +public: + typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. + typedef GenericValue ValueType; //!< Value type of the document. + typedef Allocator AllocatorType; //!< Allocator type from template parameter. + + //! Constructor + /*! Creates an empty document of specified type. + \param type Mandatory type of object to create. + \param allocator Optional allocator for allocating memory. + \param stackCapacity Optional initial capacity of stack in bytes. + \param stackAllocator Optional allocator for allocating memory for stack. + */ + explicit GenericDocument(Type type, Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) : + GenericValue(type), allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() + { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + } + + //! Constructor + /*! Creates an empty document which type is Null. + \param allocator Optional allocator for allocating memory. + \param stackCapacity Optional initial capacity of stack in bytes. + \param stackAllocator Optional allocator for allocating memory for stack. + */ + GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) : + allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() + { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericDocument(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT + : ValueType(std::forward(rhs)), // explicit cast to avoid prohibited move from Document + allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + stack_(std::move(rhs.stack_)), + parseResult_(rhs.parseResult_) + { + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.parseResult_ = ParseResult(); + } +#endif + + ~GenericDocument() { + Destroy(); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move assignment in C++11 + GenericDocument& operator=(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT + { + // The cast to ValueType is necessary here, because otherwise it would + // attempt to call GenericValue's templated assignment operator. + ValueType::operator=(std::forward(rhs)); + + // Calling the destructor here would prematurely call stack_'s destructor + Destroy(); + + allocator_ = rhs.allocator_; + ownAllocator_ = rhs.ownAllocator_; + stack_ = std::move(rhs.stack_); + parseResult_ = rhs.parseResult_; + + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.parseResult_ = ParseResult(); + + return *this; + } +#endif + + //! Exchange the contents of this document with those of another. + /*! + \param rhs Another document. + \note Constant complexity. + \see GenericValue::Swap + */ + GenericDocument& Swap(GenericDocument& rhs) RAPIDJSON_NOEXCEPT { + ValueType::Swap(rhs); + stack_.Swap(rhs.stack_); + internal::Swap(allocator_, rhs.allocator_); + internal::Swap(ownAllocator_, rhs.ownAllocator_); + internal::Swap(parseResult_, rhs.parseResult_); + return *this; + } + + //! free-standing swap function helper + /*! + Helper function to enable support for common swap implementation pattern based on \c std::swap: + \code + void swap(MyClass& a, MyClass& b) { + using std::swap; + swap(a.doc, b.doc); + // ... + } + \endcode + \see Swap() + */ + friend inline void swap(GenericDocument& a, GenericDocument& b) RAPIDJSON_NOEXCEPT { a.Swap(b); } + + //! Populate this document by a generator which produces SAX events. + /*! \tparam Generator A functor with bool f(Handler) prototype. + \param g Generator functor which sends SAX events to the parameter. + \return The document itself for fluent API. + */ + template + GenericDocument& Populate(Generator& g) { + ClearStackOnExit scope(*this); + if (g(*this)) { + RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object + ValueType::operator=(*stack_.template Pop(1));// Move value from stack to document + } + return *this; + } + + //!@name Parse from stream + //!@{ + + //! Parse JSON text from an input stream (with Encoding conversion) + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam SourceEncoding Encoding of input stream + \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + GenericReader reader( + stack_.HasAllocator() ? &stack_.GetAllocator() : 0); + ClearStackOnExit scope(*this); + parseResult_ = reader.template Parse(is, *this); + if (parseResult_) { + RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object + ValueType::operator=(*stack_.template Pop(1));// Move value from stack to document + } + return *this; + } + + //! Parse JSON text from an input stream + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + return ParseStream(is); + } + + //! Parse JSON text from an input stream (with \ref kParseDefaultFlags) + /*! \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + return ParseStream(is); + } + //!@} + + //!@name Parse in-place from mutable string + //!@{ + + //! Parse JSON text from a mutable string + /*! \tparam parseFlags Combination of \ref ParseFlag. + \param str Mutable zero-terminated string to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseInsitu(Ch* str) { + GenericInsituStringStream s(str); + return ParseStream(s); + } + + //! Parse JSON text from a mutable string (with \ref kParseDefaultFlags) + /*! \param str Mutable zero-terminated string to be parsed. + \return The document itself for fluent API. + */ + GenericDocument& ParseInsitu(Ch* str) { + return ParseInsitu(str); + } + //!@} + + //!@name Parse from read-only string + //!@{ + + //! Parse JSON text from a read-only string (with Encoding conversion) + /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag). + \tparam SourceEncoding Transcoding from input Encoding + \param str Read-only zero-terminated string to be parsed. + */ + template + GenericDocument& Parse(const typename SourceEncoding::Ch* str) { + RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag)); + GenericStringStream s(str); + return ParseStream(s); + } + + //! Parse JSON text from a read-only string + /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag). + \param str Read-only zero-terminated string to be parsed. + */ + template + GenericDocument& Parse(const Ch* str) { + return Parse(str); + } + + //! Parse JSON text from a read-only string (with \ref kParseDefaultFlags) + /*! \param str Read-only zero-terminated string to be parsed. + */ + GenericDocument& Parse(const Ch* str) { + return Parse(str); + } + + template + GenericDocument& Parse(const typename SourceEncoding::Ch* str, size_t length) { + RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag)); + MemoryStream ms(static_cast(str), length * sizeof(typename SourceEncoding::Ch)); + EncodedInputStream is(ms); + ParseStream(is); + return *this; + } + + template + GenericDocument& Parse(const Ch* str, size_t length) { + return Parse(str, length); + } + + GenericDocument& Parse(const Ch* str, size_t length) { + return Parse(str, length); + } + +#if RAPIDJSON_HAS_STDSTRING + template + GenericDocument& Parse(const std::basic_string& str) { + // c_str() is constant complexity according to standard. Should be faster than Parse(const char*, size_t) + return Parse(str.c_str()); + } + + template + GenericDocument& Parse(const std::basic_string& str) { + return Parse(str.c_str()); + } + + GenericDocument& Parse(const std::basic_string& str) { + return Parse(str); + } +#endif // RAPIDJSON_HAS_STDSTRING + + //!@} + + //!@name Handling parse errors + //!@{ + + //! Whether a parse error has occured in the last parsing. + bool HasParseError() const { return parseResult_.IsError(); } + + //! Get the \ref ParseErrorCode of last parsing. + ParseErrorCode GetParseError() const { return parseResult_.Code(); } + + //! Get the position of last parsing error in input, 0 otherwise. + size_t GetErrorOffset() const { return parseResult_.Offset(); } + + //! Implicit conversion to get the last parse result +#ifndef __clang // -Wdocumentation + /*! \return \ref ParseResult of the last parse operation + + \code + Document doc; + ParseResult ok = doc.Parse(json); + if (!ok) + printf( "JSON parse error: %s (%u)\n", GetParseError_En(ok.Code()), ok.Offset()); + \endcode + */ +#endif + operator ParseResult() const { return parseResult_; } + //!@} + + //! Get the allocator of this document. + Allocator& GetAllocator() { + RAPIDJSON_ASSERT(allocator_); + return *allocator_; + } + + //! Get the capacity of stack in bytes. + size_t GetStackCapacity() const { return stack_.GetCapacity(); } + +private: + // clear stack on any exit from ParseStream, e.g. due to exception + struct ClearStackOnExit { + explicit ClearStackOnExit(GenericDocument& d) : d_(d) {} + ~ClearStackOnExit() { d_.ClearStack(); } + private: + ClearStackOnExit(const ClearStackOnExit&); + ClearStackOnExit& operator=(const ClearStackOnExit&); + GenericDocument& d_; + }; + + // callers of the following private Handler functions + // template friend class GenericReader; // for parsing + template friend class GenericValue; // for deep copying + +public: + // Implementation of Handler + bool Null() { new (stack_.template Push()) ValueType(); return true; } + bool Bool(bool b) { new (stack_.template Push()) ValueType(b); return true; } + bool Int(int i) { new (stack_.template Push()) ValueType(i); return true; } + bool Uint(unsigned i) { new (stack_.template Push()) ValueType(i); return true; } + bool Int64(int64_t i) { new (stack_.template Push()) ValueType(i); return true; } + bool Uint64(uint64_t i) { new (stack_.template Push()) ValueType(i); return true; } + bool Double(double d) { new (stack_.template Push()) ValueType(d); return true; } + + bool RawNumber(const Ch* str, SizeType length, bool copy) { + if (copy) + new (stack_.template Push()) ValueType(str, length, GetAllocator()); + else + new (stack_.template Push()) ValueType(str, length); + return true; + } + + bool String(const Ch* str, SizeType length, bool copy) { + if (copy) + new (stack_.template Push()) ValueType(str, length, GetAllocator()); + else + new (stack_.template Push()) ValueType(str, length); + return true; + } + + bool StartObject() { new (stack_.template Push()) ValueType(kObjectType); return true; } + + bool Key(const Ch* str, SizeType length, bool copy) { return String(str, length, copy); } + + bool EndObject(SizeType memberCount) { + typename ValueType::Member* members = stack_.template Pop(memberCount); + stack_.template Top()->SetObjectRaw(members, memberCount, GetAllocator()); + return true; + } + + bool StartArray() { new (stack_.template Push()) ValueType(kArrayType); return true; } + + bool EndArray(SizeType elementCount) { + ValueType* elements = stack_.template Pop(elementCount); + stack_.template Top()->SetArrayRaw(elements, elementCount, GetAllocator()); + return true; + } + +private: + //! Prohibit copying + GenericDocument(const GenericDocument&); + //! Prohibit assignment + GenericDocument& operator=(const GenericDocument&); + + void ClearStack() { + if (Allocator::kNeedFree) + while (stack_.GetSize() > 0) // Here assumes all elements in stack array are GenericValue (Member is actually 2 GenericValue objects) + (stack_.template Pop(1))->~ValueType(); + else + stack_.Clear(); + stack_.ShrinkToFit(); + } + + void Destroy() { + RAPIDJSON_DELETE(ownAllocator_); + } + + static const size_t kDefaultStackCapacity = 1024; + Allocator* allocator_; + Allocator* ownAllocator_; + internal::Stack stack_; + ParseResult parseResult_; +}; + +//! GenericDocument with UTF8 encoding +typedef GenericDocument > Document; + +// defined here due to the dependency on GenericDocument +template +template +inline +GenericValue::GenericValue(const GenericValue& rhs, Allocator& allocator) +{ + switch (rhs.GetType()) { + case kObjectType: + case kArrayType: { // perform deep copy via SAX Handler + GenericDocument d(&allocator); + rhs.Accept(d); + RawAssign(*d.stack_.template Pop(1)); + } + break; + case kStringType: + if (rhs.data_.f.flags == kConstStringFlag) { + data_.f.flags = rhs.data_.f.flags; + data_ = *reinterpret_cast(&rhs.data_); + } else { + SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator); + } + break; + default: + data_.f.flags = rhs.data_.f.flags; + data_ = *reinterpret_cast(&rhs.data_); + break; + } +} + +//! Helper class for accessing Value of array type. +/*! + Instance of this helper class is obtained by \c GenericValue::GetArray(). + In addition to all APIs for array type, it provides range-based for loop if \c RAPIDJSON_HAS_CXX11_RANGE_FOR=1. +*/ +template +class GenericArray { +public: + typedef GenericArray ConstArray; + typedef GenericArray Array; + typedef ValueT PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + typedef ValueType* ValueIterator; // This may be const or non-const iterator + typedef const ValueT* ConstValueIterator; + typedef typename ValueType::AllocatorType AllocatorType; + typedef typename ValueType::StringRefType StringRefType; + + template + friend class GenericValue; + + GenericArray(const GenericArray& rhs) : value_(rhs.value_) {} + GenericArray& operator=(const GenericArray& rhs) { value_ = rhs.value_; return *this; } + ~GenericArray() {} + + SizeType Size() const { return value_.Size(); } + SizeType Capacity() const { return value_.Capacity(); } + bool Empty() const { return value_.Empty(); } + void Clear() const { value_.Clear(); } + ValueType& operator[](SizeType index) const { return value_[index]; } + ValueIterator Begin() const { return value_.Begin(); } + ValueIterator End() const { return value_.End(); } + GenericArray Reserve(SizeType newCapacity, AllocatorType &allocator) const { value_.Reserve(newCapacity, allocator); return *this; } + GenericArray PushBack(ValueType& value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericArray PushBack(ValueType&& value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericArray PushBack(StringRefType value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (const GenericArray&)) PushBack(T value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } + GenericArray PopBack() const { value_.PopBack(); return *this; } + ValueIterator Erase(ConstValueIterator pos) const { return value_.Erase(pos); } + ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) const { return value_.Erase(first, last); } + +#if RAPIDJSON_HAS_CXX11_RANGE_FOR + ValueIterator begin() const { return value_.Begin(); } + ValueIterator end() const { return value_.End(); } +#endif + +private: + GenericArray(); + GenericArray(ValueType& value) : value_(value) {} + ValueType& value_; +}; + +//! Helper class for accessing Value of object type. +/*! + Instance of this helper class is obtained by \c GenericValue::GetObject(). + In addition to all APIs for array type, it provides range-based for loop if \c RAPIDJSON_HAS_CXX11_RANGE_FOR=1. +*/ +template +class GenericObject { +public: + typedef GenericObject ConstObject; + typedef GenericObject Object; + typedef ValueT PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + typedef GenericMemberIterator MemberIterator; // This may be const or non-const iterator + typedef GenericMemberIterator ConstMemberIterator; + typedef typename ValueType::AllocatorType AllocatorType; + typedef typename ValueType::StringRefType StringRefType; + typedef typename ValueType::EncodingType EncodingType; + typedef typename ValueType::Ch Ch; + + template + friend class GenericValue; + + GenericObject(const GenericObject& rhs) : value_(rhs.value_) {} + GenericObject& operator=(const GenericObject& rhs) { value_ = rhs.value_; return *this; } + ~GenericObject() {} + + SizeType MemberCount() const { return value_.MemberCount(); } + bool ObjectEmpty() const { return value_.ObjectEmpty(); } + template ValueType& operator[](T* name) const { return value_[name]; } + template ValueType& operator[](const GenericValue& name) const { return value_[name]; } +#if RAPIDJSON_HAS_STDSTRING + ValueType& operator[](const std::basic_string& name) const { return value_[name]; } +#endif + MemberIterator MemberBegin() const { return value_.MemberBegin(); } + MemberIterator MemberEnd() const { return value_.MemberEnd(); } + bool HasMember(const Ch* name) const { return value_.HasMember(name); } +#if RAPIDJSON_HAS_STDSTRING + bool HasMember(const std::basic_string& name) const { return value_.HasMember(name); } +#endif + template bool HasMember(const GenericValue& name) const { return value_.HasMember(name); } + MemberIterator FindMember(const Ch* name) const { return value_.FindMember(name); } + template MemberIterator FindMember(const GenericValue& name) const { return value_.FindMember(name); } +#if RAPIDJSON_HAS_STDSTRING + MemberIterator FindMember(const std::basic_string& name) const { return value_.FindMember(name); } +#endif + GenericObject AddMember(ValueType& name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(ValueType& name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#if RAPIDJSON_HAS_STDSTRING + GenericObject AddMember(ValueType& name, std::basic_string& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#endif + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) AddMember(ValueType& name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericObject AddMember(ValueType&& name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(ValueType&& name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(ValueType& name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(StringRefType name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericObject AddMember(StringRefType name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(StringRefType name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericObject)) AddMember(StringRefType name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + void RemoveAllMembers() { return value_.RemoveAllMembers(); } + bool RemoveMember(const Ch* name) const { return value_.RemoveMember(name); } +#if RAPIDJSON_HAS_STDSTRING + bool RemoveMember(const std::basic_string& name) const { return value_.RemoveMember(name); } +#endif + template bool RemoveMember(const GenericValue& name) const { return value_.RemoveMember(name); } + MemberIterator RemoveMember(MemberIterator m) const { return value_.RemoveMember(m); } + MemberIterator EraseMember(ConstMemberIterator pos) const { return value_.EraseMember(pos); } + MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last) const { return value_.EraseMember(first, last); } + bool EraseMember(const Ch* name) const { return value_.EraseMember(name); } +#if RAPIDJSON_HAS_STDSTRING + bool EraseMember(const std::basic_string& name) const { return EraseMember(ValueType(StringRef(name))); } +#endif + template bool EraseMember(const GenericValue& name) const { return value_.EraseMember(name); } + +#if RAPIDJSON_HAS_CXX11_RANGE_FOR + MemberIterator begin() const { return value_.MemberBegin(); } + MemberIterator end() const { return value_.MemberEnd(); } +#endif + +private: + GenericObject(); + GenericObject(ValueType& value) : value_(value) {} + ValueType& value_; +}; + +RAPIDJSON_NAMESPACE_END +RAPIDJSON_DIAG_POP + +#endif // RAPIDJSON_DOCUMENT_H_ diff --git a/rapidjson-1.1.0/rapid_json/encodedstream.h b/rapidjson-1.1.0/rapid_json/encodedstream.h new file mode 100644 index 000000000..145068386 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/encodedstream.h @@ -0,0 +1,299 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ENCODEDSTREAM_H_ +#define RAPIDJSON_ENCODEDSTREAM_H_ + +#include "stream.h" +#include "memorystream.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Input byte stream wrapper with a statically bound encoding. +/*! + \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. + \tparam InputByteStream Type of input byte stream. For example, FileReadStream. +*/ +template +class EncodedInputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); +public: + typedef typename Encoding::Ch Ch; + + EncodedInputStream(InputByteStream& is) : is_(is) { + current_ = Encoding::TakeBOM(is_); + } + + Ch Peek() const { return current_; } + Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; } + size_t Tell() const { return is_.Tell(); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + EncodedInputStream(const EncodedInputStream&); + EncodedInputStream& operator=(const EncodedInputStream&); + + InputByteStream& is_; + Ch current_; +}; + +//! Specialized for UTF8 MemoryStream. +template <> +class EncodedInputStream, MemoryStream> { +public: + typedef UTF8<>::Ch Ch; + + EncodedInputStream(MemoryStream& is) : is_(is) { + if (static_cast(is_.Peek()) == 0xEFu) is_.Take(); + if (static_cast(is_.Peek()) == 0xBBu) is_.Take(); + if (static_cast(is_.Peek()) == 0xBFu) is_.Take(); + } + Ch Peek() const { return is_.Peek(); } + Ch Take() { return is_.Take(); } + size_t Tell() const { return is_.Tell(); } + + // Not implemented + void Put(Ch) {} + void Flush() {} + Ch* PutBegin() { return 0; } + size_t PutEnd(Ch*) { return 0; } + + MemoryStream& is_; + +private: + EncodedInputStream(const EncodedInputStream&); + EncodedInputStream& operator=(const EncodedInputStream&); +}; + +//! Output byte stream wrapper with statically bound encoding. +/*! + \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. + \tparam OutputByteStream Type of input byte stream. For example, FileWriteStream. +*/ +template +class EncodedOutputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); +public: + typedef typename Encoding::Ch Ch; + + EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) { + if (putBOM) + Encoding::PutBOM(os_); + } + + void Put(Ch c) { Encoding::Put(os_, c); } + void Flush() { os_.Flush(); } + + // Not implemented + Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;} + Ch Take() { RAPIDJSON_ASSERT(false); return 0;} + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + EncodedOutputStream(const EncodedOutputStream&); + EncodedOutputStream& operator=(const EncodedOutputStream&); + + OutputByteStream& os_; +}; + +#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x + +//! Input stream wrapper with dynamically bound encoding and automatic encoding detection. +/*! + \tparam CharType Type of character for reading. + \tparam InputByteStream type of input byte stream to be wrapped. +*/ +template +class AutoUTFInputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); +public: + typedef CharType Ch; + + //! Constructor. + /*! + \param is input stream to be wrapped. + \param type UTF encoding type if it is not detected from the stream. + */ + AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) { + RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); + DetectType(); + static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) }; + takeFunc_ = f[type_]; + current_ = takeFunc_(*is_); + } + + UTFType GetType() const { return type_; } + bool HasBOM() const { return hasBOM_; } + + Ch Peek() const { return current_; } + Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; } + size_t Tell() const { return is_->Tell(); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + AutoUTFInputStream(const AutoUTFInputStream&); + AutoUTFInputStream& operator=(const AutoUTFInputStream&); + + // Detect encoding type with BOM or RFC 4627 + void DetectType() { + // BOM (Byte Order Mark): + // 00 00 FE FF UTF-32BE + // FF FE 00 00 UTF-32LE + // FE FF UTF-16BE + // FF FE UTF-16LE + // EF BB BF UTF-8 + + const unsigned char* c = reinterpret_cast(is_->Peek4()); + if (!c) + return; + + unsigned bom = static_cast(c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24)); + hasBOM_ = false; + if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } + else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } + else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take(); } + else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take(); } + else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); } + + // RFC 4627: Section 3 + // "Since the first two characters of a JSON text will always be ASCII + // characters [RFC0020], it is possible to determine whether an octet + // stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking + // at the pattern of nulls in the first four octets." + // 00 00 00 xx UTF-32BE + // 00 xx 00 xx UTF-16BE + // xx 00 00 00 UTF-32LE + // xx 00 xx 00 UTF-16LE + // xx xx xx xx UTF-8 + + if (!hasBOM_) { + unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0); + switch (pattern) { + case 0x08: type_ = kUTF32BE; break; + case 0x0A: type_ = kUTF16BE; break; + case 0x01: type_ = kUTF32LE; break; + case 0x05: type_ = kUTF16LE; break; + case 0x0F: type_ = kUTF8; break; + default: break; // Use type defined by user. + } + } + + // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. + if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); + if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); + } + + typedef Ch (*TakeFunc)(InputByteStream& is); + InputByteStream* is_; + UTFType type_; + Ch current_; + TakeFunc takeFunc_; + bool hasBOM_; +}; + +//! Output stream wrapper with dynamically bound encoding and automatic encoding detection. +/*! + \tparam CharType Type of character for writing. + \tparam OutputByteStream type of output byte stream to be wrapped. +*/ +template +class AutoUTFOutputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); +public: + typedef CharType Ch; + + //! Constructor. + /*! + \param os output stream to be wrapped. + \param type UTF encoding type. + \param putBOM Whether to write BOM at the beginning of the stream. + */ + AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) { + RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); + + // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. + if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); + if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); + + static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) }; + putFunc_ = f[type_]; + + if (putBOM) + PutBOM(); + } + + UTFType GetType() const { return type_; } + + void Put(Ch c) { putFunc_(*os_, c); } + void Flush() { os_->Flush(); } + + // Not implemented + Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;} + Ch Take() { RAPIDJSON_ASSERT(false); return 0;} + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + AutoUTFOutputStream(const AutoUTFOutputStream&); + AutoUTFOutputStream& operator=(const AutoUTFOutputStream&); + + void PutBOM() { + typedef void (*PutBOMFunc)(OutputByteStream&); + static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) }; + f[type_](*os_); + } + + typedef void (*PutFunc)(OutputByteStream&, Ch); + + OutputByteStream* os_; + UTFType type_; + PutFunc putFunc_; +}; + +#undef RAPIDJSON_ENCODINGS_FUNC + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/rapidjson-1.1.0/rapid_json/encodings.h b/rapidjson-1.1.0/rapid_json/encodings.h new file mode 100644 index 000000000..baa7c2b17 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/encodings.h @@ -0,0 +1,716 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ENCODINGS_H_ +#define RAPIDJSON_ENCODINGS_H_ + +#include "rapidjson.h" + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4244) // conversion from 'type1' to 'type2', possible loss of data +RAPIDJSON_DIAG_OFF(4702) // unreachable code +#elif defined(__GNUC__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +RAPIDJSON_DIAG_OFF(overflow) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Encoding + +/*! \class rapidjson::Encoding + \brief Concept for encoding of Unicode characters. + +\code +concept Encoding { + typename Ch; //! Type of character. A "character" is actually a code unit in unicode's definition. + + enum { supportUnicode = 1 }; // or 0 if not supporting unicode + + //! \brief Encode a Unicode codepoint to an output stream. + //! \param os Output stream. + //! \param codepoint An unicode codepoint, ranging from 0x0 to 0x10FFFF inclusively. + template + static void Encode(OutputStream& os, unsigned codepoint); + + //! \brief Decode a Unicode codepoint from an input stream. + //! \param is Input stream. + //! \param codepoint Output of the unicode codepoint. + //! \return true if a valid codepoint can be decoded from the stream. + template + static bool Decode(InputStream& is, unsigned* codepoint); + + //! \brief Validate one Unicode codepoint from an encoded stream. + //! \param is Input stream to obtain codepoint. + //! \param os Output for copying one codepoint. + //! \return true if it is valid. + //! \note This function just validating and copying the codepoint without actually decode it. + template + static bool Validate(InputStream& is, OutputStream& os); + + // The following functions are deal with byte streams. + + //! Take a character from input byte stream, skip BOM if exist. + template + static CharType TakeBOM(InputByteStream& is); + + //! Take a character from input byte stream. + template + static Ch Take(InputByteStream& is); + + //! Put BOM to output byte stream. + template + static void PutBOM(OutputByteStream& os); + + //! Put a character to output byte stream. + template + static void Put(OutputByteStream& os, Ch c); +}; +\endcode +*/ + +/////////////////////////////////////////////////////////////////////////////// +// UTF8 + +//! UTF-8 encoding. +/*! http://en.wikipedia.org/wiki/UTF-8 + http://tools.ietf.org/html/rfc3629 + \tparam CharType Code unit for storing 8-bit UTF-8 data. Default is char. + \note implements Encoding concept +*/ +template +struct UTF8 { + typedef CharType Ch; + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + if (codepoint <= 0x7F) + os.Put(static_cast(codepoint & 0xFF)); + else if (codepoint <= 0x7FF) { + os.Put(static_cast(0xC0 | ((codepoint >> 6) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint & 0x3F)))); + } + else if (codepoint <= 0xFFFF) { + os.Put(static_cast(0xE0 | ((codepoint >> 12) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + os.Put(static_cast(0x80 | (codepoint & 0x3F))); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + os.Put(static_cast(0xF0 | ((codepoint >> 18) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint >> 12) & 0x3F))); + os.Put(static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + os.Put(static_cast(0x80 | (codepoint & 0x3F))); + } + } + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + if (codepoint <= 0x7F) + PutUnsafe(os, static_cast(codepoint & 0xFF)); + else if (codepoint <= 0x7FF) { + PutUnsafe(os, static_cast(0xC0 | ((codepoint >> 6) & 0xFF))); + PutUnsafe(os, static_cast(0x80 | ((codepoint & 0x3F)))); + } + else if (codepoint <= 0xFFFF) { + PutUnsafe(os, static_cast(0xE0 | ((codepoint >> 12) & 0xFF))); + PutUnsafe(os, static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + PutUnsafe(os, static_cast(0x80 | (codepoint & 0x3F))); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + PutUnsafe(os, static_cast(0xF0 | ((codepoint >> 18) & 0xFF))); + PutUnsafe(os, static_cast(0x80 | ((codepoint >> 12) & 0x3F))); + PutUnsafe(os, static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + PutUnsafe(os, static_cast(0x80 | (codepoint & 0x3F))); + } + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { +#define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | (static_cast(c) & 0x3Fu) +#define TRANS(mask) result &= ((GetRange(static_cast(c)) & mask) != 0) +#define TAIL() COPY(); TRANS(0x70) + typename InputStream::Ch c = is.Take(); + if (!(c & 0x80)) { + *codepoint = static_cast(c); + return true; + } + + unsigned char type = GetRange(static_cast(c)); + if (type >= 32) { + *codepoint = 0; + } else { + *codepoint = (0xFF >> type) & static_cast(c); + } + bool result = true; + switch (type) { + case 2: TAIL(); return result; + case 3: TAIL(); TAIL(); return result; + case 4: COPY(); TRANS(0x50); TAIL(); return result; + case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; + case 6: TAIL(); TAIL(); TAIL(); return result; + case 10: COPY(); TRANS(0x20); TAIL(); return result; + case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; + default: return false; + } +#undef COPY +#undef TRANS +#undef TAIL + } + + template + static bool Validate(InputStream& is, OutputStream& os) { +#define COPY() os.Put(c = is.Take()) +#define TRANS(mask) result &= ((GetRange(static_cast(c)) & mask) != 0) +#define TAIL() COPY(); TRANS(0x70) + Ch c; + COPY(); + if (!(c & 0x80)) + return true; + + bool result = true; + switch (GetRange(static_cast(c))) { + case 2: TAIL(); return result; + case 3: TAIL(); TAIL(); return result; + case 4: COPY(); TRANS(0x50); TAIL(); return result; + case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; + case 6: TAIL(); TAIL(); TAIL(); return result; + case 10: COPY(); TRANS(0x20); TAIL(); return result; + case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; + default: return false; + } +#undef COPY +#undef TRANS +#undef TAIL + } + + static unsigned char GetRange(unsigned char c) { + // Referring to DFA of http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ + // With new mapping 1 -> 0x10, 7 -> 0x20, 9 -> 0x40, such that AND operation can test multiple types. + static const unsigned char type[] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10, + 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40, + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, + 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, + 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, + }; + return type[c]; + } + + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + typename InputByteStream::Ch c = Take(is); + if (static_cast(c) != 0xEFu) return c; + c = is.Take(); + if (static_cast(c) != 0xBBu) return c; + c = is.Take(); + if (static_cast(c) != 0xBFu) return c; + c = is.Take(); + return c; + } + + template + static Ch Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + return static_cast(is.Take()); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xEFu)); + os.Put(static_cast(0xBBu)); + os.Put(static_cast(0xBFu)); + } + + template + static void Put(OutputByteStream& os, Ch c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// UTF16 + +//! UTF-16 encoding. +/*! http://en.wikipedia.org/wiki/UTF-16 + http://tools.ietf.org/html/rfc2781 + \tparam CharType Type for storing 16-bit UTF-16 data. Default is wchar_t. C++11 may use char16_t instead. + \note implements Encoding concept + + \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. + For streaming, use UTF16LE and UTF16BE, which handle endianness. +*/ +template +struct UTF16 { + typedef CharType Ch; + RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 2); + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + if (codepoint <= 0xFFFF) { + RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair + os.Put(static_cast(codepoint)); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + unsigned v = codepoint - 0x10000; + os.Put(static_cast((v >> 10) | 0xD800)); + os.Put((v & 0x3FF) | 0xDC00); + } + } + + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + if (codepoint <= 0xFFFF) { + RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair + PutUnsafe(os, static_cast(codepoint)); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + unsigned v = codepoint - 0x10000; + PutUnsafe(os, static_cast((v >> 10) | 0xD800)); + PutUnsafe(os, (v & 0x3FF) | 0xDC00); + } + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); + typename InputStream::Ch c = is.Take(); + if (c < 0xD800 || c > 0xDFFF) { + *codepoint = static_cast(c); + return true; + } + else if (c <= 0xDBFF) { + *codepoint = (static_cast(c) & 0x3FF) << 10; + c = is.Take(); + *codepoint |= (static_cast(c) & 0x3FF); + *codepoint += 0x10000; + return c >= 0xDC00 && c <= 0xDFFF; + } + return false; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + typename InputStream::Ch c; + os.Put(static_cast(c = is.Take())); + if (c < 0xD800 || c > 0xDFFF) + return true; + else if (c <= 0xDBFF) { + os.Put(c = is.Take()); + return c >= 0xDC00 && c <= 0xDFFF; + } + return false; + } +}; + +//! UTF-16 little endian encoding. +template +struct UTF16LE : UTF16 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0xFEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(is.Take()); + c |= static_cast(static_cast(is.Take())) << 8; + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xFFu)); + os.Put(static_cast(0xFEu)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(static_cast(c) & 0xFFu)); + os.Put(static_cast((static_cast(c) >> 8) & 0xFFu)); + } +}; + +//! UTF-16 big endian encoding. +template +struct UTF16BE : UTF16 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0xFEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(static_cast(is.Take())) << 8; + c |= static_cast(is.Take()); + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xFEu)); + os.Put(static_cast(0xFFu)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast((static_cast(c) >> 8) & 0xFFu)); + os.Put(static_cast(static_cast(c) & 0xFFu)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// UTF32 + +//! UTF-32 encoding. +/*! http://en.wikipedia.org/wiki/UTF-32 + \tparam CharType Type for storing 32-bit UTF-32 data. Default is unsigned. C++11 may use char32_t instead. + \note implements Encoding concept + + \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. + For streaming, use UTF32LE and UTF32BE, which handle endianness. +*/ +template +struct UTF32 { + typedef CharType Ch; + RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 4); + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + os.Put(codepoint); + } + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + PutUnsafe(os, codepoint); + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); + Ch c = is.Take(); + *codepoint = c; + return c <= 0x10FFFF; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); + Ch c; + os.Put(c = is.Take()); + return c <= 0x10FFFF; + } +}; + +//! UTF-32 little endian enocoding. +template +struct UTF32LE : UTF32 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0x0000FEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(is.Take()); + c |= static_cast(static_cast(is.Take())) << 8; + c |= static_cast(static_cast(is.Take())) << 16; + c |= static_cast(static_cast(is.Take())) << 24; + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xFFu)); + os.Put(static_cast(0xFEu)); + os.Put(static_cast(0x00u)); + os.Put(static_cast(0x00u)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c & 0xFFu)); + os.Put(static_cast((c >> 8) & 0xFFu)); + os.Put(static_cast((c >> 16) & 0xFFu)); + os.Put(static_cast((c >> 24) & 0xFFu)); + } +}; + +//! UTF-32 big endian encoding. +template +struct UTF32BE : UTF32 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0x0000FEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(static_cast(is.Take())) << 24; + c |= static_cast(static_cast(is.Take())) << 16; + c |= static_cast(static_cast(is.Take())) << 8; + c |= static_cast(static_cast(is.Take())); + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0x00u)); + os.Put(static_cast(0x00u)); + os.Put(static_cast(0xFEu)); + os.Put(static_cast(0xFFu)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast((c >> 24) & 0xFFu)); + os.Put(static_cast((c >> 16) & 0xFFu)); + os.Put(static_cast((c >> 8) & 0xFFu)); + os.Put(static_cast(c & 0xFFu)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// ASCII + +//! ASCII encoding. +/*! http://en.wikipedia.org/wiki/ASCII + \tparam CharType Code unit for storing 7-bit ASCII data. Default is char. + \note implements Encoding concept +*/ +template +struct ASCII { + typedef CharType Ch; + + enum { supportUnicode = 0 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_ASSERT(codepoint <= 0x7F); + os.Put(static_cast(codepoint & 0xFF)); + } + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + RAPIDJSON_ASSERT(codepoint <= 0x7F); + PutUnsafe(os, static_cast(codepoint & 0xFF)); + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + uint8_t c = static_cast(is.Take()); + *codepoint = c; + return c <= 0X7F; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + uint8_t c = static_cast(is.Take()); + os.Put(static_cast(c)); + return c <= 0x7F; + } + + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + uint8_t c = static_cast(Take(is)); + return static_cast(c); + } + + template + static Ch Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + return static_cast(is.Take()); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + (void)os; + } + + template + static void Put(OutputByteStream& os, Ch c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// AutoUTF + +//! Runtime-specified UTF encoding type of a stream. +enum UTFType { + kUTF8 = 0, //!< UTF-8. + kUTF16LE = 1, //!< UTF-16 little endian. + kUTF16BE = 2, //!< UTF-16 big endian. + kUTF32LE = 3, //!< UTF-32 little endian. + kUTF32BE = 4 //!< UTF-32 big endian. +}; + +//! Dynamically select encoding according to stream's runtime-specified UTF encoding type. +/*! \note This class can be used with AutoUTFInputtStream and AutoUTFOutputStream, which provides GetType(). +*/ +template +struct AutoUTF { + typedef CharType Ch; + + enum { supportUnicode = 1 }; + +#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x + + template + RAPIDJSON_FORCEINLINE static void Encode(OutputStream& os, unsigned codepoint) { + typedef void (*EncodeFunc)(OutputStream&, unsigned); + static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Encode) }; + (*f[os.GetType()])(os, codepoint); + } + + template + RAPIDJSON_FORCEINLINE static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + typedef void (*EncodeFunc)(OutputStream&, unsigned); + static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(EncodeUnsafe) }; + (*f[os.GetType()])(os, codepoint); + } + + template + RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) { + typedef bool (*DecodeFunc)(InputStream&, unsigned*); + static const DecodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Decode) }; + return (*f[is.GetType()])(is, codepoint); + } + + template + RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + typedef bool (*ValidateFunc)(InputStream&, OutputStream&); + static const ValidateFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Validate) }; + return (*f[is.GetType()])(is, os); + } + +#undef RAPIDJSON_ENCODINGS_FUNC +}; + +/////////////////////////////////////////////////////////////////////////////// +// Transcoder + +//! Encoding conversion. +template +struct Transcoder { + //! Take one Unicode codepoint from source encoding, convert it to target encoding and put it to the output stream. + template + RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { + unsigned codepoint; + if (!SourceEncoding::Decode(is, &codepoint)) + return false; + TargetEncoding::Encode(os, codepoint); + return true; + } + + template + RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) { + unsigned codepoint; + if (!SourceEncoding::Decode(is, &codepoint)) + return false; + TargetEncoding::EncodeUnsafe(os, codepoint); + return true; + } + + //! Validate one Unicode codepoint from an encoded stream. + template + RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + return Transcode(is, os); // Since source/target encoding is different, must transcode. + } +}; + +// Forward declaration. +template +inline void PutUnsafe(Stream& stream, typename Stream::Ch c); + +//! Specialization of Transcoder with same source and target encoding. +template +struct Transcoder { + template + RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { + os.Put(is.Take()); // Just copy one code unit. This semantic is different from primary template class. + return true; + } + + template + RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) { + PutUnsafe(os, is.Take()); // Just copy one code unit. This semantic is different from primary template class. + return true; + } + + template + RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + return Encoding::Validate(is, os); // source/target encoding are the same + } +}; + +RAPIDJSON_NAMESPACE_END + +#if defined(__GNUC__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ENCODINGS_H_ diff --git a/rapidjson-1.1.0/rapid_json/error/en.h b/rapidjson-1.1.0/rapid_json/error/en.h new file mode 100644 index 000000000..2db838bff --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/error/en.h @@ -0,0 +1,74 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ERROR_EN_H_ +#define RAPIDJSON_ERROR_EN_H_ + +#include "error.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(switch-enum) +RAPIDJSON_DIAG_OFF(covered-switch-default) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Maps error code of parsing into error message. +/*! + \ingroup RAPIDJSON_ERRORS + \param parseErrorCode Error code obtained in parsing. + \return the error message. + \note User can make a copy of this function for localization. + Using switch-case is safer for future modification of error codes. +*/ +inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErrorCode) { + switch (parseErrorCode) { + case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error."); + + case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty."); + case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not be followed by other values."); + + case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value."); + + case kParseErrorObjectMissName: return RAPIDJSON_ERROR_STRING("Missing a name for object member."); + case kParseErrorObjectMissColon: return RAPIDJSON_ERROR_STRING("Missing a colon after a name of object member."); + case kParseErrorObjectMissCommaOrCurlyBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or '}' after an object member."); + + case kParseErrorArrayMissCommaOrSquareBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or ']' after an array element."); + + case kParseErrorStringUnicodeEscapeInvalidHex: return RAPIDJSON_ERROR_STRING("Incorrect hex digit after \\u escape in string."); + case kParseErrorStringUnicodeSurrogateInvalid: return RAPIDJSON_ERROR_STRING("The surrogate pair in string is invalid."); + case kParseErrorStringEscapeInvalid: return RAPIDJSON_ERROR_STRING("Invalid escape character in string."); + case kParseErrorStringMissQuotationMark: return RAPIDJSON_ERROR_STRING("Missing a closing quotation mark in string."); + case kParseErrorStringInvalidEncoding: return RAPIDJSON_ERROR_STRING("Invalid encoding in string."); + + case kParseErrorNumberTooBig: return RAPIDJSON_ERROR_STRING("Number too big to be stored in double."); + case kParseErrorNumberMissFraction: return RAPIDJSON_ERROR_STRING("Miss fraction part in number."); + case kParseErrorNumberMissExponent: return RAPIDJSON_ERROR_STRING("Miss exponent in number."); + + case kParseErrorTermination: return RAPIDJSON_ERROR_STRING("Terminate parsing due to Handler error."); + case kParseErrorUnspecificSyntaxError: return RAPIDJSON_ERROR_STRING("Unspecific syntax error."); + + default: return RAPIDJSON_ERROR_STRING("Unknown error."); + } +} + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ERROR_EN_H_ diff --git a/rapidjson-1.1.0/rapid_json/error/error.h b/rapidjson-1.1.0/rapid_json/error/error.h new file mode 100644 index 000000000..95cb31a72 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/error/error.h @@ -0,0 +1,155 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ERROR_ERROR_H_ +#define RAPIDJSON_ERROR_ERROR_H_ + +#include "../rapidjson.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +/*! \file error.h */ + +/*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */ + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ERROR_CHARTYPE + +//! Character type of error messages. +/*! \ingroup RAPIDJSON_ERRORS + The default character type is \c char. + On Windows, user can define this macro as \c TCHAR for supporting both + unicode/non-unicode settings. +*/ +#ifndef RAPIDJSON_ERROR_CHARTYPE +#define RAPIDJSON_ERROR_CHARTYPE char +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ERROR_STRING + +//! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[]. +/*! \ingroup RAPIDJSON_ERRORS + By default this conversion macro does nothing. + On Windows, user can define this macro as \c _T(x) for supporting both + unicode/non-unicode settings. +*/ +#ifndef RAPIDJSON_ERROR_STRING +#define RAPIDJSON_ERROR_STRING(x) x +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// ParseErrorCode + +//! Error code of parsing. +/*! \ingroup RAPIDJSON_ERRORS + \see GenericReader::Parse, GenericReader::GetParseErrorCode +*/ +enum ParseErrorCode { + kParseErrorNone = 0, //!< No error. + + kParseErrorDocumentEmpty, //!< The document is empty. + kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values. + + kParseErrorValueInvalid, //!< Invalid value. + + kParseErrorObjectMissName, //!< Missing a name for object member. + kParseErrorObjectMissColon, //!< Missing a colon after a name of object member. + kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member. + + kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element. + + kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string. + kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid. + kParseErrorStringEscapeInvalid, //!< Invalid escape character in string. + kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string. + kParseErrorStringInvalidEncoding, //!< Invalid encoding in string. + + kParseErrorNumberTooBig, //!< Number too big to be stored in double. + kParseErrorNumberMissFraction, //!< Miss fraction part in number. + kParseErrorNumberMissExponent, //!< Miss exponent in number. + + kParseErrorTermination, //!< Parsing was terminated. + kParseErrorUnspecificSyntaxError //!< Unspecific syntax error. +}; + +//! Result of parsing (wraps ParseErrorCode) +/*! + \ingroup RAPIDJSON_ERRORS + \code + Document doc; + ParseResult ok = doc.Parse("[42]"); + if (!ok) { + fprintf(stderr, "JSON parse error: %s (%u)", + GetParseError_En(ok.Code()), ok.Offset()); + exit(EXIT_FAILURE); + } + \endcode + \see GenericReader::Parse, GenericDocument::Parse +*/ +struct ParseResult { +public: + //! Default constructor, no error. + ParseResult() : code_(kParseErrorNone), offset_(0) {} + //! Constructor to set an error. + ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {} + + //! Get the error code. + ParseErrorCode Code() const { return code_; } + //! Get the error offset, if \ref IsError(), 0 otherwise. + size_t Offset() const { return offset_; } + + //! Conversion to \c bool, returns \c true, iff !\ref IsError(). + operator bool() const { return !IsError(); } + //! Whether the result is an error. + bool IsError() const { return code_ != kParseErrorNone; } + + bool operator==(const ParseResult& that) const { return code_ == that.code_; } + bool operator==(ParseErrorCode code) const { return code_ == code; } + friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; } + + //! Reset error code. + void Clear() { Set(kParseErrorNone); } + //! Update error code and offset. + void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; } + +private: + ParseErrorCode code_; + size_t offset_; +}; + +//! Function pointer type of GetParseError(). +/*! \ingroup RAPIDJSON_ERRORS + + This is the prototype for \c GetParseError_X(), where \c X is a locale. + User can dynamically change locale in runtime, e.g.: +\code + GetParseErrorFunc GetParseError = GetParseError_En; // or whatever + const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode()); +\endcode +*/ +typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode); + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ERROR_ERROR_H_ diff --git a/rapidjson-1.1.0/rapid_json/filereadstream.h b/rapidjson-1.1.0/rapid_json/filereadstream.h new file mode 100644 index 000000000..b56ea13b3 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/filereadstream.h @@ -0,0 +1,99 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FILEREADSTREAM_H_ +#define RAPIDJSON_FILEREADSTREAM_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(unreachable-code) +RAPIDJSON_DIAG_OFF(missing-noreturn) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! File byte stream for input using fread(). +/*! + \note implements Stream concept +*/ +class FileReadStream { +public: + typedef char Ch; //!< Character type (byte). + + //! Constructor. + /*! + \param fp File pointer opened for read. + \param buffer user-supplied buffer. + \param bufferSize size of buffer in bytes. Must >=4 bytes. + */ + FileReadStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { + RAPIDJSON_ASSERT(fp_ != 0); + RAPIDJSON_ASSERT(bufferSize >= 4); + Read(); + } + + Ch Peek() const { return *current_; } + Ch Take() { Ch c = *current_; Read(); return c; } + size_t Tell() const { return count_ + static_cast(current_ - buffer_); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + return (current_ + 4 <= bufferLast_) ? current_ : 0; + } + +private: + void Read() { + if (current_ < bufferLast_) + ++current_; + else if (!eof_) { + count_ += readCount_; + readCount_ = fread(buffer_, 1, bufferSize_, fp_); + bufferLast_ = buffer_ + readCount_ - 1; + current_ = buffer_; + + if (readCount_ < bufferSize_) { + buffer_[readCount_] = '\0'; + ++bufferLast_; + eof_ = true; + } + } + } + + std::FILE* fp_; + Ch *buffer_; + size_t bufferSize_; + Ch *bufferLast_; + Ch *current_; + size_t readCount_; + size_t count_; //!< Number of characters read + bool eof_; +}; + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/rapidjson-1.1.0/rapid_json/filewritestream.h b/rapidjson-1.1.0/rapid_json/filewritestream.h new file mode 100644 index 000000000..6378dd60e --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/filewritestream.h @@ -0,0 +1,104 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FILEWRITESTREAM_H_ +#define RAPIDJSON_FILEWRITESTREAM_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(unreachable-code) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of C file stream for input using fread(). +/*! + \note implements Stream concept +*/ +class FileWriteStream { +public: + typedef char Ch; //!< Character type. Only support char. + + FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { + RAPIDJSON_ASSERT(fp_ != 0); + } + + void Put(char c) { + if (current_ >= bufferEnd_) + Flush(); + + *current_++ = c; + } + + void PutN(char c, size_t n) { + size_t avail = static_cast(bufferEnd_ - current_); + while (n > avail) { + std::memset(current_, c, avail); + current_ += avail; + Flush(); + n -= avail; + avail = static_cast(bufferEnd_ - current_); + } + + if (n > 0) { + std::memset(current_, c, n); + current_ += n; + } + } + + void Flush() { + if (current_ != buffer_) { + size_t result = fwrite(buffer_, 1, static_cast(current_ - buffer_), fp_); + if (result < static_cast(current_ - buffer_)) { + // failure deliberately ignored at this time + // added to avoid warn_unused_result build errors + } + current_ = buffer_; + } + } + + // Not implemented + char Peek() const { RAPIDJSON_ASSERT(false); return 0; } + char Take() { RAPIDJSON_ASSERT(false); return 0; } + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + // Prohibit copy constructor & assignment operator. + FileWriteStream(const FileWriteStream&); + FileWriteStream& operator=(const FileWriteStream&); + + std::FILE* fp_; + char *buffer_; + char *bufferEnd_; + char *current_; +}; + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(FileWriteStream& stream, char c, size_t n) { + stream.PutN(c, n); +} + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/rapidjson-1.1.0/rapid_json/fwd.h b/rapidjson-1.1.0/rapid_json/fwd.h new file mode 100644 index 000000000..e8104e841 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/fwd.h @@ -0,0 +1,151 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FWD_H_ +#define RAPIDJSON_FWD_H_ + +#include "rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN + +// encodings.h + +template struct UTF8; +template struct UTF16; +template struct UTF16BE; +template struct UTF16LE; +template struct UTF32; +template struct UTF32BE; +template struct UTF32LE; +template struct ASCII; +template struct AutoUTF; + +template +struct Transcoder; + +// allocators.h + +class CrtAllocator; + +template +class MemoryPoolAllocator; + +// stream.h + +template +struct GenericStringStream; + +typedef GenericStringStream > StringStream; + +template +struct GenericInsituStringStream; + +typedef GenericInsituStringStream > InsituStringStream; + +// stringbuffer.h + +template +class GenericStringBuffer; + +typedef GenericStringBuffer, CrtAllocator> StringBuffer; + +// filereadstream.h + +class FileReadStream; + +// filewritestream.h + +class FileWriteStream; + +// memorybuffer.h + +template +struct GenericMemoryBuffer; + +typedef GenericMemoryBuffer MemoryBuffer; + +// memorystream.h + +struct MemoryStream; + +// reader.h + +template +struct BaseReaderHandler; + +template +class GenericReader; + +typedef GenericReader, UTF8, CrtAllocator> Reader; + +// writer.h + +template +class Writer; + +// prettywriter.h + +template +class PrettyWriter; + +// document.h + +template +struct GenericMember; + +template +class GenericMemberIterator; + +template +struct GenericStringRef; + +template +class GenericValue; + +typedef GenericValue, MemoryPoolAllocator > Value; + +template +class GenericDocument; + +typedef GenericDocument, MemoryPoolAllocator, CrtAllocator> Document; + +// pointer.h + +template +class GenericPointer; + +typedef GenericPointer Pointer; + +// schema.h + +template +class IGenericRemoteSchemaDocumentProvider; + +template +class GenericSchemaDocument; + +typedef GenericSchemaDocument SchemaDocument; +typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProvider; + +template < + typename SchemaDocumentType, + typename OutputHandler, + typename StateAllocator> +class GenericSchemaValidator; + +typedef GenericSchemaValidator, void>, CrtAllocator> SchemaValidator; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_RAPIDJSONFWD_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/biginteger.h b/rapidjson-1.1.0/rapid_json/internal/biginteger.h new file mode 100644 index 000000000..9d3e88c99 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/biginteger.h @@ -0,0 +1,290 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_BIGINTEGER_H_ +#define RAPIDJSON_BIGINTEGER_H_ + +#include "../rapidjson.h" + +#if defined(_MSC_VER) && defined(_M_AMD64) +#include // for _umul128 +#pragma intrinsic(_umul128) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +class BigInteger { +public: + typedef uint64_t Type; + + BigInteger(const BigInteger& rhs) : count_(rhs.count_) { + std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type)); + } + + explicit BigInteger(uint64_t u) : count_(1) { + digits_[0] = u; + } + + BigInteger(const char* decimals, size_t length) : count_(1) { + RAPIDJSON_ASSERT(length > 0); + digits_[0] = 0; + size_t i = 0; + const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19 + while (length >= kMaxDigitPerIteration) { + AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration); + length -= kMaxDigitPerIteration; + i += kMaxDigitPerIteration; + } + + if (length > 0) + AppendDecimal64(decimals + i, decimals + i + length); + } + + BigInteger& operator=(const BigInteger &rhs) + { + if (this != &rhs) { + count_ = rhs.count_; + std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type)); + } + return *this; + } + + BigInteger& operator=(uint64_t u) { + digits_[0] = u; + count_ = 1; + return *this; + } + + BigInteger& operator+=(uint64_t u) { + Type backup = digits_[0]; + digits_[0] += u; + for (size_t i = 0; i < count_ - 1; i++) { + if (digits_[i] >= backup) + return *this; // no carry + backup = digits_[i + 1]; + digits_[i + 1] += 1; + } + + // Last carry + if (digits_[count_ - 1] < backup) + PushBack(1); + + return *this; + } + + BigInteger& operator*=(uint64_t u) { + if (u == 0) return *this = 0; + if (u == 1) return *this; + if (*this == 1) return *this = u; + + uint64_t k = 0; + for (size_t i = 0; i < count_; i++) { + uint64_t hi; + digits_[i] = MulAdd64(digits_[i], u, k, &hi); + k = hi; + } + + if (k > 0) + PushBack(k); + + return *this; + } + + BigInteger& operator*=(uint32_t u) { + if (u == 0) return *this = 0; + if (u == 1) return *this; + if (*this == 1) return *this = u; + + uint64_t k = 0; + for (size_t i = 0; i < count_; i++) { + const uint64_t c = digits_[i] >> 32; + const uint64_t d = digits_[i] & 0xFFFFFFFF; + const uint64_t uc = u * c; + const uint64_t ud = u * d; + const uint64_t p0 = ud + k; + const uint64_t p1 = uc + (p0 >> 32); + digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32); + k = p1 >> 32; + } + + if (k > 0) + PushBack(k); + + return *this; + } + + BigInteger& operator<<=(size_t shift) { + if (IsZero() || shift == 0) return *this; + + size_t offset = shift / kTypeBit; + size_t interShift = shift % kTypeBit; + RAPIDJSON_ASSERT(count_ + offset <= kCapacity); + + if (interShift == 0) { + std::memmove(&digits_[count_ - 1 + offset], &digits_[count_ - 1], count_ * sizeof(Type)); + count_ += offset; + } + else { + digits_[count_] = 0; + for (size_t i = count_; i > 0; i--) + digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift)); + digits_[offset] = digits_[0] << interShift; + count_ += offset; + if (digits_[count_]) + count_++; + } + + std::memset(digits_, 0, offset * sizeof(Type)); + + return *this; + } + + bool operator==(const BigInteger& rhs) const { + return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0; + } + + bool operator==(const Type rhs) const { + return count_ == 1 && digits_[0] == rhs; + } + + BigInteger& MultiplyPow5(unsigned exp) { + static const uint32_t kPow5[12] = { + 5, + 5 * 5, + 5 * 5 * 5, + 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 + }; + if (exp == 0) return *this; + for (; exp >= 27; exp -= 27) *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27 + for (; exp >= 13; exp -= 13) *this *= static_cast(1220703125u); // 5^13 + if (exp > 0) *this *= kPow5[exp - 1]; + return *this; + } + + // Compute absolute difference of this and rhs. + // Assume this != rhs + bool Difference(const BigInteger& rhs, BigInteger* out) const { + int cmp = Compare(rhs); + RAPIDJSON_ASSERT(cmp != 0); + const BigInteger *a, *b; // Makes a > b + bool ret; + if (cmp < 0) { a = &rhs; b = this; ret = true; } + else { a = this; b = &rhs; ret = false; } + + Type borrow = 0; + for (size_t i = 0; i < a->count_; i++) { + Type d = a->digits_[i] - borrow; + if (i < b->count_) + d -= b->digits_[i]; + borrow = (d > a->digits_[i]) ? 1 : 0; + out->digits_[i] = d; + if (d != 0) + out->count_ = i + 1; + } + + return ret; + } + + int Compare(const BigInteger& rhs) const { + if (count_ != rhs.count_) + return count_ < rhs.count_ ? -1 : 1; + + for (size_t i = count_; i-- > 0;) + if (digits_[i] != rhs.digits_[i]) + return digits_[i] < rhs.digits_[i] ? -1 : 1; + + return 0; + } + + size_t GetCount() const { return count_; } + Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; } + bool IsZero() const { return count_ == 1 && digits_[0] == 0; } + +private: + void AppendDecimal64(const char* begin, const char* end) { + uint64_t u = ParseUint64(begin, end); + if (IsZero()) + *this = u; + else { + unsigned exp = static_cast(end - begin); + (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u + } + } + + void PushBack(Type digit) { + RAPIDJSON_ASSERT(count_ < kCapacity); + digits_[count_++] = digit; + } + + static uint64_t ParseUint64(const char* begin, const char* end) { + uint64_t r = 0; + for (const char* p = begin; p != end; ++p) { + RAPIDJSON_ASSERT(*p >= '0' && *p <= '9'); + r = r * 10u + static_cast(*p - '0'); + } + return r; + } + + // Assume a * b + k < 2^128 + static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) { +#if defined(_MSC_VER) && defined(_M_AMD64) + uint64_t low = _umul128(a, b, outHigh) + k; + if (low < k) + (*outHigh)++; + return low; +#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) + __extension__ typedef unsigned __int128 uint128; + uint128 p = static_cast(a) * static_cast(b); + p += k; + *outHigh = static_cast(p >> 64); + return static_cast(p); +#else + const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32; + uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1; + x1 += (x0 >> 32); // can't give carry + x1 += x2; + if (x1 < x2) + x3 += (static_cast(1) << 32); + uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF); + uint64_t hi = x3 + (x1 >> 32); + + lo += k; + if (lo < k) + hi++; + *outHigh = hi; + return lo; +#endif + } + + static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000 + static const size_t kCapacity = kBitCount / sizeof(Type); + static const size_t kTypeBit = sizeof(Type) * 8; + + Type digits_[kCapacity]; + size_t count_; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_BIGINTEGER_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/diyfp.h b/rapidjson-1.1.0/rapid_json/internal/diyfp.h new file mode 100644 index 000000000..c9fefdc61 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/diyfp.h @@ -0,0 +1,258 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +// This is a C++ header-only implementation of Grisu2 algorithm from the publication: +// Loitsch, Florian. "Printing floating-point numbers quickly and accurately with +// integers." ACM Sigplan Notices 45.6 (2010): 233-243. + +#ifndef RAPIDJSON_DIYFP_H_ +#define RAPIDJSON_DIYFP_H_ + +#include "../rapidjson.h" + +#if defined(_MSC_VER) && defined(_M_AMD64) +#include +#pragma intrinsic(_BitScanReverse64) +#pragma intrinsic(_umul128) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +struct DiyFp { + DiyFp() : f(), e() {} + + DiyFp(uint64_t fp, int exp) : f(fp), e(exp) {} + + explicit DiyFp(double d) { + union { + double d; + uint64_t u64; + } u = { d }; + + int biased_e = static_cast((u.u64 & kDpExponentMask) >> kDpSignificandSize); + uint64_t significand = (u.u64 & kDpSignificandMask); + if (biased_e != 0) { + f = significand + kDpHiddenBit; + e = biased_e - kDpExponentBias; + } + else { + f = significand; + e = kDpMinExponent + 1; + } + } + + DiyFp operator-(const DiyFp& rhs) const { + return DiyFp(f - rhs.f, e); + } + + DiyFp operator*(const DiyFp& rhs) const { +#if defined(_MSC_VER) && defined(_M_AMD64) + uint64_t h; + uint64_t l = _umul128(f, rhs.f, &h); + if (l & (uint64_t(1) << 63)) // rounding + h++; + return DiyFp(h, e + rhs.e + 64); +#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) + __extension__ typedef unsigned __int128 uint128; + uint128 p = static_cast(f) * static_cast(rhs.f); + uint64_t h = static_cast(p >> 64); + uint64_t l = static_cast(p); + if (l & (uint64_t(1) << 63)) // rounding + h++; + return DiyFp(h, e + rhs.e + 64); +#else + const uint64_t M32 = 0xFFFFFFFF; + const uint64_t a = f >> 32; + const uint64_t b = f & M32; + const uint64_t c = rhs.f >> 32; + const uint64_t d = rhs.f & M32; + const uint64_t ac = a * c; + const uint64_t bc = b * c; + const uint64_t ad = a * d; + const uint64_t bd = b * d; + uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32); + tmp += 1U << 31; /// mult_round + return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), e + rhs.e + 64); +#endif + } + + DiyFp Normalize() const { +#if defined(_MSC_VER) && defined(_M_AMD64) + unsigned long index; + _BitScanReverse64(&index, f); + return DiyFp(f << (63 - index), e - (63 - index)); +#elif defined(__GNUC__) && __GNUC__ >= 4 + int s = __builtin_clzll(f); + return DiyFp(f << s, e - s); +#else + DiyFp res = *this; + while (!(res.f & (static_cast(1) << 63))) { + res.f <<= 1; + res.e--; + } + return res; +#endif + } + + DiyFp NormalizeBoundary() const { + DiyFp res = *this; + while (!(res.f & (kDpHiddenBit << 1))) { + res.f <<= 1; + res.e--; + } + res.f <<= (kDiySignificandSize - kDpSignificandSize - 2); + res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 2); + return res; + } + + void NormalizedBoundaries(DiyFp* minus, DiyFp* plus) const { + DiyFp pl = DiyFp((f << 1) + 1, e - 1).NormalizeBoundary(); + DiyFp mi = (f == kDpHiddenBit) ? DiyFp((f << 2) - 1, e - 2) : DiyFp((f << 1) - 1, e - 1); + mi.f <<= mi.e - pl.e; + mi.e = pl.e; + *plus = pl; + *minus = mi; + } + + double ToDouble() const { + union { + double d; + uint64_t u64; + }u; + const uint64_t be = (e == kDpDenormalExponent && (f & kDpHiddenBit) == 0) ? 0 : + static_cast(e + kDpExponentBias); + u.u64 = (f & kDpSignificandMask) | (be << kDpSignificandSize); + return u.d; + } + + static const int kDiySignificandSize = 64; + static const int kDpSignificandSize = 52; + static const int kDpExponentBias = 0x3FF + kDpSignificandSize; + static const int kDpMaxExponent = 0x7FF - kDpExponentBias; + static const int kDpMinExponent = -kDpExponentBias; + static const int kDpDenormalExponent = -kDpExponentBias + 1; + static const uint64_t kDpExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); + static const uint64_t kDpSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); + static const uint64_t kDpHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); + + uint64_t f; + int e; +}; + +inline DiyFp GetCachedPowerByIndex(size_t index) { + // 10^-348, 10^-340, ..., 10^340 + static const uint64_t kCachedPowers_F[] = { + RAPIDJSON_UINT64_C2(0xfa8fd5a0, 0x081c0288), RAPIDJSON_UINT64_C2(0xbaaee17f, 0xa23ebf76), + RAPIDJSON_UINT64_C2(0x8b16fb20, 0x3055ac76), RAPIDJSON_UINT64_C2(0xcf42894a, 0x5dce35ea), + RAPIDJSON_UINT64_C2(0x9a6bb0aa, 0x55653b2d), RAPIDJSON_UINT64_C2(0xe61acf03, 0x3d1a45df), + RAPIDJSON_UINT64_C2(0xab70fe17, 0xc79ac6ca), RAPIDJSON_UINT64_C2(0xff77b1fc, 0xbebcdc4f), + RAPIDJSON_UINT64_C2(0xbe5691ef, 0x416bd60c), RAPIDJSON_UINT64_C2(0x8dd01fad, 0x907ffc3c), + RAPIDJSON_UINT64_C2(0xd3515c28, 0x31559a83), RAPIDJSON_UINT64_C2(0x9d71ac8f, 0xada6c9b5), + RAPIDJSON_UINT64_C2(0xea9c2277, 0x23ee8bcb), RAPIDJSON_UINT64_C2(0xaecc4991, 0x4078536d), + RAPIDJSON_UINT64_C2(0x823c1279, 0x5db6ce57), RAPIDJSON_UINT64_C2(0xc2109436, 0x4dfb5637), + RAPIDJSON_UINT64_C2(0x9096ea6f, 0x3848984f), RAPIDJSON_UINT64_C2(0xd77485cb, 0x25823ac7), + RAPIDJSON_UINT64_C2(0xa086cfcd, 0x97bf97f4), RAPIDJSON_UINT64_C2(0xef340a98, 0x172aace5), + RAPIDJSON_UINT64_C2(0xb23867fb, 0x2a35b28e), RAPIDJSON_UINT64_C2(0x84c8d4df, 0xd2c63f3b), + RAPIDJSON_UINT64_C2(0xc5dd4427, 0x1ad3cdba), RAPIDJSON_UINT64_C2(0x936b9fce, 0xbb25c996), + RAPIDJSON_UINT64_C2(0xdbac6c24, 0x7d62a584), RAPIDJSON_UINT64_C2(0xa3ab6658, 0x0d5fdaf6), + RAPIDJSON_UINT64_C2(0xf3e2f893, 0xdec3f126), RAPIDJSON_UINT64_C2(0xb5b5ada8, 0xaaff80b8), + RAPIDJSON_UINT64_C2(0x87625f05, 0x6c7c4a8b), RAPIDJSON_UINT64_C2(0xc9bcff60, 0x34c13053), + RAPIDJSON_UINT64_C2(0x964e858c, 0x91ba2655), RAPIDJSON_UINT64_C2(0xdff97724, 0x70297ebd), + RAPIDJSON_UINT64_C2(0xa6dfbd9f, 0xb8e5b88f), RAPIDJSON_UINT64_C2(0xf8a95fcf, 0x88747d94), + RAPIDJSON_UINT64_C2(0xb9447093, 0x8fa89bcf), RAPIDJSON_UINT64_C2(0x8a08f0f8, 0xbf0f156b), + RAPIDJSON_UINT64_C2(0xcdb02555, 0x653131b6), RAPIDJSON_UINT64_C2(0x993fe2c6, 0xd07b7fac), + RAPIDJSON_UINT64_C2(0xe45c10c4, 0x2a2b3b06), RAPIDJSON_UINT64_C2(0xaa242499, 0x697392d3), + RAPIDJSON_UINT64_C2(0xfd87b5f2, 0x8300ca0e), RAPIDJSON_UINT64_C2(0xbce50864, 0x92111aeb), + RAPIDJSON_UINT64_C2(0x8cbccc09, 0x6f5088cc), RAPIDJSON_UINT64_C2(0xd1b71758, 0xe219652c), + RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), RAPIDJSON_UINT64_C2(0xe8d4a510, 0x00000000), + RAPIDJSON_UINT64_C2(0xad78ebc5, 0xac620000), RAPIDJSON_UINT64_C2(0x813f3978, 0xf8940984), + RAPIDJSON_UINT64_C2(0xc097ce7b, 0xc90715b3), RAPIDJSON_UINT64_C2(0x8f7e32ce, 0x7bea5c70), + RAPIDJSON_UINT64_C2(0xd5d238a4, 0xabe98068), RAPIDJSON_UINT64_C2(0x9f4f2726, 0x179a2245), + RAPIDJSON_UINT64_C2(0xed63a231, 0xd4c4fb27), RAPIDJSON_UINT64_C2(0xb0de6538, 0x8cc8ada8), + RAPIDJSON_UINT64_C2(0x83c7088e, 0x1aab65db), RAPIDJSON_UINT64_C2(0xc45d1df9, 0x42711d9a), + RAPIDJSON_UINT64_C2(0x924d692c, 0xa61be758), RAPIDJSON_UINT64_C2(0xda01ee64, 0x1a708dea), + RAPIDJSON_UINT64_C2(0xa26da399, 0x9aef774a), RAPIDJSON_UINT64_C2(0xf209787b, 0xb47d6b85), + RAPIDJSON_UINT64_C2(0xb454e4a1, 0x79dd1877), RAPIDJSON_UINT64_C2(0x865b8692, 0x5b9bc5c2), + RAPIDJSON_UINT64_C2(0xc83553c5, 0xc8965d3d), RAPIDJSON_UINT64_C2(0x952ab45c, 0xfa97a0b3), + RAPIDJSON_UINT64_C2(0xde469fbd, 0x99a05fe3), RAPIDJSON_UINT64_C2(0xa59bc234, 0xdb398c25), + RAPIDJSON_UINT64_C2(0xf6c69a72, 0xa3989f5c), RAPIDJSON_UINT64_C2(0xb7dcbf53, 0x54e9bece), + RAPIDJSON_UINT64_C2(0x88fcf317, 0xf22241e2), RAPIDJSON_UINT64_C2(0xcc20ce9b, 0xd35c78a5), + RAPIDJSON_UINT64_C2(0x98165af3, 0x7b2153df), RAPIDJSON_UINT64_C2(0xe2a0b5dc, 0x971f303a), + RAPIDJSON_UINT64_C2(0xa8d9d153, 0x5ce3b396), RAPIDJSON_UINT64_C2(0xfb9b7cd9, 0xa4a7443c), + RAPIDJSON_UINT64_C2(0xbb764c4c, 0xa7a44410), RAPIDJSON_UINT64_C2(0x8bab8eef, 0xb6409c1a), + RAPIDJSON_UINT64_C2(0xd01fef10, 0xa657842c), RAPIDJSON_UINT64_C2(0x9b10a4e5, 0xe9913129), + RAPIDJSON_UINT64_C2(0xe7109bfb, 0xa19c0c9d), RAPIDJSON_UINT64_C2(0xac2820d9, 0x623bf429), + RAPIDJSON_UINT64_C2(0x80444b5e, 0x7aa7cf85), RAPIDJSON_UINT64_C2(0xbf21e440, 0x03acdd2d), + RAPIDJSON_UINT64_C2(0x8e679c2f, 0x5e44ff8f), RAPIDJSON_UINT64_C2(0xd433179d, 0x9c8cb841), + RAPIDJSON_UINT64_C2(0x9e19db92, 0xb4e31ba9), RAPIDJSON_UINT64_C2(0xeb96bf6e, 0xbadf77d9), + RAPIDJSON_UINT64_C2(0xaf87023b, 0x9bf0ee6b) + }; + static const int16_t kCachedPowers_E[] = { + -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, + -954, -927, -901, -874, -847, -821, -794, -768, -741, -715, + -688, -661, -635, -608, -582, -555, -529, -502, -475, -449, + -422, -396, -369, -343, -316, -289, -263, -236, -210, -183, + -157, -130, -103, -77, -50, -24, 3, 30, 56, 83, + 109, 136, 162, 189, 216, 242, 269, 295, 322, 348, + 375, 402, 428, 455, 481, 508, 534, 561, 588, 614, + 641, 667, 694, 720, 747, 774, 800, 827, 853, 880, + 907, 933, 960, 986, 1013, 1039, 1066 + }; + return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]); +} + +inline DiyFp GetCachedPower(int e, int* K) { + + //int k = static_cast(ceil((-61 - e) * 0.30102999566398114)) + 374; + double dk = (-61 - e) * 0.30102999566398114 + 347; // dk must be positive, so can do ceiling in positive + int k = static_cast(dk); + if (dk - k > 0.0) + k++; + + unsigned index = static_cast((k >> 3) + 1); + *K = -(-348 + static_cast(index << 3)); // decimal exponent no need lookup table + + return GetCachedPowerByIndex(index); +} + +inline DiyFp GetCachedPower10(int exp, int *outExp) { + unsigned index = (static_cast(exp) + 348u) / 8u; + *outExp = -348 + static_cast(index) * 8; + return GetCachedPowerByIndex(index); + } + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +RAPIDJSON_DIAG_OFF(padded) +#endif + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_DIYFP_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/dtoa.h b/rapidjson-1.1.0/rapid_json/internal/dtoa.h new file mode 100644 index 000000000..8d6350e62 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/dtoa.h @@ -0,0 +1,245 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +// This is a C++ header-only implementation of Grisu2 algorithm from the publication: +// Loitsch, Florian. "Printing floating-point numbers quickly and accurately with +// integers." ACM Sigplan Notices 45.6 (2010): 233-243. + +#ifndef RAPIDJSON_DTOA_ +#define RAPIDJSON_DTOA_ + +#include "itoa.h" // GetDigitsLut() +#include "diyfp.h" +#include "ieee754.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +RAPIDJSON_DIAG_OFF(array-bounds) // some gcc versions generate wrong warnings https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124 +#endif + +inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) { + while (rest < wp_w && delta - rest >= ten_kappa && + (rest + ten_kappa < wp_w || /// closer + wp_w - rest > rest + ten_kappa - wp_w)) { + buffer[len - 1]--; + rest += ten_kappa; + } +} + +inline unsigned CountDecimalDigit32(uint32_t n) { + // Simple pure C++ implementation was faster than __builtin_clz version in this situation. + if (n < 10) return 1; + if (n < 100) return 2; + if (n < 1000) return 3; + if (n < 10000) return 4; + if (n < 100000) return 5; + if (n < 1000000) return 6; + if (n < 10000000) return 7; + if (n < 100000000) return 8; + // Will not reach 10 digits in DigitGen() + //if (n < 1000000000) return 9; + //return 10; + return 9; +} + +inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) { + static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; + const DiyFp one(uint64_t(1) << -Mp.e, Mp.e); + const DiyFp wp_w = Mp - W; + uint32_t p1 = static_cast(Mp.f >> -one.e); + uint64_t p2 = Mp.f & (one.f - 1); + unsigned kappa = CountDecimalDigit32(p1); // kappa in [0, 9] + *len = 0; + + while (kappa > 0) { + uint32_t d = 0; + switch (kappa) { + case 9: d = p1 / 100000000; p1 %= 100000000; break; + case 8: d = p1 / 10000000; p1 %= 10000000; break; + case 7: d = p1 / 1000000; p1 %= 1000000; break; + case 6: d = p1 / 100000; p1 %= 100000; break; + case 5: d = p1 / 10000; p1 %= 10000; break; + case 4: d = p1 / 1000; p1 %= 1000; break; + case 3: d = p1 / 100; p1 %= 100; break; + case 2: d = p1 / 10; p1 %= 10; break; + case 1: d = p1; p1 = 0; break; + default:; + } + if (d || *len) + buffer[(*len)++] = static_cast('0' + static_cast(d)); + kappa--; + uint64_t tmp = (static_cast(p1) << -one.e) + p2; + if (tmp <= delta) { + *K += kappa; + GrisuRound(buffer, *len, delta, tmp, static_cast(kPow10[kappa]) << -one.e, wp_w.f); + return; + } + } + + // kappa = 0 + for (;;) { + p2 *= 10; + delta *= 10; + char d = static_cast(p2 >> -one.e); + if (d || *len) + buffer[(*len)++] = static_cast('0' + d); + p2 &= one.f - 1; + kappa--; + if (p2 < delta) { + *K += kappa; + int index = -static_cast(kappa); + GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[-static_cast(kappa)] : 0)); + return; + } + } +} + +inline void Grisu2(double value, char* buffer, int* length, int* K) { + const DiyFp v(value); + DiyFp w_m, w_p; + v.NormalizedBoundaries(&w_m, &w_p); + + const DiyFp c_mk = GetCachedPower(w_p.e, K); + const DiyFp W = v.Normalize() * c_mk; + DiyFp Wp = w_p * c_mk; + DiyFp Wm = w_m * c_mk; + Wm.f++; + Wp.f--; + DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K); +} + +inline char* WriteExponent(int K, char* buffer) { + if (K < 0) { + *buffer++ = '-'; + K = -K; + } + + if (K >= 100) { + *buffer++ = static_cast('0' + static_cast(K / 100)); + K %= 100; + const char* d = GetDigitsLut() + K * 2; + *buffer++ = d[0]; + *buffer++ = d[1]; + } + else if (K >= 10) { + const char* d = GetDigitsLut() + K * 2; + *buffer++ = d[0]; + *buffer++ = d[1]; + } + else + *buffer++ = static_cast('0' + static_cast(K)); + + return buffer; +} + +inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) { + const int kk = length + k; // 10^(kk-1) <= v < 10^kk + + if (0 <= k && kk <= 21) { + // 1234e7 -> 12340000000 + for (int i = length; i < kk; i++) + buffer[i] = '0'; + buffer[kk] = '.'; + buffer[kk + 1] = '0'; + return &buffer[kk + 2]; + } + else if (0 < kk && kk <= 21) { + // 1234e-2 -> 12.34 + std::memmove(&buffer[kk + 1], &buffer[kk], static_cast(length - kk)); + buffer[kk] = '.'; + if (0 > k + maxDecimalPlaces) { + // When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1 + // Remove extra trailing zeros (at least one) after truncation. + for (int i = kk + maxDecimalPlaces; i > kk + 1; i--) + if (buffer[i] != '0') + return &buffer[i + 1]; + return &buffer[kk + 2]; // Reserve one zero + } + else + return &buffer[length + 1]; + } + else if (-6 < kk && kk <= 0) { + // 1234e-6 -> 0.001234 + const int offset = 2 - kk; + std::memmove(&buffer[offset], &buffer[0], static_cast(length)); + buffer[0] = '0'; + buffer[1] = '.'; + for (int i = 2; i < offset; i++) + buffer[i] = '0'; + if (length - kk > maxDecimalPlaces) { + // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1 + // Remove extra trailing zeros (at least one) after truncation. + for (int i = maxDecimalPlaces + 1; i > 2; i--) + if (buffer[i] != '0') + return &buffer[i + 1]; + return &buffer[3]; // Reserve one zero + } + else + return &buffer[length + offset]; + } + else if (kk < -maxDecimalPlaces) { + // Truncate to zero + buffer[0] = '0'; + buffer[1] = '.'; + buffer[2] = '0'; + return &buffer[3]; + } + else if (length == 1) { + // 1e30 + buffer[1] = 'e'; + return WriteExponent(kk - 1, &buffer[2]); + } + else { + // 1234e30 -> 1.234e33 + std::memmove(&buffer[2], &buffer[1], static_cast(length - 1)); + buffer[1] = '.'; + buffer[length + 1] = 'e'; + return WriteExponent(kk - 1, &buffer[0 + length + 2]); + } +} + +inline char* dtoa(double value, char* buffer, int maxDecimalPlaces = 324) { + RAPIDJSON_ASSERT(maxDecimalPlaces >= 1); + Double d(value); + if (d.IsZero()) { + if (d.Sign()) + *buffer++ = '-'; // -0.0, Issue #289 + buffer[0] = '0'; + buffer[1] = '.'; + buffer[2] = '0'; + return &buffer[3]; + } + else { + if (value < 0) { + *buffer++ = '-'; + value = -value; + } + int length, K; + Grisu2(value, buffer, &length, &K); + return Prettify(buffer, length, K, maxDecimalPlaces); + } +} + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_DTOA_ diff --git a/rapidjson-1.1.0/rapid_json/internal/ieee754.h b/rapidjson-1.1.0/rapid_json/internal/ieee754.h new file mode 100644 index 000000000..82bb0b99e --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/ieee754.h @@ -0,0 +1,78 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_IEEE754_ +#define RAPIDJSON_IEEE754_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +class Double { +public: + Double() {} + Double(double d) : d_(d) {} + Double(uint64_t u) : u_(u) {} + + double Value() const { return d_; } + uint64_t Uint64Value() const { return u_; } + + double NextPositiveDouble() const { + RAPIDJSON_ASSERT(!Sign()); + return Double(u_ + 1).Value(); + } + + bool Sign() const { return (u_ & kSignMask) != 0; } + uint64_t Significand() const { return u_ & kSignificandMask; } + int Exponent() const { return static_cast(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias); } + + bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; } + bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; } + bool IsNanOrInf() const { return (u_ & kExponentMask) == kExponentMask; } + bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; } + bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; } + + uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); } + int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; } + uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; } + + static unsigned EffectiveSignificandSize(int order) { + if (order >= -1021) + return 53; + else if (order <= -1074) + return 0; + else + return static_cast(order) + 1074; + } + +private: + static const int kSignificandSize = 52; + static const int kExponentBias = 0x3FF; + static const int kDenormalExponent = 1 - kExponentBias; + static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000); + static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); + static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); + static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); + + union { + double d_; + uint64_t u_; + }; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_IEEE754_ diff --git a/rapidjson-1.1.0/rapid_json/internal/itoa.h b/rapidjson-1.1.0/rapid_json/internal/itoa.h new file mode 100644 index 000000000..01a4e7e72 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/itoa.h @@ -0,0 +1,304 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ITOA_ +#define RAPIDJSON_ITOA_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +inline const char* GetDigitsLut() { + static const char cDigitsLut[200] = { + '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9', + '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9', + '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9', + '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9', + '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9', + '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9', + '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9', + '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9', + '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9', + '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9' + }; + return cDigitsLut; +} + +inline char* u32toa(uint32_t value, char* buffer) { + const char* cDigitsLut = GetDigitsLut(); + + if (value < 10000) { + const uint32_t d1 = (value / 100) << 1; + const uint32_t d2 = (value % 100) << 1; + + if (value >= 1000) + *buffer++ = cDigitsLut[d1]; + if (value >= 100) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 10) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + } + else if (value < 100000000) { + // value = bbbbcccc + const uint32_t b = value / 10000; + const uint32_t c = value % 10000; + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + if (value >= 10000000) + *buffer++ = cDigitsLut[d1]; + if (value >= 1000000) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 100000) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + else { + // value = aabbbbcccc in decimal + + const uint32_t a = value / 100000000; // 1 to 42 + value %= 100000000; + + if (a >= 10) { + const unsigned i = a << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else + *buffer++ = static_cast('0' + static_cast(a)); + + const uint32_t b = value / 10000; // 0 to 9999 + const uint32_t c = value % 10000; // 0 to 9999 + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + *buffer++ = cDigitsLut[d1]; + *buffer++ = cDigitsLut[d1 + 1]; + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + return buffer; +} + +inline char* i32toa(int32_t value, char* buffer) { + uint32_t u = static_cast(value); + if (value < 0) { + *buffer++ = '-'; + u = ~u + 1; + } + + return u32toa(u, buffer); +} + +inline char* u64toa(uint64_t value, char* buffer) { + const char* cDigitsLut = GetDigitsLut(); + const uint64_t kTen8 = 100000000; + const uint64_t kTen9 = kTen8 * 10; + const uint64_t kTen10 = kTen8 * 100; + const uint64_t kTen11 = kTen8 * 1000; + const uint64_t kTen12 = kTen8 * 10000; + const uint64_t kTen13 = kTen8 * 100000; + const uint64_t kTen14 = kTen8 * 1000000; + const uint64_t kTen15 = kTen8 * 10000000; + const uint64_t kTen16 = kTen8 * kTen8; + + if (value < kTen8) { + uint32_t v = static_cast(value); + if (v < 10000) { + const uint32_t d1 = (v / 100) << 1; + const uint32_t d2 = (v % 100) << 1; + + if (v >= 1000) + *buffer++ = cDigitsLut[d1]; + if (v >= 100) + *buffer++ = cDigitsLut[d1 + 1]; + if (v >= 10) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + } + else { + // value = bbbbcccc + const uint32_t b = v / 10000; + const uint32_t c = v % 10000; + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + if (value >= 10000000) + *buffer++ = cDigitsLut[d1]; + if (value >= 1000000) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 100000) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + } + else if (value < kTen16) { + const uint32_t v0 = static_cast(value / kTen8); + const uint32_t v1 = static_cast(value % kTen8); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + const uint32_t d1 = (b0 / 100) << 1; + const uint32_t d2 = (b0 % 100) << 1; + + const uint32_t d3 = (c0 / 100) << 1; + const uint32_t d4 = (c0 % 100) << 1; + + const uint32_t b1 = v1 / 10000; + const uint32_t c1 = v1 % 10000; + + const uint32_t d5 = (b1 / 100) << 1; + const uint32_t d6 = (b1 % 100) << 1; + + const uint32_t d7 = (c1 / 100) << 1; + const uint32_t d8 = (c1 % 100) << 1; + + if (value >= kTen15) + *buffer++ = cDigitsLut[d1]; + if (value >= kTen14) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= kTen13) + *buffer++ = cDigitsLut[d2]; + if (value >= kTen12) + *buffer++ = cDigitsLut[d2 + 1]; + if (value >= kTen11) + *buffer++ = cDigitsLut[d3]; + if (value >= kTen10) + *buffer++ = cDigitsLut[d3 + 1]; + if (value >= kTen9) + *buffer++ = cDigitsLut[d4]; + if (value >= kTen8) + *buffer++ = cDigitsLut[d4 + 1]; + + *buffer++ = cDigitsLut[d5]; + *buffer++ = cDigitsLut[d5 + 1]; + *buffer++ = cDigitsLut[d6]; + *buffer++ = cDigitsLut[d6 + 1]; + *buffer++ = cDigitsLut[d7]; + *buffer++ = cDigitsLut[d7 + 1]; + *buffer++ = cDigitsLut[d8]; + *buffer++ = cDigitsLut[d8 + 1]; + } + else { + const uint32_t a = static_cast(value / kTen16); // 1 to 1844 + value %= kTen16; + + if (a < 10) + *buffer++ = static_cast('0' + static_cast(a)); + else if (a < 100) { + const uint32_t i = a << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else if (a < 1000) { + *buffer++ = static_cast('0' + static_cast(a / 100)); + + const uint32_t i = (a % 100) << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else { + const uint32_t i = (a / 100) << 1; + const uint32_t j = (a % 100) << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + *buffer++ = cDigitsLut[j]; + *buffer++ = cDigitsLut[j + 1]; + } + + const uint32_t v0 = static_cast(value / kTen8); + const uint32_t v1 = static_cast(value % kTen8); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + const uint32_t d1 = (b0 / 100) << 1; + const uint32_t d2 = (b0 % 100) << 1; + + const uint32_t d3 = (c0 / 100) << 1; + const uint32_t d4 = (c0 % 100) << 1; + + const uint32_t b1 = v1 / 10000; + const uint32_t c1 = v1 % 10000; + + const uint32_t d5 = (b1 / 100) << 1; + const uint32_t d6 = (b1 % 100) << 1; + + const uint32_t d7 = (c1 / 100) << 1; + const uint32_t d8 = (c1 % 100) << 1; + + *buffer++ = cDigitsLut[d1]; + *buffer++ = cDigitsLut[d1 + 1]; + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + *buffer++ = cDigitsLut[d5]; + *buffer++ = cDigitsLut[d5 + 1]; + *buffer++ = cDigitsLut[d6]; + *buffer++ = cDigitsLut[d6 + 1]; + *buffer++ = cDigitsLut[d7]; + *buffer++ = cDigitsLut[d7 + 1]; + *buffer++ = cDigitsLut[d8]; + *buffer++ = cDigitsLut[d8 + 1]; + } + + return buffer; +} + +inline char* i64toa(int64_t value, char* buffer) { + uint64_t u = static_cast(value); + if (value < 0) { + *buffer++ = '-'; + u = ~u + 1; + } + + return u64toa(u, buffer); +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ITOA_ diff --git a/rapidjson-1.1.0/rapid_json/internal/meta.h b/rapidjson-1.1.0/rapid_json/internal/meta.h new file mode 100644 index 000000000..5a9aaa428 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/meta.h @@ -0,0 +1,181 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_META_H_ +#define RAPIDJSON_INTERNAL_META_H_ + +#include "../rapidjson.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif +#if defined(_MSC_VER) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(6334) +#endif + +#if RAPIDJSON_HAS_CXX11_TYPETRAITS +#include +#endif + +//@cond RAPIDJSON_INTERNAL +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +// Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching +template struct Void { typedef void Type; }; + +/////////////////////////////////////////////////////////////////////////////// +// BoolType, TrueType, FalseType +// +template struct BoolType { + static const bool Value = Cond; + typedef BoolType Type; +}; +typedef BoolType TrueType; +typedef BoolType FalseType; + + +/////////////////////////////////////////////////////////////////////////////// +// SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr +// + +template struct SelectIfImpl { template struct Apply { typedef T1 Type; }; }; +template <> struct SelectIfImpl { template struct Apply { typedef T2 Type; }; }; +template struct SelectIfCond : SelectIfImpl::template Apply {}; +template struct SelectIf : SelectIfCond {}; + +template struct AndExprCond : FalseType {}; +template <> struct AndExprCond : TrueType {}; +template struct OrExprCond : TrueType {}; +template <> struct OrExprCond : FalseType {}; + +template struct BoolExpr : SelectIf::Type {}; +template struct NotExpr : SelectIf::Type {}; +template struct AndExpr : AndExprCond::Type {}; +template struct OrExpr : OrExprCond::Type {}; + + +/////////////////////////////////////////////////////////////////////////////// +// AddConst, MaybeAddConst, RemoveConst +template struct AddConst { typedef const T Type; }; +template struct MaybeAddConst : SelectIfCond {}; +template struct RemoveConst { typedef T Type; }; +template struct RemoveConst { typedef T Type; }; + + +/////////////////////////////////////////////////////////////////////////////// +// IsSame, IsConst, IsMoreConst, IsPointer +// +template struct IsSame : FalseType {}; +template struct IsSame : TrueType {}; + +template struct IsConst : FalseType {}; +template struct IsConst : TrueType {}; + +template +struct IsMoreConst + : AndExpr::Type, typename RemoveConst::Type>, + BoolType::Value >= IsConst::Value> >::Type {}; + +template struct IsPointer : FalseType {}; +template struct IsPointer : TrueType {}; + +/////////////////////////////////////////////////////////////////////////////// +// IsBaseOf +// +#if RAPIDJSON_HAS_CXX11_TYPETRAITS + +template struct IsBaseOf + : BoolType< ::std::is_base_of::value> {}; + +#else // simplified version adopted from Boost + +template struct IsBaseOfImpl { + RAPIDJSON_STATIC_ASSERT(sizeof(B) != 0); + RAPIDJSON_STATIC_ASSERT(sizeof(D) != 0); + + typedef char (&Yes)[1]; + typedef char (&No) [2]; + + template + static Yes Check(const D*, T); + static No Check(const B*, int); + + struct Host { + operator const B*() const; + operator const D*(); + }; + + enum { Value = (sizeof(Check(Host(), 0)) == sizeof(Yes)) }; +}; + +template struct IsBaseOf + : OrExpr, BoolExpr > >::Type {}; + +#endif // RAPIDJSON_HAS_CXX11_TYPETRAITS + + +////////////////////////////////////////////////////////////////////////// +// EnableIf / DisableIf +// +template struct EnableIfCond { typedef T Type; }; +template struct EnableIfCond { /* empty */ }; + +template struct DisableIfCond { typedef T Type; }; +template struct DisableIfCond { /* empty */ }; + +template +struct EnableIf : EnableIfCond {}; + +template +struct DisableIf : DisableIfCond {}; + +// SFINAE helpers +struct SfinaeTag {}; +template struct RemoveSfinaeTag; +template struct RemoveSfinaeTag { typedef T Type; }; + +#define RAPIDJSON_REMOVEFPTR_(type) \ + typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag \ + < ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*) type>::Type + +#define RAPIDJSON_ENABLEIF(cond) \ + typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ + ::Type * = NULL + +#define RAPIDJSON_DISABLEIF(cond) \ + typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ + ::Type * = NULL + +#define RAPIDJSON_ENABLEIF_RETURN(cond,returntype) \ + typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ + ::Type + +#define RAPIDJSON_DISABLEIF_RETURN(cond,returntype) \ + typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ + ::Type + +} // namespace internal +RAPIDJSON_NAMESPACE_END +//@endcond + +#if defined(__GNUC__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_META_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/pow10.h b/rapidjson-1.1.0/rapid_json/internal/pow10.h new file mode 100644 index 000000000..02f475d70 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/pow10.h @@ -0,0 +1,55 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_POW10_ +#define RAPIDJSON_POW10_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Computes integer powers of 10 in double (10.0^n). +/*! This function uses lookup table for fast and accurate results. + \param n non-negative exponent. Must <= 308. + \return 10.0^n +*/ +inline double Pow10(int n) { + static const double e[] = { // 1e-0...1e308: 309 * 8 bytes = 2472 bytes + 1e+0, + 1e+1, 1e+2, 1e+3, 1e+4, 1e+5, 1e+6, 1e+7, 1e+8, 1e+9, 1e+10, 1e+11, 1e+12, 1e+13, 1e+14, 1e+15, 1e+16, 1e+17, 1e+18, 1e+19, 1e+20, + 1e+21, 1e+22, 1e+23, 1e+24, 1e+25, 1e+26, 1e+27, 1e+28, 1e+29, 1e+30, 1e+31, 1e+32, 1e+33, 1e+34, 1e+35, 1e+36, 1e+37, 1e+38, 1e+39, 1e+40, + 1e+41, 1e+42, 1e+43, 1e+44, 1e+45, 1e+46, 1e+47, 1e+48, 1e+49, 1e+50, 1e+51, 1e+52, 1e+53, 1e+54, 1e+55, 1e+56, 1e+57, 1e+58, 1e+59, 1e+60, + 1e+61, 1e+62, 1e+63, 1e+64, 1e+65, 1e+66, 1e+67, 1e+68, 1e+69, 1e+70, 1e+71, 1e+72, 1e+73, 1e+74, 1e+75, 1e+76, 1e+77, 1e+78, 1e+79, 1e+80, + 1e+81, 1e+82, 1e+83, 1e+84, 1e+85, 1e+86, 1e+87, 1e+88, 1e+89, 1e+90, 1e+91, 1e+92, 1e+93, 1e+94, 1e+95, 1e+96, 1e+97, 1e+98, 1e+99, 1e+100, + 1e+101,1e+102,1e+103,1e+104,1e+105,1e+106,1e+107,1e+108,1e+109,1e+110,1e+111,1e+112,1e+113,1e+114,1e+115,1e+116,1e+117,1e+118,1e+119,1e+120, + 1e+121,1e+122,1e+123,1e+124,1e+125,1e+126,1e+127,1e+128,1e+129,1e+130,1e+131,1e+132,1e+133,1e+134,1e+135,1e+136,1e+137,1e+138,1e+139,1e+140, + 1e+141,1e+142,1e+143,1e+144,1e+145,1e+146,1e+147,1e+148,1e+149,1e+150,1e+151,1e+152,1e+153,1e+154,1e+155,1e+156,1e+157,1e+158,1e+159,1e+160, + 1e+161,1e+162,1e+163,1e+164,1e+165,1e+166,1e+167,1e+168,1e+169,1e+170,1e+171,1e+172,1e+173,1e+174,1e+175,1e+176,1e+177,1e+178,1e+179,1e+180, + 1e+181,1e+182,1e+183,1e+184,1e+185,1e+186,1e+187,1e+188,1e+189,1e+190,1e+191,1e+192,1e+193,1e+194,1e+195,1e+196,1e+197,1e+198,1e+199,1e+200, + 1e+201,1e+202,1e+203,1e+204,1e+205,1e+206,1e+207,1e+208,1e+209,1e+210,1e+211,1e+212,1e+213,1e+214,1e+215,1e+216,1e+217,1e+218,1e+219,1e+220, + 1e+221,1e+222,1e+223,1e+224,1e+225,1e+226,1e+227,1e+228,1e+229,1e+230,1e+231,1e+232,1e+233,1e+234,1e+235,1e+236,1e+237,1e+238,1e+239,1e+240, + 1e+241,1e+242,1e+243,1e+244,1e+245,1e+246,1e+247,1e+248,1e+249,1e+250,1e+251,1e+252,1e+253,1e+254,1e+255,1e+256,1e+257,1e+258,1e+259,1e+260, + 1e+261,1e+262,1e+263,1e+264,1e+265,1e+266,1e+267,1e+268,1e+269,1e+270,1e+271,1e+272,1e+273,1e+274,1e+275,1e+276,1e+277,1e+278,1e+279,1e+280, + 1e+281,1e+282,1e+283,1e+284,1e+285,1e+286,1e+287,1e+288,1e+289,1e+290,1e+291,1e+292,1e+293,1e+294,1e+295,1e+296,1e+297,1e+298,1e+299,1e+300, + 1e+301,1e+302,1e+303,1e+304,1e+305,1e+306,1e+307,1e+308 + }; + RAPIDJSON_ASSERT(n >= 0 && n <= 308); + return e[n]; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_POW10_ diff --git a/rapidjson-1.1.0/rapid_json/internal/regex.h b/rapidjson-1.1.0/rapid_json/internal/regex.h new file mode 100644 index 000000000..422a5240b --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/regex.h @@ -0,0 +1,701 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_REGEX_H_ +#define RAPIDJSON_INTERNAL_REGEX_H_ + +#include "../allocators.h" +#include "../stream.h" +#include "stack.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(switch-enum) +RAPIDJSON_DIAG_OFF(implicit-fallthrough) +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +#ifndef RAPIDJSON_REGEX_VERBOSE +#define RAPIDJSON_REGEX_VERBOSE 0 +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +/////////////////////////////////////////////////////////////////////////////// +// GenericRegex + +static const SizeType kRegexInvalidState = ~SizeType(0); //!< Represents an invalid index in GenericRegex::State::out, out1 +static const SizeType kRegexInvalidRange = ~SizeType(0); + +//! Regular expression engine with subset of ECMAscript grammar. +/*! + Supported regular expression syntax: + - \c ab Concatenation + - \c a|b Alternation + - \c a? Zero or one + - \c a* Zero or more + - \c a+ One or more + - \c a{3} Exactly 3 times + - \c a{3,} At least 3 times + - \c a{3,5} 3 to 5 times + - \c (ab) Grouping + - \c ^a At the beginning + - \c a$ At the end + - \c . Any character + - \c [abc] Character classes + - \c [a-c] Character class range + - \c [a-z0-9_] Character class combination + - \c [^abc] Negated character classes + - \c [^a-c] Negated character class range + - \c [\b] Backspace (U+0008) + - \c \\| \\\\ ... Escape characters + - \c \\f Form feed (U+000C) + - \c \\n Line feed (U+000A) + - \c \\r Carriage return (U+000D) + - \c \\t Tab (U+0009) + - \c \\v Vertical tab (U+000B) + + \note This is a Thompson NFA engine, implemented with reference to + Cox, Russ. "Regular Expression Matching Can Be Simple And Fast (but is slow in Java, Perl, PHP, Python, Ruby,...).", + https://swtch.com/~rsc/regexp/regexp1.html +*/ +template +class GenericRegex { +public: + typedef typename Encoding::Ch Ch; + + GenericRegex(const Ch* source, Allocator* allocator = 0) : + states_(allocator, 256), ranges_(allocator, 256), root_(kRegexInvalidState), stateCount_(), rangeCount_(), + stateSet_(), state0_(allocator, 0), state1_(allocator, 0), anchorBegin_(), anchorEnd_() + { + GenericStringStream ss(source); + DecodedStream > ds(ss); + Parse(ds); + } + + ~GenericRegex() { + Allocator::Free(stateSet_); + } + + bool IsValid() const { + return root_ != kRegexInvalidState; + } + + template + bool Match(InputStream& is) const { + return SearchWithAnchoring(is, true, true); + } + + bool Match(const Ch* s) const { + GenericStringStream is(s); + return Match(is); + } + + template + bool Search(InputStream& is) const { + return SearchWithAnchoring(is, anchorBegin_, anchorEnd_); + } + + bool Search(const Ch* s) const { + GenericStringStream is(s); + return Search(is); + } + +private: + enum Operator { + kZeroOrOne, + kZeroOrMore, + kOneOrMore, + kConcatenation, + kAlternation, + kLeftParenthesis + }; + + static const unsigned kAnyCharacterClass = 0xFFFFFFFF; //!< For '.' + static const unsigned kRangeCharacterClass = 0xFFFFFFFE; + static const unsigned kRangeNegationFlag = 0x80000000; + + struct Range { + unsigned start; // + unsigned end; + SizeType next; + }; + + struct State { + SizeType out; //!< Equals to kInvalid for matching state + SizeType out1; //!< Equals to non-kInvalid for split + SizeType rangeStart; + unsigned codepoint; + }; + + struct Frag { + Frag(SizeType s, SizeType o, SizeType m) : start(s), out(o), minIndex(m) {} + SizeType start; + SizeType out; //!< link-list of all output states + SizeType minIndex; + }; + + template + class DecodedStream { + public: + DecodedStream(SourceStream& ss) : ss_(ss), codepoint_() { Decode(); } + unsigned Peek() { return codepoint_; } + unsigned Take() { + unsigned c = codepoint_; + if (c) // No further decoding when '\0' + Decode(); + return c; + } + + private: + void Decode() { + if (!Encoding::Decode(ss_, &codepoint_)) + codepoint_ = 0; + } + + SourceStream& ss_; + unsigned codepoint_; + }; + + State& GetState(SizeType index) { + RAPIDJSON_ASSERT(index < stateCount_); + return states_.template Bottom()[index]; + } + + const State& GetState(SizeType index) const { + RAPIDJSON_ASSERT(index < stateCount_); + return states_.template Bottom()[index]; + } + + Range& GetRange(SizeType index) { + RAPIDJSON_ASSERT(index < rangeCount_); + return ranges_.template Bottom()[index]; + } + + const Range& GetRange(SizeType index) const { + RAPIDJSON_ASSERT(index < rangeCount_); + return ranges_.template Bottom()[index]; + } + + template + void Parse(DecodedStream& ds) { + Allocator allocator; + Stack operandStack(&allocator, 256); // Frag + Stack operatorStack(&allocator, 256); // Operator + Stack atomCountStack(&allocator, 256); // unsigned (Atom per parenthesis) + + *atomCountStack.template Push() = 0; + + unsigned codepoint; + while (ds.Peek() != 0) { + switch (codepoint = ds.Take()) { + case '^': + anchorBegin_ = true; + break; + + case '$': + anchorEnd_ = true; + break; + + case '|': + while (!operatorStack.Empty() && *operatorStack.template Top() < kAlternation) + if (!Eval(operandStack, *operatorStack.template Pop(1))) + return; + *operatorStack.template Push() = kAlternation; + *atomCountStack.template Top() = 0; + break; + + case '(': + *operatorStack.template Push() = kLeftParenthesis; + *atomCountStack.template Push() = 0; + break; + + case ')': + while (!operatorStack.Empty() && *operatorStack.template Top() != kLeftParenthesis) + if (!Eval(operandStack, *operatorStack.template Pop(1))) + return; + if (operatorStack.Empty()) + return; + operatorStack.template Pop(1); + atomCountStack.template Pop(1); + ImplicitConcatenation(atomCountStack, operatorStack); + break; + + case '?': + if (!Eval(operandStack, kZeroOrOne)) + return; + break; + + case '*': + if (!Eval(operandStack, kZeroOrMore)) + return; + break; + + case '+': + if (!Eval(operandStack, kOneOrMore)) + return; + break; + + case '{': + { + unsigned n, m; + if (!ParseUnsigned(ds, &n)) + return; + + if (ds.Peek() == ',') { + ds.Take(); + if (ds.Peek() == '}') + m = kInfinityQuantifier; + else if (!ParseUnsigned(ds, &m) || m < n) + return; + } + else + m = n; + + if (!EvalQuantifier(operandStack, n, m) || ds.Peek() != '}') + return; + ds.Take(); + } + break; + + case '.': + PushOperand(operandStack, kAnyCharacterClass); + ImplicitConcatenation(atomCountStack, operatorStack); + break; + + case '[': + { + SizeType range; + if (!ParseRange(ds, &range)) + return; + SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, kRangeCharacterClass); + GetState(s).rangeStart = range; + *operandStack.template Push() = Frag(s, s, s); + } + ImplicitConcatenation(atomCountStack, operatorStack); + break; + + case '\\': // Escape character + if (!CharacterEscape(ds, &codepoint)) + return; // Unsupported escape character + // fall through to default + + default: // Pattern character + PushOperand(operandStack, codepoint); + ImplicitConcatenation(atomCountStack, operatorStack); + } + } + + while (!operatorStack.Empty()) + if (!Eval(operandStack, *operatorStack.template Pop(1))) + return; + + // Link the operand to matching state. + if (operandStack.GetSize() == sizeof(Frag)) { + Frag* e = operandStack.template Pop(1); + Patch(e->out, NewState(kRegexInvalidState, kRegexInvalidState, 0)); + root_ = e->start; + +#if RAPIDJSON_REGEX_VERBOSE + printf("root: %d\n", root_); + for (SizeType i = 0; i < stateCount_ ; i++) { + State& s = GetState(i); + printf("[%2d] out: %2d out1: %2d c: '%c'\n", i, s.out, s.out1, (char)s.codepoint); + } + printf("\n"); +#endif + } + + // Preallocate buffer for SearchWithAnchoring() + RAPIDJSON_ASSERT(stateSet_ == 0); + if (stateCount_ > 0) { + stateSet_ = static_cast(states_.GetAllocator().Malloc(GetStateSetSize())); + state0_.template Reserve(stateCount_); + state1_.template Reserve(stateCount_); + } + } + + SizeType NewState(SizeType out, SizeType out1, unsigned codepoint) { + State* s = states_.template Push(); + s->out = out; + s->out1 = out1; + s->codepoint = codepoint; + s->rangeStart = kRegexInvalidRange; + return stateCount_++; + } + + void PushOperand(Stack& operandStack, unsigned codepoint) { + SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, codepoint); + *operandStack.template Push() = Frag(s, s, s); + } + + void ImplicitConcatenation(Stack& atomCountStack, Stack& operatorStack) { + if (*atomCountStack.template Top()) + *operatorStack.template Push() = kConcatenation; + (*atomCountStack.template Top())++; + } + + SizeType Append(SizeType l1, SizeType l2) { + SizeType old = l1; + while (GetState(l1).out != kRegexInvalidState) + l1 = GetState(l1).out; + GetState(l1).out = l2; + return old; + } + + void Patch(SizeType l, SizeType s) { + for (SizeType next; l != kRegexInvalidState; l = next) { + next = GetState(l).out; + GetState(l).out = s; + } + } + + bool Eval(Stack& operandStack, Operator op) { + switch (op) { + case kConcatenation: + RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag) * 2); + { + Frag e2 = *operandStack.template Pop(1); + Frag e1 = *operandStack.template Pop(1); + Patch(e1.out, e2.start); + *operandStack.template Push() = Frag(e1.start, e2.out, Min(e1.minIndex, e2.minIndex)); + } + return true; + + case kAlternation: + if (operandStack.GetSize() >= sizeof(Frag) * 2) { + Frag e2 = *operandStack.template Pop(1); + Frag e1 = *operandStack.template Pop(1); + SizeType s = NewState(e1.start, e2.start, 0); + *operandStack.template Push() = Frag(s, Append(e1.out, e2.out), Min(e1.minIndex, e2.minIndex)); + return true; + } + return false; + + case kZeroOrOne: + if (operandStack.GetSize() >= sizeof(Frag)) { + Frag e = *operandStack.template Pop(1); + SizeType s = NewState(kRegexInvalidState, e.start, 0); + *operandStack.template Push() = Frag(s, Append(e.out, s), e.minIndex); + return true; + } + return false; + + case kZeroOrMore: + if (operandStack.GetSize() >= sizeof(Frag)) { + Frag e = *operandStack.template Pop(1); + SizeType s = NewState(kRegexInvalidState, e.start, 0); + Patch(e.out, s); + *operandStack.template Push() = Frag(s, s, e.minIndex); + return true; + } + return false; + + default: + RAPIDJSON_ASSERT(op == kOneOrMore); + if (operandStack.GetSize() >= sizeof(Frag)) { + Frag e = *operandStack.template Pop(1); + SizeType s = NewState(kRegexInvalidState, e.start, 0); + Patch(e.out, s); + *operandStack.template Push() = Frag(e.start, s, e.minIndex); + return true; + } + return false; + } + } + + bool EvalQuantifier(Stack& operandStack, unsigned n, unsigned m) { + RAPIDJSON_ASSERT(n <= m); + RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag)); + + if (n == 0) { + if (m == 0) // a{0} not support + return false; + else if (m == kInfinityQuantifier) + Eval(operandStack, kZeroOrMore); // a{0,} -> a* + else { + Eval(operandStack, kZeroOrOne); // a{0,5} -> a? + for (unsigned i = 0; i < m - 1; i++) + CloneTopOperand(operandStack); // a{0,5} -> a? a? a? a? a? + for (unsigned i = 0; i < m - 1; i++) + Eval(operandStack, kConcatenation); // a{0,5} -> a?a?a?a?a? + } + return true; + } + + for (unsigned i = 0; i < n - 1; i++) // a{3} -> a a a + CloneTopOperand(operandStack); + + if (m == kInfinityQuantifier) + Eval(operandStack, kOneOrMore); // a{3,} -> a a a+ + else if (m > n) { + CloneTopOperand(operandStack); // a{3,5} -> a a a a + Eval(operandStack, kZeroOrOne); // a{3,5} -> a a a a? + for (unsigned i = n; i < m - 1; i++) + CloneTopOperand(operandStack); // a{3,5} -> a a a a? a? + for (unsigned i = n; i < m; i++) + Eval(operandStack, kConcatenation); // a{3,5} -> a a aa?a? + } + + for (unsigned i = 0; i < n - 1; i++) + Eval(operandStack, kConcatenation); // a{3} -> aaa, a{3,} -> aaa+, a{3.5} -> aaaa?a? + + return true; + } + + static SizeType Min(SizeType a, SizeType b) { return a < b ? a : b; } + + void CloneTopOperand(Stack& operandStack) { + const Frag src = *operandStack.template Top(); // Copy constructor to prevent invalidation + SizeType count = stateCount_ - src.minIndex; // Assumes top operand contains states in [src->minIndex, stateCount_) + State* s = states_.template Push(count); + memcpy(s, &GetState(src.minIndex), count * sizeof(State)); + for (SizeType j = 0; j < count; j++) { + if (s[j].out != kRegexInvalidState) + s[j].out += count; + if (s[j].out1 != kRegexInvalidState) + s[j].out1 += count; + } + *operandStack.template Push() = Frag(src.start + count, src.out + count, src.minIndex + count); + stateCount_ += count; + } + + template + bool ParseUnsigned(DecodedStream& ds, unsigned* u) { + unsigned r = 0; + if (ds.Peek() < '0' || ds.Peek() > '9') + return false; + while (ds.Peek() >= '0' && ds.Peek() <= '9') { + if (r >= 429496729 && ds.Peek() > '5') // 2^32 - 1 = 4294967295 + return false; // overflow + r = r * 10 + (ds.Take() - '0'); + } + *u = r; + return true; + } + + template + bool ParseRange(DecodedStream& ds, SizeType* range) { + bool isBegin = true; + bool negate = false; + int step = 0; + SizeType start = kRegexInvalidRange; + SizeType current = kRegexInvalidRange; + unsigned codepoint; + while ((codepoint = ds.Take()) != 0) { + if (isBegin) { + isBegin = false; + if (codepoint == '^') { + negate = true; + continue; + } + } + + switch (codepoint) { + case ']': + if (start == kRegexInvalidRange) + return false; // Error: nothing inside [] + if (step == 2) { // Add trailing '-' + SizeType r = NewRange('-'); + RAPIDJSON_ASSERT(current != kRegexInvalidRange); + GetRange(current).next = r; + } + if (negate) + GetRange(start).start |= kRangeNegationFlag; + *range = start; + return true; + + case '\\': + if (ds.Peek() == 'b') { + ds.Take(); + codepoint = 0x0008; // Escape backspace character + } + else if (!CharacterEscape(ds, &codepoint)) + return false; + // fall through to default + + default: + switch (step) { + case 1: + if (codepoint == '-') { + step++; + break; + } + // fall through to step 0 for other characters + + case 0: + { + SizeType r = NewRange(codepoint); + if (current != kRegexInvalidRange) + GetRange(current).next = r; + if (start == kRegexInvalidRange) + start = r; + current = r; + } + step = 1; + break; + + default: + RAPIDJSON_ASSERT(step == 2); + GetRange(current).end = codepoint; + step = 0; + } + } + } + return false; + } + + SizeType NewRange(unsigned codepoint) { + Range* r = ranges_.template Push(); + r->start = r->end = codepoint; + r->next = kRegexInvalidRange; + return rangeCount_++; + } + + template + bool CharacterEscape(DecodedStream& ds, unsigned* escapedCodepoint) { + unsigned codepoint; + switch (codepoint = ds.Take()) { + case '^': + case '$': + case '|': + case '(': + case ')': + case '?': + case '*': + case '+': + case '.': + case '[': + case ']': + case '{': + case '}': + case '\\': + *escapedCodepoint = codepoint; return true; + case 'f': *escapedCodepoint = 0x000C; return true; + case 'n': *escapedCodepoint = 0x000A; return true; + case 'r': *escapedCodepoint = 0x000D; return true; + case 't': *escapedCodepoint = 0x0009; return true; + case 'v': *escapedCodepoint = 0x000B; return true; + default: + return false; // Unsupported escape character + } + } + + template + bool SearchWithAnchoring(InputStream& is, bool anchorBegin, bool anchorEnd) const { + RAPIDJSON_ASSERT(IsValid()); + DecodedStream ds(is); + + state0_.Clear(); + Stack *current = &state0_, *next = &state1_; + const size_t stateSetSize = GetStateSetSize(); + std::memset(stateSet_, 0, stateSetSize); + + bool matched = AddState(*current, root_); + unsigned codepoint; + while (!current->Empty() && (codepoint = ds.Take()) != 0) { + std::memset(stateSet_, 0, stateSetSize); + next->Clear(); + matched = false; + for (const SizeType* s = current->template Bottom(); s != current->template End(); ++s) { + const State& sr = GetState(*s); + if (sr.codepoint == codepoint || + sr.codepoint == kAnyCharacterClass || + (sr.codepoint == kRangeCharacterClass && MatchRange(sr.rangeStart, codepoint))) + { + matched = AddState(*next, sr.out) || matched; + if (!anchorEnd && matched) + return true; + } + if (!anchorBegin) + AddState(*next, root_); + } + internal::Swap(current, next); + } + + return matched; + } + + size_t GetStateSetSize() const { + return (stateCount_ + 31) / 32 * 4; + } + + // Return whether the added states is a match state + bool AddState(Stack& l, SizeType index) const { + RAPIDJSON_ASSERT(index != kRegexInvalidState); + + const State& s = GetState(index); + if (s.out1 != kRegexInvalidState) { // Split + bool matched = AddState(l, s.out); + return AddState(l, s.out1) || matched; + } + else if (!(stateSet_[index >> 5] & (1 << (index & 31)))) { + stateSet_[index >> 5] |= (1 << (index & 31)); + *l.template PushUnsafe() = index; + } + return s.out == kRegexInvalidState; // by using PushUnsafe() above, we can ensure s is not validated due to reallocation. + } + + bool MatchRange(SizeType rangeIndex, unsigned codepoint) const { + bool yes = (GetRange(rangeIndex).start & kRangeNegationFlag) == 0; + while (rangeIndex != kRegexInvalidRange) { + const Range& r = GetRange(rangeIndex); + if (codepoint >= (r.start & ~kRangeNegationFlag) && codepoint <= r.end) + return yes; + rangeIndex = r.next; + } + return !yes; + } + + Stack states_; + Stack ranges_; + SizeType root_; + SizeType stateCount_; + SizeType rangeCount_; + + static const unsigned kInfinityQuantifier = ~0u; + + // For SearchWithAnchoring() + uint32_t* stateSet_; // allocated by states_.GetAllocator() + mutable Stack state0_; + mutable Stack state1_; + bool anchorBegin_; + bool anchorEnd_; +}; + +typedef GenericRegex > Regex; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_REGEX_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/stack.h b/rapidjson-1.1.0/rapid_json/internal/stack.h new file mode 100644 index 000000000..022c9aab4 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/stack.h @@ -0,0 +1,230 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_STACK_H_ +#define RAPIDJSON_INTERNAL_STACK_H_ + +#include "../allocators.h" +#include "swap.h" + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +/////////////////////////////////////////////////////////////////////////////// +// Stack + +//! A type-unsafe stack for storing different types of data. +/*! \tparam Allocator Allocator for allocating stack memory. +*/ +template +class Stack { +public: + // Optimization note: Do not allocate memory for stack_ in constructor. + // Do it lazily when first Push() -> Expand() -> Resize(). + Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) { + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + Stack(Stack&& rhs) + : allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + stack_(rhs.stack_), + stackTop_(rhs.stackTop_), + stackEnd_(rhs.stackEnd_), + initialCapacity_(rhs.initialCapacity_) + { + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.stack_ = 0; + rhs.stackTop_ = 0; + rhs.stackEnd_ = 0; + rhs.initialCapacity_ = 0; + } +#endif + + ~Stack() { + Destroy(); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + Stack& operator=(Stack&& rhs) { + if (&rhs != this) + { + Destroy(); + + allocator_ = rhs.allocator_; + ownAllocator_ = rhs.ownAllocator_; + stack_ = rhs.stack_; + stackTop_ = rhs.stackTop_; + stackEnd_ = rhs.stackEnd_; + initialCapacity_ = rhs.initialCapacity_; + + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.stack_ = 0; + rhs.stackTop_ = 0; + rhs.stackEnd_ = 0; + rhs.initialCapacity_ = 0; + } + return *this; + } +#endif + + void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT { + internal::Swap(allocator_, rhs.allocator_); + internal::Swap(ownAllocator_, rhs.ownAllocator_); + internal::Swap(stack_, rhs.stack_); + internal::Swap(stackTop_, rhs.stackTop_); + internal::Swap(stackEnd_, rhs.stackEnd_); + internal::Swap(initialCapacity_, rhs.initialCapacity_); + } + + void Clear() { stackTop_ = stack_; } + + void ShrinkToFit() { + if (Empty()) { + // If the stack is empty, completely deallocate the memory. + Allocator::Free(stack_); + stack_ = 0; + stackTop_ = 0; + stackEnd_ = 0; + } + else + Resize(GetSize()); + } + + // Optimization note: try to minimize the size of this function for force inline. + // Expansion is run very infrequently, so it is moved to another (probably non-inline) function. + template + RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) { + // Expand the stack if needed + if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count > stackEnd_)) + Expand(count); + } + + template + RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) { + Reserve(count); + return PushUnsafe(count); + } + + template + RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) { + RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_); + T* ret = reinterpret_cast(stackTop_); + stackTop_ += sizeof(T) * count; + return ret; + } + + template + T* Pop(size_t count) { + RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T)); + stackTop_ -= count * sizeof(T); + return reinterpret_cast(stackTop_); + } + + template + T* Top() { + RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); + return reinterpret_cast(stackTop_ - sizeof(T)); + } + + template + const T* Top() const { + RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); + return reinterpret_cast(stackTop_ - sizeof(T)); + } + + template + T* End() { return reinterpret_cast(stackTop_); } + + template + const T* End() const { return reinterpret_cast(stackTop_); } + + template + T* Bottom() { return reinterpret_cast(stack_); } + + template + const T* Bottom() const { return reinterpret_cast(stack_); } + + bool HasAllocator() const { + return allocator_ != 0; + } + + Allocator& GetAllocator() { + RAPIDJSON_ASSERT(allocator_); + return *allocator_; + } + + bool Empty() const { return stackTop_ == stack_; } + size_t GetSize() const { return static_cast(stackTop_ - stack_); } + size_t GetCapacity() const { return static_cast(stackEnd_ - stack_); } + +private: + template + void Expand(size_t count) { + // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity. + size_t newCapacity; + if (stack_ == 0) { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + newCapacity = initialCapacity_; + } else { + newCapacity = GetCapacity(); + newCapacity += (newCapacity + 1) / 2; + } + size_t newSize = GetSize() + sizeof(T) * count; + if (newCapacity < newSize) + newCapacity = newSize; + + Resize(newCapacity); + } + + void Resize(size_t newCapacity) { + const size_t size = GetSize(); // Backup the current size + stack_ = static_cast(allocator_->Realloc(stack_, GetCapacity(), newCapacity)); + stackTop_ = stack_ + size; + stackEnd_ = stack_ + newCapacity; + } + + void Destroy() { + Allocator::Free(stack_); + RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack + } + + // Prohibit copy constructor & assignment operator. + Stack(const Stack&); + Stack& operator=(const Stack&); + + Allocator* allocator_; + Allocator* ownAllocator_; + char *stack_; + char *stackTop_; + char *stackEnd_; + size_t initialCapacity_; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_STACK_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/strfunc.h b/rapidjson-1.1.0/rapid_json/internal/strfunc.h new file mode 100644 index 000000000..2edfae526 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/strfunc.h @@ -0,0 +1,55 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_STRFUNC_H_ +#define RAPIDJSON_INTERNAL_STRFUNC_H_ + +#include "../stream.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Custom strlen() which works on different character types. +/*! \tparam Ch Character type (e.g. char, wchar_t, short) + \param s Null-terminated input string. + \return Number of characters in the string. + \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints. +*/ +template +inline SizeType StrLen(const Ch* s) { + const Ch* p = s; + while (*p) ++p; + return SizeType(p - s); +} + +//! Returns number of code points in a encoded string. +template +bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) { + GenericStringStream is(s); + const typename Encoding::Ch* end = s + length; + SizeType count = 0; + while (is.src_ < end) { + unsigned codepoint; + if (!Encoding::Decode(is, &codepoint)) + return false; + count++; + } + *outCount = count; + return true; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_INTERNAL_STRFUNC_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/strtod.h b/rapidjson-1.1.0/rapid_json/internal/strtod.h new file mode 100644 index 000000000..289c413b0 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/strtod.h @@ -0,0 +1,269 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_STRTOD_ +#define RAPIDJSON_STRTOD_ + +#include "ieee754.h" +#include "biginteger.h" +#include "diyfp.h" +#include "pow10.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +inline double FastPath(double significand, int exp) { + if (exp < -308) + return 0.0; + else if (exp >= 0) + return significand * internal::Pow10(exp); + else + return significand / internal::Pow10(-exp); +} + +inline double StrtodNormalPrecision(double d, int p) { + if (p < -308) { + // Prevent expSum < -308, making Pow10(p) = 0 + d = FastPath(d, -308); + d = FastPath(d, p + 308); + } + else + d = FastPath(d, p); + return d; +} + +template +inline T Min3(T a, T b, T c) { + T m = a; + if (m > b) m = b; + if (m > c) m = c; + return m; +} + +inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) { + const Double db(b); + const uint64_t bInt = db.IntegerSignificand(); + const int bExp = db.IntegerExponent(); + const int hExp = bExp - 1; + + int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0; + + // Adjust for decimal exponent + if (dExp >= 0) { + dS_Exp2 += dExp; + dS_Exp5 += dExp; + } + else { + bS_Exp2 -= dExp; + bS_Exp5 -= dExp; + hS_Exp2 -= dExp; + hS_Exp5 -= dExp; + } + + // Adjust for binary exponent + if (bExp >= 0) + bS_Exp2 += bExp; + else { + dS_Exp2 -= bExp; + hS_Exp2 -= bExp; + } + + // Adjust for half ulp exponent + if (hExp >= 0) + hS_Exp2 += hExp; + else { + dS_Exp2 -= hExp; + bS_Exp2 -= hExp; + } + + // Remove common power of two factor from all three scaled values + int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2); + dS_Exp2 -= common_Exp2; + bS_Exp2 -= common_Exp2; + hS_Exp2 -= common_Exp2; + + BigInteger dS = d; + dS.MultiplyPow5(static_cast(dS_Exp5)) <<= static_cast(dS_Exp2); + + BigInteger bS(bInt); + bS.MultiplyPow5(static_cast(bS_Exp5)) <<= static_cast(bS_Exp2); + + BigInteger hS(1); + hS.MultiplyPow5(static_cast(hS_Exp5)) <<= static_cast(hS_Exp2); + + BigInteger delta(0); + dS.Difference(bS, &delta); + + return delta.Compare(hS); +} + +inline bool StrtodFast(double d, int p, double* result) { + // Use fast path for string-to-double conversion if possible + // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/ + if (p > 22 && p < 22 + 16) { + // Fast Path Cases In Disguise + d *= internal::Pow10(p - 22); + p = 22; + } + + if (p >= -22 && p <= 22 && d <= 9007199254740991.0) { // 2^53 - 1 + *result = FastPath(d, p); + return true; + } + else + return false; +} + +// Compute an approximation and see if it is within 1/2 ULP +inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosition, int exp, double* result) { + uint64_t significand = 0; + size_t i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999 + for (; i < length; i++) { + if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || + (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > '5')) + break; + significand = significand * 10u + static_cast(decimals[i] - '0'); + } + + if (i < length && decimals[i] >= '5') // Rounding + significand++; + + size_t remaining = length - i; + const unsigned kUlpShift = 3; + const unsigned kUlp = 1 << kUlpShift; + int64_t error = (remaining == 0) ? 0 : kUlp / 2; + + DiyFp v(significand, 0); + v = v.Normalize(); + error <<= -v.e; + + const int dExp = static_cast(decimalPosition) - static_cast(i) + exp; + + int actualExp; + DiyFp cachedPower = GetCachedPower10(dExp, &actualExp); + if (actualExp != dExp) { + static const DiyFp kPow10[] = { + DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 00000000), -60), // 10^1 + DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 00000000), -57), // 10^2 + DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 00000000), -54), // 10^3 + DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 00000000), -50), // 10^4 + DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 00000000), -47), // 10^5 + DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 00000000), -44), // 10^6 + DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 00000000), -40) // 10^7 + }; + int adjustment = dExp - actualExp - 1; + RAPIDJSON_ASSERT(adjustment >= 0 && adjustment < 7); + v = v * kPow10[adjustment]; + if (length + static_cast(adjustment)> 19u) // has more digits than decimal digits in 64-bit + error += kUlp / 2; + } + + v = v * cachedPower; + + error += kUlp + (error == 0 ? 0 : 1); + + const int oldExp = v.e; + v = v.Normalize(); + error <<= oldExp - v.e; + + const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e); + unsigned precisionSize = 64 - effectiveSignificandSize; + if (precisionSize + kUlpShift >= 64) { + unsigned scaleExp = (precisionSize + kUlpShift) - 63; + v.f >>= scaleExp; + v.e += scaleExp; + error = (error >> scaleExp) + 1 + static_cast(kUlp); + precisionSize -= scaleExp; + } + + DiyFp rounded(v.f >> precisionSize, v.e + static_cast(precisionSize)); + const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp; + const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp; + if (precisionBits >= halfWay + static_cast(error)) { + rounded.f++; + if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340) + rounded.f >>= 1; + rounded.e++; + } + } + + *result = rounded.ToDouble(); + + return halfWay - static_cast(error) >= precisionBits || precisionBits >= halfWay + static_cast(error); +} + +inline double StrtodBigInteger(double approx, const char* decimals, size_t length, size_t decimalPosition, int exp) { + const BigInteger dInt(decimals, length); + const int dExp = static_cast(decimalPosition) - static_cast(length) + exp; + Double a(approx); + int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp); + if (cmp < 0) + return a.Value(); // within half ULP + else if (cmp == 0) { + // Round towards even + if (a.Significand() & 1) + return a.NextPositiveDouble(); + else + return a.Value(); + } + else // adjustment + return a.NextPositiveDouble(); +} + +inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t length, size_t decimalPosition, int exp) { + RAPIDJSON_ASSERT(d >= 0.0); + RAPIDJSON_ASSERT(length >= 1); + + double result; + if (StrtodFast(d, p, &result)) + return result; + + // Trim leading zeros + while (*decimals == '0' && length > 1) { + length--; + decimals++; + decimalPosition--; + } + + // Trim trailing zeros + while (decimals[length - 1] == '0' && length > 1) { + length--; + decimalPosition--; + exp++; + } + + // Trim right-most digits + const int kMaxDecimalDigit = 780; + if (static_cast(length) > kMaxDecimalDigit) { + int delta = (static_cast(length) - kMaxDecimalDigit); + exp += delta; + decimalPosition -= static_cast(delta); + length = kMaxDecimalDigit; + } + + // If too small, underflow to zero + if (int(length) + exp < -324) + return 0.0; + + if (StrtodDiyFp(decimals, length, decimalPosition, exp, &result)) + return result; + + // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison + return StrtodBigInteger(result, decimals, length, decimalPosition, exp); +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_STRTOD_ diff --git a/rapidjson-1.1.0/rapid_json/internal/swap.h b/rapidjson-1.1.0/rapid_json/internal/swap.h new file mode 100644 index 000000000..666e49f97 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/swap.h @@ -0,0 +1,46 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_SWAP_H_ +#define RAPIDJSON_INTERNAL_SWAP_H_ + +#include "../rapidjson.h" + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Custom swap() to avoid dependency on C++ header +/*! \tparam T Type of the arguments to swap, should be instantiated with primitive C++ types only. + \note This has the same semantics as std::swap(). +*/ +template +inline void Swap(T& a, T& b) RAPIDJSON_NOEXCEPT { + T tmp = a; + a = b; + b = tmp; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_SWAP_H_ diff --git a/rapidjson-1.1.0/rapid_json/istreamwrapper.h b/rapidjson-1.1.0/rapid_json/istreamwrapper.h new file mode 100644 index 000000000..f5fe28977 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/istreamwrapper.h @@ -0,0 +1,115 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ISTREAMWRAPPER_H_ +#define RAPIDJSON_ISTREAMWRAPPER_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of \c std::basic_istream into RapidJSON's Stream concept. +/*! + The classes can be wrapped including but not limited to: + + - \c std::istringstream + - \c std::stringstream + - \c std::wistringstream + - \c std::wstringstream + - \c std::ifstream + - \c std::fstream + - \c std::wifstream + - \c std::wfstream + + \tparam StreamType Class derived from \c std::basic_istream. +*/ + +template +class BasicIStreamWrapper { +public: + typedef typename StreamType::char_type Ch; + BasicIStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {} + + Ch Peek() const { + typename StreamType::int_type c = stream_.peek(); + return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast(c) : '\0'; + } + + Ch Take() { + typename StreamType::int_type c = stream_.get(); + if (RAPIDJSON_LIKELY(c != StreamType::traits_type::eof())) { + count_++; + return static_cast(c); + } + else + return '\0'; + } + + // tellg() may return -1 when failed. So we count by ourself. + size_t Tell() const { return count_; } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + RAPIDJSON_ASSERT(sizeof(Ch) == 1); // Only usable for byte stream. + int i; + bool hasError = false; + for (i = 0; i < 4; ++i) { + typename StreamType::int_type c = stream_.get(); + if (c == StreamType::traits_type::eof()) { + hasError = true; + stream_.clear(); + break; + } + peekBuffer_[i] = static_cast(c); + } + for (--i; i >= 0; --i) + stream_.putback(peekBuffer_[i]); + return !hasError ? peekBuffer_ : 0; + } + +private: + BasicIStreamWrapper(const BasicIStreamWrapper&); + BasicIStreamWrapper& operator=(const BasicIStreamWrapper&); + + StreamType& stream_; + size_t count_; //!< Number of characters read. Note: + mutable Ch peekBuffer_[4]; +}; + +typedef BasicIStreamWrapper IStreamWrapper; +typedef BasicIStreamWrapper WIStreamWrapper; + +#if defined(__clang__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ISTREAMWRAPPER_H_ diff --git a/rapidjson-1.1.0/rapid_json/memorybuffer.h b/rapidjson-1.1.0/rapid_json/memorybuffer.h new file mode 100644 index 000000000..39bee1dec --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/memorybuffer.h @@ -0,0 +1,70 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_MEMORYBUFFER_H_ +#define RAPIDJSON_MEMORYBUFFER_H_ + +#include "stream.h" +#include "internal/stack.h" + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory output byte stream. +/*! + This class is mainly for being wrapped by EncodedOutputStream or AutoUTFOutputStream. + + It is similar to FileWriteBuffer but the destination is an in-memory buffer instead of a file. + + Differences between MemoryBuffer and StringBuffer: + 1. StringBuffer has Encoding but MemoryBuffer is only a byte buffer. + 2. StringBuffer::GetString() returns a null-terminated string. MemoryBuffer::GetBuffer() returns a buffer without terminator. + + \tparam Allocator type for allocating memory buffer. + \note implements Stream concept +*/ +template +struct GenericMemoryBuffer { + typedef char Ch; // byte + + GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} + + void Put(Ch c) { *stack_.template Push() = c; } + void Flush() {} + + void Clear() { stack_.Clear(); } + void ShrinkToFit() { stack_.ShrinkToFit(); } + Ch* Push(size_t count) { return stack_.template Push(count); } + void Pop(size_t count) { stack_.template Pop(count); } + + const Ch* GetBuffer() const { + return stack_.template Bottom(); + } + + size_t GetSize() const { return stack_.GetSize(); } + + static const size_t kDefaultCapacity = 256; + mutable internal::Stack stack_; +}; + +typedef GenericMemoryBuffer<> MemoryBuffer; + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) { + std::memset(memoryBuffer.stack_.Push(n), c, n * sizeof(c)); +} + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_MEMORYBUFFER_H_ diff --git a/rapidjson-1.1.0/rapid_json/memorystream.h b/rapidjson-1.1.0/rapid_json/memorystream.h new file mode 100644 index 000000000..1d71d8a4f --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/memorystream.h @@ -0,0 +1,71 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_MEMORYSTREAM_H_ +#define RAPIDJSON_MEMORYSTREAM_H_ + +#include "stream.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(unreachable-code) +RAPIDJSON_DIAG_OFF(missing-noreturn) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory input byte stream. +/*! + This class is mainly for being wrapped by EncodedInputStream or AutoUTFInputStream. + + It is similar to FileReadBuffer but the source is an in-memory buffer instead of a file. + + Differences between MemoryStream and StringStream: + 1. StringStream has encoding but MemoryStream is a byte stream. + 2. MemoryStream needs size of the source buffer and the buffer don't need to be null terminated. StringStream assume null-terminated string as source. + 3. MemoryStream supports Peek4() for encoding detection. StringStream is specified with an encoding so it should not have Peek4(). + \note implements Stream concept +*/ +struct MemoryStream { + typedef char Ch; // byte + + MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {} + + Ch Peek() const { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_; } + Ch Take() { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_++; } + size_t Tell() const { return static_cast(src_ - begin_); } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + return Tell() + 4 <= size_ ? src_ : 0; + } + + const Ch* src_; //!< Current read position. + const Ch* begin_; //!< Original head of the string. + const Ch* end_; //!< End of stream. + size_t size_; //!< Size of the stream. +}; + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_MEMORYBUFFER_H_ diff --git a/rapidjson-1.1.0/rapid_json/ostreamwrapper.h b/rapidjson-1.1.0/rapid_json/ostreamwrapper.h new file mode 100644 index 000000000..6f4667c08 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/ostreamwrapper.h @@ -0,0 +1,81 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_OSTREAMWRAPPER_H_ +#define RAPIDJSON_OSTREAMWRAPPER_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of \c std::basic_ostream into RapidJSON's Stream concept. +/*! + The classes can be wrapped including but not limited to: + + - \c std::ostringstream + - \c std::stringstream + - \c std::wpstringstream + - \c std::wstringstream + - \c std::ifstream + - \c std::fstream + - \c std::wofstream + - \c std::wfstream + + \tparam StreamType Class derived from \c std::basic_ostream. +*/ + +template +class BasicOStreamWrapper { +public: + typedef typename StreamType::char_type Ch; + BasicOStreamWrapper(StreamType& stream) : stream_(stream) {} + + void Put(Ch c) { + stream_.put(c); + } + + void Flush() { + stream_.flush(); + } + + // Not implemented + char Peek() const { RAPIDJSON_ASSERT(false); return 0; } + char Take() { RAPIDJSON_ASSERT(false); return 0; } + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + BasicOStreamWrapper(const BasicOStreamWrapper&); + BasicOStreamWrapper& operator=(const BasicOStreamWrapper&); + + StreamType& stream_; +}; + +typedef BasicOStreamWrapper OStreamWrapper; +typedef BasicOStreamWrapper WOStreamWrapper; + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_OSTREAMWRAPPER_H_ diff --git a/rapidjson-1.1.0/rapid_json/pointer.h b/rapidjson-1.1.0/rapid_json/pointer.h new file mode 100644 index 000000000..0206ac1c8 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/pointer.h @@ -0,0 +1,1358 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_POINTER_H_ +#define RAPIDJSON_POINTER_H_ + +#include "document.h" +#include "internal/itoa.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(switch-enum) +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +static const SizeType kPointerInvalidIndex = ~SizeType(0); //!< Represents an invalid index in GenericPointer::Token + +//! Error code of parsing. +/*! \ingroup RAPIDJSON_ERRORS + \see GenericPointer::GenericPointer, GenericPointer::GetParseErrorCode +*/ +enum PointerParseErrorCode { + kPointerParseErrorNone = 0, //!< The parse is successful + + kPointerParseErrorTokenMustBeginWithSolidus, //!< A token must begin with a '/' + kPointerParseErrorInvalidEscape, //!< Invalid escape + kPointerParseErrorInvalidPercentEncoding, //!< Invalid percent encoding in URI fragment + kPointerParseErrorCharacterMustPercentEncode //!< A character must percent encoded in URI fragment +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericPointer + +//! Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator. +/*! + This class implements RFC 6901 "JavaScript Object Notation (JSON) Pointer" + (https://tools.ietf.org/html/rfc6901). + + A JSON pointer is for identifying a specific value in a JSON document + (GenericDocument). It can simplify coding of DOM tree manipulation, because it + can access multiple-level depth of DOM tree with single API call. + + After it parses a string representation (e.g. "/foo/0" or URI fragment + representation (e.g. "#/foo/0") into its internal representation (tokens), + it can be used to resolve a specific value in multiple documents, or sub-tree + of documents. + + Contrary to GenericValue, Pointer can be copy constructed and copy assigned. + Apart from assignment, a Pointer cannot be modified after construction. + + Although Pointer is very convenient, please aware that constructing Pointer + involves parsing and dynamic memory allocation. A special constructor with user- + supplied tokens eliminates these. + + GenericPointer depends on GenericDocument and GenericValue. + + \tparam ValueType The value type of the DOM tree. E.g. GenericValue > + \tparam Allocator The allocator type for allocating memory for internal representation. + + \note GenericPointer uses same encoding of ValueType. + However, Allocator of GenericPointer is independent of Allocator of Value. +*/ +template +class GenericPointer { +public: + typedef typename ValueType::EncodingType EncodingType; //!< Encoding type from Value + typedef typename ValueType::Ch Ch; //!< Character type from Value + + //! A token is the basic units of internal representation. + /*! + A JSON pointer string representation "/foo/123" is parsed to two tokens: + "foo" and 123. 123 will be represented in both numeric form and string form. + They are resolved according to the actual value type (object or array). + + For token that are not numbers, or the numeric value is out of bound + (greater than limits of SizeType), they are only treated as string form + (i.e. the token's index will be equal to kPointerInvalidIndex). + + This struct is public so that user can create a Pointer without parsing and + allocation, using a special constructor. + */ + struct Token { + const Ch* name; //!< Name of the token. It has null character at the end but it can contain null character. + SizeType length; //!< Length of the name. + SizeType index; //!< A valid array index, if it is not equal to kPointerInvalidIndex. + }; + + //!@name Constructors and destructor. + //@{ + + //! Default constructor. + GenericPointer(Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) {} + + //! Constructor that parses a string or URI fragment representation. + /*! + \param source A null-terminated, string or URI fragment representation of JSON pointer. + \param allocator User supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one. + */ + explicit GenericPointer(const Ch* source, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + Parse(source, internal::StrLen(source)); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Constructor that parses a string or URI fragment representation. + /*! + \param source A string or URI fragment representation of JSON pointer. + \param allocator User supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one. + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + explicit GenericPointer(const std::basic_string& source, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + Parse(source.c_str(), source.size()); + } +#endif + + //! Constructor that parses a string or URI fragment representation, with length of the source string. + /*! + \param source A string or URI fragment representation of JSON pointer. + \param length Length of source. + \param allocator User supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one. + \note Slightly faster than the overload without length. + */ + GenericPointer(const Ch* source, size_t length, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + Parse(source, length); + } + + //! Constructor with user-supplied tokens. + /*! + This constructor let user supplies const array of tokens. + This prevents the parsing process and eliminates allocation. + This is preferred for memory constrained environments. + + \param tokens An constant array of tokens representing the JSON pointer. + \param tokenCount Number of tokens. + + \b Example + \code + #define NAME(s) { s, sizeof(s) / sizeof(s[0]) - 1, kPointerInvalidIndex } + #define INDEX(i) { #i, sizeof(#i) - 1, i } + + static const Pointer::Token kTokens[] = { NAME("foo"), INDEX(123) }; + static const Pointer p(kTokens, sizeof(kTokens) / sizeof(kTokens[0])); + // Equivalent to static const Pointer p("/foo/123"); + + #undef NAME + #undef INDEX + \endcode + */ + GenericPointer(const Token* tokens, size_t tokenCount) : allocator_(), ownAllocator_(), nameBuffer_(), tokens_(const_cast(tokens)), tokenCount_(tokenCount), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) {} + + //! Copy constructor. + GenericPointer(const GenericPointer& rhs, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + *this = rhs; + } + + //! Destructor. + ~GenericPointer() { + if (nameBuffer_) // If user-supplied tokens constructor is used, nameBuffer_ is nullptr and tokens_ are not deallocated. + Allocator::Free(tokens_); + RAPIDJSON_DELETE(ownAllocator_); + } + + //! Assignment operator. + GenericPointer& operator=(const GenericPointer& rhs) { + if (this != &rhs) { + // Do not delete ownAllcator + if (nameBuffer_) + Allocator::Free(tokens_); + + tokenCount_ = rhs.tokenCount_; + parseErrorOffset_ = rhs.parseErrorOffset_; + parseErrorCode_ = rhs.parseErrorCode_; + + if (rhs.nameBuffer_) + CopyFromRaw(rhs); // Normally parsed tokens. + else { + tokens_ = rhs.tokens_; // User supplied const tokens. + nameBuffer_ = 0; + } + } + return *this; + } + + //@} + + //!@name Append token + //@{ + + //! Append a token and return a new Pointer + /*! + \param token Token to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const Token& token, Allocator* allocator = 0) const { + GenericPointer r; + r.allocator_ = allocator; + Ch *p = r.CopyFromRaw(*this, 1, token.length + 1); + std::memcpy(p, token.name, (token.length + 1) * sizeof(Ch)); + r.tokens_[tokenCount_].name = p; + r.tokens_[tokenCount_].length = token.length; + r.tokens_[tokenCount_].index = token.index; + return r; + } + + //! Append a name token with length, and return a new Pointer + /*! + \param name Name to be appended. + \param length Length of name. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const Ch* name, SizeType length, Allocator* allocator = 0) const { + Token token = { name, length, kPointerInvalidIndex }; + return Append(token, allocator); + } + + //! Append a name token without length, and return a new Pointer + /*! + \param name Name (const Ch*) to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >), (GenericPointer)) + Append(T* name, Allocator* allocator = 0) const { + return Append(name, StrLen(name), allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Append a name token, and return a new Pointer + /*! + \param name Name to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const std::basic_string& name, Allocator* allocator = 0) const { + return Append(name.c_str(), static_cast(name.size()), allocator); + } +#endif + + //! Append a index token, and return a new Pointer + /*! + \param index Index to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(SizeType index, Allocator* allocator = 0) const { + char buffer[21]; + char* end = sizeof(SizeType) == 4 ? internal::u32toa(index, buffer) : internal::u64toa(index, buffer); + SizeType length = static_cast(end - buffer); + buffer[length] = '\0'; + + if (sizeof(Ch) == 1) { + Token token = { reinterpret_cast(buffer), length, index }; + return Append(token, allocator); + } + else { + Ch name[21]; + for (size_t i = 0; i <= length; i++) + name[i] = buffer[i]; + Token token = { name, length, index }; + return Append(token, allocator); + } + } + + //! Append a token by value, and return a new Pointer + /*! + \param token token to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const ValueType& token, Allocator* allocator = 0) const { + if (token.IsString()) + return Append(token.GetString(), token.GetStringLength(), allocator); + else { + RAPIDJSON_ASSERT(token.IsUint64()); + RAPIDJSON_ASSERT(token.GetUint64() <= SizeType(~0)); + return Append(static_cast(token.GetUint64()), allocator); + } + } + + //!@name Handling Parse Error + //@{ + + //! Check whether this is a valid pointer. + bool IsValid() const { return parseErrorCode_ == kPointerParseErrorNone; } + + //! Get the parsing error offset in code unit. + size_t GetParseErrorOffset() const { return parseErrorOffset_; } + + //! Get the parsing error code. + PointerParseErrorCode GetParseErrorCode() const { return parseErrorCode_; } + + //@} + + //! Get the allocator of this pointer. + Allocator& GetAllocator() { return *allocator_; } + + //!@name Tokens + //@{ + + //! Get the token array (const version only). + const Token* GetTokens() const { return tokens_; } + + //! Get the number of tokens. + size_t GetTokenCount() const { return tokenCount_; } + + //@} + + //!@name Equality/inequality operators + //@{ + + //! Equality operator. + /*! + \note When any pointers are invalid, always returns false. + */ + bool operator==(const GenericPointer& rhs) const { + if (!IsValid() || !rhs.IsValid() || tokenCount_ != rhs.tokenCount_) + return false; + + for (size_t i = 0; i < tokenCount_; i++) { + if (tokens_[i].index != rhs.tokens_[i].index || + tokens_[i].length != rhs.tokens_[i].length || + (tokens_[i].length != 0 && std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch)* tokens_[i].length) != 0)) + { + return false; + } + } + + return true; + } + + //! Inequality operator. + /*! + \note When any pointers are invalid, always returns true. + */ + bool operator!=(const GenericPointer& rhs) const { return !(*this == rhs); } + + //@} + + //!@name Stringify + //@{ + + //! Stringify the pointer into string representation. + /*! + \tparam OutputStream Type of output stream. + \param os The output stream. + */ + template + bool Stringify(OutputStream& os) const { + return Stringify(os); + } + + //! Stringify the pointer into URI fragment representation. + /*! + \tparam OutputStream Type of output stream. + \param os The output stream. + */ + template + bool StringifyUriFragment(OutputStream& os) const { + return Stringify(os); + } + + //@} + + //!@name Create value + //@{ + + //! Create a value in a subtree. + /*! + If the value is not exist, it creates all parent values and a JSON Null value. + So it always succeed and return the newly created or existing value. + + Remind that it may change types of parents according to tokens, so it + potentially removes previously stored values. For example, if a document + was an array, and "/foo" is used to create a value, then the document + will be changed to an object, and all existing array elements are lost. + + \param root Root value of a DOM subtree to be resolved. It can be any value other than document root. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \param alreadyExist If non-null, it stores whether the resolved value is already exist. + \return The resolved newly created (a JSON Null value), or already exists value. + */ + ValueType& Create(ValueType& root, typename ValueType::AllocatorType& allocator, bool* alreadyExist = 0) const { + RAPIDJSON_ASSERT(IsValid()); + ValueType* v = &root; + bool exist = true; + for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + if (v->IsArray() && t->name[0] == '-' && t->length == 1) { + v->PushBack(ValueType().Move(), allocator); + v = &((*v)[v->Size() - 1]); + exist = false; + } + else { + if (t->index == kPointerInvalidIndex) { // must be object name + if (!v->IsObject()) + v->SetObject(); // Change to Object + } + else { // object name or array index + if (!v->IsArray() && !v->IsObject()) + v->SetArray(); // Change to Array + } + + if (v->IsArray()) { + if (t->index >= v->Size()) { + v->Reserve(t->index + 1, allocator); + while (t->index >= v->Size()) + v->PushBack(ValueType().Move(), allocator); + exist = false; + } + v = &((*v)[t->index]); + } + else { + typename ValueType::MemberIterator m = v->FindMember(GenericStringRef(t->name, t->length)); + if (m == v->MemberEnd()) { + v->AddMember(ValueType(t->name, t->length, allocator).Move(), ValueType().Move(), allocator); + v = &(--v->MemberEnd())->value; // Assumes AddMember() appends at the end + exist = false; + } + else + v = &m->value; + } + } + } + + if (alreadyExist) + *alreadyExist = exist; + + return *v; + } + + //! Creates a value in a document. + /*! + \param document A document to be resolved. + \param alreadyExist If non-null, it stores whether the resolved value is already exist. + \return The resolved newly created, or already exists value. + */ + template + ValueType& Create(GenericDocument& document, bool* alreadyExist = 0) const { + return Create(document, document.GetAllocator(), alreadyExist); + } + + //@} + + //!@name Query value + //@{ + + //! Query a value in a subtree. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param unresolvedTokenIndex If the pointer cannot resolve a token in the pointer, this parameter can obtain the index of unresolved token. + \return Pointer to the value if it can be resolved. Otherwise null. + + \note + There are only 3 situations when a value cannot be resolved: + 1. A value in the path is not an array nor object. + 2. An object value does not contain the token. + 3. A token is out of range of an array value. + + Use unresolvedTokenIndex to retrieve the token index. + */ + ValueType* Get(ValueType& root, size_t* unresolvedTokenIndex = 0) const { + RAPIDJSON_ASSERT(IsValid()); + ValueType* v = &root; + for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + switch (v->GetType()) { + case kObjectType: + { + typename ValueType::MemberIterator m = v->FindMember(GenericStringRef(t->name, t->length)); + if (m == v->MemberEnd()) + break; + v = &m->value; + } + continue; + case kArrayType: + if (t->index == kPointerInvalidIndex || t->index >= v->Size()) + break; + v = &((*v)[t->index]); + continue; + default: + break; + } + + // Error: unresolved token + if (unresolvedTokenIndex) + *unresolvedTokenIndex = static_cast(t - tokens_); + return 0; + } + return v; + } + + //! Query a const value in a const subtree. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \return Pointer to the value if it can be resolved. Otherwise null. + */ + const ValueType* Get(const ValueType& root, size_t* unresolvedTokenIndex = 0) const { + return Get(const_cast(root), unresolvedTokenIndex); + } + + //@} + + //!@name Query a value with default + //@{ + + //! Query a value in a subtree with default value. + /*! + Similar to Get(), but if the specified value do not exists, it creates all parents and clone the default value. + So that this function always succeed. + + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param defaultValue Default value to be cloned if the value was not exists. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \see Create() + */ + ValueType& GetWithDefault(ValueType& root, const ValueType& defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + Value& v = Create(root, allocator, &alreadyExist); + return alreadyExist ? v : v.CopyFrom(defaultValue, allocator); + } + + //! Query a value in a subtree with default null-terminated string. + ValueType& GetWithDefault(ValueType& root, const Ch* defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + Value& v = Create(root, allocator, &alreadyExist); + return alreadyExist ? v : v.SetString(defaultValue, allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Query a value in a subtree with default std::basic_string. + ValueType& GetWithDefault(ValueType& root, const std::basic_string& defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + Value& v = Create(root, allocator, &alreadyExist); + return alreadyExist ? v : v.SetString(defaultValue, allocator); + } +#endif + + //! Query a value in a subtree with default primitive value. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + GetWithDefault(ValueType& root, T defaultValue, typename ValueType::AllocatorType& allocator) const { + return GetWithDefault(root, ValueType(defaultValue).Move(), allocator); + } + + //! Query a value in a document with default value. + template + ValueType& GetWithDefault(GenericDocument& document, const ValueType& defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } + + //! Query a value in a document with default null-terminated string. + template + ValueType& GetWithDefault(GenericDocument& document, const Ch* defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Query a value in a document with default std::basic_string. + template + ValueType& GetWithDefault(GenericDocument& document, const std::basic_string& defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } +#endif + + //! Query a value in a document with default primitive value. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + GetWithDefault(GenericDocument& document, T defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } + + //@} + + //!@name Set a value + //@{ + + //! Set a value in a subtree, with move semantics. + /*! + It creates all parents if they are not exist or types are different to the tokens. + So this function always succeeds but potentially remove existing values. + + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param value Value to be set. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \see Create() + */ + ValueType& Set(ValueType& root, ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = value; + } + + //! Set a value in a subtree, with copy semantics. + ValueType& Set(ValueType& root, const ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator).CopyFrom(value, allocator); + } + + //! Set a null-terminated string in a subtree. + ValueType& Set(ValueType& root, const Ch* value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = ValueType(value, allocator).Move(); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Set a std::basic_string in a subtree. + ValueType& Set(ValueType& root, const std::basic_string& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = ValueType(value, allocator).Move(); + } +#endif + + //! Set a primitive value in a subtree. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + Set(ValueType& root, T value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = ValueType(value).Move(); + } + + //! Set a value in a document, with move semantics. + template + ValueType& Set(GenericDocument& document, ValueType& value) const { + return Create(document) = value; + } + + //! Set a value in a document, with copy semantics. + template + ValueType& Set(GenericDocument& document, const ValueType& value) const { + return Create(document).CopyFrom(value, document.GetAllocator()); + } + + //! Set a null-terminated string in a document. + template + ValueType& Set(GenericDocument& document, const Ch* value) const { + return Create(document) = ValueType(value, document.GetAllocator()).Move(); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Sets a std::basic_string in a document. + template + ValueType& Set(GenericDocument& document, const std::basic_string& value) const { + return Create(document) = ValueType(value, document.GetAllocator()).Move(); + } +#endif + + //! Set a primitive value in a document. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + Set(GenericDocument& document, T value) const { + return Create(document) = value; + } + + //@} + + //!@name Swap a value + //@{ + + //! Swap a value with a value in a subtree. + /*! + It creates all parents if they are not exist or types are different to the tokens. + So this function always succeeds but potentially remove existing values. + + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param value Value to be swapped. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \see Create() + */ + ValueType& Swap(ValueType& root, ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator).Swap(value); + } + + //! Swap a value with a value in a document. + template + ValueType& Swap(GenericDocument& document, ValueType& value) const { + return Create(document).Swap(value); + } + + //@} + + //! Erase a value in a subtree. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \return Whether the resolved value is found and erased. + + \note Erasing with an empty pointer \c Pointer(""), i.e. the root, always fail and return false. + */ + bool Erase(ValueType& root) const { + RAPIDJSON_ASSERT(IsValid()); + if (tokenCount_ == 0) // Cannot erase the root + return false; + + ValueType* v = &root; + const Token* last = tokens_ + (tokenCount_ - 1); + for (const Token *t = tokens_; t != last; ++t) { + switch (v->GetType()) { + case kObjectType: + { + typename ValueType::MemberIterator m = v->FindMember(GenericStringRef(t->name, t->length)); + if (m == v->MemberEnd()) + return false; + v = &m->value; + } + break; + case kArrayType: + if (t->index == kPointerInvalidIndex || t->index >= v->Size()) + return false; + v = &((*v)[t->index]); + break; + default: + return false; + } + } + + switch (v->GetType()) { + case kObjectType: + return v->EraseMember(GenericStringRef(last->name, last->length)); + case kArrayType: + if (last->index == kPointerInvalidIndex || last->index >= v->Size()) + return false; + v->Erase(v->Begin() + last->index); + return true; + default: + return false; + } + } + +private: + //! Clone the content from rhs to this. + /*! + \param rhs Source pointer. + \param extraToken Extra tokens to be allocated. + \param extraNameBufferSize Extra name buffer size (in number of Ch) to be allocated. + \return Start of non-occupied name buffer, for storing extra names. + */ + Ch* CopyFromRaw(const GenericPointer& rhs, size_t extraToken = 0, size_t extraNameBufferSize = 0) { + if (!allocator_) // allocator is independently owned. + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + + size_t nameBufferSize = rhs.tokenCount_; // null terminators for tokens + for (Token *t = rhs.tokens_; t != rhs.tokens_ + rhs.tokenCount_; ++t) + nameBufferSize += t->length; + + tokenCount_ = rhs.tokenCount_ + extraToken; + tokens_ = static_cast(allocator_->Malloc(tokenCount_ * sizeof(Token) + (nameBufferSize + extraNameBufferSize) * sizeof(Ch))); + nameBuffer_ = reinterpret_cast(tokens_ + tokenCount_); + if (rhs.tokenCount_ > 0) { + std::memcpy(tokens_, rhs.tokens_, rhs.tokenCount_ * sizeof(Token)); + } + if (nameBufferSize > 0) { + std::memcpy(nameBuffer_, rhs.nameBuffer_, nameBufferSize * sizeof(Ch)); + } + + // Adjust pointers to name buffer + std::ptrdiff_t diff = nameBuffer_ - rhs.nameBuffer_; + for (Token *t = tokens_; t != tokens_ + rhs.tokenCount_; ++t) + t->name += diff; + + return nameBuffer_ + nameBufferSize; + } + + //! Check whether a character should be percent-encoded. + /*! + According to RFC 3986 2.3 Unreserved Characters. + \param c The character (code unit) to be tested. + */ + bool NeedPercentEncode(Ch c) const { + return !((c >= '0' && c <= '9') || (c >= 'A' && c <='Z') || (c >= 'a' && c <= 'z') || c == '-' || c == '.' || c == '_' || c =='~'); + } + + //! Parse a JSON String or its URI fragment representation into tokens. +#ifndef __clang__ // -Wdocumentation + /*! + \param source Either a JSON Pointer string, or its URI fragment representation. Not need to be null terminated. + \param length Length of the source string. + \note Source cannot be JSON String Representation of JSON Pointer, e.g. In "/\u0000", \u0000 will not be unescaped. + */ +#endif + void Parse(const Ch* source, size_t length) { + RAPIDJSON_ASSERT(source != NULL); + RAPIDJSON_ASSERT(nameBuffer_ == 0); + RAPIDJSON_ASSERT(tokens_ == 0); + + // Create own allocator if user did not supply. + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + + // Count number of '/' as tokenCount + tokenCount_ = 0; + for (const Ch* s = source; s != source + length; s++) + if (*s == '/') + tokenCount_++; + + Token* token = tokens_ = static_cast(allocator_->Malloc(tokenCount_ * sizeof(Token) + length * sizeof(Ch))); + Ch* name = nameBuffer_ = reinterpret_cast(tokens_ + tokenCount_); + size_t i = 0; + + // Detect if it is a URI fragment + bool uriFragment = false; + if (source[i] == '#') { + uriFragment = true; + i++; + } + + if (i != length && source[i] != '/') { + parseErrorCode_ = kPointerParseErrorTokenMustBeginWithSolidus; + goto error; + } + + while (i < length) { + RAPIDJSON_ASSERT(source[i] == '/'); + i++; // consumes '/' + + token->name = name; + bool isNumber = true; + + while (i < length && source[i] != '/') { + Ch c = source[i]; + if (uriFragment) { + // Decoding percent-encoding for URI fragment + if (c == '%') { + PercentDecodeStream is(&source[i], source + length); + GenericInsituStringStream os(name); + Ch* begin = os.PutBegin(); + if (!Transcoder, EncodingType>().Validate(is, os) || !is.IsValid()) { + parseErrorCode_ = kPointerParseErrorInvalidPercentEncoding; + goto error; + } + size_t len = os.PutEnd(begin); + i += is.Tell() - 1; + if (len == 1) + c = *name; + else { + name += len; + isNumber = false; + i++; + continue; + } + } + else if (NeedPercentEncode(c)) { + parseErrorCode_ = kPointerParseErrorCharacterMustPercentEncode; + goto error; + } + } + + i++; + + // Escaping "~0" -> '~', "~1" -> '/' + if (c == '~') { + if (i < length) { + c = source[i]; + if (c == '0') c = '~'; + else if (c == '1') c = '/'; + else { + parseErrorCode_ = kPointerParseErrorInvalidEscape; + goto error; + } + i++; + } + else { + parseErrorCode_ = kPointerParseErrorInvalidEscape; + goto error; + } + } + + // First check for index: all of characters are digit + if (c < '0' || c > '9') + isNumber = false; + + *name++ = c; + } + token->length = static_cast(name - token->name); + if (token->length == 0) + isNumber = false; + *name++ = '\0'; // Null terminator + + // Second check for index: more than one digit cannot have leading zero + if (isNumber && token->length > 1 && token->name[0] == '0') + isNumber = false; + + // String to SizeType conversion + SizeType n = 0; + if (isNumber) { + for (size_t j = 0; j < token->length; j++) { + SizeType m = n * 10 + static_cast(token->name[j] - '0'); + if (m < n) { // overflow detection + isNumber = false; + break; + } + n = m; + } + } + + token->index = isNumber ? n : kPointerInvalidIndex; + token++; + } + + RAPIDJSON_ASSERT(name <= nameBuffer_ + length); // Should not overflow buffer + parseErrorCode_ = kPointerParseErrorNone; + return; + + error: + Allocator::Free(tokens_); + nameBuffer_ = 0; + tokens_ = 0; + tokenCount_ = 0; + parseErrorOffset_ = i; + return; + } + + //! Stringify to string or URI fragment representation. + /*! + \tparam uriFragment True for stringifying to URI fragment representation. False for string representation. + \tparam OutputStream type of output stream. + \param os The output stream. + */ + template + bool Stringify(OutputStream& os) const { + RAPIDJSON_ASSERT(IsValid()); + + if (uriFragment) + os.Put('#'); + + for (Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + os.Put('/'); + for (size_t j = 0; j < t->length; j++) { + Ch c = t->name[j]; + if (c == '~') { + os.Put('~'); + os.Put('0'); + } + else if (c == '/') { + os.Put('~'); + os.Put('1'); + } + else if (uriFragment && NeedPercentEncode(c)) { + // Transcode to UTF8 sequence + GenericStringStream source(&t->name[j]); + PercentEncodeStream target(os); + if (!Transcoder >().Validate(source, target)) + return false; + j += source.Tell() - 1; + } + else + os.Put(c); + } + } + return true; + } + + //! A helper stream for decoding a percent-encoded sequence into code unit. + /*! + This stream decodes %XY triplet into code unit (0-255). + If it encounters invalid characters, it sets output code unit as 0 and + mark invalid, and to be checked by IsValid(). + */ + class PercentDecodeStream { + public: + typedef typename ValueType::Ch Ch; + + //! Constructor + /*! + \param source Start of the stream + \param end Past-the-end of the stream. + */ + PercentDecodeStream(const Ch* source, const Ch* end) : src_(source), head_(source), end_(end), valid_(true) {} + + Ch Take() { + if (*src_ != '%' || src_ + 3 > end_) { // %XY triplet + valid_ = false; + return 0; + } + src_++; + Ch c = 0; + for (int j = 0; j < 2; j++) { + c = static_cast(c << 4); + Ch h = *src_; + if (h >= '0' && h <= '9') c = static_cast(c + h - '0'); + else if (h >= 'A' && h <= 'F') c = static_cast(c + h - 'A' + 10); + else if (h >= 'a' && h <= 'f') c = static_cast(c + h - 'a' + 10); + else { + valid_ = false; + return 0; + } + src_++; + } + return c; + } + + size_t Tell() const { return static_cast(src_ - head_); } + bool IsValid() const { return valid_; } + + private: + const Ch* src_; //!< Current read position. + const Ch* head_; //!< Original head of the string. + const Ch* end_; //!< Past-the-end position. + bool valid_; //!< Whether the parsing is valid. + }; + + //! A helper stream to encode character (UTF-8 code unit) into percent-encoded sequence. + template + class PercentEncodeStream { + public: + PercentEncodeStream(OutputStream& os) : os_(os) {} + void Put(char c) { // UTF-8 must be byte + unsigned char u = static_cast(c); + static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + os_.Put('%'); + os_.Put(hexDigits[u >> 4]); + os_.Put(hexDigits[u & 15]); + } + private: + OutputStream& os_; + }; + + Allocator* allocator_; //!< The current allocator. It is either user-supplied or equal to ownAllocator_. + Allocator* ownAllocator_; //!< Allocator owned by this Pointer. + Ch* nameBuffer_; //!< A buffer containing all names in tokens. + Token* tokens_; //!< A list of tokens. + size_t tokenCount_; //!< Number of tokens in tokens_. + size_t parseErrorOffset_; //!< Offset in code unit when parsing fail. + PointerParseErrorCode parseErrorCode_; //!< Parsing error code. +}; + +//! GenericPointer for Value (UTF-8, default allocator). +typedef GenericPointer Pointer; + +//!@name Helper functions for GenericPointer +//@{ + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& CreateValueByPointer(T& root, const GenericPointer& pointer, typename T::AllocatorType& a) { + return pointer.Create(root, a); +} + +template +typename T::ValueType& CreateValueByPointer(T& root, const CharType(&source)[N], typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Create(root, a); +} + +// No allocator parameter + +template +typename DocumentType::ValueType& CreateValueByPointer(DocumentType& document, const GenericPointer& pointer) { + return pointer.Create(document); +} + +template +typename DocumentType::ValueType& CreateValueByPointer(DocumentType& document, const CharType(&source)[N]) { + return GenericPointer(source, N - 1).Create(document); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType* GetValueByPointer(T& root, const GenericPointer& pointer, size_t* unresolvedTokenIndex = 0) { + return pointer.Get(root, unresolvedTokenIndex); +} + +template +const typename T::ValueType* GetValueByPointer(const T& root, const GenericPointer& pointer, size_t* unresolvedTokenIndex = 0) { + return pointer.Get(root, unresolvedTokenIndex); +} + +template +typename T::ValueType* GetValueByPointer(T& root, const CharType (&source)[N], size_t* unresolvedTokenIndex = 0) { + return GenericPointer(source, N - 1).Get(root, unresolvedTokenIndex); +} + +template +const typename T::ValueType* GetValueByPointer(const T& root, const CharType(&source)[N], size_t* unresolvedTokenIndex = 0) { + return GenericPointer(source, N - 1).Get(root, unresolvedTokenIndex); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, const typename T::ValueType& defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, const typename T::Ch* defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, const std::basic_string& defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, T2 defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const typename T::ValueType& defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const typename T::Ch* defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const std::basic_string& defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +GetValueByPointerWithDefault(T& root, const CharType(&source)[N], T2 defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} + +// No allocator parameter + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::ValueType& defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::Ch* defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, const std::basic_string& defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, T2 defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], const typename DocumentType::ValueType& defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], const typename DocumentType::Ch* defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], const std::basic_string& defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], T2 defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, typename T::ValueType& value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const typename T::ValueType& value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const typename T::Ch* value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const std::basic_string& value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +SetValueByPointer(T& root, const GenericPointer& pointer, T2 value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], typename T::ValueType& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const typename T::ValueType& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const typename T::Ch* value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const std::basic_string& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +SetValueByPointer(T& root, const CharType(&source)[N], T2 value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +// No allocator parameter + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, typename DocumentType::ValueType& value) { + return pointer.Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::ValueType& value) { + return pointer.Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::Ch* value) { + return pointer.Set(document, value); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, const std::basic_string& value) { + return pointer.Set(document, value); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +SetValueByPointer(DocumentType& document, const GenericPointer& pointer, T2 value) { + return pointer.Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], typename DocumentType::ValueType& value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], const typename DocumentType::ValueType& value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], const typename DocumentType::Ch* value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], const std::basic_string& value) { + return GenericPointer(source, N - 1).Set(document, value); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +SetValueByPointer(DocumentType& document, const CharType(&source)[N], T2 value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& SwapValueByPointer(T& root, const GenericPointer& pointer, typename T::ValueType& value, typename T::AllocatorType& a) { + return pointer.Swap(root, value, a); +} + +template +typename T::ValueType& SwapValueByPointer(T& root, const CharType(&source)[N], typename T::ValueType& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Swap(root, value, a); +} + +template +typename DocumentType::ValueType& SwapValueByPointer(DocumentType& document, const GenericPointer& pointer, typename DocumentType::ValueType& value) { + return pointer.Swap(document, value); +} + +template +typename DocumentType::ValueType& SwapValueByPointer(DocumentType& document, const CharType(&source)[N], typename DocumentType::ValueType& value) { + return GenericPointer(source, N - 1).Swap(document, value); +} + +////////////////////////////////////////////////////////////////////////////// + +template +bool EraseValueByPointer(T& root, const GenericPointer& pointer) { + return pointer.Erase(root); +} + +template +bool EraseValueByPointer(T& root, const CharType(&source)[N]) { + return GenericPointer(source, N - 1).Erase(root); +} + +//@} + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_POINTER_H_ diff --git a/rapidjson-1.1.0/rapid_json/prettywriter.h b/rapidjson-1.1.0/rapid_json/prettywriter.h new file mode 100644 index 000000000..0dcb0fee9 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/prettywriter.h @@ -0,0 +1,255 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_PRETTYWRITER_H_ +#define RAPIDJSON_PRETTYWRITER_H_ + +#include "writer.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Combination of PrettyWriter format flags. +/*! \see PrettyWriter::SetFormatOptions + */ +enum PrettyFormatOptions { + kFormatDefault = 0, //!< Default pretty formatting. + kFormatSingleLineArray = 1 //!< Format arrays on a single line. +}; + +//! Writer with indentation and spacing. +/*! + \tparam OutputStream Type of ouptut os. + \tparam SourceEncoding Encoding of source string. + \tparam TargetEncoding Encoding of output stream. + \tparam StackAllocator Type of allocator for allocating memory of stack. +*/ +template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags> +class PrettyWriter : public Writer { +public: + typedef Writer Base; + typedef typename Base::Ch Ch; + + //! Constructor + /*! \param os Output stream. + \param allocator User supplied allocator. If it is null, it will create a private one. + \param levelDepth Initial capacity of stack. + */ + explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : + Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {} + + + explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : + Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {} + + //! Set custom indentation. + /*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r'). + \param indentCharCount Number of indent characters for each indentation level. + \note The default indentation is 4 spaces. + */ + PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount) { + RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r'); + indentChar_ = indentChar; + indentCharCount_ = indentCharCount; + return *this; + } + + //! Set pretty writer formatting options. + /*! \param options Formatting options. + */ + PrettyWriter& SetFormatOptions(PrettyFormatOptions options) { + formatOptions_ = options; + return *this; + } + + /*! @name Implementation of Handler + \see Handler + */ + //@{ + + bool Null() { PrettyPrefix(kNullType); return Base::WriteNull(); } + bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::WriteBool(b); } + bool Int(int i) { PrettyPrefix(kNumberType); return Base::WriteInt(i); } + bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::WriteUint(u); } + bool Int64(int64_t i64) { PrettyPrefix(kNumberType); return Base::WriteInt64(i64); } + bool Uint64(uint64_t u64) { PrettyPrefix(kNumberType); return Base::WriteUint64(u64); } + bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); } + + bool RawNumber(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + PrettyPrefix(kNumberType); + return Base::WriteString(str, length); + } + + bool String(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + PrettyPrefix(kStringType); + return Base::WriteString(str, length); + } + +#if RAPIDJSON_HAS_STDSTRING + bool String(const std::basic_string& str) { + return String(str.data(), SizeType(str.size())); + } +#endif + + bool StartObject() { + PrettyPrefix(kObjectType); + new (Base::level_stack_.template Push()) typename Base::Level(false); + return Base::WriteStartObject(); + } + + bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + +#if RAPIDJSON_HAS_STDSTRING + bool Key(const std::basic_string& str) { + return Key(str.data(), SizeType(str.size())); + } +#endif + + bool EndObject(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); + RAPIDJSON_ASSERT(!Base::level_stack_.template Top()->inArray); + bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; + + if (!empty) { + Base::os_->Put('\n'); + WriteIndent(); + } + bool ret = Base::WriteEndObject(); + (void)ret; + RAPIDJSON_ASSERT(ret == true); + if (Base::level_stack_.Empty()) // end of json text + Base::os_->Flush(); + return true; + } + + bool StartArray() { + PrettyPrefix(kArrayType); + new (Base::level_stack_.template Push()) typename Base::Level(true); + return Base::WriteStartArray(); + } + + bool EndArray(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); + RAPIDJSON_ASSERT(Base::level_stack_.template Top()->inArray); + bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; + + if (!empty && !(formatOptions_ & kFormatSingleLineArray)) { + Base::os_->Put('\n'); + WriteIndent(); + } + bool ret = Base::WriteEndArray(); + (void)ret; + RAPIDJSON_ASSERT(ret == true); + if (Base::level_stack_.Empty()) // end of json text + Base::os_->Flush(); + return true; + } + + //@} + + /*! @name Convenience extensions */ + //@{ + + //! Simpler but slower overload. + bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } + + //@} + + //! Write a raw JSON value. + /*! + For user to write a stringified JSON as a value. + + \param json A well-formed JSON value. It should not contain null character within [0, length - 1] range. + \param length Length of the json. + \param type Type of the root of json. + \note When using PrettyWriter::RawValue(), the result json may not be indented correctly. + */ + bool RawValue(const Ch* json, size_t length, Type type) { PrettyPrefix(type); return Base::WriteRawValue(json, length); } + +protected: + void PrettyPrefix(Type type) { + (void)type; + if (Base::level_stack_.GetSize() != 0) { // this value is not at root + typename Base::Level* level = Base::level_stack_.template Top(); + + if (level->inArray) { + if (level->valueCount > 0) { + Base::os_->Put(','); // add comma if it is not the first element in array + if (formatOptions_ & kFormatSingleLineArray) + Base::os_->Put(' '); + } + + if (!(formatOptions_ & kFormatSingleLineArray)) { + Base::os_->Put('\n'); + WriteIndent(); + } + } + else { // in object + if (level->valueCount > 0) { + if (level->valueCount % 2 == 0) { + Base::os_->Put(','); + Base::os_->Put('\n'); + } + else { + Base::os_->Put(':'); + Base::os_->Put(' '); + } + } + else + Base::os_->Put('\n'); + + if (level->valueCount % 2 == 0) + WriteIndent(); + } + if (!level->inArray && level->valueCount % 2 == 0) + RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name + level->valueCount++; + } + else { + RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root. + Base::hasRoot_ = true; + } + } + + void WriteIndent() { + size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_; + PutN(*Base::os_, static_cast(indentChar_), count); + } + + Ch indentChar_; + unsigned indentCharCount_; + PrettyFormatOptions formatOptions_; + +private: + // Prohibit copy constructor & assignment operator. + PrettyWriter(const PrettyWriter&); + PrettyWriter& operator=(const PrettyWriter&); +}; + +RAPIDJSON_NAMESPACE_END + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/rapidjson-1.1.0/rapid_json/rapidjson.h b/rapidjson-1.1.0/rapid_json/rapidjson.h new file mode 100644 index 000000000..053b2ce43 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/rapidjson.h @@ -0,0 +1,615 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_RAPIDJSON_H_ +#define RAPIDJSON_RAPIDJSON_H_ + +/*!\file rapidjson.h + \brief common definitions and configuration + + \see RAPIDJSON_CONFIG + */ + +/*! \defgroup RAPIDJSON_CONFIG RapidJSON configuration + \brief Configuration macros for library features + + Some RapidJSON features are configurable to adapt the library to a wide + variety of platforms, environments and usage scenarios. Most of the + features can be configured in terms of overriden or predefined + preprocessor macros at compile-time. + + Some additional customization is available in the \ref RAPIDJSON_ERRORS APIs. + + \note These macros should be given on the compiler command-line + (where applicable) to avoid inconsistent values when compiling + different translation units of a single application. + */ + +#include // malloc(), realloc(), free(), size_t +#include // memset(), memcpy(), memmove(), memcmp() + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_VERSION_STRING +// +// ALWAYS synchronize the following 3 macros with corresponding variables in /CMakeLists.txt. +// + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +// token stringification +#define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x) +#define RAPIDJSON_DO_STRINGIFY(x) #x +//!@endcond + +/*! \def RAPIDJSON_MAJOR_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Major version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_MINOR_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Minor version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_PATCH_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Patch version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_VERSION_STRING + \ingroup RAPIDJSON_CONFIG + \brief Version of RapidJSON in ".." string format. +*/ +#define RAPIDJSON_MAJOR_VERSION 1 +#define RAPIDJSON_MINOR_VERSION 1 +#define RAPIDJSON_PATCH_VERSION 0 +#define RAPIDJSON_VERSION_STRING \ + RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION) + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NAMESPACE_(BEGIN|END) +/*! \def RAPIDJSON_NAMESPACE + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace + + In order to avoid symbol clashes and/or "One Definition Rule" errors + between multiple inclusions of (different versions of) RapidJSON in + a single binary, users can customize the name of the main RapidJSON + namespace. + + In case of a single nesting level, defining \c RAPIDJSON_NAMESPACE + to a custom name (e.g. \c MyRapidJSON) is sufficient. If multiple + levels are needed, both \ref RAPIDJSON_NAMESPACE_BEGIN and \ref + RAPIDJSON_NAMESPACE_END need to be defined as well: + + \code + // in some .cpp file + #define RAPIDJSON_NAMESPACE my::rapidjson + #define RAPIDJSON_NAMESPACE_BEGIN namespace my { namespace rapidjson { + #define RAPIDJSON_NAMESPACE_END } } + #include "rapidjson/..." + \endcode + + \see rapidjson + */ +/*! \def RAPIDJSON_NAMESPACE_BEGIN + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace (opening expression) + \see RAPIDJSON_NAMESPACE +*/ +/*! \def RAPIDJSON_NAMESPACE_END + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace (closing expression) + \see RAPIDJSON_NAMESPACE +*/ +#ifndef RAPIDJSON_NAMESPACE +#define RAPIDJSON_NAMESPACE rapidjson +#endif +#ifndef RAPIDJSON_NAMESPACE_BEGIN +#define RAPIDJSON_NAMESPACE_BEGIN namespace RAPIDJSON_NAMESPACE { +#endif +#ifndef RAPIDJSON_NAMESPACE_END +#define RAPIDJSON_NAMESPACE_END } +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_HAS_STDSTRING + +#ifndef RAPIDJSON_HAS_STDSTRING +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_HAS_STDSTRING 1 // force generation of documentation +#else +#define RAPIDJSON_HAS_STDSTRING 0 // no std::string support by default +#endif +/*! \def RAPIDJSON_HAS_STDSTRING + \ingroup RAPIDJSON_CONFIG + \brief Enable RapidJSON support for \c std::string + + By defining this preprocessor symbol to \c 1, several convenience functions for using + \ref rapidjson::GenericValue with \c std::string are enabled, especially + for construction and comparison. + + \hideinitializer +*/ +#endif // !defined(RAPIDJSON_HAS_STDSTRING) + +#if RAPIDJSON_HAS_STDSTRING +#include +#endif // RAPIDJSON_HAS_STDSTRING + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NO_INT64DEFINE + +/*! \def RAPIDJSON_NO_INT64DEFINE + \ingroup RAPIDJSON_CONFIG + \brief Use external 64-bit integer types. + + RapidJSON requires the 64-bit integer types \c int64_t and \c uint64_t types + to be available at global scope. + + If users have their own definition, define RAPIDJSON_NO_INT64DEFINE to + prevent RapidJSON from defining its own types. +*/ +#ifndef RAPIDJSON_NO_INT64DEFINE +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#if defined(_MSC_VER) && (_MSC_VER < 1800) // Visual Studio 2013 +#include "msinttypes/stdint.h" +#include "msinttypes/inttypes.h" +#else +// Other compilers should have this. +#include +#include +#endif +//!@endcond +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_NO_INT64DEFINE +#endif +#endif // RAPIDJSON_NO_INT64TYPEDEF + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_FORCEINLINE + +#ifndef RAPIDJSON_FORCEINLINE +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#if defined(_MSC_VER) && defined(NDEBUG) +#define RAPIDJSON_FORCEINLINE __forceinline +#elif defined(__GNUC__) && __GNUC__ >= 4 && defined(NDEBUG) +#define RAPIDJSON_FORCEINLINE __attribute__((always_inline)) +#else +#define RAPIDJSON_FORCEINLINE +#endif +//!@endcond +#endif // RAPIDJSON_FORCEINLINE + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ENDIAN +#define RAPIDJSON_LITTLEENDIAN 0 //!< Little endian machine +#define RAPIDJSON_BIGENDIAN 1 //!< Big endian machine + +//! Endianness of the machine. +/*! + \def RAPIDJSON_ENDIAN + \ingroup RAPIDJSON_CONFIG + + GCC 4.6 provided macro for detecting endianness of the target machine. But other + compilers may not have this. User can define RAPIDJSON_ENDIAN to either + \ref RAPIDJSON_LITTLEENDIAN or \ref RAPIDJSON_BIGENDIAN. + + Default detection implemented with reference to + \li https://gcc.gnu.org/onlinedocs/gcc-4.6.0/cpp/Common-Predefined-Macros.html + \li http://www.boost.org/doc/libs/1_42_0/boost/detail/endian.hpp +*/ +#ifndef RAPIDJSON_ENDIAN +// Detect with GCC 4.6's macro +# ifdef __BYTE_ORDER__ +# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# else +# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. +# endif // __BYTE_ORDER__ +// Detect with GLIBC's endian.h +# elif defined(__GLIBC__) +# include +# if (__BYTE_ORDER == __LITTLE_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif (__BYTE_ORDER == __BIG_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# else +# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. +# endif // __GLIBC__ +// Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro +# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +// Detect with architecture macros +# elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__bfin__) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(_MSC_VER) && defined(_M_ARM) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(RAPIDJSON_DOXYGEN_RUNNING) +# define RAPIDJSON_ENDIAN +# else +# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. +# endif +#endif // RAPIDJSON_ENDIAN + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_64BIT + +//! Whether using 64-bit architecture +#ifndef RAPIDJSON_64BIT +#if defined(__LP64__) || (defined(__x86_64__) && defined(__ILP32__)) || defined(_WIN64) || defined(__EMSCRIPTEN__) +#define RAPIDJSON_64BIT 1 +#else +#define RAPIDJSON_64BIT 0 +#endif +#endif // RAPIDJSON_64BIT + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ALIGN + +//! Data alignment of the machine. +/*! \ingroup RAPIDJSON_CONFIG + \param x pointer to align + + Some machines require strict data alignment. Currently the default uses 4 bytes + alignment on 32-bit platforms and 8 bytes alignment for 64-bit platforms. + User can customize by defining the RAPIDJSON_ALIGN function macro. +*/ +#ifndef RAPIDJSON_ALIGN +#if RAPIDJSON_64BIT == 1 +#define RAPIDJSON_ALIGN(x) (((x) + static_cast(7u)) & ~static_cast(7u)) +#else +#define RAPIDJSON_ALIGN(x) (((x) + 3u) & ~3u) +#endif +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_UINT64_C2 + +//! Construct a 64-bit literal by a pair of 32-bit integer. +/*! + 64-bit literal with or without ULL suffix is prone to compiler warnings. + UINT64_C() is C macro which cause compilation problems. + Use this macro to define 64-bit constants by a pair of 32-bit integer. +*/ +#ifndef RAPIDJSON_UINT64_C2 +#define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast(high32) << 32) | static_cast(low32)) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_48BITPOINTER_OPTIMIZATION + +//! Use only lower 48-bit address for some pointers. +/*! + \ingroup RAPIDJSON_CONFIG + + This optimization uses the fact that current X86-64 architecture only implement lower 48-bit virtual address. + The higher 16-bit can be used for storing other data. + \c GenericValue uses this optimization to reduce its size form 24 bytes to 16 bytes in 64-bit architecture. +*/ +#ifndef RAPIDJSON_48BITPOINTER_OPTIMIZATION +#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64) +#define RAPIDJSON_48BITPOINTER_OPTIMIZATION 1 +#else +#define RAPIDJSON_48BITPOINTER_OPTIMIZATION 0 +#endif +#endif // RAPIDJSON_48BITPOINTER_OPTIMIZATION + +#if RAPIDJSON_48BITPOINTER_OPTIMIZATION == 1 +#if RAPIDJSON_64BIT != 1 +#error RAPIDJSON_48BITPOINTER_OPTIMIZATION can only be set to 1 when RAPIDJSON_64BIT=1 +#endif +#define RAPIDJSON_SETPOINTER(type, p, x) (p = reinterpret_cast((reinterpret_cast(p) & static_cast(RAPIDJSON_UINT64_C2(0xFFFF0000, 0x00000000))) | reinterpret_cast(reinterpret_cast(x)))) +#define RAPIDJSON_GETPOINTER(type, p) (reinterpret_cast(reinterpret_cast(p) & static_cast(RAPIDJSON_UINT64_C2(0x0000FFFF, 0xFFFFFFFF)))) +#else +#define RAPIDJSON_SETPOINTER(type, p, x) (p = (x)) +#define RAPIDJSON_GETPOINTER(type, p) (p) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD + +/*! \def RAPIDJSON_SIMD + \ingroup RAPIDJSON_CONFIG + \brief Enable SSE2/SSE4.2 optimization. + + RapidJSON supports optimized implementations for some parsing operations + based on the SSE2 or SSE4.2 SIMD extensions on modern Intel-compatible + processors. + + To enable these optimizations, two different symbols can be defined; + \code + // Enable SSE2 optimization. + #define RAPIDJSON_SSE2 + + // Enable SSE4.2 optimization. + #define RAPIDJSON_SSE42 + \endcode + + \c RAPIDJSON_SSE42 takes precedence, if both are defined. + + If any of these symbols is defined, RapidJSON defines the macro + \c RAPIDJSON_SIMD to indicate the availability of the optimized code. +*/ +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) \ + || defined(RAPIDJSON_DOXYGEN_RUNNING) +#define RAPIDJSON_SIMD +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NO_SIZETYPEDEFINE + +#ifndef RAPIDJSON_NO_SIZETYPEDEFINE +/*! \def RAPIDJSON_NO_SIZETYPEDEFINE + \ingroup RAPIDJSON_CONFIG + \brief User-provided \c SizeType definition. + + In order to avoid using 32-bit size types for indexing strings and arrays, + define this preprocessor symbol and provide the type rapidjson::SizeType + before including RapidJSON: + \code + #define RAPIDJSON_NO_SIZETYPEDEFINE + namespace rapidjson { typedef ::std::size_t SizeType; } + #include "rapidjson/..." + \endcode + + \see rapidjson::SizeType +*/ +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_NO_SIZETYPEDEFINE +#endif +RAPIDJSON_NAMESPACE_BEGIN +//! Size type (for string lengths, array sizes, etc.) +/*! RapidJSON uses 32-bit array/string indices even on 64-bit platforms, + instead of using \c size_t. Users may override the SizeType by defining + \ref RAPIDJSON_NO_SIZETYPEDEFINE. +*/ +typedef unsigned SizeType; +RAPIDJSON_NAMESPACE_END +#endif + +// always import std::size_t to rapidjson namespace +RAPIDJSON_NAMESPACE_BEGIN +using std::size_t; +RAPIDJSON_NAMESPACE_END + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ASSERT + +//! Assertion. +/*! \ingroup RAPIDJSON_CONFIG + By default, rapidjson uses C \c assert() for internal assertions. + User can override it by defining RAPIDJSON_ASSERT(x) macro. + + \note Parsing errors are handled and can be customized by the + \ref RAPIDJSON_ERRORS APIs. +*/ +#ifndef RAPIDJSON_ASSERT +#include +#define RAPIDJSON_ASSERT(x) assert(x) +#endif // RAPIDJSON_ASSERT + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_STATIC_ASSERT + +// Adopt from boost +#ifndef RAPIDJSON_STATIC_ASSERT +#ifndef __clang__ +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#endif +RAPIDJSON_NAMESPACE_BEGIN +template struct STATIC_ASSERTION_FAILURE; +template <> struct STATIC_ASSERTION_FAILURE { enum { value = 1 }; }; +template struct StaticAssertTest {}; +RAPIDJSON_NAMESPACE_END + +#define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y) +#define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y) +#define RAPIDJSON_DO_JOIN2(X, Y) X##Y + +#if defined(__GNUC__) +#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused)) +#else +#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE +#endif +#ifndef __clang__ +//!@endcond +#endif + +/*! \def RAPIDJSON_STATIC_ASSERT + \brief (Internal) macro to check for conditions at compile-time + \param x compile-time condition + \hideinitializer + */ +#define RAPIDJSON_STATIC_ASSERT(x) \ + typedef ::RAPIDJSON_NAMESPACE::StaticAssertTest< \ + sizeof(::RAPIDJSON_NAMESPACE::STATIC_ASSERTION_FAILURE)> \ + RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_LIKELY, RAPIDJSON_UNLIKELY + +//! Compiler branching hint for expression with high probability to be true. +/*! + \ingroup RAPIDJSON_CONFIG + \param x Boolean expression likely to be true. +*/ +#ifndef RAPIDJSON_LIKELY +#if defined(__GNUC__) || defined(__clang__) +#define RAPIDJSON_LIKELY(x) __builtin_expect(!!(x), 1) +#else +#define RAPIDJSON_LIKELY(x) (x) +#endif +#endif + +//! Compiler branching hint for expression with low probability to be true. +/*! + \ingroup RAPIDJSON_CONFIG + \param x Boolean expression unlikely to be true. +*/ +#ifndef RAPIDJSON_UNLIKELY +#if defined(__GNUC__) || defined(__clang__) +#define RAPIDJSON_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +#define RAPIDJSON_UNLIKELY(x) (x) +#endif +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Helpers + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN + +#define RAPIDJSON_MULTILINEMACRO_BEGIN do { +#define RAPIDJSON_MULTILINEMACRO_END \ +} while((void)0, 0) + +// adopted from Boost +#define RAPIDJSON_VERSION_CODE(x,y,z) \ + (((x)*100000) + ((y)*100) + (z)) + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF + +#if defined(__GNUC__) +#define RAPIDJSON_GNUC \ + RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__) +#endif + +#if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,2,0)) + +#define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x)) +#define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x) +#define RAPIDJSON_DIAG_OFF(x) \ + RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x))) + +// push/pop support in Clang and GCC>=4.6 +#if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) +#define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push) +#define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop) +#else // GCC >= 4.2, < 4.6 +#define RAPIDJSON_DIAG_PUSH /* ignored */ +#define RAPIDJSON_DIAG_POP /* ignored */ +#endif + +#elif defined(_MSC_VER) + +// pragma (MSVC specific) +#define RAPIDJSON_PRAGMA(x) __pragma(x) +#define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x)) + +#define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable: x) +#define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push) +#define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop) + +#else + +#define RAPIDJSON_DIAG_OFF(x) /* ignored */ +#define RAPIDJSON_DIAG_PUSH /* ignored */ +#define RAPIDJSON_DIAG_POP /* ignored */ + +#endif // RAPIDJSON_DIAG_* + +/////////////////////////////////////////////////////////////////////////////// +// C++11 features + +#ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS +#if defined(__clang__) +#if __has_feature(cxx_rvalue_references) && \ + (defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306) +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1 +#else +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0 +#endif +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ + (defined(_MSC_VER) && _MSC_VER >= 1600) + +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1 +#else +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0 +#endif +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + +#ifndef RAPIDJSON_HAS_CXX11_NOEXCEPT +#if defined(__clang__) +#define RAPIDJSON_HAS_CXX11_NOEXCEPT __has_feature(cxx_noexcept) +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) +// (defined(_MSC_VER) && _MSC_VER >= ????) // not yet supported +#define RAPIDJSON_HAS_CXX11_NOEXCEPT 1 +#else +#define RAPIDJSON_HAS_CXX11_NOEXCEPT 0 +#endif +#endif +#if RAPIDJSON_HAS_CXX11_NOEXCEPT +#define RAPIDJSON_NOEXCEPT noexcept +#else +#define RAPIDJSON_NOEXCEPT /* noexcept */ +#endif // RAPIDJSON_HAS_CXX11_NOEXCEPT + +// no automatic detection, yet +#ifndef RAPIDJSON_HAS_CXX11_TYPETRAITS +#define RAPIDJSON_HAS_CXX11_TYPETRAITS 0 +#endif + +#ifndef RAPIDJSON_HAS_CXX11_RANGE_FOR +#if defined(__clang__) +#define RAPIDJSON_HAS_CXX11_RANGE_FOR __has_feature(cxx_range_for) +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ + (defined(_MSC_VER) && _MSC_VER >= 1700) +#define RAPIDJSON_HAS_CXX11_RANGE_FOR 1 +#else +#define RAPIDJSON_HAS_CXX11_RANGE_FOR 0 +#endif +#endif // RAPIDJSON_HAS_CXX11_RANGE_FOR + +//!@endcond + +/////////////////////////////////////////////////////////////////////////////// +// new/delete + +#ifndef RAPIDJSON_NEW +///! customization point for global \c new +#define RAPIDJSON_NEW(x) new x +#endif +#ifndef RAPIDJSON_DELETE +///! customization point for global \c delete +#define RAPIDJSON_DELETE(x) delete x +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Type + +/*! \namespace rapidjson + \brief main RapidJSON namespace + \see RAPIDJSON_NAMESPACE +*/ +RAPIDJSON_NAMESPACE_BEGIN + +//! Type of JSON value +enum Type { + kNullType = 0, //!< null + kFalseType = 1, //!< false + kTrueType = 2, //!< true + kObjectType = 3, //!< object + kArrayType = 4, //!< array + kStringType = 5, //!< string + kNumberType = 6 //!< number +}; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/rapidjson-1.1.0/rapid_json/reader.h b/rapidjson-1.1.0/rapid_json/reader.h new file mode 100644 index 000000000..19f8849b1 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/reader.h @@ -0,0 +1,1879 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_READER_H_ +#define RAPIDJSON_READER_H_ + +/*! \file reader.h */ + +#include "allocators.h" +#include "stream.h" +#include "encodedstream.h" +#include "internal/meta.h" +#include "internal/stack.h" +#include "internal/strtod.h" +#include + +#if defined(RAPIDJSON_SIMD) && defined(_MSC_VER) +#include +#pragma intrinsic(_BitScanForward) +#endif +#ifdef RAPIDJSON_SSE42 +#include +#elif defined(RAPIDJSON_SSE2) +#include +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +RAPIDJSON_DIAG_OFF(4702) // unreachable code +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(old-style-cast) +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(switch-enum) +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define RAPIDJSON_NOTHING /* deliberately empty */ +#ifndef RAPIDJSON_PARSE_ERROR_EARLY_RETURN +#define RAPIDJSON_PARSE_ERROR_EARLY_RETURN(value) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + if (RAPIDJSON_UNLIKELY(HasParseError())) { return value; } \ + RAPIDJSON_MULTILINEMACRO_END +#endif +#define RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID \ + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(RAPIDJSON_NOTHING) +//!@endcond + +/*! \def RAPIDJSON_PARSE_ERROR_NORETURN + \ingroup RAPIDJSON_ERRORS + \brief Macro to indicate a parse error. + \param parseErrorCode \ref rapidjson::ParseErrorCode of the error + \param offset position of the error in JSON input (\c size_t) + + This macros can be used as a customization point for the internal + error handling mechanism of RapidJSON. + + A common usage model is to throw an exception instead of requiring the + caller to explicitly check the \ref rapidjson::GenericReader::Parse's + return value: + + \code + #define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode,offset) \ + throw ParseException(parseErrorCode, #parseErrorCode, offset) + + #include // std::runtime_error + #include "rapidjson/error/error.h" // rapidjson::ParseResult + + struct ParseException : std::runtime_error, rapidjson::ParseResult { + ParseException(rapidjson::ParseErrorCode code, const char* msg, size_t offset) + : std::runtime_error(msg), ParseResult(code, offset) {} + }; + + #include "rapidjson/reader.h" + \endcode + + \see RAPIDJSON_PARSE_ERROR, rapidjson::GenericReader::Parse + */ +#ifndef RAPIDJSON_PARSE_ERROR_NORETURN +#define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + RAPIDJSON_ASSERT(!HasParseError()); /* Error can only be assigned once */ \ + SetParseError(parseErrorCode, offset); \ + RAPIDJSON_MULTILINEMACRO_END +#endif + +/*! \def RAPIDJSON_PARSE_ERROR + \ingroup RAPIDJSON_ERRORS + \brief (Internal) macro to indicate and handle a parse error. + \param parseErrorCode \ref rapidjson::ParseErrorCode of the error + \param offset position of the error in JSON input (\c size_t) + + Invokes RAPIDJSON_PARSE_ERROR_NORETURN and stops the parsing. + + \see RAPIDJSON_PARSE_ERROR_NORETURN + \hideinitializer + */ +#ifndef RAPIDJSON_PARSE_ERROR +#define RAPIDJSON_PARSE_ERROR(parseErrorCode, offset) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset); \ + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; \ + RAPIDJSON_MULTILINEMACRO_END +#endif + +#include "error/error.h" // ParseErrorCode, ParseResult + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// ParseFlag + +/*! \def RAPIDJSON_PARSE_DEFAULT_FLAGS + \ingroup RAPIDJSON_CONFIG + \brief User-defined kParseDefaultFlags definition. + + User can define this as any \c ParseFlag combinations. +*/ +#ifndef RAPIDJSON_PARSE_DEFAULT_FLAGS +#define RAPIDJSON_PARSE_DEFAULT_FLAGS kParseNoFlags +#endif + +//! Combination of parseFlags +/*! \see Reader::Parse, Document::Parse, Document::ParseInsitu, Document::ParseStream + */ +enum ParseFlag { + kParseNoFlags = 0, //!< No flags are set. + kParseInsituFlag = 1, //!< In-situ(destructive) parsing. + kParseValidateEncodingFlag = 2, //!< Validate encoding of JSON strings. + kParseIterativeFlag = 4, //!< Iterative(constant complexity in terms of function call stack size) parsing. + kParseStopWhenDoneFlag = 8, //!< After parsing a complete JSON root from stream, stop further processing the rest of stream. When this flag is used, parser will not generate kParseErrorDocumentRootNotSingular error. + kParseFullPrecisionFlag = 16, //!< Parse number in full precision (but slower). + kParseCommentsFlag = 32, //!< Allow one-line (//) and multi-line (/**/) comments. + kParseNumbersAsStringsFlag = 64, //!< Parse all numbers (ints/doubles) as strings. + kParseTrailingCommasFlag = 128, //!< Allow trailing commas at the end of objects and arrays. + kParseNanAndInfFlag = 256, //!< Allow parsing NaN, Inf, Infinity, -Inf and -Infinity as doubles. + kParseDefaultFlags = RAPIDJSON_PARSE_DEFAULT_FLAGS //!< Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS +}; + +/////////////////////////////////////////////////////////////////////////////// +// Handler + +/*! \class rapidjson::Handler + \brief Concept for receiving events from GenericReader upon parsing. + The functions return true if no error occurs. If they return false, + the event publisher should terminate the process. +\code +concept Handler { + typename Ch; + + bool Null(); + bool Bool(bool b); + bool Int(int i); + bool Uint(unsigned i); + bool Int64(int64_t i); + bool Uint64(uint64_t i); + bool Double(double d); + /// enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length) + bool RawNumber(const Ch* str, SizeType length, bool copy); + bool String(const Ch* str, SizeType length, bool copy); + bool StartObject(); + bool Key(const Ch* str, SizeType length, bool copy); + bool EndObject(SizeType memberCount); + bool StartArray(); + bool EndArray(SizeType elementCount); +}; +\endcode +*/ +/////////////////////////////////////////////////////////////////////////////// +// BaseReaderHandler + +//! Default implementation of Handler. +/*! This can be used as base class of any reader handler. + \note implements Handler concept +*/ +template, typename Derived = void> +struct BaseReaderHandler { + typedef typename Encoding::Ch Ch; + + typedef typename internal::SelectIf, BaseReaderHandler, Derived>::Type Override; + + bool Default() { return true; } + bool Null() { return static_cast(*this).Default(); } + bool Bool(bool) { return static_cast(*this).Default(); } + bool Int(int) { return static_cast(*this).Default(); } + bool Uint(unsigned) { return static_cast(*this).Default(); } + bool Int64(int64_t) { return static_cast(*this).Default(); } + bool Uint64(uint64_t) { return static_cast(*this).Default(); } + bool Double(double) { return static_cast(*this).Default(); } + /// enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length) + bool RawNumber(const Ch* str, SizeType len, bool copy) { return static_cast(*this).String(str, len, copy); } + bool String(const Ch*, SizeType, bool) { return static_cast(*this).Default(); } + bool StartObject() { return static_cast(*this).Default(); } + bool Key(const Ch* str, SizeType len, bool copy) { return static_cast(*this).String(str, len, copy); } + bool EndObject(SizeType) { return static_cast(*this).Default(); } + bool StartArray() { return static_cast(*this).Default(); } + bool EndArray(SizeType) { return static_cast(*this).Default(); } +}; + +/////////////////////////////////////////////////////////////////////////////// +// StreamLocalCopy + +namespace internal { + +template::copyOptimization> +class StreamLocalCopy; + +//! Do copy optimization. +template +class StreamLocalCopy { +public: + StreamLocalCopy(Stream& original) : s(original), original_(original) {} + ~StreamLocalCopy() { original_ = s; } + + Stream s; + +private: + StreamLocalCopy& operator=(const StreamLocalCopy&) /* = delete */; + + Stream& original_; +}; + +//! Keep reference. +template +class StreamLocalCopy { +public: + StreamLocalCopy(Stream& original) : s(original) {} + + Stream& s; + +private: + StreamLocalCopy& operator=(const StreamLocalCopy&) /* = delete */; +}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// SkipWhitespace + +//! Skip the JSON white spaces in a stream. +/*! \param is A input stream for skipping white spaces. + \note This function has SSE2/SSE4.2 specialization. +*/ +template +void SkipWhitespace(InputStream& is) { + internal::StreamLocalCopy copy(is); + InputStream& s(copy.s); + + typename InputStream::Ch c; + while ((c = s.Peek()) == ' ' || c == '\n' || c == '\r' || c == '\t') + s.Take(); +} + +inline const char* SkipWhitespace(const char* p, const char* end) { + while (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + return p; +} + +#ifdef RAPIDJSON_SSE42 +//! Skip whitespace with SSE 4.2 pcmpistrm instruction, testing 16 8-byte characters at once. +inline const char *SkipWhitespace_SIMD(const char* p) { + // Fast return for single non-whitespace + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // 16-byte align to the next boundary + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // The rest of string using SIMD + static const char whitespace[16] = " \n\r\t"; + const __m128i w = _mm_loadu_si128(reinterpret_cast(&whitespace[0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const int r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY)); + if (r != 0) { // some of characters is non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } +} + +inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { + // Fast return for single non-whitespace + if (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + else + return p; + + // The middle of string using SIMD + static const char whitespace[16] = " \n\r\t"; + const __m128i w = _mm_loadu_si128(reinterpret_cast(&whitespace[0])); + + for (; p <= end - 16; p += 16) { + const __m128i s = _mm_loadu_si128(reinterpret_cast(p)); + const int r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY)); + if (r != 0) { // some of characters is non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } + + return SkipWhitespace(p, end); +} + +#elif defined(RAPIDJSON_SSE2) + +//! Skip whitespace with SSE2 instructions, testing 16 8-byte characters at once. +inline const char *SkipWhitespace_SIMD(const char* p) { + // Fast return for single non-whitespace + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // 16-byte align to the next boundary + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // The rest of string + #define C16(c) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c } + static const char whitespaces[4][16] = { C16(' '), C16('\n'), C16('\r'), C16('\t') }; + #undef C16 + + const __m128i w0 = _mm_loadu_si128(reinterpret_cast(&whitespaces[0][0])); + const __m128i w1 = _mm_loadu_si128(reinterpret_cast(&whitespaces[1][0])); + const __m128i w2 = _mm_loadu_si128(reinterpret_cast(&whitespaces[2][0])); + const __m128i w3 = _mm_loadu_si128(reinterpret_cast(&whitespaces[3][0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + __m128i x = _mm_cmpeq_epi8(s, w0); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3)); + unsigned short r = static_cast(~_mm_movemask_epi8(x)); + if (r != 0) { // some of characters may be non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } +} + +inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { + // Fast return for single non-whitespace + if (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + else + return p; + + // The rest of string + #define C16(c) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c } + static const char whitespaces[4][16] = { C16(' '), C16('\n'), C16('\r'), C16('\t') }; + #undef C16 + + const __m128i w0 = _mm_loadu_si128(reinterpret_cast(&whitespaces[0][0])); + const __m128i w1 = _mm_loadu_si128(reinterpret_cast(&whitespaces[1][0])); + const __m128i w2 = _mm_loadu_si128(reinterpret_cast(&whitespaces[2][0])); + const __m128i w3 = _mm_loadu_si128(reinterpret_cast(&whitespaces[3][0])); + + for (; p <= end - 16; p += 16) { + const __m128i s = _mm_loadu_si128(reinterpret_cast(p)); + __m128i x = _mm_cmpeq_epi8(s, w0); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3)); + unsigned short r = static_cast(~_mm_movemask_epi8(x)); + if (r != 0) { // some of characters may be non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } + + return SkipWhitespace(p, end); +} + +#endif // RAPIDJSON_SSE2 + +#ifdef RAPIDJSON_SIMD +//! Template function specialization for InsituStringStream +template<> inline void SkipWhitespace(InsituStringStream& is) { + is.src_ = const_cast(SkipWhitespace_SIMD(is.src_)); +} + +//! Template function specialization for StringStream +template<> inline void SkipWhitespace(StringStream& is) { + is.src_ = SkipWhitespace_SIMD(is.src_); +} + +template<> inline void SkipWhitespace(EncodedInputStream, MemoryStream>& is) { + is.is_.src_ = SkipWhitespace_SIMD(is.is_.src_, is.is_.end_); +} +#endif // RAPIDJSON_SIMD + +/////////////////////////////////////////////////////////////////////////////// +// GenericReader + +//! SAX-style JSON parser. Use \ref Reader for UTF8 encoding and default allocator. +/*! GenericReader parses JSON text from a stream, and send events synchronously to an + object implementing Handler concept. + + It needs to allocate a stack for storing a single decoded string during + non-destructive parsing. + + For in-situ parsing, the decoded string is directly written to the source + text string, no temporary buffer is required. + + A GenericReader object can be reused for parsing multiple JSON text. + + \tparam SourceEncoding Encoding of the input stream. + \tparam TargetEncoding Encoding of the parse output. + \tparam StackAllocator Allocator type for stack. +*/ +template +class GenericReader { +public: + typedef typename SourceEncoding::Ch Ch; //!< SourceEncoding character type + + //! Constructor. + /*! \param stackAllocator Optional allocator for allocating stack memory. (Only use for non-destructive parsing) + \param stackCapacity stack capacity in bytes for storing a single decoded string. (Only use for non-destructive parsing) + */ + GenericReader(StackAllocator* stackAllocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(stackAllocator, stackCapacity), parseResult_() {} + + //! Parse JSON text. + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept. + \tparam Handler Type of handler, implementing Handler concept. + \param is Input stream to be parsed. + \param handler The handler to receive events. + \return Whether the parsing is successful. + */ + template + ParseResult Parse(InputStream& is, Handler& handler) { + if (parseFlags & kParseIterativeFlag) + return IterativeParse(is, handler); + + parseResult_.Clear(); + + ClearStackOnExit scope(*this); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (RAPIDJSON_UNLIKELY(is.Peek() == '\0')) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentEmpty, is.Tell()); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + else { + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (!(parseFlags & kParseStopWhenDoneFlag)) { + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (RAPIDJSON_UNLIKELY(is.Peek() != '\0')) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentRootNotSingular, is.Tell()); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + } + } + + return parseResult_; + } + + //! Parse JSON text (with \ref kParseDefaultFlags) + /*! \tparam InputStream Type of input stream, implementing Stream concept + \tparam Handler Type of handler, implementing Handler concept. + \param is Input stream to be parsed. + \param handler The handler to receive events. + \return Whether the parsing is successful. + */ + template + ParseResult Parse(InputStream& is, Handler& handler) { + return Parse(is, handler); + } + + //! Whether a parse error has occured in the last parsing. + bool HasParseError() const { return parseResult_.IsError(); } + + //! Get the \ref ParseErrorCode of last parsing. + ParseErrorCode GetParseErrorCode() const { return parseResult_.Code(); } + + //! Get the position of last parsing error in input, 0 otherwise. + size_t GetErrorOffset() const { return parseResult_.Offset(); } + +protected: + void SetParseError(ParseErrorCode code, size_t offset) { parseResult_.Set(code, offset); } + +private: + // Prohibit copy constructor & assignment operator. + GenericReader(const GenericReader&); + GenericReader& operator=(const GenericReader&); + + void ClearStack() { stack_.Clear(); } + + // clear stack on any exit from ParseStream, e.g. due to exception + struct ClearStackOnExit { + explicit ClearStackOnExit(GenericReader& r) : r_(r) {} + ~ClearStackOnExit() { r_.ClearStack(); } + private: + GenericReader& r_; + ClearStackOnExit(const ClearStackOnExit&); + ClearStackOnExit& operator=(const ClearStackOnExit&); + }; + + template + void SkipWhitespaceAndComments(InputStream& is) { + SkipWhitespace(is); + + if (parseFlags & kParseCommentsFlag) { + while (RAPIDJSON_UNLIKELY(Consume(is, '/'))) { + if (Consume(is, '*')) { + while (true) { + if (RAPIDJSON_UNLIKELY(is.Peek() == '\0')) + RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); + else if (Consume(is, '*')) { + if (Consume(is, '/')) + break; + } + else + is.Take(); + } + } + else if (RAPIDJSON_LIKELY(Consume(is, '/'))) + while (is.Peek() != '\0' && is.Take() != '\n'); + else + RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); + + SkipWhitespace(is); + } + } + } + + // Parse object: { string : value, ... } + template + void ParseObject(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == '{'); + is.Take(); // Skip '{' + + if (RAPIDJSON_UNLIKELY(!handler.StartObject())) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (Consume(is, '}')) { + if (RAPIDJSON_UNLIKELY(!handler.EndObject(0))) // empty object + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + + for (SizeType memberCount = 0;;) { + if (RAPIDJSON_UNLIKELY(is.Peek() != '"')) + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); + + ParseString(is, handler, true); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (RAPIDJSON_UNLIKELY(!Consume(is, ':'))) + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissColon, is.Tell()); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ++memberCount; + + switch (is.Peek()) { + case ',': + is.Take(); + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + break; + case '}': + is.Take(); + if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + default: + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); break; // This useless break is only for making warning and coverage happy + } + + if (parseFlags & kParseTrailingCommasFlag) { + if (is.Peek() == '}') { + if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + is.Take(); + return; + } + } + } + } + + // Parse array: [ value, ... ] + template + void ParseArray(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == '['); + is.Take(); // Skip '[' + + if (RAPIDJSON_UNLIKELY(!handler.StartArray())) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (Consume(is, ']')) { + if (RAPIDJSON_UNLIKELY(!handler.EndArray(0))) // empty array + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + + for (SizeType elementCount = 0;;) { + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ++elementCount; + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (Consume(is, ',')) { + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + } + else if (Consume(is, ']')) { + if (RAPIDJSON_UNLIKELY(!handler.EndArray(elementCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); + + if (parseFlags & kParseTrailingCommasFlag) { + if (is.Peek() == ']') { + if (RAPIDJSON_UNLIKELY(!handler.EndArray(elementCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + is.Take(); + return; + } + } + } + } + + template + void ParseNull(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 'n'); + is.Take(); + + if (RAPIDJSON_LIKELY(Consume(is, 'u') && Consume(is, 'l') && Consume(is, 'l'))) { + if (RAPIDJSON_UNLIKELY(!handler.Null())) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); + } + + template + void ParseTrue(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 't'); + is.Take(); + + if (RAPIDJSON_LIKELY(Consume(is, 'r') && Consume(is, 'u') && Consume(is, 'e'))) { + if (RAPIDJSON_UNLIKELY(!handler.Bool(true))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); + } + + template + void ParseFalse(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 'f'); + is.Take(); + + if (RAPIDJSON_LIKELY(Consume(is, 'a') && Consume(is, 'l') && Consume(is, 's') && Consume(is, 'e'))) { + if (RAPIDJSON_UNLIKELY(!handler.Bool(false))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); + } + + template + RAPIDJSON_FORCEINLINE static bool Consume(InputStream& is, typename InputStream::Ch expect) { + if (RAPIDJSON_LIKELY(is.Peek() == expect)) { + is.Take(); + return true; + } + else + return false; + } + + // Helper function to parse four hexidecimal digits in \uXXXX in ParseString(). + template + unsigned ParseHex4(InputStream& is, size_t escapeOffset) { + unsigned codepoint = 0; + for (int i = 0; i < 4; i++) { + Ch c = is.Peek(); + codepoint <<= 4; + codepoint += static_cast(c); + if (c >= '0' && c <= '9') + codepoint -= '0'; + else if (c >= 'A' && c <= 'F') + codepoint -= 'A' - 10; + else if (c >= 'a' && c <= 'f') + codepoint -= 'a' - 10; + else { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorStringUnicodeEscapeInvalidHex, escapeOffset); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(0); + } + is.Take(); + } + return codepoint; + } + + template + class StackStream { + public: + typedef CharType Ch; + + StackStream(internal::Stack& stack) : stack_(stack), length_(0) {} + RAPIDJSON_FORCEINLINE void Put(Ch c) { + *stack_.template Push() = c; + ++length_; + } + + RAPIDJSON_FORCEINLINE void* Push(SizeType count) { + length_ += count; + return stack_.template Push(count); + } + + size_t Length() const { return length_; } + + Ch* Pop() { + return stack_.template Pop(length_); + } + + private: + StackStream(const StackStream&); + StackStream& operator=(const StackStream&); + + internal::Stack& stack_; + SizeType length_; + }; + + // Parse string and generate String event. Different code paths for kParseInsituFlag. + template + void ParseString(InputStream& is, Handler& handler, bool isKey = false) { + internal::StreamLocalCopy copy(is); + InputStream& s(copy.s); + + RAPIDJSON_ASSERT(s.Peek() == '\"'); + s.Take(); // Skip '\"' + + bool success = false; + if (parseFlags & kParseInsituFlag) { + typename InputStream::Ch *head = s.PutBegin(); + ParseStringToStream(s, s); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + size_t length = s.PutEnd(head) - 1; + RAPIDJSON_ASSERT(length <= 0xFFFFFFFF); + const typename TargetEncoding::Ch* const str = reinterpret_cast(head); + success = (isKey ? handler.Key(str, SizeType(length), false) : handler.String(str, SizeType(length), false)); + } + else { + StackStream stackStream(stack_); + ParseStringToStream(s, stackStream); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + SizeType length = static_cast(stackStream.Length()) - 1; + const typename TargetEncoding::Ch* const str = stackStream.Pop(); + success = (isKey ? handler.Key(str, length, true) : handler.String(str, length, true)); + } + if (RAPIDJSON_UNLIKELY(!success)) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell()); + } + + // Parse string to an output is + // This function handles the prefix/suffix double quotes, escaping, and optional encoding validation. + template + RAPIDJSON_FORCEINLINE void ParseStringToStream(InputStream& is, OutputStream& os) { +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 + static const char escape[256] = { + Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'/', + Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, + 0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0, + 0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 + }; +#undef Z16 +//!@endcond + + for (;;) { + // Scan and copy string before "\\\"" or < 0x20. This is an optional optimzation. + if (!(parseFlags & kParseValidateEncodingFlag)) + ScanCopyUnescapedString(is, os); + + Ch c = is.Peek(); + if (RAPIDJSON_UNLIKELY(c == '\\')) { // Escape + size_t escapeOffset = is.Tell(); // For invalid escaping, report the inital '\\' as error offset + is.Take(); + Ch e = is.Peek(); + if ((sizeof(Ch) == 1 || unsigned(e) < 256) && RAPIDJSON_LIKELY(escape[static_cast(e)])) { + is.Take(); + os.Put(static_cast(escape[static_cast(e)])); + } + else if (RAPIDJSON_LIKELY(e == 'u')) { // Unicode + is.Take(); + unsigned codepoint = ParseHex4(is, escapeOffset); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + if (RAPIDJSON_UNLIKELY(codepoint >= 0xD800 && codepoint <= 0xDBFF)) { + // Handle UTF-16 surrogate pair + if (RAPIDJSON_UNLIKELY(!Consume(is, '\\') || !Consume(is, 'u'))) + RAPIDJSON_PARSE_ERROR(kParseErrorStringUnicodeSurrogateInvalid, escapeOffset); + unsigned codepoint2 = ParseHex4(is, escapeOffset); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + if (RAPIDJSON_UNLIKELY(codepoint2 < 0xDC00 || codepoint2 > 0xDFFF)) + RAPIDJSON_PARSE_ERROR(kParseErrorStringUnicodeSurrogateInvalid, escapeOffset); + codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000; + } + TEncoding::Encode(os, codepoint); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorStringEscapeInvalid, escapeOffset); + } + else if (RAPIDJSON_UNLIKELY(c == '"')) { // Closing double quote + is.Take(); + os.Put('\0'); // null-terminate the string + return; + } + else if (RAPIDJSON_UNLIKELY(static_cast(c) < 0x20)) { // RFC 4627: unescaped = %x20-21 / %x23-5B / %x5D-10FFFF + if (c == '\0') + RAPIDJSON_PARSE_ERROR(kParseErrorStringMissQuotationMark, is.Tell()); + else + RAPIDJSON_PARSE_ERROR(kParseErrorStringEscapeInvalid, is.Tell()); + } + else { + size_t offset = is.Tell(); + if (RAPIDJSON_UNLIKELY((parseFlags & kParseValidateEncodingFlag ? + !Transcoder::Validate(is, os) : + !Transcoder::Transcode(is, os)))) + RAPIDJSON_PARSE_ERROR(kParseErrorStringInvalidEncoding, offset); + } + } + } + + template + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(InputStream&, OutputStream&) { + // Do nothing for generic version + } + +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) + // StringStream -> StackStream + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(StringStream& is, StackStream& os) { + const char* p = is.src_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = p; + return; + } + else + os.Put(*p++); + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + SizeType length; + #ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + length = offset; + #else + length = static_cast(__builtin_ffs(r) - 1); + #endif + char* q = reinterpret_cast(os.Push(length)); + for (size_t i = 0; i < length; i++) + q[i] = p[i]; + + p += length; + break; + } + _mm_storeu_si128(reinterpret_cast<__m128i *>(os.Push(16)), s); + } + + is.src_ = p; + } + + // InsituStringStream -> InsituStringStream + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(InsituStringStream& is, InsituStringStream& os) { + RAPIDJSON_ASSERT(&is == &os); + (void)os; + + if (is.src_ == is.dst_) { + SkipUnescapedString(is); + return; + } + + char* p = is.src_; + char *q = is.dst_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = p; + is.dst_ = q; + return; + } + else + *q++ = *p++; + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (;; p += 16, q += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + size_t length; +#ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + length = offset; +#else + length = static_cast(__builtin_ffs(r) - 1); +#endif + for (const char* pend = p + length; p != pend; ) + *q++ = *p++; + break; + } + _mm_storeu_si128(reinterpret_cast<__m128i *>(q), s); + } + + is.src_ = p; + is.dst_ = q; + } + + // When read/write pointers are the same for insitu stream, just skip unescaped characters + static RAPIDJSON_FORCEINLINE void SkipUnescapedString(InsituStringStream& is) { + RAPIDJSON_ASSERT(is.src_ == is.dst_); + char* p = is.src_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + for (; p != nextAligned; p++) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = is.dst_ = p; + return; + } + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + size_t length; +#ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + length = offset; +#else + length = static_cast(__builtin_ffs(r) - 1); +#endif + p += length; + break; + } + } + + is.src_ = is.dst_ = p; + } +#endif + + template + class NumberStream; + + template + class NumberStream { + public: + typedef typename InputStream::Ch Ch; + + NumberStream(GenericReader& reader, InputStream& s) : is(s) { (void)reader; } + ~NumberStream() {} + + RAPIDJSON_FORCEINLINE Ch Peek() const { return is.Peek(); } + RAPIDJSON_FORCEINLINE Ch TakePush() { return is.Take(); } + RAPIDJSON_FORCEINLINE Ch Take() { return is.Take(); } + RAPIDJSON_FORCEINLINE void Push(char) {} + + size_t Tell() { return is.Tell(); } + size_t Length() { return 0; } + const char* Pop() { return 0; } + + protected: + NumberStream& operator=(const NumberStream&); + + InputStream& is; + }; + + template + class NumberStream : public NumberStream { + typedef NumberStream Base; + public: + NumberStream(GenericReader& reader, InputStream& is) : Base(reader, is), stackStream(reader.stack_) {} + ~NumberStream() {} + + RAPIDJSON_FORCEINLINE Ch TakePush() { + stackStream.Put(static_cast(Base::is.Peek())); + return Base::is.Take(); + } + + RAPIDJSON_FORCEINLINE void Push(char c) { + stackStream.Put(c); + } + + size_t Length() { return stackStream.Length(); } + + const char* Pop() { + stackStream.Put('\0'); + return stackStream.Pop(); + } + + private: + StackStream stackStream; + }; + + template + class NumberStream : public NumberStream { + typedef NumberStream Base; + public: + NumberStream(GenericReader& reader, InputStream& is) : Base(reader, is) {} + ~NumberStream() {} + + RAPIDJSON_FORCEINLINE Ch Take() { return Base::TakePush(); } + }; + + template + void ParseNumber(InputStream& is, Handler& handler) { + internal::StreamLocalCopy copy(is); + NumberStream s(*this, copy.s); + + size_t startOffset = s.Tell(); + double d = 0.0; + bool useNanOrInf = false; + + // Parse minus + bool minus = Consume(s, '-'); + + // Parse int: zero / ( digit1-9 *DIGIT ) + unsigned i = 0; + uint64_t i64 = 0; + bool use64bit = false; + int significandDigit = 0; + if (RAPIDJSON_UNLIKELY(s.Peek() == '0')) { + i = 0; + s.TakePush(); + } + else if (RAPIDJSON_LIKELY(s.Peek() >= '1' && s.Peek() <= '9')) { + i = static_cast(s.TakePush() - '0'); + + if (minus) + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i >= 214748364)) { // 2^31 = 2147483648 + if (RAPIDJSON_LIKELY(i != 214748364 || s.Peek() > '8')) { + i64 = i; + use64bit = true; + break; + } + } + i = i * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + else + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i >= 429496729)) { // 2^32 - 1 = 4294967295 + if (RAPIDJSON_LIKELY(i != 429496729 || s.Peek() > '5')) { + i64 = i; + use64bit = true; + break; + } + } + i = i * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + } + // Parse NaN or Infinity here + else if ((parseFlags & kParseNanAndInfFlag) && RAPIDJSON_LIKELY((s.Peek() == 'I' || s.Peek() == 'N'))) { + useNanOrInf = true; + if (RAPIDJSON_LIKELY(Consume(s, 'N') && Consume(s, 'a') && Consume(s, 'N'))) { + d = std::numeric_limits::quiet_NaN(); + } + else if (RAPIDJSON_LIKELY(Consume(s, 'I') && Consume(s, 'n') && Consume(s, 'f'))) { + d = (minus ? -std::numeric_limits::infinity() : std::numeric_limits::infinity()); + if (RAPIDJSON_UNLIKELY(s.Peek() == 'i' && !(Consume(s, 'i') && Consume(s, 'n') + && Consume(s, 'i') && Consume(s, 't') && Consume(s, 'y')))) + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + + // Parse 64bit int + bool useDouble = false; + if (use64bit) { + if (minus) + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC))) // 2^63 = 9223372036854775808 + if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC) || s.Peek() > '8')) { + d = static_cast(i64); + useDouble = true; + break; + } + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + else + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x19999999, 0x99999999))) // 2^64 - 1 = 18446744073709551615 + if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || s.Peek() > '5')) { + d = static_cast(i64); + useDouble = true; + break; + } + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + } + + // Force double for big integer + if (useDouble) { + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(d >= 1.7976931348623157e307)) // DBL_MAX / 10.0 + RAPIDJSON_PARSE_ERROR(kParseErrorNumberTooBig, startOffset); + d = d * 10 + (s.TakePush() - '0'); + } + } + + // Parse frac = decimal-point 1*DIGIT + int expFrac = 0; + size_t decimalPosition; + if (Consume(s, '.')) { + decimalPosition = s.Length(); + + if (RAPIDJSON_UNLIKELY(!(s.Peek() >= '0' && s.Peek() <= '9'))) + RAPIDJSON_PARSE_ERROR(kParseErrorNumberMissFraction, s.Tell()); + + if (!useDouble) { +#if RAPIDJSON_64BIT + // Use i64 to store significand in 64-bit architecture + if (!use64bit) + i64 = i; + + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (i64 > RAPIDJSON_UINT64_C2(0x1FFFFF, 0xFFFFFFFF)) // 2^53 - 1 for fast path + break; + else { + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + --expFrac; + if (i64 != 0) + significandDigit++; + } + } + + d = static_cast(i64); +#else + // Use double to store significand in 32-bit architecture + d = static_cast(use64bit ? i64 : i); +#endif + useDouble = true; + } + + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (significandDigit < 17) { + d = d * 10.0 + (s.TakePush() - '0'); + --expFrac; + if (RAPIDJSON_LIKELY(d > 0.0)) + significandDigit++; + } + else + s.TakePush(); + } + } + else + decimalPosition = s.Length(); // decimal position at the end of integer. + + // Parse exp = e [ minus / plus ] 1*DIGIT + int exp = 0; + if (Consume(s, 'e') || Consume(s, 'E')) { + if (!useDouble) { + d = static_cast(use64bit ? i64 : i); + useDouble = true; + } + + bool expMinus = false; + if (Consume(s, '+')) + ; + else if (Consume(s, '-')) + expMinus = true; + + if (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + exp = static_cast(s.Take() - '0'); + if (expMinus) { + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + exp = exp * 10 + static_cast(s.Take() - '0'); + if (exp >= 214748364) { // Issue #313: prevent overflow exponent + while (RAPIDJSON_UNLIKELY(s.Peek() >= '0' && s.Peek() <= '9')) // Consume the rest of exponent + s.Take(); + } + } + } + else { // positive exp + int maxExp = 308 - expFrac; + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + exp = exp * 10 + static_cast(s.Take() - '0'); + if (RAPIDJSON_UNLIKELY(exp > maxExp)) + RAPIDJSON_PARSE_ERROR(kParseErrorNumberTooBig, startOffset); + } + } + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorNumberMissExponent, s.Tell()); + + if (expMinus) + exp = -exp; + } + + // Finish parsing, call event according to the type of number. + bool cont = true; + + if (parseFlags & kParseNumbersAsStringsFlag) { + if (parseFlags & kParseInsituFlag) { + s.Pop(); // Pop stack no matter if it will be used or not. + typename InputStream::Ch* head = is.PutBegin(); + const size_t length = s.Tell() - startOffset; + RAPIDJSON_ASSERT(length <= 0xFFFFFFFF); + // unable to insert the \0 character here, it will erase the comma after this number + const typename TargetEncoding::Ch* const str = reinterpret_cast(head); + cont = handler.RawNumber(str, SizeType(length), false); + } + else { + SizeType numCharsToCopy = static_cast(s.Length()); + StringStream srcStream(s.Pop()); + StackStream dstStream(stack_); + while (numCharsToCopy--) { + Transcoder, TargetEncoding>::Transcode(srcStream, dstStream); + } + dstStream.Put('\0'); + const typename TargetEncoding::Ch* str = dstStream.Pop(); + const SizeType length = static_cast(dstStream.Length()) - 1; + cont = handler.RawNumber(str, SizeType(length), true); + } + } + else { + size_t length = s.Length(); + const char* decimal = s.Pop(); // Pop stack no matter if it will be used or not. + + if (useDouble) { + int p = exp + expFrac; + if (parseFlags & kParseFullPrecisionFlag) + d = internal::StrtodFullPrecision(d, p, decimal, length, decimalPosition, exp); + else + d = internal::StrtodNormalPrecision(d, p); + + cont = handler.Double(minus ? -d : d); + } + else if (useNanOrInf) { + cont = handler.Double(d); + } + else { + if (use64bit) { + if (minus) + cont = handler.Int64(static_cast(~i64 + 1)); + else + cont = handler.Uint64(i64); + } + else { + if (minus) + cont = handler.Int(static_cast(~i + 1)); + else + cont = handler.Uint(i); + } + } + } + if (RAPIDJSON_UNLIKELY(!cont)) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, startOffset); + } + + // Parse any JSON value + template + void ParseValue(InputStream& is, Handler& handler) { + switch (is.Peek()) { + case 'n': ParseNull (is, handler); break; + case 't': ParseTrue (is, handler); break; + case 'f': ParseFalse (is, handler); break; + case '"': ParseString(is, handler); break; + case '{': ParseObject(is, handler); break; + case '[': ParseArray (is, handler); break; + default : + ParseNumber(is, handler); + break; + + } + } + + // Iterative Parsing + + // States + enum IterativeParsingState { + IterativeParsingStartState = 0, + IterativeParsingFinishState, + IterativeParsingErrorState, + + // Object states + IterativeParsingObjectInitialState, + IterativeParsingMemberKeyState, + IterativeParsingKeyValueDelimiterState, + IterativeParsingMemberValueState, + IterativeParsingMemberDelimiterState, + IterativeParsingObjectFinishState, + + // Array states + IterativeParsingArrayInitialState, + IterativeParsingElementState, + IterativeParsingElementDelimiterState, + IterativeParsingArrayFinishState, + + // Single value state + IterativeParsingValueState + }; + + enum { cIterativeParsingStateCount = IterativeParsingValueState + 1 }; + + // Tokens + enum Token { + LeftBracketToken = 0, + RightBracketToken, + + LeftCurlyBracketToken, + RightCurlyBracketToken, + + CommaToken, + ColonToken, + + StringToken, + FalseToken, + TrueToken, + NullToken, + NumberToken, + + kTokenCount + }; + + RAPIDJSON_FORCEINLINE Token Tokenize(Ch c) { + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define N NumberToken +#define N16 N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N + // Maps from ASCII to Token + static const unsigned char tokenMap[256] = { + N16, // 00~0F + N16, // 10~1F + N, N, StringToken, N, N, N, N, N, N, N, N, N, CommaToken, N, N, N, // 20~2F + N, N, N, N, N, N, N, N, N, N, ColonToken, N, N, N, N, N, // 30~3F + N16, // 40~4F + N, N, N, N, N, N, N, N, N, N, N, LeftBracketToken, N, RightBracketToken, N, N, // 50~5F + N, N, N, N, N, N, FalseToken, N, N, N, N, N, N, N, NullToken, N, // 60~6F + N, N, N, N, TrueToken, N, N, N, N, N, N, LeftCurlyBracketToken, N, RightCurlyBracketToken, N, N, // 70~7F + N16, N16, N16, N16, N16, N16, N16, N16 // 80~FF + }; +#undef N +#undef N16 +//!@endcond + + if (sizeof(Ch) == 1 || static_cast(c) < 256) + return static_cast(tokenMap[static_cast(c)]); + else + return NumberToken; + } + + RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState state, Token token) { + // current state x one lookahead token -> new state + static const char G[cIterativeParsingStateCount][kTokenCount] = { + // Start + { + IterativeParsingArrayInitialState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingValueState, // String + IterativeParsingValueState, // False + IterativeParsingValueState, // True + IterativeParsingValueState, // Null + IterativeParsingValueState // Number + }, + // Finish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Error(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // ObjectInitial + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberKeyState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // MemberKey + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingKeyValueDelimiterState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // KeyValueDelimiter + { + IterativeParsingArrayInitialState, // Left bracket(push MemberValue state) + IterativeParsingErrorState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberValueState, // String + IterativeParsingMemberValueState, // False + IterativeParsingMemberValueState, // True + IterativeParsingMemberValueState, // Null + IterativeParsingMemberValueState // Number + }, + // MemberValue + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingMemberDelimiterState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // MemberDelimiter + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberKeyState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // ObjectFinish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // ArrayInitial + { + IterativeParsingArrayInitialState, // Left bracket(push Element state) + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push Element state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingElementState, // String + IterativeParsingElementState, // False + IterativeParsingElementState, // True + IterativeParsingElementState, // Null + IterativeParsingElementState // Number + }, + // Element + { + IterativeParsingErrorState, // Left bracket + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingElementDelimiterState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // ElementDelimiter + { + IterativeParsingArrayInitialState, // Left bracket(push Element state) + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push Element state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingElementState, // String + IterativeParsingElementState, // False + IterativeParsingElementState, // True + IterativeParsingElementState, // Null + IterativeParsingElementState // Number + }, + // ArrayFinish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Single Value (sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + } + }; // End of G + + return static_cast(G[state][token]); + } + + // Make an advance in the token stream and state based on the candidate destination state which was returned by Transit(). + // May return a new state on state pop. + template + RAPIDJSON_FORCEINLINE IterativeParsingState Transit(IterativeParsingState src, Token token, IterativeParsingState dst, InputStream& is, Handler& handler) { + (void)token; + + switch (dst) { + case IterativeParsingErrorState: + return dst; + + case IterativeParsingObjectInitialState: + case IterativeParsingArrayInitialState: + { + // Push the state(Element or MemeberValue) if we are nested in another array or value of member. + // In this way we can get the correct state on ObjectFinish or ArrayFinish by frame pop. + IterativeParsingState n = src; + if (src == IterativeParsingArrayInitialState || src == IterativeParsingElementDelimiterState) + n = IterativeParsingElementState; + else if (src == IterativeParsingKeyValueDelimiterState) + n = IterativeParsingMemberValueState; + // Push current state. + *stack_.template Push(1) = n; + // Initialize and push the member/element count. + *stack_.template Push(1) = 0; + // Call handler + bool hr = (dst == IterativeParsingObjectInitialState) ? handler.StartObject() : handler.StartArray(); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return dst; + } + } + + case IterativeParsingMemberKeyState: + ParseString(is, handler, true); + if (HasParseError()) + return IterativeParsingErrorState; + else + return dst; + + case IterativeParsingKeyValueDelimiterState: + RAPIDJSON_ASSERT(token == ColonToken); + is.Take(); + return dst; + + case IterativeParsingMemberValueState: + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return dst; + + case IterativeParsingElementState: + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return dst; + + case IterativeParsingMemberDelimiterState: + case IterativeParsingElementDelimiterState: + is.Take(); + // Update member/element count. + *stack_.template Top() = *stack_.template Top() + 1; + return dst; + + case IterativeParsingObjectFinishState: + { + // Transit from delimiter is only allowed when trailing commas are enabled + if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingMemberDelimiterState) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorObjectMissName, is.Tell()); + return IterativeParsingErrorState; + } + // Get member count. + SizeType c = *stack_.template Pop(1); + // If the object is not empty, count the last member. + if (src == IterativeParsingMemberValueState) + ++c; + // Restore the state. + IterativeParsingState n = static_cast(*stack_.template Pop(1)); + // Transit to Finish state if this is the topmost scope. + if (n == IterativeParsingStartState) + n = IterativeParsingFinishState; + // Call handler + bool hr = handler.EndObject(c); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return n; + } + } + + case IterativeParsingArrayFinishState: + { + // Transit from delimiter is only allowed when trailing commas are enabled + if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingElementDelimiterState) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorValueInvalid, is.Tell()); + return IterativeParsingErrorState; + } + // Get element count. + SizeType c = *stack_.template Pop(1); + // If the array is not empty, count the last element. + if (src == IterativeParsingElementState) + ++c; + // Restore the state. + IterativeParsingState n = static_cast(*stack_.template Pop(1)); + // Transit to Finish state if this is the topmost scope. + if (n == IterativeParsingStartState) + n = IterativeParsingFinishState; + // Call handler + bool hr = handler.EndArray(c); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return n; + } + } + + default: + // This branch is for IterativeParsingValueState actually. + // Use `default:` rather than + // `case IterativeParsingValueState:` is for code coverage. + + // The IterativeParsingStartState is not enumerated in this switch-case. + // It is impossible for that case. And it can be caught by following assertion. + + // The IterativeParsingFinishState is not enumerated in this switch-case either. + // It is a "derivative" state which cannot triggered from Predict() directly. + // Therefore it cannot happen here. And it can be caught by following assertion. + RAPIDJSON_ASSERT(dst == IterativeParsingValueState); + + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return IterativeParsingFinishState; + } + } + + template + void HandleError(IterativeParsingState src, InputStream& is) { + if (HasParseError()) { + // Error flag has been set. + return; + } + + switch (src) { + case IterativeParsingStartState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentEmpty, is.Tell()); return; + case IterativeParsingFinishState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentRootNotSingular, is.Tell()); return; + case IterativeParsingObjectInitialState: + case IterativeParsingMemberDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); return; + case IterativeParsingMemberKeyState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissColon, is.Tell()); return; + case IterativeParsingMemberValueState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); return; + case IterativeParsingKeyValueDelimiterState: + case IterativeParsingArrayInitialState: + case IterativeParsingElementDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); return; + default: RAPIDJSON_ASSERT(src == IterativeParsingElementState); RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); return; + } + } + + template + ParseResult IterativeParse(InputStream& is, Handler& handler) { + parseResult_.Clear(); + ClearStackOnExit scope(*this); + IterativeParsingState state = IterativeParsingStartState; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + while (is.Peek() != '\0') { + Token t = Tokenize(is.Peek()); + IterativeParsingState n = Predict(state, t); + IterativeParsingState d = Transit(state, t, n, is, handler); + + if (d == IterativeParsingErrorState) { + HandleError(state, is); + break; + } + + state = d; + + // Do not further consume streams if a root JSON has been parsed. + if ((parseFlags & kParseStopWhenDoneFlag) && state == IterativeParsingFinishState) + break; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + + // Handle the end of file. + if (state != IterativeParsingFinishState) + HandleError(state, is); + + return parseResult_; + } + + static const size_t kDefaultStackCapacity = 256; //!< Default stack capacity in bytes for storing a single decoded string. + internal::Stack stack_; //!< A stack for storing decoded string temporarily during non-destructive parsing. + ParseResult parseResult_; +}; // class GenericReader + +//! Reader with UTF8 encoding and default allocator. +typedef GenericReader, UTF8<> > Reader; + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_READER_H_ diff --git a/rapidjson-1.1.0/rapid_json/schema.h b/rapidjson-1.1.0/rapid_json/schema.h new file mode 100644 index 000000000..b182aa27f --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/schema.h @@ -0,0 +1,2006 @@ +// Tencent is pleased to support the open source community by making RapidJSON available-> +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip-> All rights reserved-> +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License-> You may obtain a copy of the License at +// +// http://opensource->org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied-> See the License for the +// specific language governing permissions and limitations under the License-> + +#ifndef RAPIDJSON_SCHEMA_H_ +#define RAPIDJSON_SCHEMA_H_ + +#include "document.h" +#include "pointer.h" +#include // abs, floor + +#if !defined(RAPIDJSON_SCHEMA_USE_INTERNALREGEX) +#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 1 +#else +#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 0 +#endif + +#if !RAPIDJSON_SCHEMA_USE_INTERNALREGEX && !defined(RAPIDJSON_SCHEMA_USE_STDREGEX) && (__cplusplus >=201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)) +#define RAPIDJSON_SCHEMA_USE_STDREGEX 1 +#else +#define RAPIDJSON_SCHEMA_USE_STDREGEX 0 +#endif + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX +#include "internal/regex.h" +#elif RAPIDJSON_SCHEMA_USE_STDREGEX +#include +#endif + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX || RAPIDJSON_SCHEMA_USE_STDREGEX +#define RAPIDJSON_SCHEMA_HAS_REGEX 1 +#else +#define RAPIDJSON_SCHEMA_HAS_REGEX 0 +#endif + +#ifndef RAPIDJSON_SCHEMA_VERBOSE +#define RAPIDJSON_SCHEMA_VERBOSE 0 +#endif + +#if RAPIDJSON_SCHEMA_VERBOSE +#include "stringbuffer.h" +#endif + +RAPIDJSON_DIAG_PUSH + +#if defined(__GNUC__) +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_OFF(weak-vtables) +RAPIDJSON_DIAG_OFF(exit-time-destructors) +RAPIDJSON_DIAG_OFF(c++98-compat-pedantic) +RAPIDJSON_DIAG_OFF(variadic-macros) +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Verbose Utilities + +#if RAPIDJSON_SCHEMA_VERBOSE + +namespace internal { + +inline void PrintInvalidKeyword(const char* keyword) { + printf("Fail keyword: %s\n", keyword); +} + +inline void PrintInvalidKeyword(const wchar_t* keyword) { + wprintf(L"Fail keyword: %ls\n", keyword); +} + +inline void PrintInvalidDocument(const char* document) { + printf("Fail document: %s\n\n", document); +} + +inline void PrintInvalidDocument(const wchar_t* document) { + wprintf(L"Fail document: %ls\n\n", document); +} + +inline void PrintValidatorPointers(unsigned depth, const char* s, const char* d) { + printf("S: %*s%s\nD: %*s%s\n\n", depth * 4, " ", s, depth * 4, " ", d); +} + +inline void PrintValidatorPointers(unsigned depth, const wchar_t* s, const wchar_t* d) { + wprintf(L"S: %*ls%ls\nD: %*ls%ls\n\n", depth * 4, L" ", s, depth * 4, L" ", d); +} + +} // namespace internal + +#endif // RAPIDJSON_SCHEMA_VERBOSE + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_INVALID_KEYWORD_RETURN + +#if RAPIDJSON_SCHEMA_VERBOSE +#define RAPIDJSON_INVALID_KEYWORD_VERBOSE(keyword) internal::PrintInvalidKeyword(keyword) +#else +#define RAPIDJSON_INVALID_KEYWORD_VERBOSE(keyword) +#endif + +#define RAPIDJSON_INVALID_KEYWORD_RETURN(keyword)\ +RAPIDJSON_MULTILINEMACRO_BEGIN\ + context.invalidKeyword = keyword.GetString();\ + RAPIDJSON_INVALID_KEYWORD_VERBOSE(keyword.GetString());\ + return false;\ +RAPIDJSON_MULTILINEMACRO_END + +/////////////////////////////////////////////////////////////////////////////// +// Forward declarations + +template +class GenericSchemaDocument; + +namespace internal { + +template +class Schema; + +/////////////////////////////////////////////////////////////////////////////// +// ISchemaValidator + +class ISchemaValidator { +public: + virtual ~ISchemaValidator() {} + virtual bool IsValid() const = 0; +}; + +/////////////////////////////////////////////////////////////////////////////// +// ISchemaStateFactory + +template +class ISchemaStateFactory { +public: + virtual ~ISchemaStateFactory() {} + virtual ISchemaValidator* CreateSchemaValidator(const SchemaType&) = 0; + virtual void DestroySchemaValidator(ISchemaValidator* validator) = 0; + virtual void* CreateHasher() = 0; + virtual uint64_t GetHashCode(void* hasher) = 0; + virtual void DestroryHasher(void* hasher) = 0; + virtual void* MallocState(size_t size) = 0; + virtual void FreeState(void* p) = 0; +}; + +/////////////////////////////////////////////////////////////////////////////// +// Hasher + +// For comparison of compound value +template +class Hasher { +public: + typedef typename Encoding::Ch Ch; + + Hasher(Allocator* allocator = 0, size_t stackCapacity = kDefaultSize) : stack_(allocator, stackCapacity) {} + + bool Null() { return WriteType(kNullType); } + bool Bool(bool b) { return WriteType(b ? kTrueType : kFalseType); } + bool Int(int i) { Number n; n.u.i = i; n.d = static_cast(i); return WriteNumber(n); } + bool Uint(unsigned u) { Number n; n.u.u = u; n.d = static_cast(u); return WriteNumber(n); } + bool Int64(int64_t i) { Number n; n.u.i = i; n.d = static_cast(i); return WriteNumber(n); } + bool Uint64(uint64_t u) { Number n; n.u.u = u; n.d = static_cast(u); return WriteNumber(n); } + bool Double(double d) { + Number n; + if (d < 0) n.u.i = static_cast(d); + else n.u.u = static_cast(d); + n.d = d; + return WriteNumber(n); + } + + bool RawNumber(const Ch* str, SizeType len, bool) { + WriteBuffer(kNumberType, str, len * sizeof(Ch)); + return true; + } + + bool String(const Ch* str, SizeType len, bool) { + WriteBuffer(kStringType, str, len * sizeof(Ch)); + return true; + } + + bool StartObject() { return true; } + bool Key(const Ch* str, SizeType len, bool copy) { return String(str, len, copy); } + bool EndObject(SizeType memberCount) { + uint64_t h = Hash(0, kObjectType); + uint64_t* kv = stack_.template Pop(memberCount * 2); + for (SizeType i = 0; i < memberCount; i++) + h ^= Hash(kv[i * 2], kv[i * 2 + 1]); // Use xor to achieve member order insensitive + *stack_.template Push() = h; + return true; + } + + bool StartArray() { return true; } + bool EndArray(SizeType elementCount) { + uint64_t h = Hash(0, kArrayType); + uint64_t* e = stack_.template Pop(elementCount); + for (SizeType i = 0; i < elementCount; i++) + h = Hash(h, e[i]); // Use hash to achieve element order sensitive + *stack_.template Push() = h; + return true; + } + + bool IsValid() const { return stack_.GetSize() == sizeof(uint64_t); } + + uint64_t GetHashCode() const { + RAPIDJSON_ASSERT(IsValid()); + return *stack_.template Top(); + } + +private: + static const size_t kDefaultSize = 256; + struct Number { + union U { + uint64_t u; + int64_t i; + }u; + double d; + }; + + bool WriteType(Type type) { return WriteBuffer(type, 0, 0); } + + bool WriteNumber(const Number& n) { return WriteBuffer(kNumberType, &n, sizeof(n)); } + + bool WriteBuffer(Type type, const void* data, size_t len) { + // FNV-1a from http://isthe.com/chongo/tech/comp/fnv/ + uint64_t h = Hash(RAPIDJSON_UINT64_C2(0x84222325, 0xcbf29ce4), type); + const unsigned char* d = static_cast(data); + for (size_t i = 0; i < len; i++) + h = Hash(h, d[i]); + *stack_.template Push() = h; + return true; + } + + static uint64_t Hash(uint64_t h, uint64_t d) { + static const uint64_t kPrime = RAPIDJSON_UINT64_C2(0x00000100, 0x000001b3); + h ^= d; + h *= kPrime; + return h; + } + + Stack stack_; +}; + +/////////////////////////////////////////////////////////////////////////////// +// SchemaValidationContext + +template +struct SchemaValidationContext { + typedef Schema SchemaType; + typedef ISchemaStateFactory SchemaValidatorFactoryType; + typedef typename SchemaType::ValueType ValueType; + typedef typename ValueType::Ch Ch; + + enum PatternValidatorType { + kPatternValidatorOnly, + kPatternValidatorWithProperty, + kPatternValidatorWithAdditionalProperty + }; + + SchemaValidationContext(SchemaValidatorFactoryType& f, const SchemaType* s) : + factory(f), + schema(s), + valueSchema(), + invalidKeyword(), + hasher(), + arrayElementHashCodes(), + validators(), + validatorCount(), + patternPropertiesValidators(), + patternPropertiesValidatorCount(), + patternPropertiesSchemas(), + patternPropertiesSchemaCount(), + valuePatternValidatorType(kPatternValidatorOnly), + propertyExist(), + inArray(false), + valueUniqueness(false), + arrayUniqueness(false) + { + } + + ~SchemaValidationContext() { + if (hasher) + factory.DestroryHasher(hasher); + if (validators) { + for (SizeType i = 0; i < validatorCount; i++) + factory.DestroySchemaValidator(validators[i]); + factory.FreeState(validators); + } + if (patternPropertiesValidators) { + for (SizeType i = 0; i < patternPropertiesValidatorCount; i++) + factory.DestroySchemaValidator(patternPropertiesValidators[i]); + factory.FreeState(patternPropertiesValidators); + } + if (patternPropertiesSchemas) + factory.FreeState(patternPropertiesSchemas); + if (propertyExist) + factory.FreeState(propertyExist); + } + + SchemaValidatorFactoryType& factory; + const SchemaType* schema; + const SchemaType* valueSchema; + const Ch* invalidKeyword; + void* hasher; // Only validator access + void* arrayElementHashCodes; // Only validator access this + ISchemaValidator** validators; + SizeType validatorCount; + ISchemaValidator** patternPropertiesValidators; + SizeType patternPropertiesValidatorCount; + const SchemaType** patternPropertiesSchemas; + SizeType patternPropertiesSchemaCount; + PatternValidatorType valuePatternValidatorType; + PatternValidatorType objectPatternValidatorType; + SizeType arrayElementIndex; + bool* propertyExist; + bool inArray; + bool valueUniqueness; + bool arrayUniqueness; +}; + +/////////////////////////////////////////////////////////////////////////////// +// Schema + +template +class Schema { +public: + typedef typename SchemaDocumentType::ValueType ValueType; + typedef typename SchemaDocumentType::AllocatorType AllocatorType; + typedef typename SchemaDocumentType::PointerType PointerType; + typedef typename ValueType::EncodingType EncodingType; + typedef typename EncodingType::Ch Ch; + typedef SchemaValidationContext Context; + typedef Schema SchemaType; + typedef GenericValue SValue; + friend class GenericSchemaDocument; + + Schema(SchemaDocumentType* schemaDocument, const PointerType& p, const ValueType& value, const ValueType& document, AllocatorType* allocator) : + allocator_(allocator), + enum_(), + enumCount_(), + not_(), + type_((1 << kTotalSchemaType) - 1), // typeless + validatorCount_(), + properties_(), + additionalPropertiesSchema_(), + patternProperties_(), + patternPropertyCount_(), + propertyCount_(), + minProperties_(), + maxProperties_(SizeType(~0)), + additionalProperties_(true), + hasDependencies_(), + hasRequired_(), + hasSchemaDependencies_(), + additionalItemsSchema_(), + itemsList_(), + itemsTuple_(), + itemsTupleCount_(), + minItems_(), + maxItems_(SizeType(~0)), + additionalItems_(true), + uniqueItems_(false), + pattern_(), + minLength_(0), + maxLength_(~SizeType(0)), + exclusiveMinimum_(false), + exclusiveMaximum_(false) + { + typedef typename SchemaDocumentType::ValueType ValueType; + typedef typename ValueType::ConstValueIterator ConstValueIterator; + typedef typename ValueType::ConstMemberIterator ConstMemberIterator; + + if (!value.IsObject()) + return; + + if (const ValueType* v = GetMember(value, GetTypeString())) { + type_ = 0; + if (v->IsString()) + AddType(*v); + else if (v->IsArray()) + for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr) + AddType(*itr); + } + + if (const ValueType* v = GetMember(value, GetEnumString())) + if (v->IsArray() && v->Size() > 0) { + enum_ = static_cast(allocator_->Malloc(sizeof(uint64_t) * v->Size())); + for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr) { + typedef Hasher > EnumHasherType; + char buffer[256 + 24]; + MemoryPoolAllocator<> hasherAllocator(buffer, sizeof(buffer)); + EnumHasherType h(&hasherAllocator, 256); + itr->Accept(h); + enum_[enumCount_++] = h.GetHashCode(); + } + } + + if (schemaDocument) { + AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document); + AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document); + AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document); + } + + if (const ValueType* v = GetMember(value, GetNotString())) { + schemaDocument->CreateSchema(¬_, p.Append(GetNotString(), allocator_), *v, document); + notValidatorIndex_ = validatorCount_; + validatorCount_++; + } + + // Object + + const ValueType* properties = GetMember(value, GetPropertiesString()); + const ValueType* required = GetMember(value, GetRequiredString()); + const ValueType* dependencies = GetMember(value, GetDependenciesString()); + { + // Gather properties from properties/required/dependencies + SValue allProperties(kArrayType); + + if (properties && properties->IsObject()) + for (ConstMemberIterator itr = properties->MemberBegin(); itr != properties->MemberEnd(); ++itr) + AddUniqueElement(allProperties, itr->name); + + if (required && required->IsArray()) + for (ConstValueIterator itr = required->Begin(); itr != required->End(); ++itr) + if (itr->IsString()) + AddUniqueElement(allProperties, *itr); + + if (dependencies && dependencies->IsObject()) + for (ConstMemberIterator itr = dependencies->MemberBegin(); itr != dependencies->MemberEnd(); ++itr) { + AddUniqueElement(allProperties, itr->name); + if (itr->value.IsArray()) + for (ConstValueIterator i = itr->value.Begin(); i != itr->value.End(); ++i) + if (i->IsString()) + AddUniqueElement(allProperties, *i); + } + + if (allProperties.Size() > 0) { + propertyCount_ = allProperties.Size(); + properties_ = static_cast(allocator_->Malloc(sizeof(Property) * propertyCount_)); + for (SizeType i = 0; i < propertyCount_; i++) { + new (&properties_[i]) Property(); + properties_[i].name = allProperties[i]; + properties_[i].schema = GetTypeless(); + } + } + } + + if (properties && properties->IsObject()) { + PointerType q = p.Append(GetPropertiesString(), allocator_); + for (ConstMemberIterator itr = properties->MemberBegin(); itr != properties->MemberEnd(); ++itr) { + SizeType index; + if (FindPropertyIndex(itr->name, &index)) + schemaDocument->CreateSchema(&properties_[index].schema, q.Append(itr->name, allocator_), itr->value, document); + } + } + + if (const ValueType* v = GetMember(value, GetPatternPropertiesString())) { + PointerType q = p.Append(GetPatternPropertiesString(), allocator_); + patternProperties_ = static_cast(allocator_->Malloc(sizeof(PatternProperty) * v->MemberCount())); + patternPropertyCount_ = 0; + + for (ConstMemberIterator itr = v->MemberBegin(); itr != v->MemberEnd(); ++itr) { + new (&patternProperties_[patternPropertyCount_]) PatternProperty(); + patternProperties_[patternPropertyCount_].pattern = CreatePattern(itr->name); + schemaDocument->CreateSchema(&patternProperties_[patternPropertyCount_].schema, q.Append(itr->name, allocator_), itr->value, document); + patternPropertyCount_++; + } + } + + if (required && required->IsArray()) + for (ConstValueIterator itr = required->Begin(); itr != required->End(); ++itr) + if (itr->IsString()) { + SizeType index; + if (FindPropertyIndex(*itr, &index)) { + properties_[index].required = true; + hasRequired_ = true; + } + } + + if (dependencies && dependencies->IsObject()) { + PointerType q = p.Append(GetDependenciesString(), allocator_); + hasDependencies_ = true; + for (ConstMemberIterator itr = dependencies->MemberBegin(); itr != dependencies->MemberEnd(); ++itr) { + SizeType sourceIndex; + if (FindPropertyIndex(itr->name, &sourceIndex)) { + if (itr->value.IsArray()) { + properties_[sourceIndex].dependencies = static_cast(allocator_->Malloc(sizeof(bool) * propertyCount_)); + std::memset(properties_[sourceIndex].dependencies, 0, sizeof(bool)* propertyCount_); + for (ConstValueIterator targetItr = itr->value.Begin(); targetItr != itr->value.End(); ++targetItr) { + SizeType targetIndex; + if (FindPropertyIndex(*targetItr, &targetIndex)) + properties_[sourceIndex].dependencies[targetIndex] = true; + } + } + else if (itr->value.IsObject()) { + hasSchemaDependencies_ = true; + schemaDocument->CreateSchema(&properties_[sourceIndex].dependenciesSchema, q.Append(itr->name, allocator_), itr->value, document); + properties_[sourceIndex].dependenciesValidatorIndex = validatorCount_; + validatorCount_++; + } + } + } + } + + if (const ValueType* v = GetMember(value, GetAdditionalPropertiesString())) { + if (v->IsBool()) + additionalProperties_ = v->GetBool(); + else if (v->IsObject()) + schemaDocument->CreateSchema(&additionalPropertiesSchema_, p.Append(GetAdditionalPropertiesString(), allocator_), *v, document); + } + + AssignIfExist(minProperties_, value, GetMinPropertiesString()); + AssignIfExist(maxProperties_, value, GetMaxPropertiesString()); + + // Array + if (const ValueType* v = GetMember(value, GetItemsString())) { + PointerType q = p.Append(GetItemsString(), allocator_); + if (v->IsObject()) // List validation + schemaDocument->CreateSchema(&itemsList_, q, *v, document); + else if (v->IsArray()) { // Tuple validation + itemsTuple_ = static_cast(allocator_->Malloc(sizeof(const Schema*) * v->Size())); + SizeType index = 0; + for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr, index++) + schemaDocument->CreateSchema(&itemsTuple_[itemsTupleCount_++], q.Append(index, allocator_), *itr, document); + } + } + + AssignIfExist(minItems_, value, GetMinItemsString()); + AssignIfExist(maxItems_, value, GetMaxItemsString()); + + if (const ValueType* v = GetMember(value, GetAdditionalItemsString())) { + if (v->IsBool()) + additionalItems_ = v->GetBool(); + else if (v->IsObject()) + schemaDocument->CreateSchema(&additionalItemsSchema_, p.Append(GetAdditionalItemsString(), allocator_), *v, document); + } + + AssignIfExist(uniqueItems_, value, GetUniqueItemsString()); + + // String + AssignIfExist(minLength_, value, GetMinLengthString()); + AssignIfExist(maxLength_, value, GetMaxLengthString()); + + if (const ValueType* v = GetMember(value, GetPatternString())) + pattern_ = CreatePattern(*v); + + // Number + if (const ValueType* v = GetMember(value, GetMinimumString())) + if (v->IsNumber()) + minimum_.CopyFrom(*v, *allocator_); + + if (const ValueType* v = GetMember(value, GetMaximumString())) + if (v->IsNumber()) + maximum_.CopyFrom(*v, *allocator_); + + AssignIfExist(exclusiveMinimum_, value, GetExclusiveMinimumString()); + AssignIfExist(exclusiveMaximum_, value, GetExclusiveMaximumString()); + + if (const ValueType* v = GetMember(value, GetMultipleOfString())) + if (v->IsNumber() && v->GetDouble() > 0.0) + multipleOf_.CopyFrom(*v, *allocator_); + } + + ~Schema() { + if (allocator_) { + allocator_->Free(enum_); + } + if (properties_) { + for (SizeType i = 0; i < propertyCount_; i++) + properties_[i].~Property(); + AllocatorType::Free(properties_); + } + if (patternProperties_) { + for (SizeType i = 0; i < patternPropertyCount_; i++) + patternProperties_[i].~PatternProperty(); + AllocatorType::Free(patternProperties_); + } + AllocatorType::Free(itemsTuple_); +#if RAPIDJSON_SCHEMA_HAS_REGEX + if (pattern_) { + pattern_->~RegexType(); + allocator_->Free(pattern_); + } +#endif + } + + bool BeginValue(Context& context) const { + if (context.inArray) { + if (uniqueItems_) + context.valueUniqueness = true; + + if (itemsList_) + context.valueSchema = itemsList_; + else if (itemsTuple_) { + if (context.arrayElementIndex < itemsTupleCount_) + context.valueSchema = itemsTuple_[context.arrayElementIndex]; + else if (additionalItemsSchema_) + context.valueSchema = additionalItemsSchema_; + else if (additionalItems_) + context.valueSchema = GetTypeless(); + else + RAPIDJSON_INVALID_KEYWORD_RETURN(GetItemsString()); + } + else + context.valueSchema = GetTypeless(); + + context.arrayElementIndex++; + } + return true; + } + + RAPIDJSON_FORCEINLINE bool EndValue(Context& context) const { + if (context.patternPropertiesValidatorCount > 0) { + bool otherValid = false; + SizeType count = context.patternPropertiesValidatorCount; + if (context.objectPatternValidatorType != Context::kPatternValidatorOnly) + otherValid = context.patternPropertiesValidators[--count]->IsValid(); + + bool patternValid = true; + for (SizeType i = 0; i < count; i++) + if (!context.patternPropertiesValidators[i]->IsValid()) { + patternValid = false; + break; + } + + if (context.objectPatternValidatorType == Context::kPatternValidatorOnly) { + if (!patternValid) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternPropertiesString()); + } + else if (context.objectPatternValidatorType == Context::kPatternValidatorWithProperty) { + if (!patternValid || !otherValid) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternPropertiesString()); + } + else if (!patternValid && !otherValid) // kPatternValidatorWithAdditionalProperty) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternPropertiesString()); + } + + if (enum_) { + const uint64_t h = context.factory.GetHashCode(context.hasher); + for (SizeType i = 0; i < enumCount_; i++) + if (enum_[i] == h) + goto foundEnum; + RAPIDJSON_INVALID_KEYWORD_RETURN(GetEnumString()); + foundEnum:; + } + + if (allOf_.schemas) + for (SizeType i = allOf_.begin; i < allOf_.begin + allOf_.count; i++) + if (!context.validators[i]->IsValid()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetAllOfString()); + + if (anyOf_.schemas) { + for (SizeType i = anyOf_.begin; i < anyOf_.begin + anyOf_.count; i++) + if (context.validators[i]->IsValid()) + goto foundAny; + RAPIDJSON_INVALID_KEYWORD_RETURN(GetAnyOfString()); + foundAny:; + } + + if (oneOf_.schemas) { + bool oneValid = false; + for (SizeType i = oneOf_.begin; i < oneOf_.begin + oneOf_.count; i++) + if (context.validators[i]->IsValid()) { + if (oneValid) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetOneOfString()); + else + oneValid = true; + } + if (!oneValid) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetOneOfString()); + } + + if (not_ && context.validators[notValidatorIndex_]->IsValid()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetNotString()); + + return true; + } + + bool Null(Context& context) const { + if (!(type_ & (1 << kNullSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + return CreateParallelValidator(context); + } + + bool Bool(Context& context, bool) const { + if (!(type_ & (1 << kBooleanSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + return CreateParallelValidator(context); + } + + bool Int(Context& context, int i) const { + if (!CheckInt(context, i)) + return false; + return CreateParallelValidator(context); + } + + bool Uint(Context& context, unsigned u) const { + if (!CheckUint(context, u)) + return false; + return CreateParallelValidator(context); + } + + bool Int64(Context& context, int64_t i) const { + if (!CheckInt(context, i)) + return false; + return CreateParallelValidator(context); + } + + bool Uint64(Context& context, uint64_t u) const { + if (!CheckUint(context, u)) + return false; + return CreateParallelValidator(context); + } + + bool Double(Context& context, double d) const { + if (!(type_ & (1 << kNumberSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (!minimum_.IsNull() && !CheckDoubleMinimum(context, d)) + return false; + + if (!maximum_.IsNull() && !CheckDoubleMaximum(context, d)) + return false; + + if (!multipleOf_.IsNull() && !CheckDoubleMultipleOf(context, d)) + return false; + + return CreateParallelValidator(context); + } + + bool String(Context& context, const Ch* str, SizeType length, bool) const { + if (!(type_ & (1 << kStringSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (minLength_ != 0 || maxLength_ != SizeType(~0)) { + SizeType count; + if (internal::CountStringCodePoint(str, length, &count)) { + if (count < minLength_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinLengthString()); + if (count > maxLength_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaxLengthString()); + } + } + + if (pattern_ && !IsPatternMatch(pattern_, str, length)) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternString()); + + return CreateParallelValidator(context); + } + + bool StartObject(Context& context) const { + if (!(type_ & (1 << kObjectSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (hasDependencies_ || hasRequired_) { + context.propertyExist = static_cast(context.factory.MallocState(sizeof(bool) * propertyCount_)); + std::memset(context.propertyExist, 0, sizeof(bool) * propertyCount_); + } + + if (patternProperties_) { // pre-allocate schema array + SizeType count = patternPropertyCount_ + 1; // extra for valuePatternValidatorType + context.patternPropertiesSchemas = static_cast(context.factory.MallocState(sizeof(const SchemaType*) * count)); + context.patternPropertiesSchemaCount = 0; + std::memset(context.patternPropertiesSchemas, 0, sizeof(SchemaType*) * count); + } + + return CreateParallelValidator(context); + } + + bool Key(Context& context, const Ch* str, SizeType len, bool) const { + if (patternProperties_) { + context.patternPropertiesSchemaCount = 0; + for (SizeType i = 0; i < patternPropertyCount_; i++) + if (patternProperties_[i].pattern && IsPatternMatch(patternProperties_[i].pattern, str, len)) + context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = patternProperties_[i].schema; + } + + SizeType index; + if (FindPropertyIndex(ValueType(str, len).Move(), &index)) { + if (context.patternPropertiesSchemaCount > 0) { + context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = properties_[index].schema; + context.valueSchema = GetTypeless(); + context.valuePatternValidatorType = Context::kPatternValidatorWithProperty; + } + else + context.valueSchema = properties_[index].schema; + + if (context.propertyExist) + context.propertyExist[index] = true; + + return true; + } + + if (additionalPropertiesSchema_) { + if (additionalPropertiesSchema_ && context.patternPropertiesSchemaCount > 0) { + context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = additionalPropertiesSchema_; + context.valueSchema = GetTypeless(); + context.valuePatternValidatorType = Context::kPatternValidatorWithAdditionalProperty; + } + else + context.valueSchema = additionalPropertiesSchema_; + return true; + } + else if (additionalProperties_) { + context.valueSchema = GetTypeless(); + return true; + } + + if (context.patternPropertiesSchemaCount == 0) // patternProperties are not additional properties + RAPIDJSON_INVALID_KEYWORD_RETURN(GetAdditionalPropertiesString()); + + return true; + } + + bool EndObject(Context& context, SizeType memberCount) const { + if (hasRequired_) + for (SizeType index = 0; index < propertyCount_; index++) + if (properties_[index].required) + if (!context.propertyExist[index]) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString()); + + if (memberCount < minProperties_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinPropertiesString()); + + if (memberCount > maxProperties_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaxPropertiesString()); + + if (hasDependencies_) { + for (SizeType sourceIndex = 0; sourceIndex < propertyCount_; sourceIndex++) + if (context.propertyExist[sourceIndex]) { + if (properties_[sourceIndex].dependencies) { + for (SizeType targetIndex = 0; targetIndex < propertyCount_; targetIndex++) + if (properties_[sourceIndex].dependencies[targetIndex] && !context.propertyExist[targetIndex]) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetDependenciesString()); + } + else if (properties_[sourceIndex].dependenciesSchema) + if (!context.validators[properties_[sourceIndex].dependenciesValidatorIndex]->IsValid()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetDependenciesString()); + } + } + + return true; + } + + bool StartArray(Context& context) const { + if (!(type_ & (1 << kArraySchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + context.arrayElementIndex = 0; + context.inArray = true; + + return CreateParallelValidator(context); + } + + bool EndArray(Context& context, SizeType elementCount) const { + context.inArray = false; + + if (elementCount < minItems_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinItemsString()); + + if (elementCount > maxItems_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaxItemsString()); + + return true; + } + + // Generate functions for string literal according to Ch +#define RAPIDJSON_STRING_(name, ...) \ + static const ValueType& Get##name##String() {\ + static const Ch s[] = { __VA_ARGS__, '\0' };\ + static const ValueType v(s, sizeof(s) / sizeof(Ch) - 1);\ + return v;\ + } + + RAPIDJSON_STRING_(Null, 'n', 'u', 'l', 'l') + RAPIDJSON_STRING_(Boolean, 'b', 'o', 'o', 'l', 'e', 'a', 'n') + RAPIDJSON_STRING_(Object, 'o', 'b', 'j', 'e', 'c', 't') + RAPIDJSON_STRING_(Array, 'a', 'r', 'r', 'a', 'y') + RAPIDJSON_STRING_(String, 's', 't', 'r', 'i', 'n', 'g') + RAPIDJSON_STRING_(Number, 'n', 'u', 'm', 'b', 'e', 'r') + RAPIDJSON_STRING_(Integer, 'i', 'n', 't', 'e', 'g', 'e', 'r') + RAPIDJSON_STRING_(Type, 't', 'y', 'p', 'e') + RAPIDJSON_STRING_(Enum, 'e', 'n', 'u', 'm') + RAPIDJSON_STRING_(AllOf, 'a', 'l', 'l', 'O', 'f') + RAPIDJSON_STRING_(AnyOf, 'a', 'n', 'y', 'O', 'f') + RAPIDJSON_STRING_(OneOf, 'o', 'n', 'e', 'O', 'f') + RAPIDJSON_STRING_(Not, 'n', 'o', 't') + RAPIDJSON_STRING_(Properties, 'p', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(Required, 'r', 'e', 'q', 'u', 'i', 'r', 'e', 'd') + RAPIDJSON_STRING_(Dependencies, 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'i', 'e', 's') + RAPIDJSON_STRING_(PatternProperties, 'p', 'a', 't', 't', 'e', 'r', 'n', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(AdditionalProperties, 'a', 'd', 'd', 'i', 't', 'i', 'o', 'n', 'a', 'l', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(MinProperties, 'm', 'i', 'n', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(MaxProperties, 'm', 'a', 'x', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(Items, 'i', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(MinItems, 'm', 'i', 'n', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(MaxItems, 'm', 'a', 'x', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(AdditionalItems, 'a', 'd', 'd', 'i', 't', 'i', 'o', 'n', 'a', 'l', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(UniqueItems, 'u', 'n', 'i', 'q', 'u', 'e', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(MinLength, 'm', 'i', 'n', 'L', 'e', 'n', 'g', 't', 'h') + RAPIDJSON_STRING_(MaxLength, 'm', 'a', 'x', 'L', 'e', 'n', 'g', 't', 'h') + RAPIDJSON_STRING_(Pattern, 'p', 'a', 't', 't', 'e', 'r', 'n') + RAPIDJSON_STRING_(Minimum, 'm', 'i', 'n', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(Maximum, 'm', 'a', 'x', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(ExclusiveMinimum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'i', 'n', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(ExclusiveMaximum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'a', 'x', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(MultipleOf, 'm', 'u', 'l', 't', 'i', 'p', 'l', 'e', 'O', 'f') + +#undef RAPIDJSON_STRING_ + +private: + enum SchemaValueType { + kNullSchemaType, + kBooleanSchemaType, + kObjectSchemaType, + kArraySchemaType, + kStringSchemaType, + kNumberSchemaType, + kIntegerSchemaType, + kTotalSchemaType + }; + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX + typedef internal::GenericRegex RegexType; +#elif RAPIDJSON_SCHEMA_USE_STDREGEX + typedef std::basic_regex RegexType; +#else + typedef char RegexType; +#endif + + struct SchemaArray { + SchemaArray() : schemas(), count() {} + ~SchemaArray() { AllocatorType::Free(schemas); } + const SchemaType** schemas; + SizeType begin; // begin index of context.validators + SizeType count; + }; + + static const SchemaType* GetTypeless() { + static SchemaType typeless(0, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0); + return &typeless; + } + + template + void AddUniqueElement(V1& a, const V2& v) { + for (typename V1::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) + if (*itr == v) + return; + V1 c(v, *allocator_); + a.PushBack(c, *allocator_); + } + + static const ValueType* GetMember(const ValueType& value, const ValueType& name) { + typename ValueType::ConstMemberIterator itr = value.FindMember(name); + return itr != value.MemberEnd() ? &(itr->value) : 0; + } + + static void AssignIfExist(bool& out, const ValueType& value, const ValueType& name) { + if (const ValueType* v = GetMember(value, name)) + if (v->IsBool()) + out = v->GetBool(); + } + + static void AssignIfExist(SizeType& out, const ValueType& value, const ValueType& name) { + if (const ValueType* v = GetMember(value, name)) + if (v->IsUint64() && v->GetUint64() <= SizeType(~0)) + out = static_cast(v->GetUint64()); + } + + void AssignIfExist(SchemaArray& out, SchemaDocumentType& schemaDocument, const PointerType& p, const ValueType& value, const ValueType& name, const ValueType& document) { + if (const ValueType* v = GetMember(value, name)) { + if (v->IsArray() && v->Size() > 0) { + PointerType q = p.Append(name, allocator_); + out.count = v->Size(); + out.schemas = static_cast(allocator_->Malloc(out.count * sizeof(const Schema*))); + memset(out.schemas, 0, sizeof(Schema*)* out.count); + for (SizeType i = 0; i < out.count; i++) + schemaDocument.CreateSchema(&out.schemas[i], q.Append(i, allocator_), (*v)[i], document); + out.begin = validatorCount_; + validatorCount_ += out.count; + } + } + } + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX + template + RegexType* CreatePattern(const ValueType& value) { + if (value.IsString()) { + RegexType* r = new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString()); + if (!r->IsValid()) { + r->~RegexType(); + AllocatorType::Free(r); + r = 0; + } + return r; + } + return 0; + } + + static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType) { + return pattern->Search(str); + } +#elif RAPIDJSON_SCHEMA_USE_STDREGEX + template + RegexType* CreatePattern(const ValueType& value) { + if (value.IsString()) + try { + return new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString(), std::size_t(value.GetStringLength()), std::regex_constants::ECMAScript); + } + catch (const std::regex_error&) { + } + return 0; + } + + static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType length) { + std::match_results r; + return std::regex_search(str, str + length, r, *pattern); + } +#else + template + RegexType* CreatePattern(const ValueType&) { return 0; } + + static bool IsPatternMatch(const RegexType*, const Ch *, SizeType) { return true; } +#endif // RAPIDJSON_SCHEMA_USE_STDREGEX + + void AddType(const ValueType& type) { + if (type == GetNullString() ) type_ |= 1 << kNullSchemaType; + else if (type == GetBooleanString()) type_ |= 1 << kBooleanSchemaType; + else if (type == GetObjectString() ) type_ |= 1 << kObjectSchemaType; + else if (type == GetArrayString() ) type_ |= 1 << kArraySchemaType; + else if (type == GetStringString() ) type_ |= 1 << kStringSchemaType; + else if (type == GetIntegerString()) type_ |= 1 << kIntegerSchemaType; + else if (type == GetNumberString() ) type_ |= (1 << kNumberSchemaType) | (1 << kIntegerSchemaType); + } + + bool CreateParallelValidator(Context& context) const { + if (enum_ || context.arrayUniqueness) + context.hasher = context.factory.CreateHasher(); + + if (validatorCount_) { + RAPIDJSON_ASSERT(context.validators == 0); + context.validators = static_cast(context.factory.MallocState(sizeof(ISchemaValidator*) * validatorCount_)); + context.validatorCount = validatorCount_; + + if (allOf_.schemas) + CreateSchemaValidators(context, allOf_); + + if (anyOf_.schemas) + CreateSchemaValidators(context, anyOf_); + + if (oneOf_.schemas) + CreateSchemaValidators(context, oneOf_); + + if (not_) + context.validators[notValidatorIndex_] = context.factory.CreateSchemaValidator(*not_); + + if (hasSchemaDependencies_) { + for (SizeType i = 0; i < propertyCount_; i++) + if (properties_[i].dependenciesSchema) + context.validators[properties_[i].dependenciesValidatorIndex] = context.factory.CreateSchemaValidator(*properties_[i].dependenciesSchema); + } + } + + return true; + } + + void CreateSchemaValidators(Context& context, const SchemaArray& schemas) const { + for (SizeType i = 0; i < schemas.count; i++) + context.validators[schemas.begin + i] = context.factory.CreateSchemaValidator(*schemas.schemas[i]); + } + + // O(n) + bool FindPropertyIndex(const ValueType& name, SizeType* outIndex) const { + SizeType len = name.GetStringLength(); + const Ch* str = name.GetString(); + for (SizeType index = 0; index < propertyCount_; index++) + if (properties_[index].name.GetStringLength() == len && + (std::memcmp(properties_[index].name.GetString(), str, sizeof(Ch) * len) == 0)) + { + *outIndex = index; + return true; + } + return false; + } + + bool CheckInt(Context& context, int64_t i) const { + if (!(type_ & ((1 << kIntegerSchemaType) | (1 << kNumberSchemaType)))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (!minimum_.IsNull()) { + if (minimum_.IsInt64()) { + if (exclusiveMinimum_ ? i <= minimum_.GetInt64() : i < minimum_.GetInt64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); + } + else if (minimum_.IsUint64()) { + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); // i <= max(int64_t) < minimum.GetUint64() + } + else if (!CheckDoubleMinimum(context, static_cast(i))) + return false; + } + + if (!maximum_.IsNull()) { + if (maximum_.IsInt64()) { + if (exclusiveMaximum_ ? i >= maximum_.GetInt64() : i > maximum_.GetInt64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); + } + else if (maximum_.IsUint64()) + /* do nothing */; // i <= max(int64_t) < maximum_.GetUint64() + else if (!CheckDoubleMaximum(context, static_cast(i))) + return false; + } + + if (!multipleOf_.IsNull()) { + if (multipleOf_.IsUint64()) { + if (static_cast(i >= 0 ? i : -i) % multipleOf_.GetUint64() != 0) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMultipleOfString()); + } + else if (!CheckDoubleMultipleOf(context, static_cast(i))) + return false; + } + + return true; + } + + bool CheckUint(Context& context, uint64_t i) const { + if (!(type_ & ((1 << kIntegerSchemaType) | (1 << kNumberSchemaType)))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (!minimum_.IsNull()) { + if (minimum_.IsUint64()) { + if (exclusiveMinimum_ ? i <= minimum_.GetUint64() : i < minimum_.GetUint64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); + } + else if (minimum_.IsInt64()) + /* do nothing */; // i >= 0 > minimum.Getint64() + else if (!CheckDoubleMinimum(context, static_cast(i))) + return false; + } + + if (!maximum_.IsNull()) { + if (maximum_.IsUint64()) { + if (exclusiveMaximum_ ? i >= maximum_.GetUint64() : i > maximum_.GetUint64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); + } + else if (maximum_.IsInt64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); // i >= 0 > maximum_ + else if (!CheckDoubleMaximum(context, static_cast(i))) + return false; + } + + if (!multipleOf_.IsNull()) { + if (multipleOf_.IsUint64()) { + if (i % multipleOf_.GetUint64() != 0) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMultipleOfString()); + } + else if (!CheckDoubleMultipleOf(context, static_cast(i))) + return false; + } + + return true; + } + + bool CheckDoubleMinimum(Context& context, double d) const { + if (exclusiveMinimum_ ? d <= minimum_.GetDouble() : d < minimum_.GetDouble()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); + return true; + } + + bool CheckDoubleMaximum(Context& context, double d) const { + if (exclusiveMaximum_ ? d >= maximum_.GetDouble() : d > maximum_.GetDouble()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); + return true; + } + + bool CheckDoubleMultipleOf(Context& context, double d) const { + double a = std::abs(d), b = std::abs(multipleOf_.GetDouble()); + double q = std::floor(a / b); + double r = a - q * b; + if (r > 0.0) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMultipleOfString()); + return true; + } + + struct Property { + Property() : schema(), dependenciesSchema(), dependenciesValidatorIndex(), dependencies(), required(false) {} + ~Property() { AllocatorType::Free(dependencies); } + SValue name; + const SchemaType* schema; + const SchemaType* dependenciesSchema; + SizeType dependenciesValidatorIndex; + bool* dependencies; + bool required; + }; + + struct PatternProperty { + PatternProperty() : schema(), pattern() {} + ~PatternProperty() { + if (pattern) { + pattern->~RegexType(); + AllocatorType::Free(pattern); + } + } + const SchemaType* schema; + RegexType* pattern; + }; + + AllocatorType* allocator_; + uint64_t* enum_; + SizeType enumCount_; + SchemaArray allOf_; + SchemaArray anyOf_; + SchemaArray oneOf_; + const SchemaType* not_; + unsigned type_; // bitmask of kSchemaType + SizeType validatorCount_; + SizeType notValidatorIndex_; + + Property* properties_; + const SchemaType* additionalPropertiesSchema_; + PatternProperty* patternProperties_; + SizeType patternPropertyCount_; + SizeType propertyCount_; + SizeType minProperties_; + SizeType maxProperties_; + bool additionalProperties_; + bool hasDependencies_; + bool hasRequired_; + bool hasSchemaDependencies_; + + const SchemaType* additionalItemsSchema_; + const SchemaType* itemsList_; + const SchemaType** itemsTuple_; + SizeType itemsTupleCount_; + SizeType minItems_; + SizeType maxItems_; + bool additionalItems_; + bool uniqueItems_; + + RegexType* pattern_; + SizeType minLength_; + SizeType maxLength_; + + SValue minimum_; + SValue maximum_; + SValue multipleOf_; + bool exclusiveMinimum_; + bool exclusiveMaximum_; +}; + +template +struct TokenHelper { + RAPIDJSON_FORCEINLINE static void AppendIndexToken(Stack& documentStack, SizeType index) { + *documentStack.template Push() = '/'; + char buffer[21]; + size_t length = static_cast((sizeof(SizeType) == 4 ? u32toa(index, buffer) : u64toa(index, buffer)) - buffer); + for (size_t i = 0; i < length; i++) + *documentStack.template Push() = buffer[i]; + } +}; + +// Partial specialized version for char to prevent buffer copying. +template +struct TokenHelper { + RAPIDJSON_FORCEINLINE static void AppendIndexToken(Stack& documentStack, SizeType index) { + if (sizeof(SizeType) == 4) { + char *buffer = documentStack.template Push(1 + 10); // '/' + uint + *buffer++ = '/'; + const char* end = internal::u32toa(index, buffer); + documentStack.template Pop(static_cast(10 - (end - buffer))); + } + else { + char *buffer = documentStack.template Push(1 + 20); // '/' + uint64 + *buffer++ = '/'; + const char* end = internal::u64toa(index, buffer); + documentStack.template Pop(static_cast(20 - (end - buffer))); + } + } +}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// IGenericRemoteSchemaDocumentProvider + +template +class IGenericRemoteSchemaDocumentProvider { +public: + typedef typename SchemaDocumentType::Ch Ch; + + virtual ~IGenericRemoteSchemaDocumentProvider() {} + virtual const SchemaDocumentType* GetRemoteDocument(const Ch* uri, SizeType length) = 0; +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericSchemaDocument + +//! JSON schema document. +/*! + A JSON schema document is a compiled version of a JSON schema. + It is basically a tree of internal::Schema. + + \note This is an immutable class (i.e. its instance cannot be modified after construction). + \tparam ValueT Type of JSON value (e.g. \c Value ), which also determine the encoding. + \tparam Allocator Allocator type for allocating memory of this document. +*/ +template +class GenericSchemaDocument { +public: + typedef ValueT ValueType; + typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProviderType; + typedef Allocator AllocatorType; + typedef typename ValueType::EncodingType EncodingType; + typedef typename EncodingType::Ch Ch; + typedef internal::Schema SchemaType; + typedef GenericPointer PointerType; + friend class internal::Schema; + template + friend class GenericSchemaValidator; + + //! Constructor. + /*! + Compile a JSON document into schema document. + + \param document A JSON document as source. + \param remoteProvider An optional remote schema document provider for resolving remote reference. Can be null. + \param allocator An optional allocator instance for allocating memory. Can be null. + */ + explicit GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) : + remoteProvider_(remoteProvider), + allocator_(allocator), + ownAllocator_(), + root_(), + schemaMap_(allocator, kInitialSchemaMapSize), + schemaRef_(allocator, kInitialSchemaRefSize) + { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + + // Generate root schema, it will call CreateSchema() to create sub-schemas, + // And call AddRefSchema() if there are $ref. + CreateSchemaRecursive(&root_, PointerType(), document, document); + + // Resolve $ref + while (!schemaRef_.Empty()) { + SchemaRefEntry* refEntry = schemaRef_.template Pop(1); + if (const SchemaType* s = GetSchema(refEntry->target)) { + if (refEntry->schema) + *refEntry->schema = s; + + // Create entry in map if not exist + if (!GetSchema(refEntry->source)) { + new (schemaMap_.template Push()) SchemaEntry(refEntry->source, const_cast(s), false, allocator_); + } + } + refEntry->~SchemaRefEntry(); + } + + RAPIDJSON_ASSERT(root_ != 0); + + schemaRef_.ShrinkToFit(); // Deallocate all memory for ref + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericSchemaDocument(GenericSchemaDocument&& rhs) RAPIDJSON_NOEXCEPT : + remoteProvider_(rhs.remoteProvider_), + allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + root_(rhs.root_), + schemaMap_(std::move(rhs.schemaMap_)), + schemaRef_(std::move(rhs.schemaRef_)) + { + rhs.remoteProvider_ = 0; + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + } +#endif + + //! Destructor + ~GenericSchemaDocument() { + while (!schemaMap_.Empty()) + schemaMap_.template Pop(1)->~SchemaEntry(); + + RAPIDJSON_DELETE(ownAllocator_); + } + + //! Get the root schema. + const SchemaType& GetRoot() const { return *root_; } + +private: + //! Prohibit copying + GenericSchemaDocument(const GenericSchemaDocument&); + //! Prohibit assignment + GenericSchemaDocument& operator=(const GenericSchemaDocument&); + + struct SchemaRefEntry { + SchemaRefEntry(const PointerType& s, const PointerType& t, const SchemaType** outSchema, Allocator *allocator) : source(s, allocator), target(t, allocator), schema(outSchema) {} + PointerType source; + PointerType target; + const SchemaType** schema; + }; + + struct SchemaEntry { + SchemaEntry(const PointerType& p, SchemaType* s, bool o, Allocator* allocator) : pointer(p, allocator), schema(s), owned(o) {} + ~SchemaEntry() { + if (owned) { + schema->~SchemaType(); + Allocator::Free(schema); + } + } + PointerType pointer; + SchemaType* schema; + bool owned; + }; + + void CreateSchemaRecursive(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document) { + if (schema) + *schema = SchemaType::GetTypeless(); + + if (v.GetType() == kObjectType) { + const SchemaType* s = GetSchema(pointer); + if (!s) + CreateSchema(schema, pointer, v, document); + + for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr) + CreateSchemaRecursive(0, pointer.Append(itr->name, allocator_), itr->value, document); + } + else if (v.GetType() == kArrayType) + for (SizeType i = 0; i < v.Size(); i++) + CreateSchemaRecursive(0, pointer.Append(i, allocator_), v[i], document); + } + + void CreateSchema(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document) { + RAPIDJSON_ASSERT(pointer.IsValid()); + if (v.IsObject()) { + if (!HandleRefSchema(pointer, schema, v, document)) { + SchemaType* s = new (allocator_->Malloc(sizeof(SchemaType))) SchemaType(this, pointer, v, document, allocator_); + new (schemaMap_.template Push()) SchemaEntry(pointer, s, true, allocator_); + if (schema) + *schema = s; + } + } + } + + bool HandleRefSchema(const PointerType& source, const SchemaType** schema, const ValueType& v, const ValueType& document) { + static const Ch kRefString[] = { '$', 'r', 'e', 'f', '\0' }; + static const ValueType kRefValue(kRefString, 4); + + typename ValueType::ConstMemberIterator itr = v.FindMember(kRefValue); + if (itr == v.MemberEnd()) + return false; + + if (itr->value.IsString()) { + SizeType len = itr->value.GetStringLength(); + if (len > 0) { + const Ch* s = itr->value.GetString(); + SizeType i = 0; + while (i < len && s[i] != '#') // Find the first # + i++; + + if (i > 0) { // Remote reference, resolve immediately + if (remoteProvider_) { + if (const GenericSchemaDocument* remoteDocument = remoteProvider_->GetRemoteDocument(s, i - 1)) { + PointerType pointer(&s[i], len - i, allocator_); + if (pointer.IsValid()) { + if (const SchemaType* sc = remoteDocument->GetSchema(pointer)) { + if (schema) + *schema = sc; + return true; + } + } + } + } + } + else if (s[i] == '#') { // Local reference, defer resolution + PointerType pointer(&s[i], len - i, allocator_); + if (pointer.IsValid()) { + if (const ValueType* nv = pointer.Get(document)) + if (HandleRefSchema(source, schema, *nv, document)) + return true; + + new (schemaRef_.template Push()) SchemaRefEntry(source, pointer, schema, allocator_); + return true; + } + } + } + } + return false; + } + + const SchemaType* GetSchema(const PointerType& pointer) const { + for (const SchemaEntry* target = schemaMap_.template Bottom(); target != schemaMap_.template End(); ++target) + if (pointer == target->pointer) + return target->schema; + return 0; + } + + PointerType GetPointer(const SchemaType* schema) const { + for (const SchemaEntry* target = schemaMap_.template Bottom(); target != schemaMap_.template End(); ++target) + if (schema == target->schema) + return target->pointer; + return PointerType(); + } + + static const size_t kInitialSchemaMapSize = 64; + static const size_t kInitialSchemaRefSize = 64; + + IRemoteSchemaDocumentProviderType* remoteProvider_; + Allocator *allocator_; + Allocator *ownAllocator_; + const SchemaType* root_; //!< Root schema. + internal::Stack schemaMap_; // Stores created Pointer -> Schemas + internal::Stack schemaRef_; // Stores Pointer from $ref and schema which holds the $ref +}; + +//! GenericSchemaDocument using Value type. +typedef GenericSchemaDocument SchemaDocument; +//! IGenericRemoteSchemaDocumentProvider using SchemaDocument. +typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProvider; + +/////////////////////////////////////////////////////////////////////////////// +// GenericSchemaValidator + +//! JSON Schema Validator. +/*! + A SAX style JSON schema validator. + It uses a \c GenericSchemaDocument to validate SAX events. + It delegates the incoming SAX events to an output handler. + The default output handler does nothing. + It can be reused multiple times by calling \c Reset(). + + \tparam SchemaDocumentType Type of schema document. + \tparam OutputHandler Type of output handler. Default handler does nothing. + \tparam StateAllocator Allocator for storing the internal validation states. +*/ +template < + typename SchemaDocumentType, + typename OutputHandler = BaseReaderHandler, + typename StateAllocator = CrtAllocator> +class GenericSchemaValidator : + public internal::ISchemaStateFactory, + public internal::ISchemaValidator +{ +public: + typedef typename SchemaDocumentType::SchemaType SchemaType; + typedef typename SchemaDocumentType::PointerType PointerType; + typedef typename SchemaType::EncodingType EncodingType; + typedef typename EncodingType::Ch Ch; + + //! Constructor without output handler. + /*! + \param schemaDocument The schema document to conform to. + \param allocator Optional allocator for storing internal validation states. + \param schemaStackCapacity Optional initial capacity of schema path stack. + \param documentStackCapacity Optional initial capacity of document path stack. + */ + GenericSchemaValidator( + const SchemaDocumentType& schemaDocument, + StateAllocator* allocator = 0, + size_t schemaStackCapacity = kDefaultSchemaStackCapacity, + size_t documentStackCapacity = kDefaultDocumentStackCapacity) + : + schemaDocument_(&schemaDocument), + root_(schemaDocument.GetRoot()), + outputHandler_(GetNullHandler()), + stateAllocator_(allocator), + ownStateAllocator_(0), + schemaStack_(allocator, schemaStackCapacity), + documentStack_(allocator, documentStackCapacity), + valid_(true) +#if RAPIDJSON_SCHEMA_VERBOSE + , depth_(0) +#endif + { + } + + //! Constructor with output handler. + /*! + \param schemaDocument The schema document to conform to. + \param allocator Optional allocator for storing internal validation states. + \param schemaStackCapacity Optional initial capacity of schema path stack. + \param documentStackCapacity Optional initial capacity of document path stack. + */ + GenericSchemaValidator( + const SchemaDocumentType& schemaDocument, + OutputHandler& outputHandler, + StateAllocator* allocator = 0, + size_t schemaStackCapacity = kDefaultSchemaStackCapacity, + size_t documentStackCapacity = kDefaultDocumentStackCapacity) + : + schemaDocument_(&schemaDocument), + root_(schemaDocument.GetRoot()), + outputHandler_(outputHandler), + stateAllocator_(allocator), + ownStateAllocator_(0), + schemaStack_(allocator, schemaStackCapacity), + documentStack_(allocator, documentStackCapacity), + valid_(true) +#if RAPIDJSON_SCHEMA_VERBOSE + , depth_(0) +#endif + { + } + + //! Destructor. + ~GenericSchemaValidator() { + Reset(); + RAPIDJSON_DELETE(ownStateAllocator_); + } + + //! Reset the internal states. + void Reset() { + while (!schemaStack_.Empty()) + PopSchema(); + documentStack_.Clear(); + valid_ = true; + } + + //! Checks whether the current state is valid. + // Implementation of ISchemaValidator + virtual bool IsValid() const { return valid_; } + + //! Gets the JSON pointer pointed to the invalid schema. + PointerType GetInvalidSchemaPointer() const { + return schemaStack_.Empty() ? PointerType() : schemaDocument_->GetPointer(&CurrentSchema()); + } + + //! Gets the keyword of invalid schema. + const Ch* GetInvalidSchemaKeyword() const { + return schemaStack_.Empty() ? 0 : CurrentContext().invalidKeyword; + } + + //! Gets the JSON pointer pointed to the invalid value. + PointerType GetInvalidDocumentPointer() const { + return documentStack_.Empty() ? PointerType() : PointerType(documentStack_.template Bottom(), documentStack_.GetSize() / sizeof(Ch)); + } + +#if RAPIDJSON_SCHEMA_VERBOSE +#define RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_() \ +RAPIDJSON_MULTILINEMACRO_BEGIN\ + *documentStack_.template Push() = '\0';\ + documentStack_.template Pop(1);\ + internal::PrintInvalidDocument(documentStack_.template Bottom());\ +RAPIDJSON_MULTILINEMACRO_END +#else +#define RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_() +#endif + +#define RAPIDJSON_SCHEMA_HANDLE_BEGIN_(method, arg1)\ + if (!valid_) return false; \ + if (!BeginValue() || !CurrentSchema().method arg1) {\ + RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_();\ + return valid_ = false;\ + } + +#define RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(method, arg2)\ + for (Context* context = schemaStack_.template Bottom(); context != schemaStack_.template End(); context++) {\ + if (context->hasher)\ + static_cast(context->hasher)->method arg2;\ + if (context->validators)\ + for (SizeType i_ = 0; i_ < context->validatorCount; i_++)\ + static_cast(context->validators[i_])->method arg2;\ + if (context->patternPropertiesValidators)\ + for (SizeType i_ = 0; i_ < context->patternPropertiesValidatorCount; i_++)\ + static_cast(context->patternPropertiesValidators[i_])->method arg2;\ + } + +#define RAPIDJSON_SCHEMA_HANDLE_END_(method, arg2)\ + return valid_ = EndValue() && outputHandler_.method arg2 + +#define RAPIDJSON_SCHEMA_HANDLE_VALUE_(method, arg1, arg2) \ + RAPIDJSON_SCHEMA_HANDLE_BEGIN_ (method, arg1);\ + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(method, arg2);\ + RAPIDJSON_SCHEMA_HANDLE_END_ (method, arg2) + + bool Null() { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Null, (CurrentContext() ), ( )); } + bool Bool(bool b) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Bool, (CurrentContext(), b), (b)); } + bool Int(int i) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Int, (CurrentContext(), i), (i)); } + bool Uint(unsigned u) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Uint, (CurrentContext(), u), (u)); } + bool Int64(int64_t i) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Int64, (CurrentContext(), i), (i)); } + bool Uint64(uint64_t u) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Uint64, (CurrentContext(), u), (u)); } + bool Double(double d) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Double, (CurrentContext(), d), (d)); } + bool RawNumber(const Ch* str, SizeType length, bool copy) + { RAPIDJSON_SCHEMA_HANDLE_VALUE_(String, (CurrentContext(), str, length, copy), (str, length, copy)); } + bool String(const Ch* str, SizeType length, bool copy) + { RAPIDJSON_SCHEMA_HANDLE_VALUE_(String, (CurrentContext(), str, length, copy), (str, length, copy)); } + + bool StartObject() { + RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartObject, (CurrentContext())); + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartObject, ()); + return valid_ = outputHandler_.StartObject(); + } + + bool Key(const Ch* str, SizeType len, bool copy) { + if (!valid_) return false; + AppendToken(str, len); + if (!CurrentSchema().Key(CurrentContext(), str, len, copy)) return valid_ = false; + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(Key, (str, len, copy)); + return valid_ = outputHandler_.Key(str, len, copy); + } + + bool EndObject(SizeType memberCount) { + if (!valid_) return false; + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(EndObject, (memberCount)); + if (!CurrentSchema().EndObject(CurrentContext(), memberCount)) return valid_ = false; + RAPIDJSON_SCHEMA_HANDLE_END_(EndObject, (memberCount)); + } + + bool StartArray() { + RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartArray, (CurrentContext())); + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartArray, ()); + return valid_ = outputHandler_.StartArray(); + } + + bool EndArray(SizeType elementCount) { + if (!valid_) return false; + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(EndArray, (elementCount)); + if (!CurrentSchema().EndArray(CurrentContext(), elementCount)) return valid_ = false; + RAPIDJSON_SCHEMA_HANDLE_END_(EndArray, (elementCount)); + } + +#undef RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_ +#undef RAPIDJSON_SCHEMA_HANDLE_BEGIN_ +#undef RAPIDJSON_SCHEMA_HANDLE_PARALLEL_ +#undef RAPIDJSON_SCHEMA_HANDLE_VALUE_ + + // Implementation of ISchemaStateFactory + virtual ISchemaValidator* CreateSchemaValidator(const SchemaType& root) { + return new (GetStateAllocator().Malloc(sizeof(GenericSchemaValidator))) GenericSchemaValidator(*schemaDocument_, root, +#if RAPIDJSON_SCHEMA_VERBOSE + depth_ + 1, +#endif + &GetStateAllocator()); + } + + virtual void DestroySchemaValidator(ISchemaValidator* validator) { + GenericSchemaValidator* v = static_cast(validator); + v->~GenericSchemaValidator(); + StateAllocator::Free(v); + } + + virtual void* CreateHasher() { + return new (GetStateAllocator().Malloc(sizeof(HasherType))) HasherType(&GetStateAllocator()); + } + + virtual uint64_t GetHashCode(void* hasher) { + return static_cast(hasher)->GetHashCode(); + } + + virtual void DestroryHasher(void* hasher) { + HasherType* h = static_cast(hasher); + h->~HasherType(); + StateAllocator::Free(h); + } + + virtual void* MallocState(size_t size) { + return GetStateAllocator().Malloc(size); + } + + virtual void FreeState(void* p) { + return StateAllocator::Free(p); + } + +private: + typedef typename SchemaType::Context Context; + typedef GenericValue, StateAllocator> HashCodeArray; + typedef internal::Hasher HasherType; + + GenericSchemaValidator( + const SchemaDocumentType& schemaDocument, + const SchemaType& root, +#if RAPIDJSON_SCHEMA_VERBOSE + unsigned depth, +#endif + StateAllocator* allocator = 0, + size_t schemaStackCapacity = kDefaultSchemaStackCapacity, + size_t documentStackCapacity = kDefaultDocumentStackCapacity) + : + schemaDocument_(&schemaDocument), + root_(root), + outputHandler_(GetNullHandler()), + stateAllocator_(allocator), + ownStateAllocator_(0), + schemaStack_(allocator, schemaStackCapacity), + documentStack_(allocator, documentStackCapacity), + valid_(true) +#if RAPIDJSON_SCHEMA_VERBOSE + , depth_(depth) +#endif + { + } + + StateAllocator& GetStateAllocator() { + if (!stateAllocator_) + stateAllocator_ = ownStateAllocator_ = RAPIDJSON_NEW(StateAllocator()); + return *stateAllocator_; + } + + bool BeginValue() { + if (schemaStack_.Empty()) + PushSchema(root_); + else { + if (CurrentContext().inArray) + internal::TokenHelper, Ch>::AppendIndexToken(documentStack_, CurrentContext().arrayElementIndex); + + if (!CurrentSchema().BeginValue(CurrentContext())) + return false; + + SizeType count = CurrentContext().patternPropertiesSchemaCount; + const SchemaType** sa = CurrentContext().patternPropertiesSchemas; + typename Context::PatternValidatorType patternValidatorType = CurrentContext().valuePatternValidatorType; + bool valueUniqueness = CurrentContext().valueUniqueness; + if (CurrentContext().valueSchema) + PushSchema(*CurrentContext().valueSchema); + + if (count > 0) { + CurrentContext().objectPatternValidatorType = patternValidatorType; + ISchemaValidator**& va = CurrentContext().patternPropertiesValidators; + SizeType& validatorCount = CurrentContext().patternPropertiesValidatorCount; + va = static_cast(MallocState(sizeof(ISchemaValidator*) * count)); + for (SizeType i = 0; i < count; i++) + va[validatorCount++] = CreateSchemaValidator(*sa[i]); + } + + CurrentContext().arrayUniqueness = valueUniqueness; + } + return true; + } + + bool EndValue() { + if (!CurrentSchema().EndValue(CurrentContext())) + return false; + +#if RAPIDJSON_SCHEMA_VERBOSE + GenericStringBuffer sb; + schemaDocument_->GetPointer(&CurrentSchema()).Stringify(sb); + + *documentStack_.template Push() = '\0'; + documentStack_.template Pop(1); + internal::PrintValidatorPointers(depth_, sb.GetString(), documentStack_.template Bottom()); +#endif + + uint64_t h = CurrentContext().arrayUniqueness ? static_cast(CurrentContext().hasher)->GetHashCode() : 0; + + PopSchema(); + + if (!schemaStack_.Empty()) { + Context& context = CurrentContext(); + if (context.valueUniqueness) { + HashCodeArray* a = static_cast(context.arrayElementHashCodes); + if (!a) + CurrentContext().arrayElementHashCodes = a = new (GetStateAllocator().Malloc(sizeof(HashCodeArray))) HashCodeArray(kArrayType); + for (typename HashCodeArray::ConstValueIterator itr = a->Begin(); itr != a->End(); ++itr) + if (itr->GetUint64() == h) + RAPIDJSON_INVALID_KEYWORD_RETURN(SchemaType::GetUniqueItemsString()); + a->PushBack(h, GetStateAllocator()); + } + } + + // Remove the last token of document pointer + while (!documentStack_.Empty() && *documentStack_.template Pop(1) != '/') + ; + + return true; + } + + void AppendToken(const Ch* str, SizeType len) { + documentStack_.template Reserve(1 + len * 2); // worst case all characters are escaped as two characters + *documentStack_.template PushUnsafe() = '/'; + for (SizeType i = 0; i < len; i++) { + if (str[i] == '~') { + *documentStack_.template PushUnsafe() = '~'; + *documentStack_.template PushUnsafe() = '0'; + } + else if (str[i] == '/') { + *documentStack_.template PushUnsafe() = '~'; + *documentStack_.template PushUnsafe() = '1'; + } + else + *documentStack_.template PushUnsafe() = str[i]; + } + } + + RAPIDJSON_FORCEINLINE void PushSchema(const SchemaType& schema) { new (schemaStack_.template Push()) Context(*this, &schema); } + + RAPIDJSON_FORCEINLINE void PopSchema() { + Context* c = schemaStack_.template Pop(1); + if (HashCodeArray* a = static_cast(c->arrayElementHashCodes)) { + a->~HashCodeArray(); + StateAllocator::Free(a); + } + c->~Context(); + } + + const SchemaType& CurrentSchema() const { return *schemaStack_.template Top()->schema; } + Context& CurrentContext() { return *schemaStack_.template Top(); } + const Context& CurrentContext() const { return *schemaStack_.template Top(); } + + static OutputHandler& GetNullHandler() { + static OutputHandler nullHandler; + return nullHandler; + } + + static const size_t kDefaultSchemaStackCapacity = 1024; + static const size_t kDefaultDocumentStackCapacity = 256; + const SchemaDocumentType* schemaDocument_; + const SchemaType& root_; + OutputHandler& outputHandler_; + StateAllocator* stateAllocator_; + StateAllocator* ownStateAllocator_; + internal::Stack schemaStack_; //!< stack to store the current path of schema (BaseSchemaType *) + internal::Stack documentStack_; //!< stack to store the current path of validating document (Ch) + bool valid_; +#if RAPIDJSON_SCHEMA_VERBOSE + unsigned depth_; +#endif +}; + +typedef GenericSchemaValidator SchemaValidator; + +/////////////////////////////////////////////////////////////////////////////// +// SchemaValidatingReader + +//! A helper class for parsing with validation. +/*! + This helper class is a functor, designed as a parameter of \ref GenericDocument::Populate(). + + \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept. + \tparam SourceEncoding Encoding of the input stream. + \tparam SchemaDocumentType Type of schema document. + \tparam StackAllocator Allocator type for stack. +*/ +template < + unsigned parseFlags, + typename InputStream, + typename SourceEncoding, + typename SchemaDocumentType = SchemaDocument, + typename StackAllocator = CrtAllocator> +class SchemaValidatingReader { +public: + typedef typename SchemaDocumentType::PointerType PointerType; + typedef typename InputStream::Ch Ch; + + //! Constructor + /*! + \param is Input stream. + \param sd Schema document. + */ + SchemaValidatingReader(InputStream& is, const SchemaDocumentType& sd) : is_(is), sd_(sd), invalidSchemaKeyword_(), isValid_(true) {} + + template + bool operator()(Handler& handler) { + GenericReader reader; + GenericSchemaValidator validator(sd_, handler); + parseResult_ = reader.template Parse(is_, validator); + + isValid_ = validator.IsValid(); + if (isValid_) { + invalidSchemaPointer_ = PointerType(); + invalidSchemaKeyword_ = 0; + invalidDocumentPointer_ = PointerType(); + } + else { + invalidSchemaPointer_ = validator.GetInvalidSchemaPointer(); + invalidSchemaKeyword_ = validator.GetInvalidSchemaKeyword(); + invalidDocumentPointer_ = validator.GetInvalidDocumentPointer(); + } + + return parseResult_; + } + + const ParseResult& GetParseResult() const { return parseResult_; } + bool IsValid() const { return isValid_; } + const PointerType& GetInvalidSchemaPointer() const { return invalidSchemaPointer_; } + const Ch* GetInvalidSchemaKeyword() const { return invalidSchemaKeyword_; } + const PointerType& GetInvalidDocumentPointer() const { return invalidDocumentPointer_; } + +private: + InputStream& is_; + const SchemaDocumentType& sd_; + + ParseResult parseResult_; + PointerType invalidSchemaPointer_; + const Ch* invalidSchemaKeyword_; + PointerType invalidDocumentPointer_; + bool isValid_; +}; + +RAPIDJSON_NAMESPACE_END +RAPIDJSON_DIAG_POP + +#endif // RAPIDJSON_SCHEMA_H_ diff --git a/rapidjson-1.1.0/rapid_json/stream.h b/rapidjson-1.1.0/rapid_json/stream.h new file mode 100644 index 000000000..fef82c252 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/stream.h @@ -0,0 +1,179 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#include "rapidjson.h" + +#ifndef RAPIDJSON_STREAM_H_ +#define RAPIDJSON_STREAM_H_ + +#include "encodings.h" + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Stream + +/*! \class rapidjson::Stream + \brief Concept for reading and writing characters. + + For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd(). + + For write-only stream, only need to implement Put() and Flush(). + +\code +concept Stream { + typename Ch; //!< Character type of the stream. + + //! Read the current character from stream without moving the read cursor. + Ch Peek() const; + + //! Read the current character from stream and moving the read cursor to next character. + Ch Take(); + + //! Get the current read cursor. + //! \return Number of characters read from start. + size_t Tell(); + + //! Begin writing operation at the current read pointer. + //! \return The begin writer pointer. + Ch* PutBegin(); + + //! Write a character. + void Put(Ch c); + + //! Flush the buffer. + void Flush(); + + //! End the writing operation. + //! \param begin The begin write pointer returned by PutBegin(). + //! \return Number of characters written. + size_t PutEnd(Ch* begin); +} +\endcode +*/ + +//! Provides additional information for stream. +/*! + By using traits pattern, this type provides a default configuration for stream. + For custom stream, this type can be specialized for other configuration. + See TEST(Reader, CustomStringStream) in readertest.cpp for example. +*/ +template +struct StreamTraits { + //! Whether to make local copy of stream for optimization during parsing. + /*! + By default, for safety, streams do not use local copy optimization. + Stream that can be copied fast should specialize this, like StreamTraits. + */ + enum { copyOptimization = 0 }; +}; + +//! Reserve n characters for writing to a stream. +template +inline void PutReserve(Stream& stream, size_t count) { + (void)stream; + (void)count; +} + +//! Write character to a stream, presuming buffer is reserved. +template +inline void PutUnsafe(Stream& stream, typename Stream::Ch c) { + stream.Put(c); +} + +//! Put N copies of a character to a stream. +template +inline void PutN(Stream& stream, Ch c, size_t n) { + PutReserve(stream, n); + for (size_t i = 0; i < n; i++) + PutUnsafe(stream, c); +} + +/////////////////////////////////////////////////////////////////////////////// +// StringStream + +//! Read-only string stream. +/*! \note implements Stream concept +*/ +template +struct GenericStringStream { + typedef typename Encoding::Ch Ch; + + GenericStringStream(const Ch *src) : src_(src), head_(src) {} + + Ch Peek() const { return *src_; } + Ch Take() { return *src_++; } + size_t Tell() const { return static_cast(src_ - head_); } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + const Ch* src_; //!< Current read position. + const Ch* head_; //!< Original head of the string. +}; + +template +struct StreamTraits > { + enum { copyOptimization = 1 }; +}; + +//! String stream with UTF8 encoding. +typedef GenericStringStream > StringStream; + +/////////////////////////////////////////////////////////////////////////////// +// InsituStringStream + +//! A read-write string stream. +/*! This string stream is particularly designed for in-situ parsing. + \note implements Stream concept +*/ +template +struct GenericInsituStringStream { + typedef typename Encoding::Ch Ch; + + GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {} + + // Read + Ch Peek() { return *src_; } + Ch Take() { return *src_++; } + size_t Tell() { return static_cast(src_ - head_); } + + // Write + void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; } + + Ch* PutBegin() { return dst_ = src_; } + size_t PutEnd(Ch* begin) { return static_cast(dst_ - begin); } + void Flush() {} + + Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; } + void Pop(size_t count) { dst_ -= count; } + + Ch* src_; + Ch* dst_; + Ch* head_; +}; + +template +struct StreamTraits > { + enum { copyOptimization = 1 }; +}; + +//! Insitu string stream with UTF8 encoding. +typedef GenericInsituStringStream > InsituStringStream; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_STREAM_H_ diff --git a/rapidjson-1.1.0/rapid_json/stringbuffer.h b/rapidjson-1.1.0/rapid_json/stringbuffer.h new file mode 100644 index 000000000..78f34d209 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/stringbuffer.h @@ -0,0 +1,117 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_STRINGBUFFER_H_ +#define RAPIDJSON_STRINGBUFFER_H_ + +#include "stream.h" +#include "internal/stack.h" + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +#include // std::move +#endif + +#include "internal/stack.h" + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory output stream. +/*! + \tparam Encoding Encoding of the stream. + \tparam Allocator type for allocating memory buffer. + \note implements Stream concept +*/ +template +class GenericStringBuffer { +public: + typedef typename Encoding::Ch Ch; + + GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {} + GenericStringBuffer& operator=(GenericStringBuffer&& rhs) { + if (&rhs != this) + stack_ = std::move(rhs.stack_); + return *this; + } +#endif + + void Put(Ch c) { *stack_.template Push() = c; } + void PutUnsafe(Ch c) { *stack_.template PushUnsafe() = c; } + void Flush() {} + + void Clear() { stack_.Clear(); } + void ShrinkToFit() { + // Push and pop a null terminator. This is safe. + *stack_.template Push() = '\0'; + stack_.ShrinkToFit(); + stack_.template Pop(1); + } + + void Reserve(size_t count) { stack_.template Reserve(count); } + Ch* Push(size_t count) { return stack_.template Push(count); } + Ch* PushUnsafe(size_t count) { return stack_.template PushUnsafe(count); } + void Pop(size_t count) { stack_.template Pop(count); } + + const Ch* GetString() const { + // Push and pop a null terminator. This is safe. + *stack_.template Push() = '\0'; + stack_.template Pop(1); + + return stack_.template Bottom(); + } + + size_t GetSize() const { return stack_.GetSize(); } + + static const size_t kDefaultCapacity = 256; + mutable internal::Stack stack_; + +private: + // Prohibit copy constructor & assignment operator. + GenericStringBuffer(const GenericStringBuffer&); + GenericStringBuffer& operator=(const GenericStringBuffer&); +}; + +//! String buffer with UTF8 encoding +typedef GenericStringBuffer > StringBuffer; + +template +inline void PutReserve(GenericStringBuffer& stream, size_t count) { + stream.Reserve(count); +} + +template +inline void PutUnsafe(GenericStringBuffer& stream, typename Encoding::Ch c) { + stream.PutUnsafe(c); +} + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(GenericStringBuffer >& stream, char c, size_t n) { + std::memset(stream.stack_.Push(n), c, n * sizeof(c)); +} + +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_STRINGBUFFER_H_ diff --git a/rapidjson-1.1.0/rapid_json/writer.h b/rapidjson-1.1.0/rapid_json/writer.h new file mode 100644 index 000000000..94f22dd5f --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/writer.h @@ -0,0 +1,610 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_WRITER_H_ +#define RAPIDJSON_WRITER_H_ + +#include "stream.h" +#include "internal/stack.h" +#include "internal/strfunc.h" +#include "internal/dtoa.h" +#include "internal/itoa.h" +#include "stringbuffer.h" +#include // placement new + +#if defined(RAPIDJSON_SIMD) && defined(_MSC_VER) +#include +#pragma intrinsic(_BitScanForward) +#endif +#ifdef RAPIDJSON_SSE42 +#include +#elif defined(RAPIDJSON_SSE2) +#include +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(unreachable-code) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// WriteFlag + +/*! \def RAPIDJSON_WRITE_DEFAULT_FLAGS + \ingroup RAPIDJSON_CONFIG + \brief User-defined kWriteDefaultFlags definition. + + User can define this as any \c WriteFlag combinations. +*/ +#ifndef RAPIDJSON_WRITE_DEFAULT_FLAGS +#define RAPIDJSON_WRITE_DEFAULT_FLAGS kWriteNoFlags +#endif + +//! Combination of writeFlags +enum WriteFlag { + kWriteNoFlags = 0, //!< No flags are set. + kWriteValidateEncodingFlag = 1, //!< Validate encoding of JSON strings. + kWriteNanAndInfFlag = 2, //!< Allow writing of Infinity, -Infinity and NaN. + kWriteDefaultFlags = RAPIDJSON_WRITE_DEFAULT_FLAGS //!< Default write flags. Can be customized by defining RAPIDJSON_WRITE_DEFAULT_FLAGS +}; + +//! JSON writer +/*! Writer implements the concept Handler. + It generates JSON text by events to an output os. + + User may programmatically calls the functions of a writer to generate JSON text. + + On the other side, a writer can also be passed to objects that generates events, + + for example Reader::Parse() and Document::Accept(). + + \tparam OutputStream Type of output stream. + \tparam SourceEncoding Encoding of source string. + \tparam TargetEncoding Encoding of output stream. + \tparam StackAllocator Type of allocator for allocating memory of stack. + \note implements Handler concept +*/ +template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags> +class Writer { +public: + typedef typename SourceEncoding::Ch Ch; + + static const int kDefaultMaxDecimalPlaces = 324; + + //! Constructor + /*! \param os Output stream. + \param stackAllocator User supplied allocator. If it is null, it will create a private one. + \param levelDepth Initial capacity of stack. + */ + explicit + Writer(OutputStream& os, StackAllocator* stackAllocator = 0, size_t levelDepth = kDefaultLevelDepth) : + os_(&os), level_stack_(stackAllocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {} + + explicit + Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : + os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {} + + //! Reset the writer with a new stream. + /*! + This function reset the writer with a new stream and default settings, + in order to make a Writer object reusable for output multiple JSONs. + + \param os New output stream. + \code + Writer writer(os1); + writer.StartObject(); + // ... + writer.EndObject(); + + writer.Reset(os2); + writer.StartObject(); + // ... + writer.EndObject(); + \endcode + */ + void Reset(OutputStream& os) { + os_ = &os; + hasRoot_ = false; + level_stack_.Clear(); + } + + //! Checks whether the output is a complete JSON. + /*! + A complete JSON has a complete root object or array. + */ + bool IsComplete() const { + return hasRoot_ && level_stack_.Empty(); + } + + int GetMaxDecimalPlaces() const { + return maxDecimalPlaces_; + } + + //! Sets the maximum number of decimal places for double output. + /*! + This setting truncates the output with specified number of decimal places. + + For example, + + \code + writer.SetMaxDecimalPlaces(3); + writer.StartArray(); + writer.Double(0.12345); // "0.123" + writer.Double(0.0001); // "0.0" + writer.Double(1.234567890123456e30); // "1.234567890123456e30" (do not truncate significand for positive exponent) + writer.Double(1.23e-4); // "0.0" (do truncate significand for negative exponent) + writer.EndArray(); + \endcode + + The default setting does not truncate any decimal places. You can restore to this setting by calling + \code + writer.SetMaxDecimalPlaces(Writer::kDefaultMaxDecimalPlaces); + \endcode + */ + void SetMaxDecimalPlaces(int maxDecimalPlaces) { + maxDecimalPlaces_ = maxDecimalPlaces; + } + + /*!@name Implementation of Handler + \see Handler + */ + //@{ + + bool Null() { Prefix(kNullType); return EndValue(WriteNull()); } + bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return EndValue(WriteBool(b)); } + bool Int(int i) { Prefix(kNumberType); return EndValue(WriteInt(i)); } + bool Uint(unsigned u) { Prefix(kNumberType); return EndValue(WriteUint(u)); } + bool Int64(int64_t i64) { Prefix(kNumberType); return EndValue(WriteInt64(i64)); } + bool Uint64(uint64_t u64) { Prefix(kNumberType); return EndValue(WriteUint64(u64)); } + + //! Writes the given \c double value to the stream + /*! + \param d The value to be written. + \return Whether it is succeed. + */ + bool Double(double d) { Prefix(kNumberType); return EndValue(WriteDouble(d)); } + + bool RawNumber(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + Prefix(kNumberType); + return EndValue(WriteString(str, length)); + } + + bool String(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + Prefix(kStringType); + return EndValue(WriteString(str, length)); + } + +#if RAPIDJSON_HAS_STDSTRING + bool String(const std::basic_string& str) { + return String(str.data(), SizeType(str.size())); + } +#endif + + bool StartObject() { + Prefix(kObjectType); + new (level_stack_.template Push()) Level(false); + return WriteStartObject(); + } + + bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + + bool EndObject(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); + RAPIDJSON_ASSERT(!level_stack_.template Top()->inArray); + level_stack_.template Pop(1); + return EndValue(WriteEndObject()); + } + + bool StartArray() { + Prefix(kArrayType); + new (level_stack_.template Push()) Level(true); + return WriteStartArray(); + } + + bool EndArray(SizeType elementCount = 0) { + (void)elementCount; + RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); + RAPIDJSON_ASSERT(level_stack_.template Top()->inArray); + level_stack_.template Pop(1); + return EndValue(WriteEndArray()); + } + //@} + + /*! @name Convenience extensions */ + //@{ + + //! Simpler but slower overload. + bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } + + //@} + + //! Write a raw JSON value. + /*! + For user to write a stringified JSON as a value. + + \param json A well-formed JSON value. It should not contain null character within [0, length - 1] range. + \param length Length of the json. + \param type Type of the root of json. + */ + bool RawValue(const Ch* json, size_t length, Type type) { Prefix(type); return EndValue(WriteRawValue(json, length)); } + +protected: + //! Information for each nested level + struct Level { + Level(bool inArray_) : valueCount(0), inArray(inArray_) {} + size_t valueCount; //!< number of values in this level + bool inArray; //!< true if in array, otherwise in object + }; + + static const size_t kDefaultLevelDepth = 32; + + bool WriteNull() { + PutReserve(*os_, 4); + PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'u'); PutUnsafe(*os_, 'l'); PutUnsafe(*os_, 'l'); return true; + } + + bool WriteBool(bool b) { + if (b) { + PutReserve(*os_, 4); + PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'r'); PutUnsafe(*os_, 'u'); PutUnsafe(*os_, 'e'); + } + else { + PutReserve(*os_, 5); + PutUnsafe(*os_, 'f'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'l'); PutUnsafe(*os_, 's'); PutUnsafe(*os_, 'e'); + } + return true; + } + + bool WriteInt(int i) { + char buffer[11]; + const char* end = internal::i32toa(i, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (const char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteUint(unsigned u) { + char buffer[10]; + const char* end = internal::u32toa(u, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (const char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteInt64(int64_t i64) { + char buffer[21]; + const char* end = internal::i64toa(i64, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (const char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteUint64(uint64_t u64) { + char buffer[20]; + char* end = internal::u64toa(u64, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteDouble(double d) { + if (internal::Double(d).IsNanOrInf()) { + if (!(writeFlags & kWriteNanAndInfFlag)) + return false; + if (internal::Double(d).IsNan()) { + PutReserve(*os_, 3); + PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N'); + return true; + } + if (internal::Double(d).Sign()) { + PutReserve(*os_, 9); + PutUnsafe(*os_, '-'); + } + else + PutReserve(*os_, 8); + PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f'); + PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y'); + return true; + } + + char buffer[25]; + char* end = internal::dtoa(d, buffer, maxDecimalPlaces_); + PutReserve(*os_, static_cast(end - buffer)); + for (char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteString(const Ch* str, SizeType length) { + static const typename TargetEncoding::Ch hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + static const char escape[256] = { +#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 + //0 1 2 3 4 5 6 7 8 9 A B C D E F + 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'b', 't', 'n', 'u', 'f', 'r', 'u', 'u', // 00 + 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 10 + 0, 0, '"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 + Z16, Z16, // 30~4F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, // 50 + Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 // 60~FF +#undef Z16 + }; + + if (TargetEncoding::supportUnicode) + PutReserve(*os_, 2 + length * 6); // "\uxxxx..." + else + PutReserve(*os_, 2 + length * 12); // "\uxxxx\uyyyy..." + + PutUnsafe(*os_, '\"'); + GenericStringStream is(str); + while (ScanWriteUnescapedString(is, length)) { + const Ch c = is.Peek(); + if (!TargetEncoding::supportUnicode && static_cast(c) >= 0x80) { + // Unicode escaping + unsigned codepoint; + if (RAPIDJSON_UNLIKELY(!SourceEncoding::Decode(is, &codepoint))) + return false; + PutUnsafe(*os_, '\\'); + PutUnsafe(*os_, 'u'); + if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) { + PutUnsafe(*os_, hexDigits[(codepoint >> 12) & 15]); + PutUnsafe(*os_, hexDigits[(codepoint >> 8) & 15]); + PutUnsafe(*os_, hexDigits[(codepoint >> 4) & 15]); + PutUnsafe(*os_, hexDigits[(codepoint ) & 15]); + } + else { + RAPIDJSON_ASSERT(codepoint >= 0x010000 && codepoint <= 0x10FFFF); + // Surrogate pair + unsigned s = codepoint - 0x010000; + unsigned lead = (s >> 10) + 0xD800; + unsigned trail = (s & 0x3FF) + 0xDC00; + PutUnsafe(*os_, hexDigits[(lead >> 12) & 15]); + PutUnsafe(*os_, hexDigits[(lead >> 8) & 15]); + PutUnsafe(*os_, hexDigits[(lead >> 4) & 15]); + PutUnsafe(*os_, hexDigits[(lead ) & 15]); + PutUnsafe(*os_, '\\'); + PutUnsafe(*os_, 'u'); + PutUnsafe(*os_, hexDigits[(trail >> 12) & 15]); + PutUnsafe(*os_, hexDigits[(trail >> 8) & 15]); + PutUnsafe(*os_, hexDigits[(trail >> 4) & 15]); + PutUnsafe(*os_, hexDigits[(trail ) & 15]); + } + } + else if ((sizeof(Ch) == 1 || static_cast(c) < 256) && RAPIDJSON_UNLIKELY(escape[static_cast(c)])) { + is.Take(); + PutUnsafe(*os_, '\\'); + PutUnsafe(*os_, static_cast(escape[static_cast(c)])); + if (escape[static_cast(c)] == 'u') { + PutUnsafe(*os_, '0'); + PutUnsafe(*os_, '0'); + PutUnsafe(*os_, hexDigits[static_cast(c) >> 4]); + PutUnsafe(*os_, hexDigits[static_cast(c) & 0xF]); + } + } + else if (RAPIDJSON_UNLIKELY(!(writeFlags & kWriteValidateEncodingFlag ? + Transcoder::Validate(is, *os_) : + Transcoder::TranscodeUnsafe(is, *os_)))) + return false; + } + PutUnsafe(*os_, '\"'); + return true; + } + + bool ScanWriteUnescapedString(GenericStringStream& is, size_t length) { + return RAPIDJSON_LIKELY(is.Tell() < length); + } + + bool WriteStartObject() { os_->Put('{'); return true; } + bool WriteEndObject() { os_->Put('}'); return true; } + bool WriteStartArray() { os_->Put('['); return true; } + bool WriteEndArray() { os_->Put(']'); return true; } + + bool WriteRawValue(const Ch* json, size_t length) { + PutReserve(*os_, length); + for (size_t i = 0; i < length; i++) { + RAPIDJSON_ASSERT(json[i] != '\0'); + PutUnsafe(*os_, json[i]); + } + return true; + } + + void Prefix(Type type) { + (void)type; + if (RAPIDJSON_LIKELY(level_stack_.GetSize() != 0)) { // this value is not at root + Level* level = level_stack_.template Top(); + if (level->valueCount > 0) { + if (level->inArray) + os_->Put(','); // add comma if it is not the first element in array + else // in object + os_->Put((level->valueCount % 2 == 0) ? ',' : ':'); + } + if (!level->inArray && level->valueCount % 2 == 0) + RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name + level->valueCount++; + } + else { + RAPIDJSON_ASSERT(!hasRoot_); // Should only has one and only one root. + hasRoot_ = true; + } + } + + // Flush the value if it is the top level one. + bool EndValue(bool ret) { + if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text + os_->Flush(); + return ret; + } + + OutputStream* os_; + internal::Stack level_stack_; + int maxDecimalPlaces_; + bool hasRoot_; + +private: + // Prohibit copy constructor & assignment operator. + Writer(const Writer&); + Writer& operator=(const Writer&); +}; + +// Full specialization for StringStream to prevent memory copying + +template<> +inline bool Writer::WriteInt(int i) { + char *buffer = os_->Push(11); + const char* end = internal::i32toa(i, buffer); + os_->Pop(static_cast(11 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteUint(unsigned u) { + char *buffer = os_->Push(10); + const char* end = internal::u32toa(u, buffer); + os_->Pop(static_cast(10 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteInt64(int64_t i64) { + char *buffer = os_->Push(21); + const char* end = internal::i64toa(i64, buffer); + os_->Pop(static_cast(21 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteUint64(uint64_t u) { + char *buffer = os_->Push(20); + const char* end = internal::u64toa(u, buffer); + os_->Pop(static_cast(20 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteDouble(double d) { + if (internal::Double(d).IsNanOrInf()) { + // Note: This code path can only be reached if (RAPIDJSON_WRITE_DEFAULT_FLAGS & kWriteNanAndInfFlag). + if (!(kWriteDefaultFlags & kWriteNanAndInfFlag)) + return false; + if (internal::Double(d).IsNan()) { + PutReserve(*os_, 3); + PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N'); + return true; + } + if (internal::Double(d).Sign()) { + PutReserve(*os_, 9); + PutUnsafe(*os_, '-'); + } + else + PutReserve(*os_, 8); + PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f'); + PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y'); + return true; + } + + char *buffer = os_->Push(25); + char* end = internal::dtoa(d, buffer, maxDecimalPlaces_); + os_->Pop(static_cast(25 - (end - buffer))); + return true; +} + +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) +template<> +inline bool Writer::ScanWriteUnescapedString(StringStream& is, size_t length) { + if (length < 16) + return RAPIDJSON_LIKELY(is.Tell() < length); + + if (!RAPIDJSON_LIKELY(is.Tell() < length)) + return false; + + const char* p = is.src_; + const char* end = is.head_ + length; + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + const char* endAligned = reinterpret_cast(reinterpret_cast(end) & static_cast(~15)); + if (nextAligned > end) + return true; + + while (p != nextAligned) + if (*p < 0x20 || *p == '\"' || *p == '\\') { + is.src_ = p; + return RAPIDJSON_LIKELY(is.Tell() < length); + } + else + os_->PutUnsafe(*p++); + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (; p != endAligned; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + SizeType len; +#ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + len = offset; +#else + len = static_cast(__builtin_ffs(r) - 1); +#endif + char* q = reinterpret_cast(os_->PushUnsafe(len)); + for (size_t i = 0; i < len; i++) + q[i] = p[i]; + + p += len; + break; + } + _mm_storeu_si128(reinterpret_cast<__m128i *>(os_->PushUnsafe(16)), s); + } + + is.src_ = p; + return RAPIDJSON_LIKELY(is.Tell() < length); +} +#endif // defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) + +RAPIDJSON_NAMESPACE_END + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/retroshare-gui/src/retroshare-gui.pro b/retroshare-gui/src/retroshare-gui.pro index 30b2af946..48e10a377 100644 --- a/retroshare-gui/src/retroshare-gui.pro +++ b/retroshare-gui/src/retroshare-gui.pro @@ -10,6 +10,11 @@ DEFINES += TARGET=\\\"$${TARGET}\\\" DEPENDPATH *= $${PWD} $${RS_INCLUDE_DIR} retroshare-gui INCLUDEPATH *= $${PWD} retroshare-gui +# when rapidjson is mainstream on all distribs, we will not need the sources anymore +# in the meantime, they are part of the RS directory so that it is always possible to find them + +INCLUDEPATH += ../../rapidjson-1.1.0 + libresapihttpserver { !include("../../libresapi/src/use_libresapi.pri"):error("Including") HEADERS *= gui/settings/WebuiPage.h diff --git a/retroshare-nogui/src/retroshare-nogui.pro b/retroshare-nogui/src/retroshare-nogui.pro index 6be700be5..b8b36f3e3 100644 --- a/retroshare-nogui/src/retroshare-nogui.pro +++ b/retroshare-nogui/src/retroshare-nogui.pro @@ -16,6 +16,11 @@ libresapihttpserver { !include("../../libretroshare/src/use_libretroshare.pri"):error("Including") +# when rapidjson is mainstream on all distribs, we will not need the sources anymore +# in the meantime, they are part of the RS directory so that it is always possible to find them + +INCLUDEPATH += ../../rapidjson-1.1.0 + ################################# Linux ########################################## linux-* { #CONFIG += version_detail_bash_script From 56e8134d453c0e45fe932da6acfa50d4c79309e9 Mon Sep 17 00:00:00 2001 From: cyril soler Date: Thu, 3 May 2018 11:32:24 +0200 Subject: [PATCH 087/161] removed sqlite3 lib from ld when using sqlcipher. --- retroshare.pri | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare.pri b/retroshare.pri index ed0e3c221..89c6da7d2 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -258,7 +258,7 @@ RS_UPNP_LIB = upnp ixml threadutil sqlcipher { DEFINES -= NO_SQLCIPHER - RS_SQL_LIB = sqlcipher sqlite3 + RS_SQL_LIB = sqlcipher } no_sqlcipher { DEFINES *= NO_SQLCIPHER From 91ed367c55e480f0ffa78cafc57edad7b0f8241a Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 3 May 2018 13:45:44 +0200 Subject: [PATCH 088/161] added packaging for ubuntu bionic --- build_scripts/Debian+Ubuntu/control.bionic | 46 +++++++++++++++++++ .../Debian+Ubuntu/makeSourcePackage.sh | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 build_scripts/Debian+Ubuntu/control.bionic diff --git a/build_scripts/Debian+Ubuntu/control.bionic b/build_scripts/Debian+Ubuntu/control.bionic new file mode 100644 index 000000000..80b452523 --- /dev/null +++ b/build_scripts/Debian+Ubuntu/control.bionic @@ -0,0 +1,46 @@ +Source: retroshare +Section: devel +Priority: standard +Maintainer: Cyril Soler +Build-Depends: debhelper (>= 7), libglib2.0-dev, libupnp-dev, libssl-dev, libxss-dev, libgnome-keyring-dev, libbz2-dev, libspeex-dev, libspeexdsp-dev, libxslt1-dev, cmake, libcurl4-openssl-dev, libopencv-dev, tcl8.6, libsqlcipher-dev, libmicrohttpd-dev, libavcodec-dev, qtmultimedia5-dev, qttools5-dev, libqt5x11extras5-dev, qt5-default +Standards-Version: 3.9.6 +Homepage: http://retroshare.sourceforge.net + +Package: retroshare-voip-plugin +Architecture: any +Conflicts: retroshare06-voip-plugin +Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare, libspeex1, libspeexdsp1, libqt5multimedia5 +Description: RetroShare VOIP plugin + This package provides a plugin for RetroShare, a secured Friend-to-Friend communication + plateform. The plugin adds voice-over-IP functionality to the private chat window. Both + friends chatting together need the plugin installed to be able to talk together. + +Package: retroshare-feedreader-plugin +Architecture: any +Conflicts: retroshare06-feedreader-plugin +Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare +Description: RetroShare FeedReader plugin + This package provides a plugin for RetroShare, a secured Friend-to-Friend communication + plateform. The plugin adds a RSS feed reader tab to retroshare. + +Package: retroshare-nogui +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends}, gnome-keyring +Conflicts: retroshare,retroshare06-nogui +Description: Secure communication with friends + This is the command-line client for RetroShare network. This client + can be contacted and talked-to using SSL. Clients exist for portable + devices running e.g. Android. + +Package: retroshare +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends}, gnome-keyring +Conflicts: retroshare-nogui,retroshare06 +Description: Secure communication with friends + RetroShare is a Open Source cross-platform, private and secure decentralised + commmunication platform. It lets you to securely chat and share files with your + friends and family, using a web-of-trust to authenticate peers and OpenSSL to + encrypt all communication. RetroShare provides filesharing, chat, messages, + forums and channels. + + diff --git a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh index 26ebd651b..ddb699680 100755 --- a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh +++ b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh @@ -77,7 +77,7 @@ if test "${useretrotor}" = "true"; then fi if test "${dist}" = "" ; then - dist="trusty xenial artful" + dist="trusty xenial artful bionic" fi echo Attempting to get revision number... From ecba4c2dab628e9f1e82086673a033632d507c27 Mon Sep 17 00:00:00 2001 From: Phenom Date: Wed, 2 May 2018 20:31:52 +0200 Subject: [PATCH 089/161] Add Context Menu for GxsId in lobby textBrowser. --- .../src/gui/chat/ChatLobbyDialog.cpp | 307 ++++++++++-------- retroshare-gui/src/gui/chat/ChatLobbyDialog.h | 10 +- retroshare-gui/src/gui/chat/ChatWidget.cpp | 74 ++--- retroshare-gui/src/gui/chat/ChatWidget.h | 4 + .../src/gui/common/RSTextBrowser.cpp | 27 ++ retroshare-gui/src/gui/common/RSTextBrowser.h | 2 + 6 files changed, 234 insertions(+), 190 deletions(-) diff --git a/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp b/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp index 918939917..e490af1b8 100644 --- a/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp +++ b/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp @@ -33,6 +33,7 @@ #include "gui/ChatLobbyWidget.h" #include "gui/FriendsDialog.h" #include "gui/MainWindow.h" +#include "gui/chat/ChatWidget.h" #include "gui/common/html.h" #include "gui/common/FriendSelectionDialog.h" #include "gui/common/RSTreeWidgetItem.h" @@ -97,7 +98,7 @@ ChatLobbyDialog::ChatLobbyDialog(const ChatLobbyId& lid, QWidget *parent, Qt::Wi votePositiveAct = new QAction(QIcon(":/icons/png/thumbs-up.png"), tr("Give positive opinion"), this); distantChatAct = new QAction(QIcon(":/images/chat_24.png"), tr("Start private chat"), this); sendMessageAct = new QAction(QIcon(":/images/mail_new.png"), tr("Send Message"), this); - showinpeopleAct = new QAction(QIcon(), tr("Show author in people tab"), this); + showInPeopleAct = new QAction(QIcon(), tr("Show author in people tab"), this); QActionGroup *sortgrp = new QActionGroup(this); actionSortByName = new QAction(QIcon(), tr("Sort by Name"), this); @@ -111,17 +112,13 @@ ChatLobbyDialog::ChatLobbyDialog(const ChatLobbyId& lid, QWidget *parent, Qt::Wi actionSortByActivity->setActionGroup(sortgrp); - connect(muteAct, SIGNAL(triggered()), this, SLOT(changePartipationState())); + connect(muteAct, SIGNAL(triggered()), this, SLOT(changeParticipationState())); connect(distantChatAct, SIGNAL(triggered()), this, SLOT(distantChatParticipant())); connect(sendMessageAct, SIGNAL(triggered()), this, SLOT(sendMessage())); connect(votePositiveAct, SIGNAL(triggered()), this, SLOT(voteParticipant())); connect(voteNeutralAct, SIGNAL(triggered()), this, SLOT(voteParticipant())); connect(voteNegativeAct, SIGNAL(triggered()), this, SLOT(voteParticipant())); - connect(showinpeopleAct, SIGNAL(triggered()), this, SLOT(showInPeopleTab())); - - votePositiveAct->setData(RsReputations::OPINION_POSITIVE); - voteNeutralAct->setData(RsReputations::OPINION_NEUTRAL); - voteNegativeAct->setData(RsReputations::OPINION_NEGATIVE); + connect(showInPeopleAct, SIGNAL(triggered()), this, SLOT(showInPeopleTab())); connect(actionSortByName, SIGNAL(triggered()), this, SLOT(sortParcipants())); connect(actionSortByActivity, SIGNAL(triggered()), this, SLOT(sortParcipants())); @@ -172,7 +169,8 @@ ChatLobbyDialog::ChatLobbyDialog(const ChatLobbyId& lid, QWidget *parent, Qt::Wi ui.chatWidget->addToolsAction(checkableAction); //getChatWidget()->addChatBarWidget(ownIdChooser); - + connect(ui.chatWidget, SIGNAL(textBrowserAskContextMenu(QMenu*,QString,QPoint)), this, SLOT(textBrowserAskContextMenu(QMenu*,QString,QPoint))); + connect(ownIdChooser,SIGNAL(currentIndexChanged(int)),this,SLOT(changeNickname())) ; @@ -226,33 +224,76 @@ void ChatLobbyDialog::inviteFriends() void ChatLobbyDialog::participantsTreeWidgetCustomPopupMenu(QPoint) { QList selectedItems = ui.participantsList->selectedItems(); + QList idList; + QList::iterator item; + for (item = selectedItems.begin(); item != selectedItems.end(); ++item) + { + RsGxsId gxs_id ; + dynamic_cast(*item)->getId(gxs_id) ; + idList.append(gxs_id); + } QMenu contextMnu(this); + contextMnu.addAction(actionSortByActivity); + contextMnu.addAction(actionSortByName); - contextMnu.addAction(distantChatAct); - contextMnu.addAction(sendMessageAct); - contextMnu.addSeparator(); - contextMnu.addAction(actionSortByActivity); - contextMnu.addAction(actionSortByName); - contextMnu.addSeparator(); - contextMnu.addAction(muteAct); - contextMnu.addAction(votePositiveAct); - contextMnu.addAction(voteNeutralAct); - contextMnu.addAction(voteNegativeAct); - contextMnu.addAction(showinpeopleAct); + contextMnu.addSeparator(); + + initParticipantsContextMenu(&contextMnu, idList); + + contextMnu.exec(QCursor::pos()); +} + +void ChatLobbyDialog::textBrowserAskContextMenu(QMenu* contextMnu, QString anchorForPosition, const QPoint /*point*/) +{ + if (anchorForPosition.startsWith(PERSONID)){ + QString strId = anchorForPosition.replace(PERSONID,""); + if (strId.contains(" ")) + strId.truncate(strId.indexOf(" ")); + + contextMnu->addSeparator(); + + QList idList; + idList.append(RsGxsId(strId.toStdString())); + initParticipantsContextMenu(contextMnu, idList); + } +} + +void ChatLobbyDialog::initParticipantsContextMenu(QMenu *contextMnu, QList idList) +{ + if (!contextMnu) + return; + + contextMnu->addAction(distantChatAct); + contextMnu->addAction(sendMessageAct); + contextMnu->addSeparator(); + contextMnu->addAction(muteAct); + contextMnu->addAction(votePositiveAct); + contextMnu->addAction(voteNeutralAct); + contextMnu->addAction(voteNegativeAct); + contextMnu->addAction(showInPeopleAct); distantChatAct->setEnabled(false); - sendMessageAct->setEnabled(selectedItems.count()==1); + sendMessageAct->setEnabled(true); + muteAct->setEnabled(false); muteAct->setCheckable(true); - muteAct->setEnabled(false); - muteAct->setChecked(false); - votePositiveAct->setEnabled(false); - voteNeutralAct->setEnabled(false); - voteNegativeAct->setEnabled(false); - showinpeopleAct->setEnabled(selectedItems.count()==1); - if(selectedItems.count()==1) - { - RsGxsId gxsid(selectedItems.at(0)->text(COLUMN_ID).toStdString()); + muteAct->setChecked(false); + votePositiveAct->setEnabled(false); + voteNeutralAct->setEnabled(false); + voteNegativeAct->setEnabled(false); + showInPeopleAct->setEnabled(idList.count()==1); + + distantChatAct->setData(QVariant::fromValue(idList)); + sendMessageAct->setData(QVariant::fromValue(idList)); + muteAct->setData(QVariant::fromValue(idList)); + votePositiveAct->setData(QVariant::fromValue(idList)); + voteNeutralAct->setData(QVariant::fromValue(idList)); + voteNegativeAct->setData(QVariant::fromValue(idList)); + showInPeopleAct->setData(QVariant::fromValue(idList)); + + if(idList.count()==1) + { + RsGxsId gxsid = idList.at(0); if(!gxsid.isNull() && !rsIdentity->isOwnId(gxsid)) { @@ -263,49 +304,56 @@ void ChatLobbyDialog::participantsTreeWidgetCustomPopupMenu(QPoint) muteAct->setEnabled(true); muteAct->setChecked(isParticipantMuted(gxsid)); } - } - contextMnu.exec(QCursor::pos()); + } } void ChatLobbyDialog::voteParticipant() { - QList selectedItems = ui.participantsList->selectedItems(); - if (selectedItems.isEmpty()) - return; - QList::iterator item; - - QAction *act = dynamic_cast(sender()) ; - if(!act) - { - std::cerr << "No sender! Some bug in the code." << std::endl; - return ; - } - - RsReputations::Opinion op = RsReputations::Opinion(act->data().toUInt()) ; - - for (item = selectedItems.begin(); item != selectedItems.end(); ++item) + QAction *act = dynamic_cast(sender()) ; + if(!act) { - RsGxsId nickname; - dynamic_cast(*item)->getId(nickname) ; + std::cerr << "No sender! Some bug in the code." << std::endl; + return ; + } - rsReputations->setOwnOpinion(nickname, op); - std::cerr << "Giving opinion to GXS id " << nickname << " to " << op<< std::endl; - dynamic_cast(*item)->forceUpdate(); - } + QList idList = act->data().value>(); + + RsReputations::Opinion op = RsReputations::OPINION_NEUTRAL ; + if (act == votePositiveAct) + op = RsReputations::OPINION_POSITIVE; + if (act == voteNegativeAct) + op = RsReputations::OPINION_NEGATIVE; + + for (QList::iterator item = idList.begin(); item != idList.end(); ++item) + { + rsReputations->setOwnOpinion(*item, op); + std::cerr << "Giving opinion to GXS id " << *item << " to " << op << std::endl; + } + + updateParticipantsList(); } void ChatLobbyDialog::showInPeopleTab() { - QList selectedItems = ui.participantsList->selectedItems(); - if (selectedItems.count()!=1) - return; - RsGxsId nickname; - dynamic_cast(*selectedItems.begin())->getId(nickname); - IdDialog *idDialog = dynamic_cast(MainWindow::getPage(MainWindow::People)); - if (!idDialog) - return ; - MainWindow::showWindow(MainWindow::People); - idDialog->navigate(nickname); + QAction *act = dynamic_cast(sender()) ; + if(!act) + { + std::cerr << "No sender! Some bug in the code." << std::endl; + return ; + } + + QList idList = act->data().value>(); + if (idList.count() != 1) + return; + + RsGxsId nickname = idList.at(0); + + IdDialog *idDialog = dynamic_cast(MainWindow::getPage(MainWindow::People)); + if (!idDialog) + return ; + MainWindow::showWindow(MainWindow::People); + + idDialog->navigate(nickname); } void ChatLobbyDialog::init(const ChatId &/*id*/, const QString &/*title*/) @@ -586,34 +634,28 @@ void ChatLobbyDialog::updateParticipantsList() } /** - * Called when a Participant in QTree get Clicked / Changed + * Called when a Participant get Clicked / Changed * * Check if the Checkbox altered and Mute User - * - * @todo auf rsid - * - * @param QTreeWidgetItem Participant to check */ -void ChatLobbyDialog::changePartipationState() +void ChatLobbyDialog::changeParticipationState() { - QList selectedItems = ui.participantsList->selectedItems(); - - if (selectedItems.isEmpty()) { - return; + QAction *act = dynamic_cast(sender()) ; + if(!act) + { + std::cerr << "No sender! Some bug in the code." << std::endl; + return ; } - QList::iterator item; - for (item = selectedItems.begin(); item != selectedItems.end(); ++item) { + QList idList = act->data().value>(); - RsGxsId gxs_id ; - dynamic_cast(*item)->getId(gxs_id) ; - - std::cerr << "check Partipation status for '" << gxs_id << std::endl; - - if (muteAct->isChecked()) { - muteParticipant(gxs_id); + for (QList::iterator item = idList.begin(); item != idList.end(); ++item) + { + std::cerr << "check Partipation status for '" << *item << std::endl; + if (act->isChecked()) { + muteParticipant(*item); } else { - unMuteParticipant(gxs_id); + unMuteParticipant(*item); } } @@ -650,75 +692,68 @@ void ChatLobbyDialog::participantsTreeWidgetDoubleClicked(QTreeWidgetItem *item, void ChatLobbyDialog::distantChatParticipant() { - std::cerr << " initiating distant chat" << std::endl; + QAction *act = dynamic_cast(sender()) ; + if(!act) + { + std::cerr << "No sender! Some bug in the code." << std::endl; + return ; + } - QList selectedItems = ui.participantsList->selectedItems(); + std::cerr << " initiating distant chat" << std::endl; - if (selectedItems.isEmpty()) - return; + QList idList = act->data().value>(); + if (idList.count() != 1) + return; - if(selectedItems.size() != 1) - return ; + RsGxsId gxs_id = idList.at(0); + if (gxs_id.isNull()) + return; - GxsIdRSTreeWidgetItem *item = dynamic_cast(selectedItems.front()); + RsGxsId own_id; + rsMsgs->getIdentityForChatLobby(lobbyId, own_id); - if(!item) - return ; + DistantChatPeerId tunnel_id; + uint32_t error_code ; - RsGxsId gxs_id ; - item->getId(gxs_id) ; - RsGxsId own_id; - - rsMsgs->getIdentityForChatLobby(lobbyId, own_id); - - uint32_t error_code ; - DistantChatPeerId tunnel_id; - - if(! rsMsgs->initiateDistantChatConnexion(gxs_id,own_id,tunnel_id,error_code)) - { - QString error_str ; - switch(error_code) - { - case RS_DISTANT_CHAT_ERROR_DECRYPTION_FAILED : error_str = tr("Decryption failed.") ; break ; - case RS_DISTANT_CHAT_ERROR_SIGNATURE_MISMATCH : error_str = tr("Signature mismatch") ; break ; - case RS_DISTANT_CHAT_ERROR_UNKNOWN_KEY : error_str = tr("Unknown key") ; break ; - case RS_DISTANT_CHAT_ERROR_UNKNOWN_HASH : error_str = tr("Unknown hash") ; break ; - default: - error_str = tr("Unknown error.") ; - } - QMessageBox::warning(NULL,tr("Cannot start distant chat"),tr("Distant chat cannot be initiated:")+" "+error_str - +QString::number(error_code)) ; - } + if(! rsMsgs->initiateDistantChatConnexion(gxs_id,own_id,tunnel_id,error_code)) + { + QString error_str ; + switch(error_code) + { + case RS_DISTANT_CHAT_ERROR_DECRYPTION_FAILED : error_str = tr("Decryption failed.") ; break ; + case RS_DISTANT_CHAT_ERROR_SIGNATURE_MISMATCH : error_str = tr("Signature mismatch") ; break ; + case RS_DISTANT_CHAT_ERROR_UNKNOWN_KEY : error_str = tr("Unknown key") ; break ; + case RS_DISTANT_CHAT_ERROR_UNKNOWN_HASH : error_str = tr("Unknown hash") ; break ; + default: + error_str = tr("Unknown error.") ; + } + QMessageBox::warning(NULL,tr("Cannot start distant chat"),tr("Distant chat cannot be initiated:")+" "+error_str + +QString::number(error_code)) ; + } } void ChatLobbyDialog::sendMessage() { + QAction *act = dynamic_cast(sender()) ; + if(!act) + { + std::cerr << "No sender! Some bug in the code." << std::endl; + return ; + } - QList selectedItems = ui.participantsList->selectedItems(); + QList idList = act->data().value>(); - if (selectedItems.isEmpty()) - return; + MessageComposer *nMsgDialog = MessageComposer::newMsg(); + if (nMsgDialog == NULL) + return; - QList::iterator item; - for (item = selectedItems.begin(); item != selectedItems.end(); ++item) { + for (QList::iterator item = idList.begin(); item != idList.end(); ++item) + nMsgDialog->addRecipient(MessageComposer::TO, *item); - RsGxsId gxs_id ; - dynamic_cast(*item)->getId(gxs_id) ; - - - MessageComposer *nMsgDialog = MessageComposer::newMsg(); - if (nMsgDialog == NULL) { - return; - } - - nMsgDialog->addRecipient(MessageComposer::TO, RsGxsId(gxs_id)); - nMsgDialog->show(); - nMsgDialog->activateWindow(); - - /* window will destroy itself! */ - - } + nMsgDialog->show(); + nMsgDialog->activateWindow(); + /* window will destroy itself! */ } diff --git a/retroshare-gui/src/gui/chat/ChatLobbyDialog.h b/retroshare-gui/src/gui/chat/ChatLobbyDialog.h index dbe178678..f70eb27a6 100644 --- a/retroshare-gui/src/gui/chat/ChatLobbyDialog.h +++ b/retroshare-gui/src/gui/chat/ChatLobbyDialog.h @@ -27,6 +27,8 @@ #include "gui/common/RSTreeWidgetItem.h" #include "ChatDialog.h" +Q_DECLARE_METATYPE(RsGxsId) + class GxsIdChooser ; class QToolButton; class QWidgetAction; @@ -51,6 +53,7 @@ public: private slots: void participantsTreeWidgetCustomPopupMenu( QPoint point ); + void textBrowserAskContextMenu(QMenu* contextMnu, QString anchorForPosition, const QPoint point); void inviteFriends() ; void leaveLobby() ; void filterChanged(const QString &text); @@ -77,7 +80,7 @@ protected: protected slots: void changeNickname(); - void changePartipationState(); + void changeParticipationState(); void distantChatParticipant(); void participantsTreeWidgetDoubleClicked(QTreeWidgetItem *item, int column); void sendMessage(); @@ -85,6 +88,7 @@ protected slots: private: void updateParticipantsList(); + void initParticipantsContextMenu(QMenu* contextMnu, QList idList); void filterIds(); @@ -117,8 +121,8 @@ private: QAction *actionSortByActivity; QWidgetAction *checkableAction; QAction *sendMessageAct; - QAction *showinpeopleAct; - + QAction *showInPeopleAct; + GxsIdChooser *ownIdChooser ; //icons cache QIcon bullet_red_128, bullet_grey_128, bullet_green_128, bullet_yellow_128; diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index c3b021458..a03e8616a 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -66,8 +66,6 @@ #define FMM 2.5//fontMetricsMultiplicator -#define PERSONID "PersonId:" - /***** * #define CHAT_DEBUG 1 *****/ @@ -594,31 +592,15 @@ bool ChatWidget::eventFilter(QObject *obj, QEvent *event) if (event->type() == QEvent::ToolTip) { QHelpEvent* helpEvent = static_cast(event); - QTextCursor cursor = ui->textBrowser->cursorForPosition(helpEvent->pos()); - cursor.select(QTextCursor::WordUnderCursor); - QString toolTipText = ""; - if (!cursor.selectedText().isEmpty()){ - QRegExp rx("textBrowser->getShowImages()){ - QString imageStr; - if (ui->textBrowser->checkImage(helpEvent->pos(), imageStr)) { - toolTipText = imageStr; - } - } else if (toolTipText.startsWith(PERSONID)){ - toolTipText = toolTipText.replace(PERSONID, tr("Person id: ") ); - toolTipText = toolTipText.append(tr("\nDouble click on it to add his name on text writer.") ); + QString toolTipText = ui->textBrowser->anchorForPosition(helpEvent->pos()); + if (toolTipText.isEmpty() && !ui->textBrowser->getShowImages()){ + QString imageStr; + if (ui->textBrowser->checkImage(helpEvent->pos(), imageStr)) { + toolTipText = imageStr; } + } else if (toolTipText.startsWith(PERSONID)){ + toolTipText = toolTipText.replace(PERSONID, tr("Person id: ") ); + toolTipText = toolTipText.append(tr("\nDouble click on it to add his name on text writer.") ); } if (!toolTipText.isEmpty()){ QToolTip::showText(helpEvent->globalPos(), toolTipText); @@ -710,32 +692,19 @@ bool ChatWidget::eventFilter(QObject *obj, QEvent *event) if (event->type() == QEvent::MouseButtonDblClick) { QMouseEvent* mouseEvent = static_cast(event); - QTextCursor cursor = ui->textBrowser->cursorForPosition(mouseEvent->pos()); - cursor.select(QTextCursor::WordUnderCursor); - if (!cursor.selectedText().isEmpty()){ - QRegExp rx("textBrowser->anchorForPosition(mouseEvent->pos()); + if (!anchor.isEmpty()){ + if (anchor.startsWith(PERSONID)){ + QString strId = anchor.replace(PERSONID,""); + if (strId.contains(" ")) + strId.truncate(strId.indexOf(" ")); - if (!anchors.isEmpty()){ - if (anchors.at(0).startsWith(PERSONID)){ - QString strId = QString(anchors.at(0)).replace(PERSONID,""); - if (strId.contains(" ")) - strId.truncate(strId.indexOf(" ")); - - RsGxsId mId = RsGxsId(strId.toStdString()); - if(!mId.isNull()) { - RsIdentityDetails details; - if (rsIdentity->getIdDetails(mId, details)){ - QString text = QString("@").append(GxsIdDetails::getName(details)).append(" "); - ui->chatTextEdit->textCursor().insertText(text); - } + RsGxsId mId = RsGxsId(strId.toStdString()); + if(!mId.isNull()) { + RsIdentityDetails details; + if (rsIdentity->getIdDetails(mId, details)){ + QString text = QString("@").append(GxsIdDetails::getName(details)).append(" "); + ui->chatTextEdit->textCursor().insertText(text); } } } @@ -1138,6 +1107,9 @@ void ChatWidget::contextMenuTextBrowser(QPoint point) contextMnu->addAction(ui->actionSave_image); } + QString anchor = ui->textBrowser->anchorForPosition(point); + emit textBrowserAskContextMenu(contextMnu, anchor, point); + contextMnu->exec(ui->textBrowser->viewport()->mapToGlobal(point)); delete(contextMnu); } diff --git a/retroshare-gui/src/gui/chat/ChatWidget.h b/retroshare-gui/src/gui/chat/ChatWidget.h index 30f2e8427..8de38620e 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.h +++ b/retroshare-gui/src/gui/chat/ChatWidget.h @@ -37,6 +37,9 @@ #include #include +//For PersonId anchor. +#define PERSONID "PersonId:" + class QAction; class QTextEdit; class QPushButton; @@ -137,6 +140,7 @@ signals: void infoChanged(ChatWidget*); void newMessage(ChatWidget*); void statusChanged(int); + void textBrowserAskContextMenu(QMenu* contextMnu, QString anchorForPosition, const QPoint point); protected: bool eventFilter(QObject *obj, QEvent *event); diff --git a/retroshare-gui/src/gui/common/RSTextBrowser.cpp b/retroshare-gui/src/gui/common/RSTextBrowser.cpp index 8a18b459f..03fbfe3de 100644 --- a/retroshare-gui/src/gui/common/RSTextBrowser.cpp +++ b/retroshare-gui/src/gui/common/RSTextBrowser.cpp @@ -243,3 +243,30 @@ bool RSTextBrowser::checkImage(QPoint pos, QString &imageStr) } return false; } + +/** + * @brief RSTextBrowser::anchorForPosition Replace anchorAt that doesn't works as expected. + * @param pos Where to get anchor from text + * @return anchor If text at pos is inside anchor, else empty string. + */ +QString RSTextBrowser::anchorForPosition(const QPoint &pos) const +{ + QTextCursor cursor = cursorForPosition(pos); + cursor.select(QTextCursor::WordUnderCursor); + QString anchor = ""; + if (!cursor.selectedText().isEmpty()){ + QRegExp rx(" Date: Thu, 3 May 2018 15:04:22 +0200 Subject: [PATCH 090/161] Add GxsId in Restored Chat Message. --- retroshare-gui/src/gui/chat/ChatWidget.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index c3b021458..9c8121817 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -389,7 +389,7 @@ void ChatWidget::init(const ChatId &chat_id, const QString &title) name = QString::fromUtf8(historyIt->peerName.c_str()); } - addChatMsg(historyIt->incoming, name, QDateTime::fromTime_t(historyIt->sendTime), QDateTime::fromTime_t(historyIt->recvTime), QString::fromUtf8(historyIt->message.c_str()), MSGTYPE_HISTORY); + addChatMsg(historyIt->incoming, name, RsGxsId(historyIt->peerName.c_str()), QDateTime::fromTime_t(historyIt->sendTime), QDateTime::fromTime_t(historyIt->recvTime), QString::fromUtf8(historyIt->message.c_str()), MSGTYPE_HISTORY); } } } From bfe8e40a8adbb0ffd72bb603f0882c6153126efa Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 3 May 2018 15:22:03 +0200 Subject: [PATCH 091/161] updated ubuntu changelog --- build_scripts/Debian+Ubuntu/changelog | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/build_scripts/Debian+Ubuntu/changelog b/build_scripts/Debian+Ubuntu/changelog index 57ed5bd3e..4886eb5f0 100644 --- a/build_scripts/Debian+Ubuntu/changelog +++ b/build_scripts/Debian+Ubuntu/changelog @@ -1,5 +1,48 @@ retroshare (ZZZZZZ-1.XXXXXX~YYYYYY) YYYYYY; urgency=low + 2dc69cb RetroP Fri, 27 Apr 2018 16:50:00 +0300 embed preview for images on file attach in chat + c199199 Gioacc Sun, 8 Apr 2018 12:37:41 +0200 pqissl silence extra debug message + 8245d74 csoler Sat, 7 Apr 2018 14:33:58 +0200 Merge pull request #1230 from csoler/master + 2782494 csoler Sat, 7 Apr 2018 14:29:23 +0200 removed debug info + e2b0e27 csoler Sat, 7 Apr 2018 00:56:07 +0200 fixed costly polling in RsGenExchange + cc091cc Gioacc Sat, 7 Apr 2018 12:48:01 +0200 Fixed hidden nodes listening failure + 73c6dee csoler Tue, 27 Mar 2018 20:54:31 +0200 Merge pull request #1191 from G10h4ck/IPv6-v3 + 4be73b7 csoler Sat, 24 Mar 2018 10:18:37 +0100 Merge pull request #1216 from beardog108/security + 599c3d4 Kevin Fri, 23 Mar 2018 23:00:27 -0500 fixed clickjacking attack with x-frame-options + 24d1f5d csoler Sat, 17 Mar 2018 18:40:32 +0100 Merge pull request #1190 from PhenomRetroShare/Add_ShowEmptySubMenuRemoteTree + 09de680 csoler Sat, 17 Mar 2018 18:30:21 +0100 Merge pull request #1210 from PhenomRetroShare/Add_BackgroundInProgressBarText + 1432104 csoler Sat, 17 Mar 2018 18:25:47 +0100 Merge pull request #1214 from csoler/v0.6-FT + e1482dd Phenom Sat, 17 Mar 2018 00:00:05 +0100 Add Rounded and Gradient Background to xprogressbar text. + 7da73b3 Phenom Mon, 5 Mar 2018 20:31:39 +0100 Add differents views depends ProgressBar width + 311b190 Phenom Sun, 4 Mar 2018 20:06:33 +0100 Add Background to xprogressbar text for more readability. + e1ad21c csoler Thu, 15 Mar 2018 13:11:19 +0100 fixed wrong file count in RsCollectionDialog when downloading files + b3653d1 csoler Thu, 15 Mar 2018 11:32:55 +0100 enabled aggressive re-request of pending slices at end of transfer, thus fixing the long delay to finish files with mixed fast/slow sources + 6e8305a csoler Wed, 14 Mar 2018 20:56:30 +0100 Merge pull request #1211 from PhenomRetroShare/Fix_AvatarStatusOverlay + c7549a2 csoler Wed, 14 Mar 2018 20:49:42 +0100 Merge pull request #1208 from PhenomRetroShare/Fix_ChangeChatTextColorWhenChangeStyle + 11cfa7b csoler Wed, 14 Mar 2018 20:34:52 +0100 Merge pull request #1209 from PhenomRetroShare/Add_DescriptionGxsGroupToolTip + 3fc6314 csoler Wed, 14 Mar 2018 20:33:34 +0100 Merge pull request #1206 from PhenomRetroShare/Fix_AboutIcon + 0e6d27a csoler Tue, 13 Mar 2018 20:25:38 +0100 fixed bug in FT causing transfer lines to only update when the mouse moves over the widget + 91634ba thunde Thu, 8 Mar 2018 17:45:24 +0100 Added build of Retrotor to Windows build environment + e9b49e1 Gioacc Mon, 12 Mar 2018 17:06:01 +0100 Update Android quirks documentation + ce56a20 Gioacc Mon, 12 Mar 2018 16:22:42 +0100 Added sources verification in android toolchain + e7078b5 Gioacc Mon, 12 Mar 2018 14:27:59 +0100 Update Android documentation + 514b31e Gioacc Sun, 11 Mar 2018 20:46:07 +0100 Add Qt installation in android preparation script + 6f30aa2 Gioacc Sun, 11 Mar 2018 20:45:40 +0100 Merge branch 'master' of git://github.com/RetroShare/RetroShare + 13b18e3 csoler Sat, 10 Mar 2018 16:05:52 +0100 added missing decoration icon on transfer sources + 4dd2776 Gioacc Sat, 10 Mar 2018 13:15:53 +0100 Remove android JNI .class from source + 2b300eb Gioacc Sat, 10 Mar 2018 00:44:24 +0100 Update version in Android Manifest + f5a3b26 Gioacc Fri, 9 Mar 2018 20:26:29 +0100 More omogeneous variable naming in android build tools + c92b860 thunde Sun, 4 Mar 2018 20:08:15 +0100 Windows build environment: - Updated external libraries - Added compile of plugins to build.bat - Added copy of Qt style DLL to pack.bat and Windows Installer - Removed "CONFIG=console" for release build + 8b42873 Phenom Sun, 4 Mar 2018 22:45:11 +0100 Fix Friend Avatar status overlay no depends size scale. + 6877589 Phenom Sun, 4 Mar 2018 18:19:18 +0100 Add description in GroupTreeWidget tooltip. + 51cc94b Phenom Sun, 4 Mar 2018 17:31:40 +0100 Fix ChatWidget current text edit color when changing appearance style. + 4e6f2ae Phenom Sun, 4 Mar 2018 14:31:16 +0100 Fix Icon for About page. + a89ab8f Phenom Sat, 24 Feb 2018 14:07:32 +0100 Add ShowEmpty sub menu in Tree Remote SharedFilesDialog. + + -- Retroshare Dev Team Thu, 03 May 2018 12:00:00 +0100 + +retroshare (0.6.4-1.20180313.0e6d27ad~xenial) xenial; urgency=low + 919417a csoler Sat, 3 Mar 2018 19:04:54 +0100 make sure tor executable from config path can be reached b587ac8 csoler Sat, 3 Mar 2018 16:06:49 +0100 Merge pull request #1192 from PhenomRetroShare/Add_WarningMaxFlatViewFile 9bcff07 csoler Sat, 3 Mar 2018 15:58:11 +0100 Merge pull request #1203 from PhenomRetroShare/Fix_SharedFileDialogShowColumn From 5f12b6076dd29e0353c7c6b9788ea73767f56afb Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 3 May 2018 15:22:39 +0200 Subject: [PATCH 092/161] added -lgnome-keyring to unix LIBS when rs_autologin is set --- retroshare-nogui/src/retroshare-nogui.pro | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/retroshare-nogui/src/retroshare-nogui.pro b/retroshare-nogui/src/retroshare-nogui.pro index b8b36f3e3..0cc70fb24 100644 --- a/retroshare-nogui/src/retroshare-nogui.pro +++ b/retroshare-nogui/src/retroshare-nogui.pro @@ -27,6 +27,10 @@ linux-* { QMAKE_CXXFLAGS *= -D_FILE_OFFSET_BITS=64 LIBS *= -rdynamic + + rs_autologin { + LIBS *= -lgnome-keyring + } } unix { From 676c07015289c5d245a79e5933380bf041947ea7 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 3 May 2018 15:55:21 +0200 Subject: [PATCH 093/161] extended the rapid_json trick to plugins --- plugins/FeedReader/FeedReader.pro | 6 ++++++ plugins/VOIP/VOIP.pro | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/plugins/FeedReader/FeedReader.pro b/plugins/FeedReader/FeedReader.pro index 7e7e40fdd..c60348551 100644 --- a/plugins/FeedReader/FeedReader.pro +++ b/plugins/FeedReader/FeedReader.pro @@ -80,6 +80,12 @@ TRANSLATIONS += \ lang/FeedReader_tr.ts \ lang/FeedReader_zh_CN.ts +# when rapidjson is mainstream on all distribs, we will not need the sources anymore +# in the meantime, they are part of the RS directory so that it is always possible to find them + +INCLUDEPATH += ../../rapidjson-1.1.0 + + linux-* { CONFIG += link_pkgconfig diff --git a/plugins/VOIP/VOIP.pro b/plugins/VOIP/VOIP.pro index ccf7f00f9..c0dc92e7e 100644 --- a/plugins/VOIP/VOIP.pro +++ b/plugins/VOIP/VOIP.pro @@ -16,6 +16,12 @@ MOBILITY = multimedia DEPENDPATH += $$PWD/../../retroshare-gui/src/temp/ui INCLUDEPATH += $$PWD/../../retroshare-gui/src/temp/ui +# when rapidjson is mainstream on all distribs, we will not need the sources anymore +# in the meantime, they are part of the RS directory so that it is always possible to find them + +INCLUDEPATH += ../../rapidjson-1.1.0 + + #################################### Linux ##################################### linux-* { From 10badf5cbb4dba822b9cff3abd841adebb172df8 Mon Sep 17 00:00:00 2001 From: sehraf Date: Fri, 4 May 2018 20:43:40 +0200 Subject: [PATCH 094/161] fix retroshare-nogui.pro autologin --- retroshare-nogui/src/retroshare-nogui.pro | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/retroshare-nogui/src/retroshare-nogui.pro b/retroshare-nogui/src/retroshare-nogui.pro index 0cc70fb24..6c95d278c 100644 --- a/retroshare-nogui/src/retroshare-nogui.pro +++ b/retroshare-nogui/src/retroshare-nogui.pro @@ -23,14 +23,11 @@ INCLUDEPATH += ../../rapidjson-1.1.0 ################################# Linux ########################################## linux-* { + CONFIG += link_pkgconfig #CONFIG += version_detail_bash_script QMAKE_CXXFLAGS *= -D_FILE_OFFSET_BITS=64 LIBS *= -rdynamic - - rs_autologin { - LIBS *= -lgnome-keyring - } } unix { From 1129bcb0c063fa7c289c9d9c302b5b763e7c26cc Mon Sep 17 00:00:00 2001 From: sehraf Date: Fri, 4 May 2018 20:47:41 +0200 Subject: [PATCH 095/161] Add support for libsecret --- libretroshare/src/rsserver/rsloginhandler.cc | 98 ++++++++++++++++++-- retroshare.pri | 7 +- 2 files changed, 96 insertions(+), 9 deletions(-) diff --git a/libretroshare/src/rsserver/rsloginhandler.cc b/libretroshare/src/rsserver/rsloginhandler.cc index 453f5442c..ed952791f 100644 --- a/libretroshare/src/rsserver/rsloginhandler.cc +++ b/libretroshare/src/rsserver/rsloginhandler.cc @@ -109,6 +109,19 @@ GnomeKeyringPasswordSchema my_schema = { NULL, NULL }; +#elif defined(HAS_LIBSECRET) +#include +const SecretSchema *libsecret_get_schema(void) +{ + static const SecretSchema the_schema = { + "org.Retroshare.Password", SECRET_SCHEMA_NONE, + { + { "RetroShare SSL Id", SECRET_SCHEMA_ATTRIBUTE_STRING }, + { "NULL", (SecretSchemaAttributeType)0 }, + } + }; + return &the_schema; +} #endif @@ -209,7 +222,7 @@ bool RsLoginHandler::tryAutoLogin(const RsPeerId& ssl_id,std::string& ssl_passwd #endif if( gnome_keyring_find_password_sync(&my_schema, &passwd,"RetroShare SSL Id",ssl_id.toStdString().c_str(),NULL) == GNOME_KEYRING_RESULT_OK ) { - std::cerr << "Got SSL passwd ********************" /*<< passwd*/ << " from gnome keyring" << std::endl; + std::cout << "Got SSL passwd ********************" /*<< passwd*/ << " from gnome keyring" << std::endl; ssl_passwd = std::string(passwd); return true ; } @@ -220,7 +233,41 @@ bool RsLoginHandler::tryAutoLogin(const RsPeerId& ssl_id,std::string& ssl_passwd #endif return false ; } +#elif defined(HAS_LIBSECRET) + // do synchronous lookup +#ifdef DEBUG_RSLOGINHANDLER + std::cerr << "Using attribute: " << ssl_id << std::endl; +#endif + + GError *error = NULL; + gchar *password = secret_password_lookup_sync (libsecret_get_schema(), NULL, &error, + "RetroShare SSL Id", ssl_id.toStdString().c_str(), + NULL); + + if (error != NULL) { + g_error_free (error); +#ifdef DEBUG_RSLOGINHANDLER + std::cerr << "Could not get passwd using libsecret: error" << std::endl; +#endif + return false; + } else if (password == NULL) { + /* password will be null, if no matching password found */ +#ifdef DEBUG_RSLOGINHANDLER + std::cerr << "Could not get passwd using libsecret: not found" << std::endl; +#endif + return false; + } else { + std::cout << "Got SSL passwd ********************" /*<< passwd*/ << " using libsecret" << std::endl; + ssl_passwd = std::string(password); + + secret_password_free (password); + return true; + } +#ifdef DEBUG_RSLOGINHANDLER + std::cerr << "Could not get passwd from gnome keyring: unknown" << std::endl; +#endif + return false; #else /******************** OSX KeyChain stuff *****************************/ #ifdef __APPLE__ @@ -281,7 +328,7 @@ bool RsLoginHandler::tryAutoLogin(const RsPeerId& ssl_id,std::string& ssl_passwd /******************** OSX KeyChain stuff *****************************/ #endif // APPLE -#endif // HAS_GNOME_KEYRING +#endif // HAS_GNOME_KEYRING / HAS_LIBSECRET #else /******* WINDOWS BELOW *****/ /* try to load from file */ @@ -405,9 +452,9 @@ bool RsLoginHandler::enableAutoLogin(const RsPeerId& ssl_id,const std::string& s #ifndef __HAIKU__ #ifndef WINDOWS_SYS /* UNIX */ #if defined(HAS_GNOME_KEYRING) || defined(__FreeBSD__) || defined(__OpenBSD__) - if(GNOME_KEYRING_RESULT_OK == gnome_keyring_store_password_sync(&my_schema, NULL, (gchar*)("RetroShare password for SSL Id "+ssl_id.toStdString()).c_str(),(gchar*)ssl_passwd.c_str(),"RetroShare SSL Id",ssl_id.toStdString().c_str(),NULL)) + if(GNOME_KEYRING_RESULT_OK == gnome_keyring_store_password_sync(&my_schema, NULL, (gchar*)("RetroShare password for SSL Id "+ssl_id.toStdString()).c_str(),(gchar*)ssl_passwd.c_str(),"RetroShare SSL Id",ssl_id.toStdString().c_str(),NULL)) { - std::cerr << "Stored passwd " << "************************" << " into gnome keyring" << std::endl; + std::cout << "Stored passwd " << "************************" << " into gnome keyring" << std::endl; return true ; } else @@ -415,6 +462,24 @@ bool RsLoginHandler::enableAutoLogin(const RsPeerId& ssl_id,const std::string& s std::cerr << "Could not store passwd into gnome keyring" << std::endl; return false ; } +#elif defined(HAS_LIBSECRET) + // do synchronous store + + GError *error = NULL; + secret_password_store_sync (libsecret_get_schema(), SECRET_COLLECTION_DEFAULT, + (gchar*)("RetroShare password for SSL Id " + ssl_id.toStdString()).c_str(), + (gchar*)ssl_passwd.c_str(), + NULL, &error, + "RetroShare SSL Id", ssl_id.toStdString().c_str(), + NULL); + + if (error != NULL) { + g_error_free (error); + std::cerr << "Could not store passwd using libsecret" << std::endl; + return false; + } + std::cout << "Stored passwd " << "************************" << " using libsecret" << std::endl; + return true; #else #ifdef __APPLE__ /***************** OSX KEYCHAIN ****************/ @@ -484,7 +549,7 @@ bool RsLoginHandler::enableAutoLogin(const RsPeerId& ssl_id,const std::string& s return true; #endif // TODO_CODE_ROTTEN #endif // __APPLE__ -#endif // HAS_GNOME_KEYRING. +#endif // HAS_GNOME_KEYRING / HAS_LIBSECRET #else /* windows */ /* store password encrypted in a file */ @@ -571,7 +636,7 @@ bool RsLoginHandler::clearAutoLogin(const RsPeerId& ssl_id) #ifdef HAS_GNOME_KEYRING if(GNOME_KEYRING_RESULT_OK == gnome_keyring_delete_password_sync(&my_schema,"RetroShare SSL Id", ssl_id.toStdString().c_str(),NULL)) { - std::cerr << "Successfully Cleared gnome keyring passwd for SSLID " << ssl_id << std::endl; + std::cout << "Successfully Cleared gnome keyring passwd for SSLID " << ssl_id << std::endl; return true ; } else @@ -579,7 +644,26 @@ bool RsLoginHandler::clearAutoLogin(const RsPeerId& ssl_id) std::cerr << "Could not clear gnome keyring passwd for SSLID " << ssl_id << std::endl; return false ; } -#else +#elif defined(HAS_LIBSECRET) + // do synchronous clear + + GError *error = NULL; + gboolean removed = secret_password_clear_sync (libsecret_get_schema(), NULL, &error, + "RetroShare SSL Id", ssl_id.toStdString().c_str(), + NULL); + + if (error != NULL) { + g_error_free (error); + std::cerr << "Could not clearpasswd for SSLID " << ssl_id << " using libsecret: error" << std::endl; + return false ; + } else if (removed == FALSE) { + std::cerr << "Could not clearpasswd for SSLID " << ssl_id << " using libsecret: false" << std::endl; + return false ; + } + + std::cout << "Successfully Cleared passwd for SSLID " << ssl_id << " using libsecret" << std::endl; + return true ; +#else // HAS_GNOME_KEYRING / HAS_LIBSECRET #ifdef __APPLE__ std::cerr << "clearAutoLogin() OSX Version" << std::endl; diff --git a/retroshare.pri b/retroshare.pri index 89c6da7d2..440cceba9 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -340,8 +340,11 @@ linux-* { QMAKE_LIBDIR *= "$$RS_LIB_DIR" rs_autologin { - DEFINES *= HAS_GNOME_KEYRING - PKGCONFIG *= gnome-keyring-1 + #DEFINES *= HAS_GNOME_KEYRING + #PKGCONFIG *= gnome-keyring-1 + + DEFINES *= HAS_LIBSECRET + PKGCONFIG *= libsecret-1 } } From c89e36a665fa350dc1ca2b2795ed166243ee0910 Mon Sep 17 00:00:00 2001 From: sehraf Date: Fri, 4 May 2018 20:52:31 +0200 Subject: [PATCH 096/161] add auto selection of libsecret with fallback to libgnome-keyring --- retroshare.pri | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/retroshare.pri b/retroshare.pri index 440cceba9..6f2b371ee 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -340,11 +340,17 @@ linux-* { QMAKE_LIBDIR *= "$$RS_LIB_DIR" rs_autologin { - #DEFINES *= HAS_GNOME_KEYRING - #PKGCONFIG *= gnome-keyring-1 - - DEFINES *= HAS_LIBSECRET - PKGCONFIG *= libsecret-1 + # try libsecret first since it is not limited to gnome keyring and libgnome-keyring is deprecated + LIBSECRET_AVAILABLE = $$system(pkg-config --exists libsecret-1 && echo yes) + isEmpty(LIBSECRET_AVAILABLE) { + message("using libgnome-keyring for auto login") + DEFINES *= HAS_GNOME_KEYRING + PKGCONFIG *= gnome-keyring-1 + } else { + message("using libsecret for auto login") + DEFINES *= HAS_LIBSECRET + PKGCONFIG *= libsecret-1 + } } } From dca33daae84f7c8f651c566e8b8b06c21db19c1b Mon Sep 17 00:00:00 2001 From: Phenom Date: Sun, 6 May 2018 18:53:29 +0200 Subject: [PATCH 097/161] Fix CppCheck in ftcontroller.cc /libretroshare/src/ft/ftcontroller.cc:91: warning: Cppcheck(passedByValue): Function parameter 'fname' should be passed by reference. /libretroshare/src/ft/ftcontroller.cc:92: warning: Cppcheck(passedByValue): Function parameter 'tmppath' should be passed by reference. /libretroshare/src/ft/ftcontroller.cc:92: warning: Cppcheck(passedByValue): Function parameter 'dest' should be passed by reference. /libretroshare/src/ft/ftcontroller.cc:1482: warning: Cppcheck(stlIfStrFind): Inefficient usage of string::find() in condition; string::compare() would be faster. /libretroshare/src/ft/ftcontroller.cc:1491: warning: Cppcheck(stlIfStrFind): Inefficient usage of string::find() in condition; string::compare() would be faster. --- libretroshare/src/ft/ftcontroller.cc | 21 +++++++++------------ libretroshare/src/ft/ftcontroller.h | 6 +++--- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/libretroshare/src/ft/ftcontroller.cc b/libretroshare/src/ft/ftcontroller.cc index 0cb236358..f1500036f 100644 --- a/libretroshare/src/ft/ftcontroller.cc +++ b/libretroshare/src/ft/ftcontroller.cc @@ -88,16 +88,13 @@ ftFileControl::ftFileControl() return; } -ftFileControl::ftFileControl(std::string fname, - std::string tmppath, std::string dest, - uint64_t size, const RsFileHash &hash, TransferRequestFlags flags, - ftFileCreator *fc, ftTransferModule *tm) - :mName(fname), mCurrentPath(tmppath), mDestination(dest), - mTransfer(tm), mCreator(fc), mState(DOWNLOADING), mHash(hash), - mSize(size), mFlags(flags), mCreateTime(0), mQueuePriority(0), mQueuePosition(0) -{ - return; -} +ftFileControl::ftFileControl(const std::string& fname, const std::string& tmppath, const std::string& dest + , uint64_t size, const RsFileHash &hash, TransferRequestFlags flags + , ftFileCreator *fc, ftTransferModule *tm) + : mName(fname), mCurrentPath(tmppath), mDestination(dest) + , mTransfer(tm), mCreator(fc), mState(DOWNLOADING), mHash(hash) + , mSize(size), mFlags(flags), mCreateTime(0), mQueuePriority(0), mQueuePosition(0) +{} ftController::ftController(ftDataMultiplex *dm, p3ServiceControl *sc, uint32_t ftServiceId) : p3Config(), @@ -1479,7 +1476,7 @@ bool ftController::setPartialsDirectory(std::string path) path = RsDirUtil::convertPathToUnix(path); - if (!path.find(mDownloadPath)) { + if (path.find(mDownloadPath) == std::string::npos) { return false; } @@ -1488,7 +1485,7 @@ bool ftController::setPartialsDirectory(std::string path) std::list dirs; rsFiles->getSharedDirectories(dirs); for (it = dirs.begin(); it != dirs.end(); ++it) { - if (!path.find((*it).filename)) { + if (path.find((*it).filename) == std::string::npos) { return false; } } diff --git a/libretroshare/src/ft/ftcontroller.h b/libretroshare/src/ft/ftcontroller.h index 67b080843..5a9140267 100644 --- a/libretroshare/src/ft/ftcontroller.h +++ b/libretroshare/src/ft/ftcontroller.h @@ -73,9 +73,9 @@ class ftFileControl }; ftFileControl(); - ftFileControl(std::string fname, std::string tmppath, std::string dest, - uint64_t size, const RsFileHash& hash, TransferRequestFlags flags, - ftFileCreator *fc, ftTransferModule *tm); + ftFileControl( const std::string& fname, const std::string& tmppath, const std::string& dest + , uint64_t size, const RsFileHash& hash, TransferRequestFlags flags + , ftFileCreator *fc, ftTransferModule *tm); std::string mName; std::string mCurrentPath; /* current full path (including name) */ From d9a75a93622204fe1698f10c67259697c2a3aac7 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Sun, 29 Apr 2018 20:40:05 +0200 Subject: [PATCH 098/161] Added RapidJSON to Windows build environment --- build_scripts/Windows/build-libs/Makefile | 41 ++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/build_scripts/Windows/build-libs/Makefile b/build_scripts/Windows/build-libs/Makefile index 4d7339149..8918576a2 100755 --- a/build_scripts/Windows/build-libs/Makefile +++ b/build_scripts/Windows/build-libs/Makefile @@ -12,6 +12,7 @@ TCL_VERSION=8.6.2 SQLCIPHER_VERSION=2.2.1 LIBMICROHTTPD_VERSION=0.9.59 FFMPEG_VERSION=3.4 +RAPIDJSON_VERSION=1.1.0 MAKEFILE_PATH=$(dir $(MAKEFILE_LIST)) LIBS_PATH?=$(MAKEFILE_PATH)../../../../libs @@ -19,7 +20,7 @@ LIBS_PATH?=$(MAKEFILE_PATH)../../../../libs DOWNLOAD_PATH?=download COPY_ANSWER?= -all: dirs zlib bzip2 miniupnpc openssl speex speexdsp opencv libxml2 libxslt curl sqlcipher libmicrohttpd ffmpeg copylibs +all: dirs zlib bzip2 miniupnpc openssl speex speexdsp opencv libxml2 libxslt curl sqlcipher libmicrohttpd ffmpeg rapidjson copylibs download: \ $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz \ @@ -35,7 +36,8 @@ download: \ $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz \ $(DOWNLOAD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tar.gz \ $(DOWNLOAD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz \ - $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz + $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz \ + $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz clean: rm -r -f libs @@ -64,6 +66,7 @@ libs/zlib-$(ZLIB_VERSION): $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz cp zlib-$(ZLIB_VERSION)/zconf.h libs/zlib-$(ZLIB_VERSION).tmp/include/ mkdir -p libs/zlib-$(ZLIB_VERSION).tmp/lib cp zlib-$(ZLIB_VERSION)/libz.a libs/zlib-$(ZLIB_VERSION).tmp/lib/ + # cleanup rm -r -f zlib-$(ZLIB_VERSION) mv libs/zlib-$(ZLIB_VERSION).tmp libs/zlib-$(ZLIB_VERSION) @@ -84,6 +87,7 @@ libs/bzip2-$(BZIP2_VERSION): $(DOWNLOAD_PATH)/bzip2-$(BZIP2_VERSION).tar.gz cp bzip2-$(BZIP2_VERSION)/bzlib.h libs/bzip2-$(BZIP2_VERSION).tmp/include/ mkdir -p libs/bzip2-$(BZIP2_VERSION).tmp/lib cp bzip2-$(BZIP2_VERSION)/libbz2.a libs/bzip2-$(BZIP2_VERSION).tmp/lib/ + # cleanup rm -r -f bzip2-$(BZIP2_VERSION) mv libs/bzip2-$(BZIP2_VERSION).tmp libs/bzip2-$(BZIP2_VERSION) @@ -105,6 +109,7 @@ libs/miniupnpc-$(MINIUPNPC_VERSION): $(DOWNLOAD_PATH)/miniupnpc-$(MINIUPNPC_VERS cp miniupnpc-$(MINIUPNPC_VERSION)/miniupnpc.lib libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/lib/ mkdir -p libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/bin cp miniupnpc-$(MINIUPNPC_VERSION)/miniupnpc.dll libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/bin/ + # cleanup rm -r -f miniupnpc-$(MINIUPNPC_VERSION) mv libs/miniupnpc-$(MINIUPNPC_VERSION).tmp libs/miniupnpc-$(MINIUPNPC_VERSION) @@ -131,6 +136,7 @@ libs/openssl-$(OPENSSL_VERSION): $(DOWNLOAD_PATH)/openssl-$(OPENSSL_VERSION).tar mkdir -p libs/openssl-$(OPENSSL_VERSION).tmp/lib cp openssl-$(OPENSSL_VERSION)/libcrypto.dll.a libs/openssl-$(OPENSSL_VERSION).tmp/lib/ cp openssl-$(OPENSSL_VERSION)/libssl.dll.a libs/openssl-$(OPENSSL_VERSION).tmp/lib/ + # cleanup rm -r -f openssl-$(OPENSSL_VERSION) mv libs/openssl-$(OPENSSL_VERSION).tmp libs/openssl-$(OPENSSL_VERSION) @@ -152,6 +158,7 @@ libs/speex-$(SPEEX_VERSION): $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz cp speex-$(SPEEX_VERSION)/include/speex/*.h libs/speex-$(SPEEX_VERSION).tmp/include/speex/ mkdir -p libs/speex-$(SPEEX_VERSION).tmp/lib cp speex-$(SPEEX_VERSION)/libspeex/.libs/libspeex.a libs/speex-$(SPEEX_VERSION).tmp/lib + # cleanup rm -r -f speex-$(SPEEX_VERSION) mv libs/speex-$(SPEEX_VERSION).tmp libs/speex-$(SPEEX_VERSION) @@ -172,6 +179,7 @@ libs/speexdsp-$(SPEEXDSP_VERSION): $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION) cp speexdsp-$(SPEEXDSP_VERSION)/include/speex/*.h libs/speexdsp-$(SPEEXDSP_VERSION).tmp/include/speex/ mkdir -p libs/speexdsp-$(SPEEXDSP_VERSION).tmp/lib cp speexdsp-$(SPEEXDSP_VERSION)/libspeexdsp/.libs/libspeexdsp.a libs/speexdsp-$(SPEEXDSP_VERSION).tmp/lib + # cleanup rm -r -f speexdsp-$(SPEEXDSP_VERSION) mv libs/speexdsp-$(SPEEXDSP_VERSION).tmp libs/speexdsp-$(SPEEXDSP_VERSION) @@ -194,6 +202,7 @@ libs/opencv-$(OPENCV_VERSION): $(DOWNLOAD_PATH)/opencv-$(OPENCV_VERSION).tar.gz cp -r opencv-$(OPENCV_VERSION)/build/install/include/* libs/opencv-$(OPENCV_VERSION).tmp/include/ mkdir -p libs/opencv-$(OPENCV_VERSION).tmp/lib/opencv cp -r opencv-$(OPENCV_VERSION)/build/install/x86/mingw/staticlib/* libs/opencv-$(OPENCV_VERSION).tmp/lib/opencv/ + # cleanup rm -r -f opencv-$(OPENCV_VERSION) mv libs/opencv-$(OPENCV_VERSION).tmp libs/opencv-$(OPENCV_VERSION) @@ -215,6 +224,7 @@ libs/libxml2-$(LIBXML2_VERSION): $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar cp libxml2-$(LIBXML2_VERSION)/include/libxml/*.h libs/libxml2-$(LIBXML2_VERSION).tmp/include/libxml/ mkdir -p libs/libxml2-$(LIBXML2_VERSION).tmp/lib cp libxml2-$(LIBXML2_VERSION)/.libs/libxml2.a libs/libxml2-$(LIBXML2_VERSION).tmp/lib/ + # cleanup #rm -r -f libxml2-$(LIBXML2_VERSION) # see libxslt mv libs/libxml2-$(LIBXML2_VERSION).tmp libs/libxml2-$(LIBXML2_VERSION) @@ -237,6 +247,7 @@ libs/libxslt-$(LIBXSLT_VERSION): $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar mkdir -p libs/libxslt-$(LIBXSLT_VERSION).tmp/lib cp libxslt-$(LIBXSLT_VERSION)/libxslt/.libs/libxslt.a libs/libxslt-$(LIBXSLT_VERSION).tmp/lib/ cp libxslt-$(LIBXSLT_VERSION)/libexslt/.libs/libexslt.a libs/libxslt-$(LIBXSLT_VERSION).tmp/lib/ + # cleanup rm -r -f libxml2-$(LIBXML2_VERSION) rm -r -f libxslt-$(LIBXSLT_VERSION) mv libs/libxslt-$(LIBXSLT_VERSION).tmp libs/libxslt-$(LIBXSLT_VERSION) @@ -259,6 +270,7 @@ libs/curl-$(CURL_VERSION): $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz cp curl-$(CURL_VERSION)/include/curl/*.h libs/curl-$(CURL_VERSION).tmp/include/curl/ mkdir -p libs/curl-$(CURL_VERSION).tmp/lib cp curl-$(CURL_VERSION)/lib/.libs/libcurl.a libs/curl-$(CURL_VERSION).tmp/lib/ + # cleanup rm -r -f curl-$(CURL_VERSION) mv libs/curl-$(CURL_VERSION).tmp libs/curl-$(CURL_VERSION) @@ -292,6 +304,7 @@ libs/sqlcipher-$(SQLCIPHER_VERSION): $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar. cp sqlcipher-$(SQLCIPHER_VERSION)/install/lib/libsqlcipher.a libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/lib/ mkdir -p libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/bin cp sqlcipher-$(SQLCIPHER_VERSION)/install/bin/sqlcipher.exe libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/bin/ + # cleanup rm -r -f sqlcipher-$(SQLCIPHER_VERSION) rm -r -f tcl$(TCL_VERSION) mv libs/sqlcipher-$(SQLCIPHER_VERSION).tmp libs/sqlcipher-$(SQLCIPHER_VERSION) @@ -308,7 +321,7 @@ libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION): $(DOWNLOAD_PATH)/libmicrohttpd-$(LI # build cd libmicrohttpd-$(LIBMICROHTTPD_VERSION) && ./configure --disable-shared --enable-static --prefix="`pwd`/../libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tmp" cd libmicrohttpd-$(LIBMICROHTTPD_VERSION) && make install - # copy files + # cleanup rm -r -f libmicrohttpd-$(LIBMICROHTTPD_VERSION) mv libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tmp libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION) @@ -324,10 +337,30 @@ libs/ffmpeg-$(FFMPEG_VERSION): $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz # build cd ffmpeg-$(FFMPEG_VERSION) && ./configure --disable-shared --enable-static --disable-programs --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc --disable-htmlpages --disable-manpages --disable-podpages --disable-txtpages --disable-yasm --disable-everything --enable-encoder=mpeg4 --enable-decoder=mpeg4 --prefix="`pwd`/../libs/ffmpeg-$(FFMPEG_VERSION).tmp" cd ffmpeg-$(FFMPEG_VERSION) && make install - # copy files + # cleanup rm -r -f ffmpeg-$(FFMPEG_VERSION) mv libs/ffmpeg-$(FFMPEG_VERSION).tmp libs/ffmpeg-$(FFMPEG_VERSION) +rapidjson: libs/rapidjson-$(RAPIDJSON_VERSION) + +$(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz: + wget --no-check-certificate https://github.com/Tencent/rapidjson/archive/v$(RAPIDJSON_VERSION).tar.gz -O $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz + +libs/rapidjson-$(RAPIDJSON_VERSION): $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz + # prepare + rm -r -f libs/rapidjson-* + tar xvf $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz + # build + #mkdir -p rapidjson-$(RAPIDJSON_VERSION)/build + #cd rapidjson-$(RAPIDJSON_VERSION)/build && cmake .. -G"MSYS Makefiles" + #cd rapidjson-$(RAPIDJSON_VERSION)/build && make + # copy files + mkdir -p libs/rapidjson-$(RAPIDJSON_VERSION).tmp/include + cp -r rapidjson-$(RAPIDJSON_VERSION)/include/* libs/rapidjson-$(RAPIDJSON_VERSION).tmp/include/ + # cleanup + rm -r -f rapidjson-$(RAPIDJSON_VERSION) + mv libs/rapidjson-$(RAPIDJSON_VERSION).tmp libs/rapidjson-$(RAPIDJSON_VERSION) + copylibs: if [ "$(COPY_ANSWER)" = "" ] ; then \ read -p "Do you want to copy libs to retroshare? (y|n)" answer; \ From 38ac23486281b90ede6d8eb2c07c57b1910c4334 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Mon, 7 May 2018 07:30:29 +0200 Subject: [PATCH 099/161] Fixed Windows compile with pre-compiled libraries Added new variable EXTERNAL_LIB_DIR to specify path of external libraries --- plugins/VOIP/VOIP.pro | 2 +- retroshare.pri | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/plugins/VOIP/VOIP.pro b/plugins/VOIP/VOIP.pro index c0dc92e7e..a1102b78e 100644 --- a/plugins/VOIP/VOIP.pro +++ b/plugins/VOIP/VOIP.pro @@ -43,7 +43,7 @@ win32 { OPENCV_VERSION = "341" USE_PRECOMPILED_LIBS = - for(lib, LIB_DIR) { + for(lib, RS_LIB_DIR) { #message(Scanning $$lib) exists( $$lib/opencv/libopencv_core$${OPENCV_VERSION}.a) { isEmpty(USE_PRECOMPILED_LIBS) { diff --git a/retroshare.pri b/retroshare.pri index 6f2b371ee..4d36fd088 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -375,6 +375,11 @@ android-* { } win32-g++ { + !isEmpty(EXTERNAL_LIB_DIR) { + message(Use pre-compiled libraries in $${EXTERNAL_LIB_DIR}.) + PREFIX = $$system_path($$EXTERNAL_LIB_DIR) + } + PREFIX_MSYS2 = $$(MINGW_PREFIX) isEmpty(PREFIX_MSYS2) { message("MINGW_PREFIX is not set, attempting MSYS2 autodiscovery.") @@ -389,22 +394,24 @@ win32-g++ { PREFIX_MSYS2=$${TEMPTATIVE_MSYS2} } - isEmpty(PREFIX_MSYS2) { - error(Cannot find MSYS2 please set MINGW_PREFIX) - } else { + !isEmpty(PREFIX_MSYS2) { message(Found MSYS2: $${PREFIX_MSYS2}) + + isEmpty(PREFIX) { + PREFIX = $$system_path($${PREFIX_MSYS2}) + } } } isEmpty(PREFIX) { - PREFIX = $$system_path($${PREFIX_MSYS2}) + error(PREFIX is not set. Set either EXTERNAL_LIB_DIR or PREFIX_MSYS2.) } INCLUDEPATH *= $$system_path($${PREFIX}/include) - INCLUDEPATH *= $$system_path($${PREFIX_MSYS2}/include) + !isEmpty(PREFIX_MSYS2) : INCLUDEPATH *= $$system_path($${PREFIX_MSYS2}/include) QMAKE_LIBDIR *= $$system_path($${PREFIX}/lib) - QMAKE_LIBDIR *= $$system_path($${PREFIX_MSYS2}/lib) + !isEmpty(PREFIX_MSYS2) : QMAKE_LIBDIR *= $$system_path($${PREFIX_MSYS2}/lib) RS_BIN_DIR = $$system_path($${PREFIX}/bin) RS_INCLUDE_DIR = $$system_path($${PREFIX}/include) From 135263180744a186e2f5dd21ab46051a73f8bd5c Mon Sep 17 00:00:00 2001 From: thunder2 Date: Mon, 7 May 2018 06:42:52 +0200 Subject: [PATCH 100/161] Updated Windows build environment --- build_scripts/Windows/build-libs/Makefile | 274 +++++++++--------- .../Windows/build-libs/build-libs.bat | 6 +- build_scripts/Windows/build-retrotor.bat | 2 +- build_scripts/Windows/build.bat | 2 +- .../Windows/build/build-installer.bat | 19 +- build_scripts/Windows/build/build.bat | 23 +- build_scripts/Windows/build/env.bat | 14 +- build_scripts/Windows/build/pack.bat | 21 +- .../Windows/env/tools/prepare-msys.bat | 3 +- .../Windows/env/tools/prepare-tools.bat | 26 +- 10 files changed, 191 insertions(+), 199 deletions(-) diff --git a/build_scripts/Windows/build-libs/Makefile b/build_scripts/Windows/build-libs/Makefile index 8918576a2..6ed67ed56 100755 --- a/build_scripts/Windows/build-libs/Makefile +++ b/build_scripts/Windows/build-libs/Makefile @@ -14,11 +14,9 @@ LIBMICROHTTPD_VERSION=0.9.59 FFMPEG_VERSION=3.4 RAPIDJSON_VERSION=1.1.0 -MAKEFILE_PATH=$(dir $(MAKEFILE_LIST)) -LIBS_PATH?=$(MAKEFILE_PATH)../../../../libs - DOWNLOAD_PATH?=download -COPY_ANSWER?= +BUILD_PATH=build +LIBS_PATH?=libs all: dirs zlib bzip2 miniupnpc openssl speex speexdsp opencv libxml2 libxslt curl sqlcipher libmicrohttpd ffmpeg rapidjson copylibs @@ -40,241 +38,242 @@ download: \ $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz clean: - rm -r -f libs + rm -r -f $(BUILD_PATH) + rm -r -f $(LIBS_PATH) dirs: mkdir -p $(DOWNLOAD_PATH) - mkdir -p libs - gcc --version | head --lines 1 | tr ' ' '\n' | tail -1 >libs/gcc-version + mkdir -p $(BUILD_PATH) + gcc --version | head --lines 1 | tr ' ' '\n' | tail -1 >$(BUILD_PATH)/gcc-version $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz: wget --no-check-certificate http://sourceforge.net/projects/libpng/files/zlib/$(ZLIB_VERSION)/zlib-$(ZLIB_VERSION).tar.gz/download -O $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz -zlib: libs/zlib-$(ZLIB_VERSION) +zlib: $(BUILD_PATH)/zlib-$(ZLIB_VERSION) -libs/zlib-$(ZLIB_VERSION): $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz +$(BUILD_PATH)/zlib-$(ZLIB_VERSION): $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz # prepare - rm -r -f libs/zlib-* + rm -r -f $(BUILD_PATH)/zlib-* tar xvf $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz # build cd zlib-$(ZLIB_VERSION) && ./configure - #cd zlib-$(ZLIB_VERSION) && make install prefix="`pwd`/../libs" + #cd zlib-$(ZLIB_VERSION) && make install prefix="`pwd`/../$(BUILD_PATH)" cd zlib-$(ZLIB_VERSION) && make # copy files - mkdir -p libs/zlib-$(ZLIB_VERSION).tmp/include - cp zlib-$(ZLIB_VERSION)/zlib.h libs/zlib-$(ZLIB_VERSION).tmp/include/ - cp zlib-$(ZLIB_VERSION)/zconf.h libs/zlib-$(ZLIB_VERSION).tmp/include/ - mkdir -p libs/zlib-$(ZLIB_VERSION).tmp/lib - cp zlib-$(ZLIB_VERSION)/libz.a libs/zlib-$(ZLIB_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/zlib-$(ZLIB_VERSION).tmp/include + cp zlib-$(ZLIB_VERSION)/zlib.h $(BUILD_PATH)/zlib-$(ZLIB_VERSION).tmp/include/ + cp zlib-$(ZLIB_VERSION)/zconf.h $(BUILD_PATH)/zlib-$(ZLIB_VERSION).tmp/include/ + mkdir -p $(BUILD_PATH)/zlib-$(ZLIB_VERSION).tmp/lib + cp zlib-$(ZLIB_VERSION)/libz.a $(BUILD_PATH)/zlib-$(ZLIB_VERSION).tmp/lib/ # cleanup rm -r -f zlib-$(ZLIB_VERSION) - mv libs/zlib-$(ZLIB_VERSION).tmp libs/zlib-$(ZLIB_VERSION) + mv $(BUILD_PATH)/zlib-$(ZLIB_VERSION).tmp $(BUILD_PATH)/zlib-$(ZLIB_VERSION) -bzip2: libs/bzip2-$(BZIP2_VERSION) +bzip2: $(BUILD_PATH)/bzip2-$(BZIP2_VERSION) $(DOWNLOAD_PATH)/bzip2-$(BZIP2_VERSION).tar.gz: wget http://bzip.org/$(BZIP2_VERSION)/bzip2-$(BZIP2_VERSION).tar.gz -O $(DOWNLOAD_PATH)/bzip2-$(BZIP2_VERSION).tar.gz -libs/bzip2-$(BZIP2_VERSION): $(DOWNLOAD_PATH)/bzip2-$(BZIP2_VERSION).tar.gz +$(BUILD_PATH)/bzip2-$(BZIP2_VERSION): $(DOWNLOAD_PATH)/bzip2-$(BZIP2_VERSION).tar.gz # prepare - rm -r -f libs/bzip2-* + rm -r -f $(BUILD_PATH)/bzip2-* tar xvf $(DOWNLOAD_PATH)/bzip2-$(BZIP2_VERSION).tar.gz # build - #cd bzip2-$(BZIP2_VERSION) && make install PREFIX="`pwd`/../libs" + #cd bzip2-$(BZIP2_VERSION) && make install PREFIX="`pwd`/../$(BUILD_PATH)" cd bzip2-$(BZIP2_VERSION) && make # copy files - mkdir -p libs/bzip2-$(BZIP2_VERSION).tmp/include - cp bzip2-$(BZIP2_VERSION)/bzlib.h libs/bzip2-$(BZIP2_VERSION).tmp/include/ - mkdir -p libs/bzip2-$(BZIP2_VERSION).tmp/lib - cp bzip2-$(BZIP2_VERSION)/libbz2.a libs/bzip2-$(BZIP2_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/bzip2-$(BZIP2_VERSION).tmp/include + cp bzip2-$(BZIP2_VERSION)/bzlib.h $(BUILD_PATH)/bzip2-$(BZIP2_VERSION).tmp/include/ + mkdir -p $(BUILD_PATH)/bzip2-$(BZIP2_VERSION).tmp/lib + cp bzip2-$(BZIP2_VERSION)/libbz2.a $(BUILD_PATH)/bzip2-$(BZIP2_VERSION).tmp/lib/ # cleanup rm -r -f bzip2-$(BZIP2_VERSION) - mv libs/bzip2-$(BZIP2_VERSION).tmp libs/bzip2-$(BZIP2_VERSION) + mv $(BUILD_PATH)/bzip2-$(BZIP2_VERSION).tmp $(BUILD_PATH)/bzip2-$(BZIP2_VERSION) -miniupnpc: libs/miniupnpc-$(MINIUPNPC_VERSION) +miniupnpc: $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION) $(DOWNLOAD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tar.gz: wget http://miniupnp.free.fr/files/download.php?file=miniupnpc-$(MINIUPNPC_VERSION).tar.gz -O $(DOWNLOAD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tar.gz -libs/miniupnpc-$(MINIUPNPC_VERSION): $(DOWNLOAD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tar.gz +$(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION): $(DOWNLOAD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tar.gz # prepare - rm -r -f libs/miniupnpc-* + rm -r -f $(BUILD_PATH)/miniupnpc-* tar xvf $(DOWNLOAD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tar.gz # build cd miniupnpc-$(MINIUPNPC_VERSION) && export CC=gcc && make -f Makefile.mingw init libminiupnpc.a miniupnpc.dll # copy files - mkdir -p libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/include/miniupnpc - cp miniupnpc-$(MINIUPNPC_VERSION)/*.h libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/include/miniupnpc/ - mkdir -p libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/lib - cp miniupnpc-$(MINIUPNPC_VERSION)/miniupnpc.lib libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/lib/ - mkdir -p libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/bin - cp miniupnpc-$(MINIUPNPC_VERSION)/miniupnpc.dll libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/bin/ + mkdir -p $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp/include/miniupnpc + cp miniupnpc-$(MINIUPNPC_VERSION)/*.h $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp/include/miniupnpc/ + mkdir -p $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp/lib + cp miniupnpc-$(MINIUPNPC_VERSION)/miniupnpc.lib $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp/bin + cp miniupnpc-$(MINIUPNPC_VERSION)/miniupnpc.dll $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp/bin/ # cleanup rm -r -f miniupnpc-$(MINIUPNPC_VERSION) - mv libs/miniupnpc-$(MINIUPNPC_VERSION).tmp libs/miniupnpc-$(MINIUPNPC_VERSION) + mv $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION) -openssl: libs/openssl-$(OPENSSL_VERSION) +openssl: $(BUILD_PATH)/openssl-$(OPENSSL_VERSION) $(DOWNLOAD_PATH)/openssl-$(OPENSSL_VERSION).tar.gz: wget --no-check-certificate https://www.openssl.org/source/openssl-$(OPENSSL_VERSION).tar.gz -O $(DOWNLOAD_PATH)/openssl-$(OPENSSL_VERSION).tar.gz -libs/openssl-$(OPENSSL_VERSION): $(DOWNLOAD_PATH)/openssl-$(OPENSSL_VERSION).tar.gz +$(BUILD_PATH)/openssl-$(OPENSSL_VERSION): $(DOWNLOAD_PATH)/openssl-$(OPENSSL_VERSION).tar.gz # prepare - rm -r -f libs/openssl-* + rm -r -f $(BUILD_PATH)/openssl-* tar xvf $(DOWNLOAD_PATH)/openssl-$(OPENSSL_VERSION).tar.gz # build - #cd openssl-$(OPENSSL_VERSION) && ./config --prefix="`pwd`/../libs" + #cd openssl-$(OPENSSL_VERSION) && ./config --prefix="`pwd`/../$(BUILD_PATH)" #cd openssl-$(OPENSSL_VERSION) && make install cd openssl-$(OPENSSL_VERSION) && ./config shared cd openssl-$(OPENSSL_VERSION) && make # copy files - mkdir -p libs/openssl-$(OPENSSL_VERSION).tmp/include/openssl - cp openssl-$(OPENSSL_VERSION)/include/openssl/*.h libs/openssl-$(OPENSSL_VERSION).tmp/include/openssl/ - mkdir -p libs/openssl-$(OPENSSL_VERSION).tmp/bin - cp openssl-$(OPENSSL_VERSION)/libeay32.dll libs/openssl-$(OPENSSL_VERSION).tmp/bin/ - cp openssl-$(OPENSSL_VERSION)/ssleay32.dll libs/openssl-$(OPENSSL_VERSION).tmp/bin/ - mkdir -p libs/openssl-$(OPENSSL_VERSION).tmp/lib - cp openssl-$(OPENSSL_VERSION)/libcrypto.dll.a libs/openssl-$(OPENSSL_VERSION).tmp/lib/ - cp openssl-$(OPENSSL_VERSION)/libssl.dll.a libs/openssl-$(OPENSSL_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/include/openssl + cp openssl-$(OPENSSL_VERSION)/include/openssl/*.h $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/include/openssl/ + mkdir -p $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/bin + cp openssl-$(OPENSSL_VERSION)/libeay32.dll $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/bin/ + cp openssl-$(OPENSSL_VERSION)/ssleay32.dll $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/bin/ + mkdir -p $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/lib + cp openssl-$(OPENSSL_VERSION)/libcrypto.dll.a $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/lib/ + cp openssl-$(OPENSSL_VERSION)/libssl.dll.a $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/lib/ # cleanup rm -r -f openssl-$(OPENSSL_VERSION) - mv libs/openssl-$(OPENSSL_VERSION).tmp libs/openssl-$(OPENSSL_VERSION) + mv $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp $(BUILD_PATH)/openssl-$(OPENSSL_VERSION) -speex: libs/speex-$(SPEEX_VERSION) +speex: $(BUILD_PATH)/speex-$(SPEEX_VERSION) $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz: wget --no-check-certificate http://downloads.xiph.org/releases/speex/speex-$(SPEEX_VERSION).tar.gz -O $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz -libs/speex-$(SPEEX_VERSION): $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz +$(BUILD_PATH)/speex-$(SPEEX_VERSION): $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz # prepare - rm -r -f libs/speex-* + rm -r -f $(BUILD_PATH)/speex-* tar xvf $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz # build cd speex-$(SPEEX_VERSION) && ./configure - #cd speex-$(SPEEX_VERSION) && make install exec_prefix="`pwd`/../libs" + #cd speex-$(SPEEX_VERSION) && make install exec_prefix="`pwd`/../$(BUILD_PATH)" cd speex-$(SPEEX_VERSION) && make # copy files - mkdir -p libs/speex-$(SPEEX_VERSION).tmp/include/speex - cp speex-$(SPEEX_VERSION)/include/speex/*.h libs/speex-$(SPEEX_VERSION).tmp/include/speex/ - mkdir -p libs/speex-$(SPEEX_VERSION).tmp/lib - cp speex-$(SPEEX_VERSION)/libspeex/.libs/libspeex.a libs/speex-$(SPEEX_VERSION).tmp/lib + mkdir -p $(BUILD_PATH)/speex-$(SPEEX_VERSION).tmp/include/speex + cp speex-$(SPEEX_VERSION)/include/speex/*.h $(BUILD_PATH)/speex-$(SPEEX_VERSION).tmp/include/speex/ + mkdir -p $(BUILD_PATH)/speex-$(SPEEX_VERSION).tmp/lib + cp speex-$(SPEEX_VERSION)/libspeex/.libs/libspeex.a $(BUILD_PATH)/speex-$(SPEEX_VERSION).tmp/lib # cleanup rm -r -f speex-$(SPEEX_VERSION) - mv libs/speex-$(SPEEX_VERSION).tmp libs/speex-$(SPEEX_VERSION) + mv $(BUILD_PATH)/speex-$(SPEEX_VERSION).tmp $(BUILD_PATH)/speex-$(SPEEX_VERSION) -speexdsp: libs/speexdsp-$(SPEEXDSP_VERSION) +speexdsp: $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION) $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz: wget --no-check-certificate http://downloads.xiph.org/releases/speex/speexdsp-$(SPEEXDSP_VERSION).tar.gz -O $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz -libs/speexdsp-$(SPEEXDSP_VERSION): $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz +$(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION): $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz # prepare - rm -r -f libs/speexdsp-* + rm -r -f $(BUILD_PATH)/speexdsp-* tar xvf $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz # build cd speexdsp-$(SPEEXDSP_VERSION) && ./configure cd speexdsp-$(SPEEXDSP_VERSION) && make # copy files - mkdir -p libs/speexdsp-$(SPEEXDSP_VERSION).tmp/include/speex - cp speexdsp-$(SPEEXDSP_VERSION)/include/speex/*.h libs/speexdsp-$(SPEEXDSP_VERSION).tmp/include/speex/ - mkdir -p libs/speexdsp-$(SPEEXDSP_VERSION).tmp/lib - cp speexdsp-$(SPEEXDSP_VERSION)/libspeexdsp/.libs/libspeexdsp.a libs/speexdsp-$(SPEEXDSP_VERSION).tmp/lib + mkdir -p $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tmp/include/speex + cp speexdsp-$(SPEEXDSP_VERSION)/include/speex/*.h $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tmp/include/speex/ + mkdir -p $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tmp/lib + cp speexdsp-$(SPEEXDSP_VERSION)/libspeexdsp/.libs/libspeexdsp.a $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tmp/lib # cleanup rm -r -f speexdsp-$(SPEEXDSP_VERSION) - mv libs/speexdsp-$(SPEEXDSP_VERSION).tmp libs/speexdsp-$(SPEEXDSP_VERSION) + mv $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tmp $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION) -opencv: libs/opencv-$(OPENCV_VERSION) +opencv: $(BUILD_PATH)/opencv-$(OPENCV_VERSION) $(DOWNLOAD_PATH)/opencv-$(OPENCV_VERSION).tar.gz: wget --no-check-certificate https://github.com/opencv/opencv/archive/$(OPENCV_VERSION).tar.gz -O $(DOWNLOAD_PATH)/opencv-$(OPENCV_VERSION).tar.gz -libs/opencv-$(OPENCV_VERSION): $(DOWNLOAD_PATH)/opencv-$(OPENCV_VERSION).tar.gz +$(BUILD_PATH)/opencv-$(OPENCV_VERSION): $(DOWNLOAD_PATH)/opencv-$(OPENCV_VERSION).tar.gz # prepare - rm -r -f libs/opencv-* + rm -r -f $(BUILD_PATH)/opencv-* tar xvf $(DOWNLOAD_PATH)/opencv-$(OPENCV_VERSION).tar.gz # build mkdir -p opencv-$(OPENCV_VERSION)/build - #cd opencv-$(OPENCV_VERSION)/build && cmake .. -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX="`pwd`/../../libs" + #cd opencv-$(OPENCV_VERSION)/build && cmake .. -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX="`pwd`/../../build" cd opencv-$(OPENCV_VERSION)/build && cmake .. -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DBUILD_SHARED_LIBS=OFF -DENABLE_CXX11=ON -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -DSTRSAFE_NO_DEPRECATE" -DCMAKE_INSTALL_PREFIX="`pwd`/install" cd opencv-$(OPENCV_VERSION)/build && make install # copy files - mkdir -p libs/opencv-$(OPENCV_VERSION).tmp/include - cp -r opencv-$(OPENCV_VERSION)/build/install/include/* libs/opencv-$(OPENCV_VERSION).tmp/include/ - mkdir -p libs/opencv-$(OPENCV_VERSION).tmp/lib/opencv - cp -r opencv-$(OPENCV_VERSION)/build/install/x86/mingw/staticlib/* libs/opencv-$(OPENCV_VERSION).tmp/lib/opencv/ + mkdir -p $(BUILD_PATH)/opencv-$(OPENCV_VERSION).tmp/include + cp -r opencv-$(OPENCV_VERSION)/build/install/include/* $(BUILD_PATH)/opencv-$(OPENCV_VERSION).tmp/include/ + mkdir -p $(BUILD_PATH)/opencv-$(OPENCV_VERSION).tmp/lib/opencv + cp -r opencv-$(OPENCV_VERSION)/build/install/x86/mingw/staticlib/* $(BUILD_PATH)/opencv-$(OPENCV_VERSION).tmp/lib/opencv/ # cleanup rm -r -f opencv-$(OPENCV_VERSION) - mv libs/opencv-$(OPENCV_VERSION).tmp libs/opencv-$(OPENCV_VERSION) + mv $(BUILD_PATH)/opencv-$(OPENCV_VERSION).tmp $(BUILD_PATH)/opencv-$(OPENCV_VERSION) -libxml2: libs/libxml2-$(LIBXML2_VERSION) +libxml2: $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION) $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz: wget ftp://xmlsoft.org/libxml2/libxml2-$(LIBXML2_VERSION).tar.gz -O $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz -libs/libxml2-$(LIBXML2_VERSION): $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz +$(BUILD_PATH)/libxml2-$(LIBXML2_VERSION): $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz # prepare - rm -r -f libs/libxml2-* + rm -r -f $(BUILD_PATH)/libxml2-* tar xvf $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz # build cd libxml2-$(LIBXML2_VERSION) && ./configure --without-iconv -enable-shared=no - #cd libxml2-$(LIBXML2_VERSION) && make install exec_prefix="`pwd`/../libs" + #cd libxml2-$(LIBXML2_VERSION) && make install exec_prefix="`pwd`/../$(BUILD_PATH)" cd libxml2-$(LIBXML2_VERSION) && make # copy files - mkdir -p libs/libxml2-$(LIBXML2_VERSION).tmp/include/libxml - cp libxml2-$(LIBXML2_VERSION)/include/libxml/*.h libs/libxml2-$(LIBXML2_VERSION).tmp/include/libxml/ - mkdir -p libs/libxml2-$(LIBXML2_VERSION).tmp/lib - cp libxml2-$(LIBXML2_VERSION)/.libs/libxml2.a libs/libxml2-$(LIBXML2_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION).tmp/include/libxml + cp libxml2-$(LIBXML2_VERSION)/include/libxml/*.h $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION).tmp/include/libxml/ + mkdir -p $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION).tmp/lib + cp libxml2-$(LIBXML2_VERSION)/.libs/libxml2.a $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION).tmp/lib/ # cleanup #rm -r -f libxml2-$(LIBXML2_VERSION) # see libxslt - mv libs/libxml2-$(LIBXML2_VERSION).tmp libs/libxml2-$(LIBXML2_VERSION) + mv $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION).tmp $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION) -libxslt: libs/libxslt-$(LIBXSLT_VERSION) +libxslt: $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION) $(DOWNLOAD_PATH)/libxslt-$(LIBXSLT_VERSION).tar.gz: wget ftp://xmlsoft.org/libxml2/libxslt-$(LIBXSLT_VERSION).tar.gz -O $(DOWNLOAD_PATH)/libxslt-$(LIBXSLT_VERSION).tar.gz -libs/libxslt-$(LIBXSLT_VERSION): $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz $(DOWNLOAD_PATH)/libxslt-$(LIBXSLT_VERSION).tar.gz +$(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION): $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz $(DOWNLOAD_PATH)/libxslt-$(LIBXSLT_VERSION).tar.gz # prepare - rm -r -f libs/libxslt-* + rm -r -f $(BUILD_PATH)/libxslt-* tar xvf $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz tar xvf $(DOWNLOAD_PATH)/libxslt-$(LIBXSLT_VERSION).tar.gz # build cd libxslt-$(LIBXSLT_VERSION) && ./configure --with-libxml-src=../libxml2-$(LIBXML2_VERSION) -enable-shared=no CFLAGS=-DLIBXML_STATIC cd libxslt-$(LIBXSLT_VERSION) && make # copy files - mkdir -p libs/libxslt-$(LIBXSLT_VERSION).tmp/include/libxslt - cp libxslt-$(LIBXSLT_VERSION)/libxslt/*.h libs/libxslt-$(LIBXSLT_VERSION).tmp/include/libxslt/ - mkdir -p libs/libxslt-$(LIBXSLT_VERSION).tmp/lib - cp libxslt-$(LIBXSLT_VERSION)/libxslt/.libs/libxslt.a libs/libxslt-$(LIBXSLT_VERSION).tmp/lib/ - cp libxslt-$(LIBXSLT_VERSION)/libexslt/.libs/libexslt.a libs/libxslt-$(LIBXSLT_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION).tmp/include/libxslt + cp libxslt-$(LIBXSLT_VERSION)/libxslt/*.h $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION).tmp/include/libxslt/ + mkdir -p $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION).tmp/lib + cp libxslt-$(LIBXSLT_VERSION)/libxslt/.libs/libxslt.a $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION).tmp/lib/ + cp libxslt-$(LIBXSLT_VERSION)/libexslt/.libs/libexslt.a $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION).tmp/lib/ # cleanup rm -r -f libxml2-$(LIBXML2_VERSION) rm -r -f libxslt-$(LIBXSLT_VERSION) - mv libs/libxslt-$(LIBXSLT_VERSION).tmp libs/libxslt-$(LIBXSLT_VERSION) + mv $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION).tmp $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION) -curl: libs/curl-$(CURL_VERSION) +curl: $(BUILD_PATH)/curl-$(CURL_VERSION) $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz: wget --no-check-certificate http://curl.haxx.se/download/curl-$(CURL_VERSION).tar.gz -O $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz -libs/curl-$(CURL_VERSION): $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz +$(BUILD_PATH)/curl-$(CURL_VERSION): $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz # prepare - rm -r -f libs/curl-* + rm -r -f $(BUILD_PATH)/curl-* tar xvf $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz # build - cd curl-$(CURL_VERSION) && ./configure --disable-shared --with-ssl="`pwd`/../libs/openssl-$(OPENSSL_VERSION)" - #cd curl-$(CURL_VERSION) && make install exec_prefix="`pwd`/../libs" + cd curl-$(CURL_VERSION) && ./configure --disable-shared --with-ssl="`pwd`/../$(BUILD_PATH)/openssl-$(OPENSSL_VERSION)" + #cd curl-$(CURL_VERSION) && make install exec_prefix="`pwd`/../$(BUILD_PATH)" cd curl-$(CURL_VERSION) && make # copy files - mkdir -p libs/curl-$(CURL_VERSION).tmp/include/curl - cp curl-$(CURL_VERSION)/include/curl/*.h libs/curl-$(CURL_VERSION).tmp/include/curl/ - mkdir -p libs/curl-$(CURL_VERSION).tmp/lib - cp curl-$(CURL_VERSION)/lib/.libs/libcurl.a libs/curl-$(CURL_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/curl-$(CURL_VERSION).tmp/include/curl + cp curl-$(CURL_VERSION)/include/curl/*.h $(BUILD_PATH)/curl-$(CURL_VERSION).tmp/include/curl/ + mkdir -p $(BUILD_PATH)/curl-$(CURL_VERSION).tmp/lib + cp curl-$(CURL_VERSION)/lib/.libs/libcurl.a $(BUILD_PATH)/curl-$(CURL_VERSION).tmp/lib/ # cleanup rm -r -f curl-$(CURL_VERSION) - mv libs/curl-$(CURL_VERSION).tmp libs/curl-$(CURL_VERSION) + mv $(BUILD_PATH)/curl-$(CURL_VERSION).tmp $(BUILD_PATH)/curl-$(CURL_VERSION) -sqlcipher: libs/sqlcipher-$(SQLCIPHER_VERSION) +sqlcipher: $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION) $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz: wget --no-check-certificate http://downloads.sourceforge.net/project/tcl/Tcl/$(TCL_VERSION)/tcl$(TCL_VERSION)-src.tar.gz -O $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz @@ -282,9 +281,9 @@ $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz: $(DOWNLOAD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tar.gz: wget --no-check-certificate https://github.com/sqlcipher/sqlcipher/archive/v$(SQLCIPHER_VERSION).tar.gz -O $(DOWNLOAD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tar.gz -libs/sqlcipher-$(SQLCIPHER_VERSION): $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz $(DOWNLOAD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tar.gz +$(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION): $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz $(DOWNLOAD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tar.gz # prepare - rm -r -f libs/sqlcipher-* + rm -r -f $(BUILD_PATH)/sqlcipher-* # tcl tar xvf $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz mkdir -p tcl$(TCL_VERSION)/build @@ -296,80 +295,73 @@ libs/sqlcipher-$(SQLCIPHER_VERSION): $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar. mkdir -p tcl$(TCL_VERSION)/lib ln -s `pwd`/tcl$(TCL_VERSION)/library `pwd`/tcl$(TCL_VERSION)/lib/tcl8.6 # build - cd sqlcipher-$(SQLCIPHER_VERSION) && PATH=.:$$PATH:`pwd`/../tcl$(TCL_VERSION)/build && export LIBS="-L`pwd`/../libs/openssl-$(OPENSSL_VERSION)/lib -lgdi32 $$LIBS" && ./configure --disable-shared --enable-static --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC -I`pwd`/../libs/openssl-$(OPENSSL_VERSION)/include -I`pwd`/../tcl$(TCL_VERSION)/generic" LDFLAGS="-L`pwd`/../libs/openssl-$(OPENSSL_VERSION)/lib -lcrypto -lgdi32" --with-tcl="`pwd`/../tcl$(TCL_VERSION)/build" && make install prefix="`pwd`/install" + cd sqlcipher-$(SQLCIPHER_VERSION) && PATH=.:$$PATH:`pwd`/../tcl$(TCL_VERSION)/build && export LIBS="-L`pwd`/../$(BUILD_PATH)/openssl-$(OPENSSL_VERSION)/lib -lgdi32 $$LIBS" && ./configure --disable-shared --enable-static --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC -I`pwd`/../$(BUILD_PATH)/openssl-$(OPENSSL_VERSION)/include -I`pwd`/../tcl$(TCL_VERSION)/generic" LDFLAGS="-L`pwd`/../$(BUILD_PATH)/openssl-$(OPENSSL_VERSION)/lib -lcrypto -lgdi32" --with-tcl="`pwd`/../tcl$(TCL_VERSION)/build" && make install prefix="`pwd`/install" # copy files - mkdir -p libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/include - cp -r sqlcipher-$(SQLCIPHER_VERSION)/install/include/* libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/include/ - mkdir -p libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/lib - cp sqlcipher-$(SQLCIPHER_VERSION)/install/lib/libsqlcipher.a libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/lib/ - mkdir -p libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/bin - cp sqlcipher-$(SQLCIPHER_VERSION)/install/bin/sqlcipher.exe libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/bin/ + mkdir -p $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp/include + cp -r sqlcipher-$(SQLCIPHER_VERSION)/install/include/* $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp/include/ + mkdir -p $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp/lib + cp sqlcipher-$(SQLCIPHER_VERSION)/install/lib/libsqlcipher.a $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp/bin + cp sqlcipher-$(SQLCIPHER_VERSION)/install/bin/sqlcipher.exe $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp/bin/ # cleanup rm -r -f sqlcipher-$(SQLCIPHER_VERSION) rm -r -f tcl$(TCL_VERSION) - mv libs/sqlcipher-$(SQLCIPHER_VERSION).tmp libs/sqlcipher-$(SQLCIPHER_VERSION) + mv $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION) -libmicrohttpd: libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION) +libmicrohttpd: $(BUILD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION) $(DOWNLOAD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz: wget --no-check-certificate http://ftp.gnu.org/gnu/libmicrohttpd/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz -O $(DOWNLOAD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz -libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION): $(DOWNLOAD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz +$(BUILD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION): $(DOWNLOAD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz # prepare - rm -r -f libs/libmicrohttpd-* + rm -r -f $(BUILD_PATH)/libmicrohttpd-* tar xvf $(DOWNLOAD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz # build - cd libmicrohttpd-$(LIBMICROHTTPD_VERSION) && ./configure --disable-shared --enable-static --prefix="`pwd`/../libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tmp" + cd libmicrohttpd-$(LIBMICROHTTPD_VERSION) && ./configure --disable-shared --enable-static --prefix="`pwd`/../$(BUILD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tmp" cd libmicrohttpd-$(LIBMICROHTTPD_VERSION) && make install # cleanup rm -r -f libmicrohttpd-$(LIBMICROHTTPD_VERSION) - mv libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tmp libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION) + mv $(BUILD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tmp $(BUILD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION) -ffmpeg: libs/ffmpeg-$(FFMPEG_VERSION) +ffmpeg: $(BUILD_PATH)/ffmpeg-$(FFMPEG_VERSION) $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz: wget --no-check-certificate https://ffmpeg.org/releases/ffmpeg-$(FFMPEG_VERSION).tar.gz -O $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz -libs/ffmpeg-$(FFMPEG_VERSION): $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz +$(BUILD_PATH)/ffmpeg-$(FFMPEG_VERSION): $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz # prepare - rm -r -f libs/ffmpeg-* + rm -r -f $(BUILD_PATH)/ffmpeg-* tar xvf $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz # build - cd ffmpeg-$(FFMPEG_VERSION) && ./configure --disable-shared --enable-static --disable-programs --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc --disable-htmlpages --disable-manpages --disable-podpages --disable-txtpages --disable-yasm --disable-everything --enable-encoder=mpeg4 --enable-decoder=mpeg4 --prefix="`pwd`/../libs/ffmpeg-$(FFMPEG_VERSION).tmp" + cd ffmpeg-$(FFMPEG_VERSION) && ./configure --disable-shared --enable-static --disable-programs --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc --disable-htmlpages --disable-manpages --disable-podpages --disable-txtpages --disable-yasm --disable-everything --enable-encoder=mpeg4 --enable-decoder=mpeg4 --prefix="`pwd`/../$(BUILD_PATH)/ffmpeg-$(FFMPEG_VERSION).tmp" cd ffmpeg-$(FFMPEG_VERSION) && make install # cleanup rm -r -f ffmpeg-$(FFMPEG_VERSION) - mv libs/ffmpeg-$(FFMPEG_VERSION).tmp libs/ffmpeg-$(FFMPEG_VERSION) + mv $(BUILD_PATH)/ffmpeg-$(FFMPEG_VERSION).tmp $(BUILD_PATH)/ffmpeg-$(FFMPEG_VERSION) -rapidjson: libs/rapidjson-$(RAPIDJSON_VERSION) +rapidjson: $(BUILD_PATH)/rapidjson-$(RAPIDJSON_VERSION) $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz: wget --no-check-certificate https://github.com/Tencent/rapidjson/archive/v$(RAPIDJSON_VERSION).tar.gz -O $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz -libs/rapidjson-$(RAPIDJSON_VERSION): $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz +$(BUILD_PATH)/rapidjson-$(RAPIDJSON_VERSION): $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz # prepare - rm -r -f libs/rapidjson-* + rm -r -f $(BUILD_PATH)/rapidjson-* tar xvf $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz # build #mkdir -p rapidjson-$(RAPIDJSON_VERSION)/build #cd rapidjson-$(RAPIDJSON_VERSION)/build && cmake .. -G"MSYS Makefiles" #cd rapidjson-$(RAPIDJSON_VERSION)/build && make # copy files - mkdir -p libs/rapidjson-$(RAPIDJSON_VERSION).tmp/include - cp -r rapidjson-$(RAPIDJSON_VERSION)/include/* libs/rapidjson-$(RAPIDJSON_VERSION).tmp/include/ + mkdir -p $(BUILD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tmp/include + cp -r rapidjson-$(RAPIDJSON_VERSION)/include/* $(BUILD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tmp/include/ # cleanup rm -r -f rapidjson-$(RAPIDJSON_VERSION) - mv libs/rapidjson-$(RAPIDJSON_VERSION).tmp libs/rapidjson-$(RAPIDJSON_VERSION) + mv $(BUILD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tmp $(BUILD_PATH)/rapidjson-$(RAPIDJSON_VERSION) copylibs: - if [ "$(COPY_ANSWER)" = "" ] ; then \ - read -p "Do you want to copy libs to retroshare? (y|n)" answer; \ - else \ - answer=$(COPY_ANSWER) ; \ - fi ; \ - if [ "$$answer" = "y" ] ; then \ - rm -r -f $(LIBS_PATH) ; \ - mkdir -p $(LIBS_PATH) ; \ - cp ./libs/gcc-version $(LIBS_PATH) ; \ - find ./libs -mindepth 1 -maxdepth 1 -type d -not -name "*.tmp" -print -exec cp -r {}/. $(LIBS_PATH) \; ; \ - fi + rm -r -f $(LIBS_PATH) ; \ + mkdir -p $(LIBS_PATH) ; \ + cp $(BUILD_PATH)/gcc-version $(LIBS_PATH) ; \ + find $(BUILD_PATH) -mindepth 1 -maxdepth 1 -type d -not -name "*.tmp" -print -exec cp -r {}/. $(LIBS_PATH) \; ; \ diff --git a/build_scripts/Windows/build-libs/build-libs.bat b/build_scripts/Windows/build-libs/build-libs.bat index 58b400a2f..e0c5af09b 100644 --- a/build_scripts/Windows/build-libs/build-libs.bat +++ b/build_scripts/Windows/build-libs/build-libs.bat @@ -1,5 +1,5 @@ :: Usage: -:: call build-libs.bat [auto-copy] [make tasks] +:: call build-libs.bat [make tasks] @echo off @@ -7,7 +7,6 @@ setlocal :: Parameter set MakeParam="DOWNLOAD_PATH=../download" -if "%~1"=="auto-copy" set MakeParam=%MakeParam% "COPY_ANSWER=y"& shift /1 set MakeTask= :param_loop @@ -30,6 +29,9 @@ if not exist "%EnvMSYSSH%" %cecho% error "Please install MSYS first." & exit /B call "%~dp0env.bat" if errorlevel 1 goto error_env +:: Add tools path to PATH environment +set PATH=%EnvToolsPath%;%PATH% + call "%ToolsPath%\msys-path.bat" "%~dp0" MSYSCurPath call "%ToolsPath%\msys-path.bat" "%BuildLibsPath%" MSYSBuildLibsPath diff --git a/build_scripts/Windows/build-retrotor.bat b/build_scripts/Windows/build-retrotor.bat index cbf21e1c0..f00014187 100644 --- a/build_scripts/Windows/build-retrotor.bat +++ b/build_scripts/Windows/build-retrotor.bat @@ -9,7 +9,7 @@ call "%EnvPath%\env.bat" if errorlevel 1 goto error_env %cecho% info "Build libraries" -call "%~dp0build-libs\build-libs.bat" auto-copy +call "%~dp0build-libs\build-libs.bat" if errorlevel 1 %cecho% error "Failed to build libraries." & exit /B %ERRORLEVEL% %cecho% info "Build %SourceName%" diff --git a/build_scripts/Windows/build.bat b/build_scripts/Windows/build.bat index 22052a3e1..642f21454 100644 --- a/build_scripts/Windows/build.bat +++ b/build_scripts/Windows/build.bat @@ -9,7 +9,7 @@ call "%EnvPath%\env.bat" if errorlevel 1 goto error_env %cecho% info "Build libraries" -call "%~dp0build-libs\build-libs.bat" auto-copy +call "%~dp0build-libs\build-libs.bat" if errorlevel 1 %cecho% error "Failed to build libraries." & exit /B %ERRORLEVEL% %cecho% info "Build %SourceName%" diff --git a/build_scripts/Windows/build/build-installer.bat b/build_scripts/Windows/build/build-installer.bat index 40c408c62..1063d8680 100644 --- a/build_scripts/Windows/build/build-installer.bat +++ b/build_scripts/Windows/build/build-installer.bat @@ -8,21 +8,18 @@ if errorlevel 1 goto error_env call "%EnvPath%\env.bat" if errorlevel 1 goto error_env -:: Get gcc versions -call "%ToolsPath%\get-gcc-version.bat" GCCVersion -if "%GCCVersion%"=="" echo Cannot get gcc version.& exit /B 1 +:: Initialize environment +call "%~dp0env.bat" %* +if errorlevel 2 exit /B 2 +if errorlevel 1 goto error_env :: Check external libraries -if not exist "%RootPath%\libs" echo Please build external libraries first.& exit /B 1 +if not exist "%BuildLibsPath%\libs" %cecho% error "Please build external libraries first." & exit /B 1 :: Check gcc version of external libraries -if not exist "%RootPath%\libs\gcc-version" echo Cannot get gcc version of external libraries.& exit /B 1 -set /P LibsGCCVersion=<"%RootPath%\libs\gcc-version" -if "%LibsGCCVersion%" NEQ "%GCCVersion%" echo Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%).& exit /B 1 - -:: Initialize environment -call "%~dp0env.bat" standard -if errorlevel 1 goto error_env +if not exist "%BuildLibsPath%\libs\gcc-version" %cecho% error "Cannot get gcc version of external libraries." & exit /B 1 +set /P LibsGCCVersion=<"%BuildLibsPath%\libs\gcc-version" +if "%LibsGCCVersion%" NEQ "%GCCVersion%" %cecho% error "Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%)." & exit /B 1 :: Build defines for script set NSIS_PARAM= diff --git a/build_scripts/Windows/build/build.bat b/build_scripts/Windows/build/build.bat index 68e00235b..30d20c59d 100644 --- a/build_scripts/Windows/build/build.bat +++ b/build_scripts/Windows/build/build.bat @@ -8,22 +8,19 @@ if errorlevel 1 goto error_env call "%EnvPath%\env.bat" if errorlevel 1 goto error_env -:: Get gcc versions -call "%ToolsPath%\get-gcc-version.bat" GCCVersion -if "%GCCVersion%"=="" echo Cannot get gcc version.& exit /B 1 - -:: Check external libraries -if not exist "%RootPath%\libs" echo Please build external libraries first.& exit /B 1 - -:: Check gcc version of external libraries -if not exist "%RootPath%\libs\gcc-version" echo Cannot get gcc version of external libraries.& exit /B 1 -set /P LibsGCCVersion=<"%RootPath%\libs\gcc-version" -if "%LibsGCCVersion%" NEQ "%GCCVersion%" echo Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%).& exit /B 1 - :: Initialize environment call "%~dp0env.bat" %* +if errorlevel 2 exit /B 2 if errorlevel 1 goto error_env +:: Check external libraries +if not exist "%BuildLibsPath%\libs" %cecho% error "Please build external libraries first." & exit /B 1 + +:: Check gcc version of external libraries +if not exist "%BuildLibsPath%\libs\gcc-version" %cecho% error "Cannot get gcc version of external libraries." & exit /B 1 +set /P LibsGCCVersion=<"%BuildLibsPath%\libs\gcc-version" +if "%LibsGCCVersion%" NEQ "%GCCVersion%" %cecho% error "Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%)." & exit /B 1 + :: Check git executable set GitPath= call "%ToolsPath%\find-in-path.bat" GitPath git.exe @@ -55,7 +52,7 @@ title Build - %SourceName%%RsType%-%RsBuildConfig% [qmake] set RS_QMAKE_CONFIG=%RsBuildConfig% version_detail_bash_script rs_autologin retroshare_plugins if "%RsRetroTor%"=="1" set RS_QMAKE_CONFIG=%RS_QMAKE_CONFIG% retrotor -qmake "%SourcePath%\RetroShare.pro" -r "CONFIG+=%RS_QMAKE_CONFIG%" +qmake "%SourcePath%\RetroShare.pro" -r "CONFIG+=%RS_QMAKE_CONFIG%" "EXTERNAL_LIB_DIR=%BuildLibsPath%\libs" if errorlevel 1 goto error echo. diff --git a/build_scripts/Windows/build/env.bat b/build_scripts/Windows/build/env.bat index 094b1b686..b0c765e03 100644 --- a/build_scripts/Windows/build/env.bat +++ b/build_scripts/Windows/build/env.bat @@ -9,7 +9,7 @@ if "%~1"=="standard" ( echo. echo Usage: standard^|retrotor echo. - exit /B 1 + exit /B 2 ) ) @@ -22,16 +22,22 @@ if not exist "%DeployPath%" mkdir "%DeployPath%" :: Check Qt environment set QtPath= call "%ToolsPath%\find-in-path.bat" QtPath qmake.exe -if "%QtPath%"=="" echo Please run command in the Qt Command Prompt.& exit /B 1 +if "%QtPath%"=="" %cecho% error "Please run command in the Qt Command Prompt." & exit /B 1 :: Check MinGW environment set MinGWPath= call "%ToolsPath%\find-in-path.bat" MinGWPath gcc.exe -if "%MinGWPath%"=="" echo Please run command in the Qt Command Prompt.& exit /B 1 +if "%MinGWPath%"=="" %cecho% error "Please run command in the Qt Command Prompt." & exit /B 1 :: Get Qt version call "%ToolsPath%\get-qt-version.bat" QtVersion -if "%QtVersion%"=="" echo Cannot get Qt version.& exit /B 1 +if "%QtVersion%"=="" %cecho% error "Cannot get Qt version." & exit /B 1 + +:: Get gcc versions +call "%ToolsPath%\get-gcc-version.bat" GCCVersion +if "%GCCVersion%"=="" %cecho% error "Cannot get gcc version." & exit /B 1 + +set BuildLibsPath=%EnvRootPath%\build-libs\gcc-%GCCVersion% set RsBuildConfig=release set RsBuildPath=%BuildPath%\Qt-%QtVersion%%RsType%-%RsBuildConfig% diff --git a/build_scripts/Windows/build/pack.bat b/build_scripts/Windows/build/pack.bat index 215b3c402..8122cc2e0 100644 --- a/build_scripts/Windows/build/pack.bat +++ b/build_scripts/Windows/build/pack.bat @@ -10,22 +10,19 @@ if errorlevel 1 goto error_env call "%EnvPath%\env.bat" if errorlevel 1 goto error_env -:: Get gcc versions -call "%ToolsPath%\get-gcc-version.bat" GCCVersion -if "%GCCVersion%"=="" echo Cannot get gcc version.& exit /B 1 - -:: Check external libraries -if not exist "%RootPath%\libs" echo Please build external libraries first.& exit /B 1 - -:: Check gcc version of external libraries -if not exist "%RootPath%\libs\gcc-version" echo Cannot get gcc version of external libraries.& exit /B 1 -set /P LibsGCCVersion=<"%RootPath%\libs\gcc-version" -if "%LibsGCCVersion%" NEQ "%GCCVersion%" echo Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%).& exit /B 1 - :: Initialize environment call "%~dp0env.bat" %* +if errorlevel 2 exit /B 2 if errorlevel 1 goto error_env +:: Check external libraries +if not exist "%BuildLibsPath%\libs" %cecho% error "Please build external libraries first." & exit /B 1 + +:: Check gcc version of external libraries +if not exist "%BuildLibsPath%\libs\gcc-version" %cecho% error "Cannot get gcc version of external libraries." & exit /B 1 +set /P LibsGCCVersion=<"%BuildLibsPath%\libs\gcc-version" +if "%LibsGCCVersion%" NEQ "%GCCVersion%" %cecho% error "Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%)." & exit /B 1 + :: Remove deploy path if exist "%RsDeployPath%" rmdir /S /Q "%RsDeployPath%" diff --git a/build_scripts/Windows/env/tools/prepare-msys.bat b/build_scripts/Windows/env/tools/prepare-msys.bat index af9b9bf78..23dd391f4 100644 --- a/build_scripts/Windows/env/tools/prepare-msys.bat +++ b/build_scripts/Windows/env/tools/prepare-msys.bat @@ -52,7 +52,8 @@ mingw-get.exe install msys-autoconf mingw-get.exe install msys-automake mingw-get.exe install msys-autogen mingw-get.exe install msys-mktemp -mingw-get.exe install msys-wget +rem Use own wget binary, because MSYS version of wget is to old +rem mingw-get.exe install msys-wget popd %cecho% info "Unpack CMake" diff --git a/build_scripts/Windows/env/tools/prepare-tools.bat b/build_scripts/Windows/env/tools/prepare-tools.bat index 2295d0ca3..587327f0e 100644 --- a/build_scripts/Windows/env/tools/prepare-tools.bat +++ b/build_scripts/Windows/env/tools/prepare-tools.bat @@ -4,8 +4,8 @@ if "%EnvRootPath%"=="" exit /B 1 set CEchoUrl=https://github.com/lordmulder/cecho/releases/download/2015-10-10/cecho.2015-10-10.zip set CEchoInstall=cecho.2015-10-10.zip -set SevenZipUrl=http://7-zip.org/a/7z1602.msi -set SevenZipInstall=7z1602.msi +set SevenZipUrl=https://sourceforge.net/projects/sevenzip/files/7-Zip/18.05/7z1805.msi/download +set SevenZipInstall=7z1805.msi ::set CurlUrl=https://bintray.com/artifact/download/vszakats/generic/curl-7.50.1-win32-mingw.7z ::set CurlInstall=curl-7.50.1-win32-mingw.7z set WgetUrl=https://eternallybored.org/misc/wget/1.19.4/32/wget.exe @@ -20,13 +20,23 @@ set NSISUrl=http://prdownloads.sourceforge.net/nsis/nsis-3.0-setup.exe?download set NSISInstall=nsis-3.0-setup.exe set NSISInstallPath=%EnvToolsPath%\NSIS +if not exist "%EnvToolsPath%\wget.exe" ( + echo Download Wget installation + + if not exist "%EnvDownloadPath%\%WgetInstall%" call "%ToolsPath%\winhttpjs.bat" %WgetUrl% -saveTo "%EnvDownloadPath%\%WgetInstall%" + if not exist "%EnvDownloadPath%\%WgetInstall%" %cecho% error "Cannot download Wget installation" & goto error + + echo Copy Wget + copy "%EnvDownloadPath%\wget.exe" "%EnvToolsPath%" +) + if not exist "%EnvToolsPath%\7z.exe" ( call "%ToolsPath%\remove-dir.bat" "%EnvTempPath%" mkdir "%EnvTempPath%" echo Download 7z installation - if not exist "%EnvDownloadPath%\%SevenZipInstall%" call "%ToolsPath%\winhttpjs.bat" %SevenZipUrl% -saveTo "%EnvDownloadPath%\%SevenZipInstall%" + if not exist "%EnvDownloadPath%\%SevenZipInstall%" call "%ToolsPath%\download-file.bat" "%SevenZipUrl%" "%EnvDownloadPath%\%SevenZipInstall%" if not exist "%EnvDownloadPath%\%SevenZipInstall%" echo Cannot download 7z installation& goto error echo Unpack 7z @@ -69,16 +79,6 @@ if not exist "%EnvToolsPath%\cecho.exe" ( :: call "%ToolsPath%\remove-dir.bat" "%EnvTempPath%" ::) -if not exist "%EnvToolsPath%\wget.exe" ( - %cecho% info "Download Wget installation" - - if not exist "%EnvDownloadPath%\%WgetInstall%" call "%ToolsPath%\winhttpjs.bat" %WgetUrl% -saveTo "%EnvDownloadPath%\%WgetInstall%" - if not exist "%EnvDownloadPath%\%WgetInstall%" %cecho% error "Cannot download Wget installation" & goto error - - %cecho% info "Copy Wget" - copy "%EnvDownloadPath%\wget.exe" "%EnvToolsPath%" -) - if not exist "%EnvToolsPath%\jom.exe" ( call "%ToolsPath%\remove-dir.bat" "%EnvTempPath%" mkdir "%EnvTempPath%" From f39fd069b59218344d5e47b995b849977fef7e48 Mon Sep 17 00:00:00 2001 From: Phenom Date: Mon, 7 May 2018 17:29:11 +0200 Subject: [PATCH 101/161] Fix CppCheck duplInheritedMember warning in RsItem /libretroshare/src/rsitems/rsitem.h:92: warning: Cppcheck(duplInheritedMember): The class 'RsPeerNetItem' defines member variable with name 'peerId' also defined in its parent struct 'RsItem'. /libretroshare/src/rsitems/rsitem.h:92: warning: Cppcheck(duplInheritedMember): The class 'RsHistoryMsgItem' defines member variable with name 'peerId' also defined in its parent struct 'RsItem'. /libretroshare/src/rsitems/rsitem.h:91: warning: Cppcheck(duplInheritedMember): The class 'RsBanListConfigItem' defines member variable with name 'type' also defined in its parent struct 'RsItem'. /libretroshare/src/rsitems/rsitem.h:92: warning: Cppcheck(duplInheritedMember): The class 'RsBanListConfigItem' defines member variable with name 'peerId' also defined in its parent struct 'RsItem'. --- libretroshare/src/pqi/p3historymgr.cc | 72 +++++++++---------- libretroshare/src/pqi/p3peermgr.cc | 8 +-- libretroshare/src/rsitems/rsbanlistitems.cc | 4 +- libretroshare/src/rsitems/rsbanlistitems.h | 18 ++--- libretroshare/src/rsitems/rsconfigitems.cc | 8 +-- libretroshare/src/rsitems/rsconfigitems.h | 2 +- libretroshare/src/rsitems/rshistoryitems.cc | 2 +- libretroshare/src/rsitems/rshistoryitems.h | 2 +- libretroshare/src/services/p3banlist.cc | 26 +++---- .../serialiser/rsconfigitem_test.cc | 4 +- 10 files changed, 74 insertions(+), 72 deletions(-) diff --git a/libretroshare/src/pqi/p3historymgr.cc b/libretroshare/src/pqi/p3historymgr.cc index 39c2960a3..e1acb0888 100644 --- a/libretroshare/src/pqi/p3historymgr.cc +++ b/libretroshare/src/pqi/p3historymgr.cc @@ -67,7 +67,7 @@ p3HistoryMgr::~p3HistoryMgr() /***** p3HistoryMgr *****/ -//void p3HistoryMgr::addMessage(bool incoming, const RsPeerId &chatPeerId, const RsPeerId &peerId, const RsChatMsgItem *chatItem) +//void p3HistoryMgr::addMessage(bool incoming, const RsPeerId &chatPeerId, const RsPeerId &msgPeerId, const RsChatMsgItem *chatItem) void p3HistoryMgr::addMessage(const ChatMessage& cm) { uint32_t addMsgId = 0; @@ -84,48 +84,48 @@ void p3HistoryMgr::addMessage(const ChatMessage& cm) RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/ - RsPeerId peerId; // id of sending peer - RsPeerId chatPeerId; // id of chat endpoint - std::string peerName; //name of sending peer + RsPeerId msgPeerId; // id of sending peer + RsPeerId chatPeerId; // id of chat endpoint + std::string peerName; //name of sending peer - bool enabled = false; - if (cm.chat_id.isBroadcast() && mPublicEnable == true) { - peerName = rsPeers->getPeerName(cm.broadcast_peer_id); - enabled = true; + bool enabled = false; + if (cm.chat_id.isBroadcast() && mPublicEnable == true) { + peerName = rsPeers->getPeerName(cm.broadcast_peer_id); + enabled = true; + } + if (cm.chat_id.isPeerId() && mPrivateEnable == true) { + msgPeerId = cm.incoming ? cm.chat_id.toPeerId() : rsPeers->getOwnId(); + peerName = rsPeers->getPeerName(msgPeerId); + enabled = true; + } + if (cm.chat_id.isLobbyId() && mLobbyEnable == true) { + peerName = cm.lobby_peer_gxs_id.toStdString(); + enabled = true; } - if (cm.chat_id.isPeerId() && mPrivateEnable == true) { - peerId = cm.incoming ? cm.chat_id.toPeerId() : rsPeers->getOwnId(); - peerName = rsPeers->getPeerName(peerId); - enabled = true; - } - if (cm.chat_id.isLobbyId() && mLobbyEnable == true) { - peerName = cm.lobby_peer_gxs_id.toStdString(); - enabled = true; - } - if(cm.chat_id.isDistantChatId()) - { - DistantChatPeerInfo dcpinfo; - if (rsMsgs->getDistantChatStatus(cm.chat_id.toDistantChatId(), dcpinfo)) - peerName = cm.chat_id.toPeerId().toStdString(); - enabled = true; - } + if(cm.chat_id.isDistantChatId()) + { + DistantChatPeerInfo dcpinfo; + if (rsMsgs->getDistantChatStatus(cm.chat_id.toDistantChatId(), dcpinfo)) + peerName = cm.chat_id.toPeerId().toStdString(); + enabled = true; + } - if(enabled == false) - return; + if(enabled == false) + return; - if(!chatIdToVirtualPeerId(cm.chat_id, chatPeerId)) - return; + if(!chatIdToVirtualPeerId(cm.chat_id, chatPeerId)) + return; RsHistoryMsgItem* item = new RsHistoryMsgItem; item->chatPeerId = chatPeerId; - item->incoming = cm.incoming; - item->peerId = peerId; - item->peerName = peerName; - item->sendTime = cm.sendTime; - item->recvTime = cm.recvTime; + item->incoming = cm.incoming; + item->msgPeerId = msgPeerId; + item->peerName = peerName; + item->sendTime = cm.sendTime; + item->recvTime = cm.recvTime; - item->message = cm.msg ; + item->message = cm.msg ; //librs::util::ConvertUtf16ToUtf8(chatItem->message, item->message); std::map >::iterator mit = mMessages.find(item->chatPeerId); @@ -138,7 +138,7 @@ void p3HistoryMgr::addMessage(const ChatMessage& cm) uint32_t limit; if (chatPeerId.isNull()) limit = mPublicSaveCount; - else if (cm.chat_id.isLobbyId()) + else if (cm.chat_id.isLobbyId()) limit = mLobbySaveCount; else limit = mPrivateSaveCount; @@ -424,7 +424,7 @@ static void convertMsg(const RsHistoryMsgItem* item, HistoryMsg &msg) msg.msgId = item->msgId; msg.chatPeerId = item->chatPeerId; msg.incoming = item->incoming; - msg.peerId = item->peerId; + msg.peerId = item->msgPeerId; msg.peerName = item->peerName; msg.sendTime = item->sendTime; msg.recvTime = item->recvTime; diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index d1245579b..ba3ecbf84 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -2014,7 +2014,7 @@ bool p3PeerMgrIMPL::saveList(bool &cleanup, std::list& saveData) RsPeerNetItem *item = new RsPeerNetItem(); item->clear(); - item->peerId = getOwnId(); + item->nodePeerId = getOwnId(); item->pgpId = mOwnState.gpg_id; item->location = mOwnState.location; @@ -2065,7 +2065,7 @@ bool p3PeerMgrIMPL::saveList(bool &cleanup, std::list& saveData) item = new RsPeerNetItem(); item->clear(); - item->peerId = it->first; + item->nodePeerId = it->first; item->pgpId = (it->second).gpg_id; item->location = (it->second).location; item->netMode = (it->second).netMode; @@ -2265,7 +2265,7 @@ bool p3PeerMgrIMPL::loadList(std::list& load) RsPeerNetItem *pitem = dynamic_cast(*it); if (pitem) { - RsPeerId peer_id = pitem->peerId ; + RsPeerId peer_id = pitem->nodePeerId ; RsPgpId peer_pgp_id = pitem->pgpId ; if (peer_id == ownId) @@ -2292,7 +2292,7 @@ bool p3PeerMgrIMPL::loadList(std::list& load) /* ************* */ // permission flags is used as a mask for the existing perms, so we set it to 0xffff addFriend(peer_id, peer_pgp_id, pitem->netMode, pitem->vs_disc, pitem->vs_dht, pitem->lastContact, RS_NODE_PERM_ALL); - setLocation(pitem->peerId, pitem->location); + setLocation(pitem->nodePeerId, pitem->location); } if (pitem->netMode == RS_NET_MODE_HIDDEN) diff --git a/libretroshare/src/rsitems/rsbanlistitems.cc b/libretroshare/src/rsitems/rsbanlistitems.cc index 358d20961..44f908dee 100644 --- a/libretroshare/src/rsitems/rsbanlistitems.cc +++ b/libretroshare/src/rsitems/rsbanlistitems.cc @@ -48,8 +48,8 @@ void RsBanListItem::serial_process(RsGenericSerializer::SerializeJob j,RsGeneric void RsBanListConfigItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { - RsTypeSerializer::serial_process(j,ctx,type,"type") ; - RsTypeSerializer::serial_process (j,ctx,peerId,"peerId") ; + RsTypeSerializer::serial_process(j,ctx,banListType,"type") ; + RsTypeSerializer::serial_process (j,ctx,banListPeerId,"peerId") ; RsTypeSerializer::serial_process (j,ctx,update_time,"update_time") ; RsTypeSerializer::serial_process (j,ctx,banned_peers,"banned_peers") ; } diff --git a/libretroshare/src/rsitems/rsbanlistitems.h b/libretroshare/src/rsitems/rsbanlistitems.h index e5660e55c..6aec4b115 100644 --- a/libretroshare/src/rsitems/rsbanlistitems.h +++ b/libretroshare/src/rsitems/rsbanlistitems.h @@ -60,18 +60,20 @@ class RsBanListItem: public RsItem class RsBanListConfigItem: public RsItem { public: - RsBanListConfigItem() - :RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_BANLIST, RS_PKT_SUBTYPE_BANLIST_CONFIG_ITEM) {} + RsBanListConfigItem() + : RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_BANLIST, RS_PKT_SUBTYPE_BANLIST_CONFIG_ITEM) + , banListType(0), update_time(0) + {} - virtual ~RsBanListConfigItem(){} - virtual void clear() { banned_peers.TlvClear() ; } + virtual ~RsBanListConfigItem(){} + virtual void clear() { banned_peers.TlvClear() ; } void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx); - uint32_t type ; - RsPeerId peerId ; - time_t update_time ; - RsTlvBanList banned_peers; + uint32_t banListType ; + RsPeerId banListPeerId ; + time_t update_time ; + RsTlvBanList banned_peers; }; class RsBanListSerialiser: public RsServiceSerializer diff --git a/libretroshare/src/rsitems/rsconfigitems.cc b/libretroshare/src/rsitems/rsconfigitems.cc index 83f508794..17edb7962 100644 --- a/libretroshare/src/rsitems/rsconfigitems.cc +++ b/libretroshare/src/rsitems/rsconfigitems.cc @@ -137,9 +137,9 @@ RsItem *RsPeerConfigSerialiser::create_item(uint8_t item_type,uint8_t item_subty void RsPeerNetItem::clear() { - peerId.clear(); - pgpId.clear(); - location.clear(); + nodePeerId.clear(); + pgpId.clear(); + location.clear(); netMode = 0; vs_disc = 0; vs_dht = 0; @@ -160,7 +160,7 @@ void RsPeerNetItem::clear() } void RsPeerNetItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { - RsTypeSerializer::serial_process(j,ctx,peerId,"peerId") ; + RsTypeSerializer::serial_process(j,ctx,nodePeerId,"peerId") ; RsTypeSerializer::serial_process(j,ctx,pgpId,"pgpId") ; RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_LOCATION,location,"location") ; diff --git a/libretroshare/src/rsitems/rsconfigitems.h b/libretroshare/src/rsitems/rsconfigitems.h index bc7240e99..a22f907d6 100644 --- a/libretroshare/src/rsitems/rsconfigitems.h +++ b/libretroshare/src/rsitems/rsconfigitems.h @@ -84,7 +84,7 @@ public: virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx); /* networking information */ - RsPeerId peerId; /* Mandatory */ + RsPeerId nodePeerId; /* Mandatory */ RsPgpId pgpId; /* Mandatory */ std::string location; /* Mandatory */ uint32_t netMode; /* Mandatory */ diff --git a/libretroshare/src/rsitems/rshistoryitems.cc b/libretroshare/src/rsitems/rshistoryitems.cc index a66299b51..1f16e976f 100644 --- a/libretroshare/src/rsitems/rshistoryitems.cc +++ b/libretroshare/src/rsitems/rshistoryitems.cc @@ -44,7 +44,7 @@ void RsHistoryMsgItem::serial_process(RsGenericSerializer::SerializeJob j,RsGene RsTypeSerializer::serial_process(j,ctx,version,"version") ; RsTypeSerializer::serial_process (j,ctx,chatPeerId,"chatPeerId") ; RsTypeSerializer::serial_process (j,ctx,incoming,"incoming") ; - RsTypeSerializer::serial_process (j,ctx,peerId,"peerId") ; + RsTypeSerializer::serial_process (j,ctx,msgPeerId,"peerId") ; RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_NAME,peerName,"peerName") ; RsTypeSerializer::serial_process(j,ctx,sendTime,"sendTime") ; RsTypeSerializer::serial_process(j,ctx,recvTime,"recvTime") ; diff --git a/libretroshare/src/rsitems/rshistoryitems.h b/libretroshare/src/rsitems/rshistoryitems.h index 802754e91..7f65f3d19 100644 --- a/libretroshare/src/rsitems/rshistoryitems.h +++ b/libretroshare/src/rsitems/rshistoryitems.h @@ -46,7 +46,7 @@ public: RsPeerId chatPeerId; // empty for global chat bool incoming; - RsPeerId peerId; + RsPeerId msgPeerId; std::string peerName; uint32_t sendTime; uint32_t recvTime; diff --git a/libretroshare/src/services/p3banlist.cc b/libretroshare/src/services/p3banlist.cc index 9281307fd..b1fc07113 100644 --- a/libretroshare/src/services/p3banlist.cc +++ b/libretroshare/src/services/p3banlist.cc @@ -735,9 +735,9 @@ bool p3BanList::saveList(bool &cleanup, std::list& itemlist) { RsBanListConfigItem *item = new RsBanListConfigItem ; - item->type = RSBANLIST_TYPE_PEERLIST ; - item->peerId = it->second.mPeerId ; - item->update_time = it->second.mLastUpdate ; + item->banListType = RSBANLIST_TYPE_PEERLIST ; + item->banListPeerId = it->second.mPeerId ; + item->update_time = it->second.mLastUpdate ; item->banned_peers.TlvClear() ; for(std::map::const_iterator it2 = it->second.mBanPeers.begin();it2!=it->second.mBanPeers.end();++it2) @@ -754,8 +754,8 @@ bool p3BanList::saveList(bool &cleanup, std::list& itemlist) // Add whitelist RsBanListConfigItem *item = new RsBanListConfigItem ; - item->type = RSBANLIST_TYPE_WHITELIST ; - item->peerId.clear() ; + item->banListType = RSBANLIST_TYPE_WHITELIST ; + item->banListPeerId.clear() ; item->update_time = 0 ; item->banned_peers.TlvClear() ; @@ -773,8 +773,8 @@ bool p3BanList::saveList(bool &cleanup, std::list& itemlist) item = new RsBanListConfigItem ; - item->type = RSBANLIST_TYPE_BLACKLIST ; - item->peerId.clear(); + item->banListType = RSBANLIST_TYPE_BLACKLIST ; + item->banListPeerId.clear(); item->update_time = 0 ; item->banned_peers.TlvClear() ; @@ -850,11 +850,11 @@ bool p3BanList::loadList(std::list& load) if(citem != NULL) { - if(citem->type == RSBANLIST_TYPE_PEERLIST) + if(citem->banListType == RSBANLIST_TYPE_PEERLIST) { - BanList& bl(mBanSources[citem->peerId]) ; + BanList& bl(mBanSources[citem->banListPeerId]) ; - bl.mPeerId = citem->peerId ; + bl.mPeerId = citem->banListPeerId ; bl.mLastUpdate = citem->update_time ; bl.mBanPeers.clear() ; @@ -870,7 +870,7 @@ bool p3BanList::loadList(std::list& load) std::cerr << "(WW) removed wrong address " << sockaddr_storage_iptostring(blp.addr) << std::endl; } } - else if(citem->type == RSBANLIST_TYPE_BLACKLIST) + else if(citem->banListType == RSBANLIST_TYPE_BLACKLIST) { mBanRanges.clear() ; @@ -885,7 +885,7 @@ bool p3BanList::loadList(std::list& load) std::cerr << "(WW) removed wrong address " << sockaddr_storage_iptostring(blp.addr) << std::endl; } } - else if(citem->type == RSBANLIST_TYPE_WHITELIST) + else if(citem->banListType == RSBANLIST_TYPE_WHITELIST) { mWhiteListedRanges.clear() ; @@ -903,7 +903,7 @@ bool p3BanList::loadList(std::list& load) } } else - std::cerr << "(EE) BanList item unknown type " << citem->type << ". This is a bug." << std::endl; + std::cerr << "(EE) BanList item unknown type " << citem->banListType << ". This is a bug." << std::endl; } delete *it ; diff --git a/tests/unittests/libretroshare/serialiser/rsconfigitem_test.cc b/tests/unittests/libretroshare/serialiser/rsconfigitem_test.cc index e74a215c6..9d270a345 100644 --- a/tests/unittests/libretroshare/serialiser/rsconfigitem_test.cc +++ b/tests/unittests/libretroshare/serialiser/rsconfigitem_test.cc @@ -29,7 +29,7 @@ RsSerialType* init_item(RsPeerNetItem& rpn) { randString(SHORT_STR, rpn.dyndns); - rpn.peerId.random(); + rpn.nodePeerId.random(); rpn.pgpId.random(); randString(SHORT_STR, rpn.location); @@ -165,7 +165,7 @@ bool operator==(const RsPeerNetItem& left, const RsPeerNetItem& right) if(left.dyndns != right.dyndns) return false; if(left.pgpId != right.pgpId) return false; if(left.location != right.location) return false; - if(left.peerId != right.peerId) return false; + if(left.nodePeerId != right.nodePeerId) return false; if(left.lastContact != right.lastContact) return false; if(left.netMode != right.netMode) return false; if(left.visState != right.visState) return false; From 4d748bd079168b01ad95dc4618a98ab98e91a912 Mon Sep 17 00:00:00 2001 From: Phenom Date: Mon, 7 May 2018 18:05:07 +0200 Subject: [PATCH 102/161] Fix CppCheck duplInheritedMember warning in bdNode /libbitdht/src/bitdht/bdnode.h:259: warning: Cppcheck(duplInheritedMember): The class 'bdNodeManager' defines member variable with name 'mFns' also defined in its parent class 'bdNode'. --- libbitdht/src/bitdht/bdmanager.cc | 48 +++++++++++++++---------------- libbitdht/src/bitdht/bdmanager.h | 2 +- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/libbitdht/src/bitdht/bdmanager.cc b/libbitdht/src/bitdht/bdmanager.cc index 4fb1e4709..52ac66e9a 100644 --- a/libbitdht/src/bitdht/bdmanager.cc +++ b/libbitdht/src/bitdht/bdmanager.cc @@ -72,7 +72,7 @@ bdNodeManager::bdNodeManager(bdNodeId *id, std::string dhtVersion, std::string b :bdNode(id, dhtVersion, bootfile, filterfile, fns, this) { mMode = BITDHT_MGR_STATE_OFF; - mFns = fns; + mDhtFns = fns; mModeTS = 0 ; mStartTS = 0; mSearchingDone = false; @@ -87,7 +87,7 @@ bdNodeManager::bdNodeManager(bdNodeId *id, std::string dhtVersion, std::string b #ifdef DEBUG_MGR std::cerr << "bdNodeManager::bdNodeManager() ID: "; - mFns->bdPrintNodeId(std::cerr, id); + mDhtFns->bdPrintNodeId(std::cerr, id); std::cerr << std::endl; #endif @@ -188,7 +188,7 @@ void bdNodeManager::addFindNode(bdNodeId *id, uint32_t qflags) { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::addFindNode() "; - mFns->bdPrintNodeId(std::cerr, id); + mDhtFns->bdPrintNodeId(std::cerr, id); std::cerr << std::endl; #endif /* check if exists already */ @@ -257,7 +257,7 @@ void bdNodeManager::removeFindNode(bdNodeId *id) { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::removeFindNode() "; - mFns->bdPrintNodeId(std::cerr, id); + mDhtFns->bdPrintNodeId(std::cerr, id); std::cerr << std::endl; #endif std::map::iterator it; @@ -525,7 +525,7 @@ int bdNodeManager::QueryRandomLocalNet() else { /* calculate mid point */ - mFns->bdRandomMidId(&mOwnId, &(id.id), &targetNodeId); + mDhtFns->bdRandomMidId(&mOwnId, &(id.id), &targetNodeId); } /* do standard find_peer message */ @@ -534,13 +534,13 @@ int bdNodeManager::QueryRandomLocalNet() #ifdef DEBUG_MGR std::cerr << "bdNodeManager::QueryRandomLocalNet() Querying : "; - mFns->bdPrintId(std::cerr, &id); + mDhtFns->bdPrintId(std::cerr, &id); std::cerr << " searching for : "; - mFns->bdPrintNodeId(std::cerr, &targetNodeId); + mDhtFns->bdPrintNodeId(std::cerr, &targetNodeId); bdMetric dist; - mFns->bdDistance(&targetNodeId, &(mOwnId), &dist); - int bucket = mFns->bdBucketDistance(&dist); + mDhtFns->bdDistance(&targetNodeId, &(mOwnId), &dist); + int bucket = mDhtFns->bdBucketDistance(&dist); std::cerr << " in Bucket: " << bucket; std::cerr << std::endl; #endif @@ -593,7 +593,7 @@ void bdNodeManager::SearchForLocalNet() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::SearchForLocalNet() Existing Internal Search: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif @@ -630,7 +630,7 @@ void bdNodeManager::SearchForLocalNet() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::SearchForLocalNet() " << i << " Attempts to find OkNode: "; - mFns->bdPrintNodeId(std::cerr, &targetNodeId); + mDhtFns->bdPrintNodeId(std::cerr, &targetNodeId); std::cerr << std::endl; #endif } @@ -638,7 +638,7 @@ void bdNodeManager::SearchForLocalNet() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::SearchForLocalNet() Failed to Find FilterOk this time: "; - mFns->bdPrintNodeId(std::cerr, &targetNodeId); + mDhtFns->bdPrintNodeId(std::cerr, &targetNodeId); std::cerr << std::endl; #endif } @@ -649,7 +649,7 @@ void bdNodeManager::SearchForLocalNet() #ifdef DEBUG_MGR std::cerr << "bdNodeManager::SearchForLocalNet() Adding New Internal Search: "; - mFns->bdPrintNodeId(std::cerr, &(targetNodeId)); + mDhtFns->bdPrintNodeId(std::cerr, &(targetNodeId)); std::cerr << std::endl; #endif } @@ -709,7 +709,7 @@ int bdNodeManager::checkStatus() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::checkStatus() Query in Progress id: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif } @@ -719,7 +719,7 @@ int bdNodeManager::checkStatus() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::checkStatus() Query Failed: id: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif // BAD. @@ -733,7 +733,7 @@ int bdNodeManager::checkStatus() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::checkStatus() Found Closest: id: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif @@ -747,7 +747,7 @@ int bdNodeManager::checkStatus() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::checkStatus() the Peer Online but Unreachable: id: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif @@ -761,7 +761,7 @@ int bdNodeManager::checkStatus() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::checkStatus() Found Query: id: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif //foundId = @@ -803,7 +803,7 @@ int bdNodeManager::checkStatus() doCallback = false; #ifdef DEBUG_MGR std::cerr << "bdNodeManager::checkStatus() Internal: no cb for id: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif } @@ -1024,7 +1024,7 @@ int bdNodeManager::getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &fro { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::getDhtPeerAddress() Id: "; - mFns->bdPrintNodeId(std::cerr, id); + mDhtFns->bdPrintNodeId(std::cerr, id); std::cerr << " ... ? TODO" << std::endl; #else (void) id; @@ -1034,7 +1034,7 @@ int bdNodeManager::getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &fro pit = mActivePeers.find(*id); std::cerr << "bdNodeManager::getDhtPeerAddress() Id: "; - mFns->bdPrintNodeId(std::cerr, id); + mDhtFns->bdPrintNodeId(std::cerr, id); std::cerr << std::endl; if (pit != mActivePeers.end()) @@ -1061,7 +1061,7 @@ int bdNodeManager::getDhtValue(const bdNodeId *id, std::string key, std::string { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::getDhtValue() Id: "; - mFns->bdPrintNodeId(std::cerr, id); + mDhtFns->bdPrintNodeId(std::cerr, id); std::cerr << " key: " << key; std::cerr << " ... ? TODO" << std::endl; #else @@ -1144,7 +1144,7 @@ void bdNodeManager::doNodeCallback(const bdId *id, uint32_t peerflags) { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::doNodeCallback() "; - mFns->bdPrintId(std::cerr, id); + mDhtFns->bdPrintId(std::cerr, id); std::cerr << "peerflags: " << peerflags; std::cerr << std::endl; #endif @@ -1163,7 +1163,7 @@ void bdNodeManager::doPeerCallback(const bdId *id, uint32_t status) #ifdef DEBUG_MGR std::cerr << "bdNodeManager::doPeerCallback()"; - mFns->bdPrintId(std::cerr, id); + mDhtFns->bdPrintId(std::cerr, id); std::cerr << "status: " << status; std::cerr << std::endl; #endif diff --git a/libbitdht/src/bitdht/bdmanager.h b/libbitdht/src/bitdht/bdmanager.h index d3ddb587f..c494b2bef 100644 --- a/libbitdht/src/bitdht/bdmanager.h +++ b/libbitdht/src/bitdht/bdmanager.h @@ -184,7 +184,7 @@ void SearchForLocalNet(); time_t mSearchTS; bool mSearchingDone; - bdDhtFunctions *mFns; + bdDhtFunctions *mDhtFns; uint32_t mNetworkSize; uint32_t mBdNetworkSize; From 2ebacf36edb06413938da6444653592c4f014f6a Mon Sep 17 00:00:00 2001 From: Phenom Date: Tue, 8 May 2018 12:08:08 +0200 Subject: [PATCH 103/161] Add Last Post Column in GroupTreeWidget. Hidden by default. A context menu is added. --- .../src/gui/common/GroupTreeWidget.cpp | 39 +++- .../src/gui/common/GroupTreeWidget.h | 4 +- .../src/gui/gxs/GxsGroupFrameDialog.cpp | 174 +++++++++--------- 3 files changed, 122 insertions(+), 95 deletions(-) diff --git a/retroshare-gui/src/gui/common/GroupTreeWidget.cpp b/retroshare-gui/src/gui/common/GroupTreeWidget.cpp index fc1cfae19..fd927983a 100644 --- a/retroshare-gui/src/gui/common/GroupTreeWidget.cpp +++ b/retroshare-gui/src/gui/common/GroupTreeWidget.cpp @@ -43,7 +43,8 @@ #define COLUMN_NAME 0 #define COLUMN_UNREAD 1 #define COLUMN_POPULARITY 2 -#define COLUMN_COUNT 3 +#define COLUMN_LAST_POST 3 +#define COLUMN_COUNT 4 #define COLUMN_DATA COLUMN_NAME #define ROLE_ID Qt::UserRole @@ -100,9 +101,12 @@ GroupTreeWidget::GroupTreeWidget(QWidget *parent) : /* Initialize tree widget */ ui->treeWidget->setColumnCount(COLUMN_COUNT); + ui->treeWidget->enableColumnCustomize(true); + ui->treeWidget->setColumnCustomizable(COLUMN_NAME, false); int S = QFontMetricsF(font()).height() ; int W = QFontMetricsF(font()).width("999") ; + int D = QFontMetricsF(font()).width("9999-99-99[]") ; /* Set header resize modes and initial section sizes */ QHeaderView *header = ui->treeWidget->header (); @@ -113,6 +117,15 @@ GroupTreeWidget::GroupTreeWidget(QWidget *parent) : header->resizeSection(COLUMN_UNREAD, W+4) ; QHeaderView_setSectionResizeModeColumn(header, COLUMN_POPULARITY, QHeaderView::Fixed); header->resizeSection(COLUMN_POPULARITY, 2*S) ; + QHeaderView_setSectionResizeModeColumn(header, COLUMN_LAST_POST, QHeaderView::Fixed); + header->resizeSection(COLUMN_LAST_POST, D+4) ; + header->setSectionHidden(COLUMN_LAST_POST, true); + + QTreeWidgetItem *headerItem = ui->treeWidget->headerItem(); + headerItem->setText(COLUMN_NAME, tr("Name")); + headerItem->setText(COLUMN_UNREAD, tr("Unread")); + headerItem->setText(COLUMN_POPULARITY, tr("Popularity")); + headerItem->setText(COLUMN_LAST_POST, tr("Last Post")); /* add filter actions */ ui->filterLineEdit->addFilter(QIcon(), tr("Title"), FILTER_NAME_INDEX , tr("Search Title")); @@ -192,9 +205,10 @@ void GroupTreeWidget::addToolButton(QToolButton *toolButton) ui->titleBarFrame->layout()->addWidget(toolButton); } -void GroupTreeWidget::processSettings(RshareSettings *settings, bool load) +// Load and save settings (group must be started from the caller) +void GroupTreeWidget::processSettings(bool load) { - if (settings == NULL) { + if (Settings == NULL) { return; } @@ -204,16 +218,18 @@ void GroupTreeWidget::processSettings(RshareSettings *settings, bool load) const int SORTBY_POSTS = 4; const int SORTBY_UNREAD = 5; + ui->treeWidget->processSettings(load); + if (load) { - // load settings + // load Settings // state of order - bool ascSort = settings->value("GroupAscSort", true).toBool(); + bool ascSort = Settings->value("GroupAscSort", true).toBool(); actionSortAscending->setChecked(ascSort); actionSortDescending->setChecked(!ascSort); // state of sort - int sortby = settings->value("GroupSortBy").toInt(); + int sortby = Settings->value("GroupSortBy").toInt(); switch (sortby) { case SORTBY_NAME: if (actionSortByName) { @@ -242,10 +258,10 @@ void GroupTreeWidget::processSettings(RshareSettings *settings, bool load) break; } } else { - // save settings + // save Settings // state of order - settings->setValue("GroupAscSort", !(actionSortDescending && actionSortDescending->isChecked())); //True by default + Settings->setValue("GroupAscSort", !(actionSortDescending && actionSortDescending->isChecked())); //True by default // state of sort int sortby = SORTBY_NAME; @@ -260,7 +276,7 @@ void GroupTreeWidget::processSettings(RshareSettings *settings, bool load) } else if (actionSortByUnread && actionSortByUnread->isChecked()) { sortby = SORTBY_UNREAD; } - settings->setValue("GroupSortBy", sortby); + Settings->setValue("GroupSortBy", sortby); } } @@ -453,6 +469,11 @@ void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList< /* Set last post */ qlonglong lastPost = itemInfo.lastpost.toTime_t(); item->setData(COLUMN_DATA, ROLE_LASTPOST, -lastPost); // negative for correct sorting + if(itemInfo.lastpost == QDateTime::fromTime_t(0)) + item->setText(COLUMN_LAST_POST, tr("Never")); + else + item->setText(COLUMN_LAST_POST, itemInfo.lastpost.toString(Qt::ISODate).replace("T"," ")); + /* Set visible posts */ item->setData(COLUMN_DATA, ROLE_POSTS, -itemInfo.max_visible_posts);// negative for correct sorting diff --git a/retroshare-gui/src/gui/common/GroupTreeWidget.h b/retroshare-gui/src/gui/common/GroupTreeWidget.h index d1a708258..acd2948f0 100644 --- a/retroshare-gui/src/gui/common/GroupTreeWidget.h +++ b/retroshare-gui/src/gui/common/GroupTreeWidget.h @@ -77,8 +77,8 @@ public: // Add a tool button to the tool area void addToolButton(QToolButton *toolButton); - // Load and save settings (group must be startet from the caller) - void processSettings(RshareSettings *settings, bool load); + // Load and save settings (group must be started from the caller) + void processSettings(bool load); // Add a new category item QTreeWidgetItem *addCategoryItem(const QString &name, const QIcon &icon, bool expand); diff --git a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp index fe2ce45be..3bae96cfd 100644 --- a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp +++ b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp @@ -194,7 +194,7 @@ void GxsGroupFrameDialog::processSettings(bool load) Settings->setValue("Splitter", ui->splitter->saveState()); } - ui->groupTreeWidget->processSettings(Settings, load); + ui->groupTreeWidget->processSettings(load); Settings->endGroup(); } @@ -253,94 +253,100 @@ void GxsGroupFrameDialog::todo() void GxsGroupFrameDialog::groupTreeCustomPopupMenu(QPoint point) { - QString id = ui->groupTreeWidget->itemIdAt(point); - if (id.isEmpty()) return; - - mGroupId = RsGxsGroupId(id.toStdString()); - int subscribeFlags = ui->groupTreeWidget->subscribeFlags(QString::fromStdString(mGroupId.toStdString())); - - bool isAdmin = IS_GROUP_ADMIN(subscribeFlags); - bool isPublisher = IS_GROUP_PUBLISHER(subscribeFlags); - bool isSubscribed = IS_GROUP_SUBSCRIBED(subscribeFlags); - QMenu contextMnu(this); - QAction *action; - - if (mMessageWidget) { - action = contextMnu.addAction(QIcon(IMAGE_TABNEW), tr("Open in new tab"), this, SLOT(openInNewTab())); - if (mGroupId.isNull() || messageWidget(mGroupId, true)) { - action->setEnabled(false); + QString id = ui->groupTreeWidget->itemIdAt(point); + if (!id.isEmpty()) + { + + mGroupId = RsGxsGroupId(id.toStdString()); + int subscribeFlags = ui->groupTreeWidget->subscribeFlags(QString::fromStdString(mGroupId.toStdString())); + + bool isAdmin = IS_GROUP_ADMIN(subscribeFlags); + bool isPublisher = IS_GROUP_PUBLISHER(subscribeFlags); + bool isSubscribed = IS_GROUP_SUBSCRIBED(subscribeFlags); + + + QAction *action; + + if (mMessageWidget) { + action = contextMnu.addAction(QIcon(IMAGE_TABNEW), tr("Open in new tab"), this, SLOT(openInNewTab())); + if (mGroupId.isNull() || messageWidget(mGroupId, true)) { + action->setEnabled(false); + } + } + + if (isSubscribed) { + action = contextMnu.addAction(QIcon(IMAGE_UNSUBSCRIBE), tr("Unsubscribe"), this, SLOT(unsubscribeGroup())); + action->setEnabled (!mGroupId.isNull() && IS_GROUP_SUBSCRIBED(subscribeFlags)); + } else { + action = contextMnu.addAction(QIcon(IMAGE_SUBSCRIBE), tr("Subscribe"), this, SLOT(subscribeGroup())); + action->setDisabled (mGroupId.isNull() || IS_GROUP_SUBSCRIBED(subscribeFlags)); + } + + contextMnu.addSeparator(); + + contextMnu.addAction(QIcon(icon(ICON_NEW)), text(TEXT_NEW), this, SLOT(newGroup())); + + action = contextMnu.addAction(QIcon(IMAGE_INFO), tr("Show Details"), this, SLOT(showGroupDetails())); + action->setEnabled (!mGroupId.isNull()); + + action = contextMnu.addAction(QIcon(IMAGE_EDIT), tr("Edit Details"), this, SLOT(editGroupDetails())); + action->setEnabled (!mGroupId.isNull() && isAdmin); + + uint32_t current_store_time = mInterface->getStoragePeriod(mGroupId)/86400 ; + uint32_t current_sync_time = mInterface->getSyncPeriod(mGroupId)/86400 ; + + std::cerr << "Got sync=" << current_sync_time << ". store=" << current_store_time << std::endl; + QAction *actnn = NULL; + + QMenu *ctxMenu2 = contextMnu.addMenu(tr("Synchronise posts of last...")) ; + actnn = ctxMenu2->addAction(tr(" 5 days" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 5)) ; if(current_sync_time == 5) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 2 weeks" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 15)) ; if(current_sync_time == 15) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 1 month" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 30)) ; if(current_sync_time == 30) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 3 months" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 90)) ; if(current_sync_time == 90) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 6 months" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant(180)) ; if(current_sync_time ==180) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 1 year " ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant(372)) ; if(current_sync_time ==372) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" Indefinitly"),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 0)) ; if(current_sync_time == 0) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + + ctxMenu2 = contextMnu.addMenu(tr("Store posts for at most...")) ; + actnn = ctxMenu2->addAction(tr(" 5 days" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 5)) ; if(current_store_time == 5) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 2 weeks" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 15)) ; if(current_store_time == 15) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 1 month" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 30)) ; if(current_store_time == 30) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 3 months" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 90)) ; if(current_store_time == 90) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 6 months" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant(180)) ; if(current_store_time ==180) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 1 year " ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant(372)) ; if(current_store_time ==372) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" Indefinitly"),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 0)) ; if(current_store_time == 0) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + + if (shareKeyType()) { + action = contextMnu.addAction(QIcon(IMAGE_SHARE), tr("Share publish permissions"), this, SLOT(sharePublishKey())); + action->setEnabled(!mGroupId.isNull() && isPublisher); + } + + if (getLinkType() != RetroShareLink::TYPE_UNKNOWN) { + action = contextMnu.addAction(QIcon(IMAGE_COPYLINK), tr("Copy RetroShare Link"), this, SLOT(copyGroupLink())); + action->setEnabled(!mGroupId.isNull()); + } + + contextMnu.addSeparator(); + + action = contextMnu.addAction(QIcon(":/images/message-mail-read.png"), tr("Mark all as read"), this, SLOT(markMsgAsRead())); + action->setEnabled (!mGroupId.isNull() && isSubscribed); + + action = contextMnu.addAction(QIcon(":/images/message-mail.png"), tr("Mark all as unread"), this, SLOT(markMsgAsUnread())); + action->setEnabled (!mGroupId.isNull() && isSubscribed); + + /* Add special actions */ + QList actions; + groupTreeCustomActions(mGroupId, subscribeFlags, actions); + if (!actions.isEmpty()) { + contextMnu.addSeparator(); + contextMnu.addActions(actions); } } - if (isSubscribed) { - action = contextMnu.addAction(QIcon(IMAGE_UNSUBSCRIBE), tr("Unsubscribe"), this, SLOT(unsubscribeGroup())); - action->setEnabled (!mGroupId.isNull() && IS_GROUP_SUBSCRIBED(subscribeFlags)); - } else { - action = contextMnu.addAction(QIcon(IMAGE_SUBSCRIBE), tr("Subscribe"), this, SLOT(subscribeGroup())); - action->setDisabled (mGroupId.isNull() || IS_GROUP_SUBSCRIBED(subscribeFlags)); - } - - contextMnu.addSeparator(); - - contextMnu.addAction(QIcon(icon(ICON_NEW)), text(TEXT_NEW), this, SLOT(newGroup())); - - action = contextMnu.addAction(QIcon(IMAGE_INFO), tr("Show Details"), this, SLOT(showGroupDetails())); - action->setEnabled (!mGroupId.isNull()); - - action = contextMnu.addAction(QIcon(IMAGE_EDIT), tr("Edit Details"), this, SLOT(editGroupDetails())); - action->setEnabled (!mGroupId.isNull() && isAdmin); - - uint32_t current_store_time = mInterface->getStoragePeriod(mGroupId)/86400 ; - uint32_t current_sync_time = mInterface->getSyncPeriod(mGroupId)/86400 ; - - std::cerr << "Got sync=" << current_sync_time << ". store=" << current_store_time << std::endl; - QAction *actnn = NULL; - - QMenu *ctxMenu2 = contextMnu.addMenu(tr("Synchronise posts of last...")) ; - actnn = ctxMenu2->addAction(tr(" 5 days" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 5)) ; if(current_sync_time == 5) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 2 weeks" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 15)) ; if(current_sync_time == 15) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 1 month" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 30)) ; if(current_sync_time == 30) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 3 months" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 90)) ; if(current_sync_time == 90) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 6 months" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant(180)) ; if(current_sync_time ==180) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 1 year " ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant(372)) ; if(current_sync_time ==372) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" Indefinitly"),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 0)) ; if(current_sync_time == 0) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - - ctxMenu2 = contextMnu.addMenu(tr("Store posts for at most...")) ; - actnn = ctxMenu2->addAction(tr(" 5 days" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 5)) ; if(current_store_time == 5) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 2 weeks" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 15)) ; if(current_store_time == 15) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 1 month" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 30)) ; if(current_store_time == 30) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 3 months" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 90)) ; if(current_store_time == 90) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 6 months" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant(180)) ; if(current_store_time ==180) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 1 year " ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant(372)) ; if(current_store_time ==372) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" Indefinitly"),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 0)) ; if(current_store_time == 0) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - - if (shareKeyType()) { - action = contextMnu.addAction(QIcon(IMAGE_SHARE), tr("Share publish permissions"), this, SLOT(sharePublishKey())); - action->setEnabled(!mGroupId.isNull() && isPublisher); - } - - if (getLinkType() != RetroShareLink::TYPE_UNKNOWN) { - action = contextMnu.addAction(QIcon(IMAGE_COPYLINK), tr("Copy RetroShare Link"), this, SLOT(copyGroupLink())); - action->setEnabled(!mGroupId.isNull()); - } - - contextMnu.addSeparator(); - - action = contextMnu.addAction(QIcon(":/images/message-mail-read.png"), tr("Mark all as read"), this, SLOT(markMsgAsRead())); - action->setEnabled (!mGroupId.isNull() && isSubscribed); - - action = contextMnu.addAction(QIcon(":/images/message-mail.png"), tr("Mark all as unread"), this, SLOT(markMsgAsUnread())); - action->setEnabled (!mGroupId.isNull() && isSubscribed); - - /* Add special actions */ - QList actions; - groupTreeCustomActions(mGroupId, subscribeFlags, actions); - if (!actions.isEmpty()) { - contextMnu.addSeparator(); - contextMnu.addActions(actions); - } + //Add Standard Menu + ui->groupTreeWidget->treeWidget()->createStandardContextMenu(&contextMnu); contextMnu.exec(QCursor::pos()); } From 186ff0f21ad65282441cd66662de10b14887db4d Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 9 May 2018 10:12:34 +0200 Subject: [PATCH 104/161] created proper debian changelog using dch and updated project url --- build_scripts/Debian+Ubuntu/debian/changelog | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 build_scripts/Debian+Ubuntu/debian/changelog diff --git a/build_scripts/Debian+Ubuntu/debian/changelog b/build_scripts/Debian+Ubuntu/debian/changelog new file mode 100644 index 000000000..6879b6bd0 --- /dev/null +++ b/build_scripts/Debian+Ubuntu/debian/changelog @@ -0,0 +1,5 @@ +retroshare (0.6.4-1) UNRELEASED; urgency=medium + + * Initial release for Debian. (Closes: #659069) + + -- Cyril Soler Wed, 09 May 2018 10:11:31 +0200 From f406d9af1625ae1098881403c4b5c2a430d75441 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 9 May 2018 10:15:02 +0200 Subject: [PATCH 105/161] updated debian/control to be the control file for stretch. Updated copyright notice --- build_scripts/Debian+Ubuntu/debian/control | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build_scripts/Debian+Ubuntu/debian/control b/build_scripts/Debian+Ubuntu/debian/control index 3d1aaec9d..f5bbc9fc0 100644 --- a/build_scripts/Debian+Ubuntu/debian/control +++ b/build_scripts/Debian+Ubuntu/debian/control @@ -2,14 +2,14 @@ Source: retroshare Section: devel Priority: standard Maintainer: Cyril Soler -Build-Depends: debhelper (>= 7), libglib2.0-dev, libupnp-dev, qt4-dev-tools, libqt4-dev, libssl-dev, libxss-dev, libgnome-keyring-dev, libbz2-dev, libqt4-opengl-dev, libqtmultimediakit1, qtmobility-dev, libspeex-dev, libspeexdsp-dev, libxslt1-dev, cmake, libcurl4-openssl-dev, libopencv-dev, tcl8.5, libsqlcipher-dev, libmicrohttpd-dev, libavcodec-dev -Standards-Version: 3.9.3 +Build-Depends: debhelper (>= 7), libglib2.0-dev, libupnp-dev, libssl-dev, libxss-dev, libgnome-keyring-dev, libbz2-dev, libspeex-dev, libspeexdsp-dev, libxslt1-dev, cmake, libcurl4-openssl-dev, libopencv-dev, tcl8.6, libsqlcipher-dev (>= 3.4.1), libmicrohttpd-dev, libavcodec-dev, qtmultimedia5-dev, qttools5-dev, libqt5x11extras5-dev, qt5-default +Standards-Version: 3.9.6 Homepage: http://retroshare.sourceforge.net Package: retroshare-voip-plugin Architecture: any Conflicts: retroshare06-voip-plugin -Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare, libspeex1, libspeexdsp1, libqtmultimediakit1 +Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare, libspeex1, libspeexdsp1, libqt5multimedia5 Description: RetroShare VOIP plugin This package provides a plugin for RetroShare, a secured Friend-to-Friend communication plateform. The plugin adds voice-over-IP functionality to the private chat window. Both From 889c27726e7d8c0728bfaca00c15571785cc92f2 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 9 May 2018 10:15:32 +0200 Subject: [PATCH 106/161] fixed packaging script for debian release --- .../Debian+Ubuntu/makeSourcePackage.sh | 60 +++++++++++-------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh index ddb699680..8a3220bc3 100755 --- a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh +++ b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh @@ -4,7 +4,7 @@ gitpath="https://github.com/RetroShare/RetroShare.git" branch="master" #branch="v0.6.3-official_release" -#bubba3="Y" # comment out to compile for bubba3 +#bubba3="Y" # comment out to compile for bubba3 ###################################################### RS_MAJOR_VERSION=`fgrep RS_MAJOR_VERSION ../../libretroshare/src/retroshare/rsversion.h | cut -d\\ -f3- | sed -e s\/\ \/\/g | cut -c1` @@ -44,8 +44,11 @@ while [ ${#} -gt 0 ]; do rev=${1} shift ;; +# "-debian") shift +# debian="true" +# ;; "-retrotor") shift - useretrotor="true" + useretrotor="true" ;; "-distribution") shift dist=${1} @@ -68,16 +71,16 @@ while [ ${#} -gt 0 ]; do done if test "${useretrotor}" = "true"; then - if ! test "${dist}" = "trusty"; then - echo ERROR: retro-tor can only be packaged for trusty for now. - exit 1; - fi - #gitpath="https://github.com/csoler/RetroShare.git" - #branch="v0.6-TorOnly" + if ! test "${dist}" = "trusty"; then + echo ERROR: retro-tor can only be packaged for trusty for now. + exit 1; + fi + #gitpath="https://github.com/csoler/RetroShare.git" + #branch="v0.6-TorOnly" fi if test "${dist}" = "" ; then - dist="trusty xenial artful bionic" + dist="trusty xenial artful bionic" fi echo Attempting to get revision number... @@ -96,7 +99,7 @@ echo " Using distributions:"${dist} echo " Using PGP key id :"${gpgkey} if test ${useretrotor} = "true"; then - echo " "Specific flags : retrotor + echo " "Specific flags : retrotor fi echo Done. @@ -115,15 +118,15 @@ cd ${workdir}/src git clone --depth 1 ${gitpath} --single-branch --branch $branch . # if ! test "$hhsh" = "" ; then -# echo Checking out revision $hhsh -# git checkout $hhsh +# echo Checking out revision $hhsh +# git checkout $hhsh # fi cd - if ! test -d ${workdir}/src/libretroshare/; then - echo Git clone failed. - exit + echo Git clone failed. + exit fi cp -r debian ${workdir}/debian @@ -147,21 +150,26 @@ echo Cleaning... echo Calling debuild... for i in ${dist}; do - echo copying changelog for ${i} - sed -e s/XXXXXX/"${rev}"/g -e s/YYYYYY/"${i}"/g -e s/ZZZZZZ/"${version_number}"/g ../changelog > debian/changelog - if test ${useretrotor} = "true"; then - cp ../rules.retrotor debian/rules - cp ../control.trusty_retrotor debian/control - elif test -f ../control."${i}" ; then - echo \/\!\\ Using specific control file for distribution "${i}" - cp ../control."${i}" debian/control + if ! test "${i}" = "debian"; then + echo copying changelog for ${i} + sed -e s/XXXXXX/"${rev}"/g -e s/YYYYYY/"${i}"/g -e s/ZZZZZZ/"${version_number}"/g ../changelog > debian/changelog + + if test ${useretrotor} = "true"; then + cp ../rules.retrotor debian/rules + cp ../control.trusty_retrotor debian/control + elif test -f ../control."${i}" ; then + echo \/\!\\ Using specific control file for distribution "${i}" + cp ../control."${i}" debian/control + else + echo Using standard control file control."${i}" for distribution "${i}" + cp ../debian/control debian/control + fi else - echo Using standard control file control."${i}" for distribution "${i}" - cp ../debian/control debian/control - fi + echo creating official debian release. Using built-in changelog and control files + fi - debuild -S -k${gpgkey} + debuild -S -k${gpgkey} --lintian-opts +pedantic -EviIL done cd - From 4d287d68bc9725f403dc7d952a927d401c5d6c97 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 9 May 2018 13:27:16 +0200 Subject: [PATCH 107/161] fixed weird mistake in grouter which causes an issue only in gcc > 8 --- libretroshare/src/grouter/p3grouter.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libretroshare/src/grouter/p3grouter.cc b/libretroshare/src/grouter/p3grouter.cc index 75e8fd4d6..d184b15f5 100644 --- a/libretroshare/src/grouter/p3grouter.cc +++ b/libretroshare/src/grouter/p3grouter.cc @@ -540,7 +540,7 @@ if(itm == NULL) void GRouterTunnelInfo::removeVirtualPeer(const TurtleVirtualPeerId& vpid) { - std::set::iterator it = virtual_peers.find(vpid) ; + std::set::iterator it = virtual_peers.find(vpid) ; if(it == virtual_peers.end()) { From e81c82efb8f81007b1fa2ddc5edd427f6a955371 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 9 May 2018 13:37:25 +0200 Subject: [PATCH 108/161] added notes file for debian release --- .../Debian+Ubuntu/debian_release_howto.txt | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 build_scripts/Debian+Ubuntu/debian_release_howto.txt diff --git a/build_scripts/Debian+Ubuntu/debian_release_howto.txt b/build_scripts/Debian+Ubuntu/debian_release_howto.txt new file mode 100644 index 000000000..438750265 --- /dev/null +++ b/build_scripts/Debian+Ubuntu/debian_release_howto.txt @@ -0,0 +1,13 @@ +Creation of a new Debian changelog: + + dch --create --package retroshare --newversion 0.6.4-1 + +dget command to retrieve source package: + + dget -u https://launchpad.net/~retroshare/+archive/ubuntu/stable/+files/retroshare_0.6.4-1.20180313.0e6d27ad~xenial.dsc + + (-u means don't check PGP signature) + + +When ready: + * updload the package in a place that can be used to dget the package on mentors.debian.net. From b4ada807c805ba625e12c5a18513584598d6899e Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 9 May 2018 13:54:40 +0200 Subject: [PATCH 109/161] removed > 3.4.1 in debian/control, because pbuilder would not build --- build_scripts/Debian+Ubuntu/debian/control | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_scripts/Debian+Ubuntu/debian/control b/build_scripts/Debian+Ubuntu/debian/control index f5bbc9fc0..80b452523 100644 --- a/build_scripts/Debian+Ubuntu/debian/control +++ b/build_scripts/Debian+Ubuntu/debian/control @@ -2,7 +2,7 @@ Source: retroshare Section: devel Priority: standard Maintainer: Cyril Soler -Build-Depends: debhelper (>= 7), libglib2.0-dev, libupnp-dev, libssl-dev, libxss-dev, libgnome-keyring-dev, libbz2-dev, libspeex-dev, libspeexdsp-dev, libxslt1-dev, cmake, libcurl4-openssl-dev, libopencv-dev, tcl8.6, libsqlcipher-dev (>= 3.4.1), libmicrohttpd-dev, libavcodec-dev, qtmultimedia5-dev, qttools5-dev, libqt5x11extras5-dev, qt5-default +Build-Depends: debhelper (>= 7), libglib2.0-dev, libupnp-dev, libssl-dev, libxss-dev, libgnome-keyring-dev, libbz2-dev, libspeex-dev, libspeexdsp-dev, libxslt1-dev, cmake, libcurl4-openssl-dev, libopencv-dev, tcl8.6, libsqlcipher-dev, libmicrohttpd-dev, libavcodec-dev, qtmultimedia5-dev, qttools5-dev, libqt5x11extras5-dev, qt5-default Standards-Version: 3.9.6 Homepage: http://retroshare.sourceforge.net From 45fdbb446659a089c91418b1d580f31f5af527d3 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 10 May 2018 22:03:39 +0200 Subject: [PATCH 110/161] fixed email to match mentors login --- build_scripts/Debian+Ubuntu/debian/changelog | 2 +- build_scripts/Debian+Ubuntu/debian_release_howto.txt | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/build_scripts/Debian+Ubuntu/debian/changelog b/build_scripts/Debian+Ubuntu/debian/changelog index 6879b6bd0..dfcf8f9d7 100644 --- a/build_scripts/Debian+Ubuntu/debian/changelog +++ b/build_scripts/Debian+Ubuntu/debian/changelog @@ -2,4 +2,4 @@ retroshare (0.6.4-1) UNRELEASED; urgency=medium * Initial release for Debian. (Closes: #659069) - -- Cyril Soler Wed, 09 May 2018 10:11:31 +0200 + -- Cyril Soler Wed, 09 May 2018 10:11:31 +0200 diff --git a/build_scripts/Debian+Ubuntu/debian_release_howto.txt b/build_scripts/Debian+Ubuntu/debian_release_howto.txt index 438750265..5f98d05d7 100644 --- a/build_scripts/Debian+Ubuntu/debian_release_howto.txt +++ b/build_scripts/Debian+Ubuntu/debian_release_howto.txt @@ -11,3 +11,6 @@ dget command to retrieve source package: When ready: * updload the package in a place that can be used to dget the package on mentors.debian.net. + + dput mentors retroshare_0.6.4-1_source.changes + From 9f5409b4f7bea61df04a47d962f8e56d6d8247a9 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 10 May 2018 22:04:11 +0200 Subject: [PATCH 111/161] fixed email to match mentors login --- build_scripts/Debian+Ubuntu/debian/changelog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_scripts/Debian+Ubuntu/debian/changelog b/build_scripts/Debian+Ubuntu/debian/changelog index dfcf8f9d7..c4dd0e732 100644 --- a/build_scripts/Debian+Ubuntu/debian/changelog +++ b/build_scripts/Debian+Ubuntu/debian/changelog @@ -2,4 +2,4 @@ retroshare (0.6.4-1) UNRELEASED; urgency=medium * Initial release for Debian. (Closes: #659069) - -- Cyril Soler Wed, 09 May 2018 10:11:31 +0200 + -- Cyril Soler Wed, 09 May 2018 10:11:31 +0200 From c2492fe166417ed4abb858e5c31a18094fc23242 Mon Sep 17 00:00:00 2001 From: cyril soler Date: Thu, 10 May 2018 23:10:16 +0200 Subject: [PATCH 112/161] fixed a few lintian errors in debian/control --- build_scripts/Debian+Ubuntu/debian/control | 24 +++++++++------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/build_scripts/Debian+Ubuntu/debian/control b/build_scripts/Debian+Ubuntu/debian/control index 80b452523..ecc66b527 100644 --- a/build_scripts/Debian+Ubuntu/debian/control +++ b/build_scripts/Debian+Ubuntu/debian/control @@ -2,8 +2,9 @@ Source: retroshare Section: devel Priority: standard Maintainer: Cyril Soler -Build-Depends: debhelper (>= 7), libglib2.0-dev, libupnp-dev, libssl-dev, libxss-dev, libgnome-keyring-dev, libbz2-dev, libspeex-dev, libspeexdsp-dev, libxslt1-dev, cmake, libcurl4-openssl-dev, libopencv-dev, tcl8.6, libsqlcipher-dev, libmicrohttpd-dev, libavcodec-dev, qtmultimedia5-dev, qttools5-dev, libqt5x11extras5-dev, qt5-default -Standards-Version: 3.9.6 +Build-Depends: debhelper (>= 9), libglib2.0-dev, libupnp-dev, libssl-dev, libxss-dev, libgnome-keyring-dev, libbz2-dev, libspeex-dev, libspeexdsp-dev, libxslt1-dev, cmake, libcurl4-openssl-dev, libopencv-dev, tcl8.6, libsqlcipher-dev, libmicrohttpd-dev, libavcodec-dev, qtmultimedia5-dev, qttools5-dev, libqt5x11extras5-dev, qt5-default, qtbase5-dev, qt5-qmake, qtbase5-dev-tools + +Standards-Version: 4.1.4 Homepage: http://retroshare.sourceforge.net Package: retroshare-voip-plugin @@ -11,7 +12,7 @@ Architecture: any Conflicts: retroshare06-voip-plugin Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare, libspeex1, libspeexdsp1, libqt5multimedia5 Description: RetroShare VOIP plugin - This package provides a plugin for RetroShare, a secured Friend-to-Friend communication + This package provides a plugin for RetroShare, a secured Friend-to-Friend communication plateform. The plugin adds voice-over-IP functionality to the private chat window. Both friends chatting together need the plugin installed to be able to talk together. @@ -20,27 +21,22 @@ Architecture: any Conflicts: retroshare06-feedreader-plugin Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare Description: RetroShare FeedReader plugin - This package provides a plugin for RetroShare, a secured Friend-to-Friend communication - plateform. The plugin adds a RSS feed reader tab to retroshare. + Plugin for Retroshare, adding a RSS feed reader tab to retroshare. Package: retroshare-nogui Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, gnome-keyring Conflicts: retroshare,retroshare06-nogui -Description: Secure communication with friends - This is the command-line client for RetroShare network. This client - can be contacted and talked-to using SSL. Clients exist for portable - devices running e.g. Android. +Description: headless version of Retroshare + Headless version of the Retroshare platform. Package: retroshare Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, gnome-keyring Conflicts: retroshare-nogui,retroshare06 Description: Secure communication with friends - RetroShare is a Open Source cross-platform, private and secure decentralised - commmunication platform. It lets you to securely chat and share files with your - friends and family, using a web-of-trust to authenticate peers and OpenSSL to - encrypt all communication. RetroShare provides filesharing, chat, messages, - forums and channels. + RetroShare is a Open Source, private and secure decentralised + commmunication platform. It creates mesh of computers linked with TLS connections, + on top of which it provides file transfer, asynchronous email, forums, channels and chat. From 4b6304e341c86cf5f8c5375fb6bdeb059fcff9e5 Mon Sep 17 00:00:00 2001 From: cyril soler Date: Fri, 11 May 2018 16:33:19 +0200 Subject: [PATCH 113/161] few fixes according to lintian complaints --- build_scripts/Debian+Ubuntu/debian/control | 3 +-- build_scripts/Debian+Ubuntu/debian/rules | 2 ++ build_scripts/Debian+Ubuntu/makeSourcePackage.sh | 7 ++++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/build_scripts/Debian+Ubuntu/debian/control b/build_scripts/Debian+Ubuntu/debian/control index ecc66b527..68b7e8a68 100644 --- a/build_scripts/Debian+Ubuntu/debian/control +++ b/build_scripts/Debian+Ubuntu/debian/control @@ -2,8 +2,7 @@ Source: retroshare Section: devel Priority: standard Maintainer: Cyril Soler -Build-Depends: debhelper (>= 9), libglib2.0-dev, libupnp-dev, libssl-dev, libxss-dev, libgnome-keyring-dev, libbz2-dev, libspeex-dev, libspeexdsp-dev, libxslt1-dev, cmake, libcurl4-openssl-dev, libopencv-dev, tcl8.6, libsqlcipher-dev, libmicrohttpd-dev, libavcodec-dev, qtmultimedia5-dev, qttools5-dev, libqt5x11extras5-dev, qt5-default, qtbase5-dev, qt5-qmake, qtbase5-dev-tools - +Build-Depends: debhelper (>= 9), libglib2.0-dev, libupnp-dev, libssl-dev, libxss-dev, libgnome-keyring-dev, libbz2-dev, libspeex-dev, libspeexdsp-dev, libxslt1-dev, cmake, libcurl4-openssl-dev, libopencv-dev, tcl8.6, libsqlcipher-dev, libmicrohttpd-dev, libavcodec-dev, qtmultimedia5-dev, qttools5-dev, libqt5x11extras5-dev, qtbase5-dev, qt5-qmake, qtbase5-dev-tools Standards-Version: 4.1.4 Homepage: http://retroshare.sourceforge.net diff --git a/build_scripts/Debian+Ubuntu/debian/rules b/build_scripts/Debian+Ubuntu/debian/rules index ef740237e..6e52cdb98 100755 --- a/build_scripts/Debian+Ubuntu/debian/rules +++ b/build_scripts/Debian+Ubuntu/debian/rules @@ -20,6 +20,8 @@ build-stamp: configure-stamp cd src && $(MAKE) touch $@ +build_indep: build + clean: dh_testdir dh_testroot diff --git a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh index 8a3220bc3..f42e590cf 100755 --- a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh +++ b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh @@ -2,7 +2,8 @@ ###################### PARAMETERS #################### gitpath="https://github.com/RetroShare/RetroShare.git" -branch="master" +branch="v0.6.4-official_release" +#branch="master" #branch="v0.6.3-official_release" #bubba3="Y" # comment out to compile for bubba3 ###################################################### @@ -134,6 +135,10 @@ cp -r debian ${workdir}/debian # VOIP tweak cp ${workdir}/src/retroshare-gui/src/gui/chat/PopupChatDialog.ui ${workdir}/src/plugins/VOIP/gui/PopupChatDialog.ui +# remove unised qml code, only needed on Android + +rm -rf ${workdir}/src/retroshare-qml-app/ + # Cloning sqlcipher # git clone https://github.com/sqlcipher/sqlcipher.git From 84a02cb7fa552fd1e0a704ce9871476c32e2783f Mon Sep 17 00:00:00 2001 From: cyril soler Date: Fri, 11 May 2018 16:39:49 +0200 Subject: [PATCH 114/161] added missing targets in rules file --- build_scripts/Debian+Ubuntu/debian/rules | 6 ++++-- build_scripts/Debian+Ubuntu/makeSourcePackage.sh | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/build_scripts/Debian+Ubuntu/debian/rules b/build_scripts/Debian+Ubuntu/debian/rules index 6e52cdb98..0b90fecf5 100755 --- a/build_scripts/Debian+Ubuntu/debian/rules +++ b/build_scripts/Debian+Ubuntu/debian/rules @@ -22,18 +22,20 @@ build-stamp: configure-stamp build_indep: build +build_arch: build + clean: dh_testdir dh_testroot rm -f configure-stamp build-stamp # Add here commands to clean up after the build process. [ ! -f src/Makefile ] || (cd src && $(MAKE) distclean) - dh_clean + dh_prep install: build dh_testdir dh_testroot - dh_clean -k + dh_prep #dh_installdirs cd src && $(MAKE) INSTALL_ROOT=$(CURDIR)/debian/tmp install diff --git a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh index f42e590cf..e4d4b15a3 100755 --- a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh +++ b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh @@ -138,6 +138,7 @@ cp ${workdir}/src/retroshare-gui/src/gui/chat/PopupChatDialog.ui ${workdir}/src/ # remove unised qml code, only needed on Android rm -rf ${workdir}/src/retroshare-qml-app/ +rm -rf ${workdir}/src/build_scripts/ # Cloning sqlcipher # git clone https://github.com/sqlcipher/sqlcipher.git From 2173aab48378a941d6a4eba1c92b4f8acc39dd84 Mon Sep 17 00:00:00 2001 From: cyril soler Date: Fri, 11 May 2018 17:02:58 +0200 Subject: [PATCH 115/161] fixed rules file again --- build_scripts/Debian+Ubuntu/debian/compat | 2 +- build_scripts/Debian+Ubuntu/debian/rules | 9 ++++++--- build_scripts/Debian+Ubuntu/makeSourcePackage.sh | 1 + 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/build_scripts/Debian+Ubuntu/debian/compat b/build_scripts/Debian+Ubuntu/debian/compat index 7f8f011eb..ec635144f 100644 --- a/build_scripts/Debian+Ubuntu/debian/compat +++ b/build_scripts/Debian+Ubuntu/debian/compat @@ -1 +1 @@ -7 +9 diff --git a/build_scripts/Debian+Ubuntu/debian/rules b/build_scripts/Debian+Ubuntu/debian/rules index 0b90fecf5..f110264ae 100755 --- a/build_scripts/Debian+Ubuntu/debian/rules +++ b/build_scripts/Debian+Ubuntu/debian/rules @@ -7,7 +7,8 @@ configure-stamp: touch $@ -build: build-stamp +build: build-arch build-indep + build-stamp: configure-stamp dh_testdir # Add here commands to compile the package. @@ -20,9 +21,9 @@ build-stamp: configure-stamp cd src && $(MAKE) touch $@ -build_indep: build +build-indep: build-stamp -build_arch: build +build-arch: build-stamp clean: dh_testdir @@ -31,11 +32,13 @@ clean: # Add here commands to clean up after the build process. [ ! -f src/Makefile ] || (cd src && $(MAKE) distclean) dh_prep + dh_clean install: build dh_testdir dh_testroot dh_prep + dh_clean #dh_installdirs cd src && $(MAKE) INSTALL_ROOT=$(CURDIR)/debian/tmp install diff --git a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh index e4d4b15a3..bb9ced05a 100755 --- a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh +++ b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh @@ -139,6 +139,7 @@ cp ${workdir}/src/retroshare-gui/src/gui/chat/PopupChatDialog.ui ${workdir}/src/ rm -rf ${workdir}/src/retroshare-qml-app/ rm -rf ${workdir}/src/build_scripts/ +rm ${workdir}/debian/*~ # Cloning sqlcipher # git clone https://github.com/sqlcipher/sqlcipher.git From fe5ea9e07c97dece41a39cd2192ddf55820984c5 Mon Sep 17 00:00:00 2001 From: cyril soler Date: Fri, 11 May 2018 17:37:15 +0200 Subject: [PATCH 116/161] additional fixes to debian packaging files --- build_scripts/Debian+Ubuntu/debian/changelog | 2 +- build_scripts/Debian+Ubuntu/debian/compat | 2 +- build_scripts/Debian+Ubuntu/debian/rules | 2 +- build_scripts/Debian+Ubuntu/debian/source/format | 1 + 4 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 build_scripts/Debian+Ubuntu/debian/source/format diff --git a/build_scripts/Debian+Ubuntu/debian/changelog b/build_scripts/Debian+Ubuntu/debian/changelog index c4dd0e732..5d7671fc3 100644 --- a/build_scripts/Debian+Ubuntu/debian/changelog +++ b/build_scripts/Debian+Ubuntu/debian/changelog @@ -1,4 +1,4 @@ -retroshare (0.6.4-1) UNRELEASED; urgency=medium +retroshare (0.6.4) UNRELEASED; urgency=medium * Initial release for Debian. (Closes: #659069) diff --git a/build_scripts/Debian+Ubuntu/debian/compat b/build_scripts/Debian+Ubuntu/debian/compat index ec635144f..f599e28b8 100644 --- a/build_scripts/Debian+Ubuntu/debian/compat +++ b/build_scripts/Debian+Ubuntu/debian/compat @@ -1 +1 @@ -9 +10 diff --git a/build_scripts/Debian+Ubuntu/debian/rules b/build_scripts/Debian+Ubuntu/debian/rules index f110264ae..2e59d918e 100755 --- a/build_scripts/Debian+Ubuntu/debian/rules +++ b/build_scripts/Debian+Ubuntu/debian/rules @@ -12,7 +12,7 @@ build: build-arch build-indep build-stamp: configure-stamp dh_testdir # Add here commands to compile the package. - # cd libssh-0.6.4 && mkdir -p build && cd build && cmake -DWITH_STATIC_LIB=ON .. && make + # cd libssh-0.6.4 && mkdir -p build && cd build && cmake -DWITH_STATIC_LIB=ON .. && make # cd sqlcipher && ./configure --disable-shared --enable-static --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto" && make # mkdir lib # cp -r libssh-0.6.4 lib/ diff --git a/build_scripts/Debian+Ubuntu/debian/source/format b/build_scripts/Debian+Ubuntu/debian/source/format new file mode 100644 index 000000000..d3827e75a --- /dev/null +++ b/build_scripts/Debian+Ubuntu/debian/source/format @@ -0,0 +1 @@ +1.0 From ecbd115873e2ea674865d6e3b7f9df855dfd933b Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 11 May 2018 20:24:24 +0200 Subject: [PATCH 117/161] added missing include path for libretroshare/src in libresapi --- libresapi/src/libresapi.pro | 1 + 1 file changed, 1 insertion(+) diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index 15a2faf5c..2733cffa2 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -13,6 +13,7 @@ DESTDIR = lib # in the meantime, they are part of the RS directory so that it is always possible to find them INCLUDEPATH += ../../rapidjson-1.1.0 +INCLUDEPATH += ../../libretroshare/src libresapilocalserver { CONFIG *= qt From 57cff618734fc21850dad4e16b682382465d8363 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 11 May 2018 20:28:53 +0200 Subject: [PATCH 118/161] fixed merge --- .../Debian+Ubuntu/debian_release_howto.txt | 18 +++++++++++++++++- .../Debian+Ubuntu/makeSourcePackage.sh | 6 +----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/build_scripts/Debian+Ubuntu/debian_release_howto.txt b/build_scripts/Debian+Ubuntu/debian_release_howto.txt index 5f98d05d7..f77a9dd3e 100644 --- a/build_scripts/Debian+Ubuntu/debian_release_howto.txt +++ b/build_scripts/Debian+Ubuntu/debian_release_howto.txt @@ -2,15 +2,31 @@ Creation of a new Debian changelog: dch --create --package retroshare --newversion 0.6.4-1 + Note: dch reads email in $DEBMAIL or $USER@$HOSTNAME, so it should be made correct in debian/changlog + If the email does not match the email in mentors, the package will be rejected. + dget command to retrieve source package: dget -u https://launchpad.net/~retroshare/+archive/ubuntu/stable/+files/retroshare_0.6.4-1.20180313.0e6d27ad~xenial.dsc (-u means don't check PGP signature) - When ready: * updload the package in a place that can be used to dget the package on mentors.debian.net. dput mentors retroshare_0.6.4-1_source.changes +Checkign with lintian: + lintian --pedantic --profile debian retroshare_0.6.4-1_source.changes + +Todo + * make a sid binary package. + * test in sid using pbuilder chroot system (pbuilder login) + * upload to mentors + * request for sponsorship + +Getting help: + https://webchat.oftc.net/ + +Bug creation/report + reportbug -B debian diff --git a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh index bb9ced05a..2cb83425d 100755 --- a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh +++ b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh @@ -2,9 +2,8 @@ ###################### PARAMETERS #################### gitpath="https://github.com/RetroShare/RetroShare.git" -branch="v0.6.4-official_release" #branch="master" -#branch="v0.6.3-official_release" +branch="v0.6.4-official_release" #bubba3="Y" # comment out to compile for bubba3 ###################################################### @@ -45,9 +44,6 @@ while [ ${#} -gt 0 ]; do rev=${1} shift ;; -# "-debian") shift -# debian="true" -# ;; "-retrotor") shift useretrotor="true" ;; From f12cd5774d696b3635bb8f206862e96a97487861 Mon Sep 17 00:00:00 2001 From: Phenom Date: Sat, 12 May 2018 14:31:17 +0200 Subject: [PATCH 119/161] Fix Partial Dir Check Add WrongValue StyleSheet property, when bad directory selected. Update QLineEdit with current setting so it's possible to see if something is modified. No need to restart. --- libretroshare/src/ft/ftcontroller.cc | 30 ++++----- libretroshare/src/ft/ftcontroller.h | 4 +- libretroshare/src/ft/ftserver.cc | 61 ++++++++++++------- libretroshare/src/ft/ftserver.h | 4 +- libretroshare/src/retroshare/rsfiles.h | 4 +- .../src/gui/qss/stylesheet/qss.default | 6 ++ .../src/gui/settings/TransferPage.cpp | 56 ++++++++++++----- retroshare-gui/src/qss/blacknight.qss | 7 +++ retroshare-gui/src/qss/qdarkstyle.qss | 6 ++ 9 files changed, 120 insertions(+), 58 deletions(-) diff --git a/libretroshare/src/ft/ftcontroller.cc b/libretroshare/src/ft/ftcontroller.cc index f1500036f..9abec040f 100644 --- a/libretroshare/src/ft/ftcontroller.cc +++ b/libretroshare/src/ft/ftcontroller.cc @@ -1472,24 +1472,24 @@ bool ftController::setPartialsDirectory(std::string path) /* check it is not a subdir of download / shared directories (BAD) - TODO */ { - RsStackMutex stack(ctrlMutex); + RsStackMutex stack(ctrlMutex); - path = RsDirUtil::convertPathToUnix(path); + path = RsDirUtil::convertPathToUnix(path); - if (path.find(mDownloadPath) == std::string::npos) { - return false; - } + if (path.find(mDownloadPath) != std::string::npos) { + return false; + } - if (rsFiles) { - std::list::iterator it; - std::list dirs; - rsFiles->getSharedDirectories(dirs); - for (it = dirs.begin(); it != dirs.end(); ++it) { - if (path.find((*it).filename) == std::string::npos) { - return false; - } - } - } + if (rsFiles) { + std::list::iterator it; + std::list dirs; + rsFiles->getSharedDirectories(dirs); + for (it = dirs.begin(); it != dirs.end(); ++it) { + if (path.find((*it).filename) != std::string::npos) { + return false; + } + } + } } /* check if it exists */ diff --git a/libretroshare/src/ft/ftcontroller.h b/libretroshare/src/ft/ftcontroller.h index 5a9140267..564660879 100644 --- a/libretroshare/src/ft/ftcontroller.h +++ b/libretroshare/src/ft/ftcontroller.h @@ -175,8 +175,8 @@ class ftController: public RsTickingThread, public pqiServiceMonitor, public p3C void FileDownloads(std::list &hashs); /* Directory Handling */ - bool setDownloadDirectory(std::string path); - bool setPartialsDirectory(std::string path); + bool setDownloadDirectory(std::string path); + bool setPartialsDirectory(std::string path); std::string getDownloadDirectory(); std::string getPartialsDirectory(); bool FileDetails(const RsFileHash &hash, FileInfo &info); diff --git a/libretroshare/src/ft/ftserver.cc b/libretroshare/src/ft/ftserver.cc index f2200d7db..a3c344460 100644 --- a/libretroshare/src/ft/ftserver.cc +++ b/libretroshare/src/ft/ftserver.cc @@ -23,35 +23,40 @@ * */ -#include -#include -#include "util/rsdebug.h" -#include "util/rsdir.h" -#include "util/rsprint.h" #include "crypto/chacha20.h" -#include "retroshare/rstypes.h" -#include "retroshare/rspeers.h" //const int ftserverzone = 29539; #include "file_sharing/p3filelists.h" -#include "ft/ftturtlefiletransferitem.h" -#include "ft/ftserver.h" -#include "ft/ftextralist.h" -#include "ft/ftfilesearch.h" #include "ft/ftcontroller.h" -#include "ft/ftfileprovider.h" #include "ft/ftdatamultiplex.h" //#include "ft/ftdwlqueue.h" -#include "turtle/p3turtle.h" -#include "pqi/p3notify.h" -#include "rsserver/p3face.h" +#include "ft/ftextralist.h" +#include "ft/ftfileprovider.h" +#include "ft/ftfilesearch.h" +#include "ft/ftserver.h" +#include "ft/ftturtlefiletransferitem.h" -#include "pqi/pqi.h" #include "pqi/p3linkmgr.h" +#include "pqi/p3notify.h" +#include "pqi/pqi.h" + +#include "retroshare/rstypes.h" +#include "retroshare/rspeers.h" #include "rsitems/rsfiletransferitems.h" #include "rsitems/rsserviceids.h" +#include "rsserver/p3face.h" +#include "rsserver/rsaccounts.h" +#include "turtle/p3turtle.h" + +#include "util/rsdebug.h" +#include "util/rsdir.h" +#include "util/rsprint.h" + +#include +#include + /*** * #define SERVER_DEBUG 1 * #define SERVER_DEBUG_CACHE 1 @@ -145,9 +150,19 @@ void ftServer::SetupFtServer() /* make Controller */ mFtController = new ftController(mFtDataplex, mServiceCtrl, getServiceInfo().mServiceType); mFtController -> setFtSearchNExtra(mFtSearch, mFtExtra); - std::string tmppath = "."; - mFtController->setPartialsDirectory(tmppath); - mFtController->setDownloadDirectory(tmppath); + + std::string emergencySaveDir = rsAccounts->PathAccountDirectory(); + std::string emergencyPartialsDir = rsAccounts->PathAccountDirectory(); + if (emergencySaveDir != "") + { + emergencySaveDir += "/"; + emergencyPartialsDir += "/"; + } + emergencySaveDir += "Downloads"; + emergencyPartialsDir += "Partials"; + + mFtController->setDownloadDirectory(emergencySaveDir); + mFtController->setPartialsDirectory(emergencyPartialsDir); /* complete search setup */ mFtSearch->addSearchMode(mFtExtra, RS_FILE_HINTS_EXTRA); @@ -412,9 +427,9 @@ void ftServer::requestDirUpdate(void *ref) } /* Directory Handling */ -void ftServer::setDownloadDirectory(std::string path) +bool ftServer::setDownloadDirectory(std::string path) { - mFtController->setDownloadDirectory(path); + return mFtController->setDownloadDirectory(path); } std::string ftServer::getDownloadDirectory() @@ -422,9 +437,9 @@ std::string ftServer::getDownloadDirectory() return mFtController->getDownloadDirectory(); } -void ftServer::setPartialsDirectory(std::string path) +bool ftServer::setPartialsDirectory(std::string path) { - mFtController->setPartialsDirectory(path); + return mFtController->setPartialsDirectory(path); } std::string ftServer::getPartialsDirectory() diff --git a/libretroshare/src/ft/ftserver.h b/libretroshare/src/ft/ftserver.h index 191b201b4..440113d11 100644 --- a/libretroshare/src/ft/ftserver.h +++ b/libretroshare/src/ft/ftserver.h @@ -202,8 +202,8 @@ public: * Directory Handling ***/ virtual void requestDirUpdate(void *ref) ; // triggers the update of the given reference. Used when browsing. - virtual void setDownloadDirectory(std::string path); - virtual void setPartialsDirectory(std::string path); + virtual bool setDownloadDirectory(std::string path); + virtual bool setPartialsDirectory(std::string path); virtual std::string getDownloadDirectory(); virtual std::string getPartialsDirectory(); diff --git a/libretroshare/src/retroshare/rsfiles.h b/libretroshare/src/retroshare/rsfiles.h index d7efb2937..ccd4428be 100644 --- a/libretroshare/src/retroshare/rsfiles.h +++ b/libretroshare/src/retroshare/rsfiles.h @@ -278,8 +278,8 @@ class RsFiles ***/ virtual void requestDirUpdate(void *ref) =0 ; // triggers the update of the given reference. Used when browsing. - virtual void setDownloadDirectory(std::string path) = 0; - virtual void setPartialsDirectory(std::string path) = 0; + virtual bool setDownloadDirectory(std::string path) = 0; + virtual bool setPartialsDirectory(std::string path) = 0; virtual std::string getDownloadDirectory() = 0; virtual std::string getPartialsDirectory() = 0; diff --git a/retroshare-gui/src/gui/qss/stylesheet/qss.default b/retroshare-gui/src/gui/qss/stylesheet/qss.default index 8be8eb2e1..3e08e4d14 100644 --- a/retroshare-gui/src/gui/qss/stylesheet/qss.default +++ b/retroshare-gui/src/gui/qss/stylesheet/qss.default @@ -244,9 +244,15 @@ OpModeStatus[opMode="Minimal"] { background: #FFCCCC; } +/*Property Values at end to overwrite other settings*/ + [new=false] { background: #F8F8F8; } [new=true] { background: #DCECFD; } + +[WrongValue="true"] { + background-color: #FF8080; +} diff --git a/retroshare-gui/src/gui/settings/TransferPage.cpp b/retroshare-gui/src/gui/settings/TransferPage.cpp index a4c6a1b9d..7899f8422 100644 --- a/retroshare-gui/src/gui/settings/TransferPage.cpp +++ b/retroshare-gui/src/gui/settings/TransferPage.cpp @@ -22,15 +22,17 @@ #include "TransferPage.h" #include "rshare.h" +#include "gui/ShareManager.h" +#include "util/misc.h" + +#include "retroshare/rsiface.h" +#include "retroshare/rsfiles.h" +#include "retroshare/rspeers.h" + +#include #include -#include -#include -#include -#include -#include - TransferPage::TransferPage(QWidget * parent, Qt::WindowFlags flags) : ConfigPage(parent, flags) { @@ -225,11 +227,23 @@ void TransferPage::setIncomingDirectory() return; } - ui.incomingDir->setText(qdir); - std::string dir = ui.incomingDir->text().toUtf8().constData(); - - if(!dir.empty()) - rsFiles->setDownloadDirectory(dir); + std::string dir = qdir.toUtf8().constData(); + if(!dir.empty()) + { + if (!rsFiles->setDownloadDirectory(dir)) + { + ui.incomingDir->setToolTip( tr("Invalid Input. Have you got the right to write on it?") ); + ui.incomingDir->setProperty("WrongValue", true); + } + else + { + ui.incomingDir->setToolTip( "" ); + ui.incomingDir->setProperty("WrongValue", false); + } + } + ui.incomingDir->style()->unpolish(ui.incomingDir); + ui.incomingDir->style()->polish( ui.incomingDir); + whileBlocking(ui.incomingDir)->setText(QString::fromUtf8(rsFiles->getDownloadDirectory().c_str())); } void TransferPage::setPartialsDirectory() @@ -239,11 +253,25 @@ void TransferPage::setPartialsDirectory() return; } - ui.partialsDir->setText(qdir); - std::string dir = ui.partialsDir->text().toUtf8().constData(); + std::string dir = qdir.toUtf8().constData(); if (!dir.empty()) - rsFiles->setPartialsDirectory(dir); + { + if (!rsFiles->setPartialsDirectory(dir)) + { + ui.partialsDir->setToolTip( tr("Invalid Input. It can't be an already shared directory.") ); + ui.partialsDir->setProperty("WrongValue", true); + } + else + { + ui.partialsDir->setToolTip( "" ); + ui.partialsDir->setProperty("WrongValue", false); + } + } + ui.partialsDir->style()->unpolish(ui.partialsDir); + ui.partialsDir->style()->polish( ui.partialsDir); + whileBlocking(ui.partialsDir)->setText(QString::fromUtf8(rsFiles->getPartialsDirectory().c_str())); } + void TransferPage::toggleAutoCheckDirectories(bool b) { ui.autoCheckDirectoriesDelay_SB->setEnabled(b); diff --git a/retroshare-gui/src/qss/blacknight.qss b/retroshare-gui/src/qss/blacknight.qss index 1b89a5f4c..3d56385ab 100644 --- a/retroshare-gui/src/qss/blacknight.qss +++ b/retroshare-gui/src/qss/blacknight.qss @@ -293,9 +293,16 @@ OpModeStatus[opMode="Minimal"] { background: #700000; } +/*Property Values at end to overwrite other settings*/ + [new=false] { background: #202020; } [new=true] { background: #005000; } + +[WrongValue=true] { + background-color: #702020; +} + diff --git a/retroshare-gui/src/qss/qdarkstyle.qss b/retroshare-gui/src/qss/qdarkstyle.qss index ccb0d47f7..6c325dc1a 100644 --- a/retroshare-gui/src/qss/qdarkstyle.qss +++ b/retroshare-gui/src/qss/qdarkstyle.qss @@ -1126,9 +1126,15 @@ OpModeStatus[opMode="Minimal"] { background: #700000; } +/*Property Values at end to overwrite other settings*/ + [new=false] { background: #202020; } [new=true] { background: #005000; } + +[WrongValue="true"] { + background-color: #702020; +} From 181d99e8826c2a0e81955c46e03c7cd3bae28873 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Wed, 16 May 2018 10:38:47 +0200 Subject: [PATCH 120/161] Give proper error message if Qt version is too old --- libresapi/src/libresapi.pro | 19 ++++++------------- retroshare.pri | 8 +++++++- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index 2733cffa2..885d430f5 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -106,20 +106,13 @@ libresapihttpserver { DEFINES *= WINDOWS_SYS INCLUDEPATH += . $$INC_DIR - greaterThan(QT_MAJOR_VERSION, 4) { - # Qt 5 - PRO_PATH=$$shell_path($$_PRO_FILE_PWD_) - MAKE_SRC=$$shell_path($$PRO_PATH/webui-src/make-src) - } else { - # Qt 4 - PRO_PATH=$$replace(_PRO_FILE_PWD_, /, \\) - MAKE_SRC=$$PRO_PATH\\webui-src\\make-src - } + PRO_PATH=$$shell_path($$_PRO_FILE_PWD_) + MAKE_SRC=$$shell_path($$PRO_PATH/webui-src/make-src) - #create_webfiles.commands = $$MAKE_SRC\\build.bat $$PRO_PATH - #QMAKE_EXTRA_TARGETS += create_webfiles - #PRE_TARGETDEPS += create_webfiles - QMAKE_POST_LINK=$$MAKE_SRC\\build.bat $$PRO_PATH + #create_webfiles.commands = $$MAKE_SRC\\build.bat $$PRO_PATH + #QMAKE_EXTRA_TARGETS += create_webfiles + #PRE_TARGETDEPS += create_webfiles + QMAKE_POST_LINK=$$MAKE_SRC\\build.bat $$PRO_PATH # create dummy files system($$MAKE_SRC\\init.bat .) diff --git a/retroshare.pri b/retroshare.pri index 4d36fd088..6b8fdd870 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -160,6 +160,12 @@ rs_v07_changes { ## RetroShare qmake functions goes here as all the rest may use them ########### ################################################################################ +## Qt versions older the 5 are not supported anymore, check if the user is +## attempting use them and fail accordingly with a proper error message +lessThan(QT_MAJOR_VERSION, 5) { + error(Qt 5.0.0 or newer is needed to build RetroShare) +} + ## This function is useful to look for the location of a file in a list of paths ## like the which command on linux, first paramether is the file name, ## second parameter is the name of a variable containing the list of folders @@ -226,7 +232,7 @@ defineReplace(linkDynamicLibs) { ################################################################################ -## Statements and variables that depends on build options (CONFIG)goes here #### +## Statements and variables that depends on build options (CONFIG) goes here ### ################################################################################ ## ## Defining the following variables may be needed depending on platform and From 4661329beef49fe261c027be306c222c9c285bfe Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 16 May 2018 11:15:18 +0200 Subject: [PATCH 121/161] fixed bug causing new forums and identities to not show up immediately --- libretroshare/src/gxs/rsdataservice.cc | 2 ++ retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp | 4 ++++ retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/libretroshare/src/gxs/rsdataservice.cc b/libretroshare/src/gxs/rsdataservice.cc index 5fddf1897..bebcee21d 100644 --- a/libretroshare/src/gxs/rsdataservice.cc +++ b/libretroshare/src/gxs/rsdataservice.cc @@ -952,6 +952,8 @@ void RsDataService::locked_updateGrpMetaCache(const RsGxsGrpMetaData& meta) if(it != mGrpMetaDataCache.end()) *(it->second) = meta ; + else + mGrpMetaDataCache[meta.mGroupId] = new RsGxsGrpMetaData(meta) ; } void RsDataService::locked_clearGrpMetaCache(const RsGxsGroupId& gid) diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp index b80180c9a..bf092c3cc 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp @@ -67,6 +67,10 @@ void RsGxsUpdateBroadcastBase::updateBroadcastChanged() /* Update only update when the widget is visible. */ if (mUpdateWhenInvisible || !widget || widget->isVisible()) { + + if (!mGrpIds.empty() || !mGrpIdsMeta.empty() || !mMsgIds.empty() || !mMsgIdsMeta.empty()) + mFillComplete = true ; + securedUpdateDisplay(); } } diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h index 299e7e2a7..ed03bb15c 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h @@ -37,7 +37,7 @@ protected: // This is overloaded in subclasses. virtual void updateDisplay(bool complete) = 0; -private slots: +public slots: void fillDisplay(bool complete); private: From 8451550561daa207a13b81d66b7b6e9486e222a5 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 16 May 2018 11:40:39 +0200 Subject: [PATCH 122/161] added missing licence to file_tree.h --- libretroshare/src/file_sharing/file_tree.h | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/libretroshare/src/file_sharing/file_tree.h b/libretroshare/src/file_sharing/file_tree.h index 8aaa9dff6..0505ceed5 100644 --- a/libretroshare/src/file_sharing/file_tree.h +++ b/libretroshare/src/file_sharing/file_tree.h @@ -1,3 +1,28 @@ +/* + * RetroShare C++ File lists IO methods. + * + * file_sharing/file_tree.h + * + * Copyright 2017 by csoler + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare.project@gmail.com". + * + */ + #include "retroshare/rsfiles.h" class FileTreeImpl: public FileTree From 3f88e3e9018edcf264081a27a4e0f7c15bf03101 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 16 May 2018 13:24:52 +0200 Subject: [PATCH 123/161] added check to avoid hashing files that are currently being modified --- .../src/file_sharing/directory_storage.cc | 2 +- .../src/file_sharing/directory_updater.cc | 49 ++++++++++++++----- .../src/file_sharing/directory_updater.h | 4 +- .../src/file_sharing/file_sharing_defaults.h | 21 ++++---- 4 files changed, 52 insertions(+), 24 deletions(-) diff --git a/libretroshare/src/file_sharing/directory_storage.cc b/libretroshare/src/file_sharing/directory_storage.cc index eb3c58626..d39401e06 100644 --- a/libretroshare/src/file_sharing/directory_storage.cc +++ b/libretroshare/src/file_sharing/directory_storage.cc @@ -623,7 +623,7 @@ bool LocalDirectoryStorage::locked_getFileSharingPermissions(const EntryIndex& i if(it == mLocalDirs.end()) { - std::cerr << "(EE) very weird bug: base directory \"" << base_dir << "\" not found in shared dir list." << std::endl; + std::cerr << "(II) base directory \"" << base_dir << "\" not found in shared dir list." << std::endl; return false ; } diff --git a/libretroshare/src/file_sharing/directory_updater.cc b/libretroshare/src/file_sharing/directory_updater.cc index 6d95961ab..dffef92c7 100644 --- a/libretroshare/src/file_sharing/directory_updater.cc +++ b/libretroshare/src/file_sharing/directory_updater.cc @@ -73,10 +73,23 @@ void LocalDirectoryUpdater::data_tick() { if(now > mDelayBetweenDirectoryUpdates + mLastSweepTime) { - if(sweepSharedDirectories()) + bool some_files_not_ready = false ; + + if(sweepSharedDirectories(some_files_not_ready)) { - mNeedsFullRecheck = false; - mLastSweepTime = now ; + if(some_files_not_ready) + { + mNeedsFullRecheck = true ; + mLastSweepTime = now - mDelayBetweenDirectoryUpdates + 60 ; // retry 20 secs from now + + std::cerr << "(II) some files being modified. Will re-scan in 60 secs." << std::endl; + } + else + { + mNeedsFullRecheck = false ; + mLastSweepTime = now ; + } + mSharedDirectories->notifyTSChanged(); mForceUpdate = false ; } @@ -111,7 +124,7 @@ void LocalDirectoryUpdater::forceUpdate() mHashCache->togglePauseHashingProcess(); } -bool LocalDirectoryUpdater::sweepSharedDirectories() +bool LocalDirectoryUpdater::sweepSharedDirectories(bool& some_files_not_ready) { if(mHashSalt.isNull()) { @@ -158,8 +171,8 @@ bool LocalDirectoryUpdater::sweepSharedDirectories() #endif existing_dirs.insert(RsDirUtil::removeSymLinks(stored_dir_it.name())); - recursUpdateSharedDir(stored_dir_it.name(), *stored_dir_it,existing_dirs,1) ; // here we need to use the list that was stored, instead of the shared dir list, because the two - // are not necessarily in the same order. + recursUpdateSharedDir(stored_dir_it.name(), *stored_dir_it,existing_dirs,1,some_files_not_ready) ; // here we need to use the list that was stored, instead of the shared dir list, because the two + // are not necessarily in the same order. } RsServer::notify()->notifyListChange(NOTIFY_LIST_DIRLIST_LOCAL, 0); @@ -168,7 +181,7 @@ bool LocalDirectoryUpdater::sweepSharedDirectories() return true ; } -void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx,std::set& existing_directories,uint32_t current_depth) +void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx,std::set& existing_directories,uint32_t current_depth,bool& some_files_not_ready) { #ifdef DEBUG_LOCAL_DIR_UPDATER std::cerr << "[directory storage] parsing directory " << cumulated_path << ", index=" << indx << std::endl; @@ -187,6 +200,8 @@ void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_p return; } + time_t now = time(NULL) ; + if(mNeedsFullRecheck || dirIt.dir_modtime() > dir_local_mod_time) // the > is because we may have changed the virtual name, and therefore the TS wont match. // we only want to detect when the directory has changed on the disk { @@ -200,11 +215,23 @@ void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_p { switch(dirIt.file_type()) { - case librs::util::FolderIterator::TYPE_FILE: subfiles[dirIt.file_name()].modtime = dirIt.file_modtime() ; - subfiles[dirIt.file_name()].size = dirIt.file_size(); + case librs::util::FolderIterator::TYPE_FILE: + + if(dirIt.file_modtime() + MIN_TIME_AFTER_LAST_MODIFICATION < now) + { + subfiles[dirIt.file_name()].modtime = dirIt.file_modtime() ; + subfiles[dirIt.file_name()].size = dirIt.file_size(); #ifdef DEBUG_LOCAL_DIR_UPDATER - std::cerr << " adding sub-file \"" << dirIt.file_name() << "\"" << std::endl; + std::cerr << " adding sub-file \"" << dirIt.file_name() << "\"" << std::endl; #endif + } + else + { + some_files_not_ready = true ; + + std::cerr << "(WW) file " << dirIt.file_fullpath() << " is probably being modified. Keeping it for later." << std::endl; + } + break; case librs::util::FolderIterator::TYPE_DIR: @@ -276,7 +303,7 @@ void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_p #ifdef DEBUG_LOCAL_DIR_UPDATER std::cerr << " recursing into " << stored_dir_it.name() << std::endl; #endif - recursUpdateSharedDir(cumulated_path + "/" + stored_dir_it.name(), *stored_dir_it,existing_directories,current_depth+1) ; + recursUpdateSharedDir(cumulated_path + "/" + stored_dir_it.name(), *stored_dir_it,existing_directories,current_depth+1,some_files_not_ready) ; } } diff --git a/libretroshare/src/file_sharing/directory_updater.h b/libretroshare/src/file_sharing/directory_updater.h index e4adb28b4..4c6e05649 100644 --- a/libretroshare/src/file_sharing/directory_updater.h +++ b/libretroshare/src/file_sharing/directory_updater.h @@ -70,8 +70,8 @@ protected: virtual void hash_callback(uint32_t client_param, const std::string& name, const RsFileHash& hash, uint64_t size); virtual bool hash_confirm(uint32_t client_param) ; - void recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx, std::set& existing_directories, uint32_t current_depth); - bool sweepSharedDirectories(); + void recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx, std::set& existing_directories, uint32_t current_depth,bool& files_not_ready); + bool sweepSharedDirectories(bool &some_files_not_ready); private: bool filterFile(const std::string& fname) const ; // reponds true if the file passes the ignore lists test. diff --git a/libretroshare/src/file_sharing/file_sharing_defaults.h b/libretroshare/src/file_sharing/file_sharing_defaults.h index 9e2dd6241..8de4b31c8 100644 --- a/libretroshare/src/file_sharing/file_sharing_defaults.h +++ b/libretroshare/src/file_sharing/file_sharing_defaults.h @@ -34,16 +34,16 @@ static const uint32_t DELAY_BETWEEN_REMOTE_DIRECTORIES_SWEEP = 60 ; // 60 se static const uint32_t DELAY_BEFORE_DELETE_NON_EMPTY_REMOTE_DIR = 60*24*86400 ; // delete non empty remoe directories after 60 days of inactivity static const uint32_t DELAY_BEFORE_DELETE_EMPTY_REMOTE_DIR = 5*24*86400 ; // delete empty remote directories after 5 days of inactivity -static const std::string HASH_CACHE_DURATION_SS = "HASH_CACHE_DURATION" ; // key string to store hash remembering time -static const std::string WATCH_FILE_DURATION_SS = "WATCH_FILES_DELAY" ; // key to store delay before re-checking for new files -static const std::string WATCH_FILE_ENABLED_SS = "WATCH_FILES_ENABLED"; // key to store ON/OFF flags for file whatch -static const std::string FOLLOW_SYMLINKS_SS = "FOLLOW_SYMLINKS"; // dereference symbolic links, or just ignore them. -static const std::string IGNORE_DUPLICATES = "IGNORE_DUPLICATES"; // do not index files that are referenced multiple times because of links -static const std::string WATCH_HASH_SALT_SS = "WATCH_HASH_SALT"; // Salt that is used to hash directory names -static const std::string IGNORED_PREFIXES_SS = "IGNORED_PREFIXES"; // ignore file prefixes -static const std::string IGNORED_SUFFIXES_SS = "IGNORED_SUFFIXES"; // ignore file suffixes -static const std::string IGNORE_LIST_FLAGS_SS = "IGNORED_FLAGS"; // ignore file flags -static const std::string MAX_SHARE_DEPTH = "MAX_SHARE_DEPTH"; // maximum depth of shared directories +static const std::string HASH_CACHE_DURATION_SS = "HASH_CACHE_DURATION" ; // key string to store hash remembering time +static const std::string WATCH_FILE_DURATION_SS = "WATCH_FILES_DELAY" ; // key to store delay before re-checking for new files +static const std::string WATCH_FILE_ENABLED_SS = "WATCH_FILES_ENABLED"; // key to store ON/OFF flags for file whatch +static const std::string FOLLOW_SYMLINKS_SS = "FOLLOW_SYMLINKS"; // dereference symbolic links, or just ignore them. +static const std::string IGNORE_DUPLICATES = "IGNORE_DUPLICATES"; // do not index files that are referenced multiple times because of links +static const std::string WATCH_HASH_SALT_SS = "WATCH_HASH_SALT"; // Salt that is used to hash directory names +static const std::string IGNORED_PREFIXES_SS = "IGNORED_PREFIXES"; // ignore file prefixes +static const std::string IGNORED_SUFFIXES_SS = "IGNORED_SUFFIXES"; // ignore file suffixes +static const std::string IGNORE_LIST_FLAGS_SS = "IGNORED_FLAGS"; // ignore file flags +static const std::string MAX_SHARE_DEPTH = "MAX_SHARE_DEPTH"; // maximum depth of shared directories static const std::string FILE_SHARING_DIR_NAME = "file_sharing" ; // hard-coded directory name to store friend file lists, hash cache, etc. static const std::string HASH_CACHE_FILE_NAME = "hash_cache.bin" ; // hard-coded directory name to store encrypted hash cache. @@ -51,6 +51,7 @@ static const std::string LOCAL_SHARED_DIRS_FILE_NAME = "local_dir_hierarchy.bin" static const uint32_t MIN_INTERVAL_BETWEEN_HASH_CACHE_SAVE = 20 ; // never save hash cache more often than every 20 secs. static const uint32_t MIN_INTERVAL_BETWEEN_REMOTE_DIRECTORY_SAVE = 23 ; // never save remote directories more often than this +static const uint32_t MIN_TIME_AFTER_LAST_MODIFICATION = 20 ; // never hash a file that is just being modified, otherwise we end up with a corrupted hash static const uint32_t MAX_DIR_SYNC_RESPONSE_DATA_SIZE = 20000 ; // Maximum RsItem data size in bytes for serialised directory transmission static const uint32_t DEFAULT_HASH_STORAGE_DURATION_DAYS = 30 ; // remember deleted/inaccessible files for 30 days From 186617fc2bebca7967a2e575b4a9a92cd6f04dee Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Thu, 17 May 2018 16:43:14 +0200 Subject: [PATCH 124/161] Fix missing include --- libresapi/src/api/ChannelsHandler.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libresapi/src/api/ChannelsHandler.cpp b/libresapi/src/api/ChannelsHandler.cpp index 52a3527ac..3c5084578 100644 --- a/libresapi/src/api/ChannelsHandler.cpp +++ b/libresapi/src/api/ChannelsHandler.cpp @@ -22,6 +22,8 @@ #include #include #include +#include + #include "Operators.h" namespace resource_api From 5a301734a9324f31c285772d607f37479b4cbf2d Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Wed, 16 May 2018 16:13:44 +0200 Subject: [PATCH 125/161] Extend a bit filesharing JSON API v2 --- libresapi/src/api/FileSharingHandler.cpp | 54 +++++++++++++++++++++ libresapi/src/api/FileSharingHandler.h | 6 +++ libresapi/src/api/TransfersHandler.cpp | 62 ++++++++++++++++++++++++ libresapi/src/api/TransfersHandler.h | 3 ++ libretroshare/src/retroshare/rsfiles.h | 7 ++- 5 files changed, 128 insertions(+), 4 deletions(-) diff --git a/libresapi/src/api/FileSharingHandler.cpp b/libresapi/src/api/FileSharingHandler.cpp index a6296973d..44c94cfad 100644 --- a/libresapi/src/api/FileSharingHandler.cpp +++ b/libresapi/src/api/FileSharingHandler.cpp @@ -37,6 +37,16 @@ FileSharingHandler::FileSharingHandler(StateTokenServer *sts, RsFiles *files, addResourceHandler("get_dir_parent", this, &FileSharingHandler::handleGetDirectoryParent); addResourceHandler("get_dir_childs", this, &FileSharingHandler::handleGetDirectoryChilds); + addResourceHandler( "get_download_directory", this, + &FileSharingHandler::handleGetDownloadDirectory ); + addResourceHandler( "set_download_directory", this, + &FileSharingHandler::handleSetDownloadDirectory ); + + addResourceHandler( "get_partials_directory", this, + &FileSharingHandler::handleGetPartialsDirectory ); + addResourceHandler( "set_partials_directory", this, + &FileSharingHandler::handleSetPartialsDirectory ); + addResourceHandler("is_dl_dir_shared", this, &FileSharingHandler::handleIsDownloadDirShared); addResourceHandler("share_dl_dir", this, &FileSharingHandler::handleShareDownloadDirectory); @@ -513,4 +523,48 @@ void FileSharingHandler::handleDownload(Request& req, Response& resp) resp.setFail("Couldn't download file"); } +void FileSharingHandler::handleGetDownloadDirectory( Request& /*req*/, + Response& resp ) +{ + std::string dlDir = mRsFiles->getDownloadDirectory(); + resp.mDataStream << makeKeyValueReference("download_directory", dlDir); + resp.setOk(); +} + +void FileSharingHandler::handleSetDownloadDirectory( Request& req, + Response& resp ) +{ + std::string dlDir; + req.mStream << makeKeyValueReference("download_directory", dlDir); + + if(dlDir.empty()) resp.setFail("missing download_directory"); + else + { + mRsFiles->setDownloadDirectory(dlDir); + resp.setOk(); + } +} + +void FileSharingHandler::handleGetPartialsDirectory( Request& /*req*/, + Response& resp ) +{ + std::string partialsDir = mRsFiles->getPartialsDirectory(); + resp.mDataStream << makeKeyValueReference("partials_directory", partialsDir); + resp.setOk(); +} + +void FileSharingHandler::handleSetPartialsDirectory( Request& req, + Response& resp ) +{ + std::string partialsDir; + req.mStream << makeKeyValueReference("partials_directory", partialsDir); + + if(partialsDir.empty()) resp.setFail("missing partials_directory"); + else + { + mRsFiles->setPartialsDirectory(partialsDir); + resp.setOk(); + } +} + } // namespace resource_api diff --git a/libresapi/src/api/FileSharingHandler.h b/libresapi/src/api/FileSharingHandler.h index 0eb67764e..cd78f338f 100644 --- a/libresapi/src/api/FileSharingHandler.h +++ b/libresapi/src/api/FileSharingHandler.h @@ -57,6 +57,12 @@ private: void handleDownload(Request& req, Response& resp); + void handleGetDownloadDirectory(Request& req, Response& resp); + void handleSetDownloadDirectory(Request& req, Response& resp); + + void handleGetPartialsDirectory(Request& req, Response& resp); + void handleSetPartialsDirectory(Request& req, Response& resp); + /// Token indicating change in local shared files StateToken mLocalDirStateToken; diff --git a/libresapi/src/api/TransfersHandler.cpp b/libresapi/src/api/TransfersHandler.cpp index c696411da..dc28e9596 100644 --- a/libresapi/src/api/TransfersHandler.cpp +++ b/libresapi/src/api/TransfersHandler.cpp @@ -15,6 +15,14 @@ TransfersHandler::TransfersHandler(StateTokenServer *sts, RsFiles *files, RsPeer addResourceHandler("downloads", this, &TransfersHandler::handleDownloads); addResourceHandler("uploads", this, &TransfersHandler::handleUploads); addResourceHandler("control_download", this, &TransfersHandler::handleControlDownload); + + addResourceHandler( "set_file_destination_directory", this, + &TransfersHandler::handleSetFileDestinationDirectory ); + addResourceHandler( "set_file_destination_name", this, + &TransfersHandler::handleSetFileDestinationName ); + addResourceHandler( "set_file_chunk_strategy", this, + &TransfersHandler::handleSetFileChunkStrategy ); + mStateToken = mStateTokenServer->getNewToken(); mStateTokenServer->registerTickClient(this); mNotify.registerNotifyClient(this); @@ -288,4 +296,58 @@ void TransfersHandler::handleUploads(Request & /* req */, Response &resp) resp.setOk(); } +void TransfersHandler::handleSetFileDestinationDirectory( Request& req, + Response& resp ) +{ + mStateTokenServer->replaceToken(mStateToken); + + std::string hashString; + std::string newPath; + req.mStream << makeKeyValueReference("path", newPath); + req.mStream << makeKeyValueReference("hash", hashString); + RsFileHash hash(hashString); + + if (mFiles->setDestinationDirectory(hash, newPath)) resp.setOk(); + else resp.setFail(); +} + +void TransfersHandler::handleSetFileDestinationName( Request& req, + Response& resp ) +{ + mStateTokenServer->replaceToken(mStateToken); + + std::string hashString; + std::string newName; + req.mStream << makeKeyValueReference("name", newName); + req.mStream << makeKeyValueReference("hash", hashString); + RsFileHash hash(hashString); + + if (mFiles->setDestinationName(hash, newName)) resp.setOk(); + else resp.setFail(); +} + +void TransfersHandler::handleSetFileChunkStrategy(Request& req, Response& resp) +{ + mStateTokenServer->replaceToken(mStateToken); + + std::string hashString; + std::string newChunkStrategyStr; + req.mStream << makeKeyValueReference("chuck_stategy", newChunkStrategyStr); + req.mStream << makeKeyValueReference("hash", hashString); + + RsFileHash hash(hashString); + FileChunksInfo::ChunkStrategy newStrategy = + FileChunksInfo::CHUNK_STRATEGY_PROGRESSIVE; + + if ( newChunkStrategyStr == "streaming" ) + newStrategy = FileChunksInfo::CHUNK_STRATEGY_STREAMING; + else if ( newChunkStrategyStr == "random" ) + newStrategy = FileChunksInfo::CHUNK_STRATEGY_RANDOM; + else if ( newChunkStrategyStr == "progressive" ) + newStrategy = FileChunksInfo::CHUNK_STRATEGY_PROGRESSIVE; + + if (mFiles->setChunkStrategy(hash, newStrategy)) resp.setOk(); + else resp.setFail(); +} + } // namespace resource_api diff --git a/libresapi/src/api/TransfersHandler.h b/libresapi/src/api/TransfersHandler.h index 7d07a7198..7e43c3614 100644 --- a/libresapi/src/api/TransfersHandler.h +++ b/libresapi/src/api/TransfersHandler.h @@ -30,6 +30,9 @@ private: void handleControlDownload(Request& req, Response& resp); void handleDownloads(Request& req, Response& resp); void handleUploads(Request& req, Response& resp); + void handleSetFileDestinationDirectory(Request& req, Response& resp); + void handleSetFileDestinationName(Request& req, Response& resp); + void handleSetFileChunkStrategy(Request& req, Response& resp); StateTokenServer* mStateTokenServer; RsFiles* mFiles; diff --git a/libretroshare/src/retroshare/rsfiles.h b/libretroshare/src/retroshare/rsfiles.h index ccd4428be..83301de98 100644 --- a/libretroshare/src/retroshare/rsfiles.h +++ b/libretroshare/src/retroshare/rsfiles.h @@ -168,10 +168,9 @@ public: class RsFiles { - public: - - RsFiles() { return; } - virtual ~RsFiles() { return; } +public: + RsFiles() {} + virtual ~RsFiles() {} /** * Provides file data for the gui: media streaming or rpc clients. From 8f107cca31bc64625dff3d70e5258cdef9c7fb87 Mon Sep 17 00:00:00 2001 From: Phenom Date: Sat, 19 May 2018 16:37:29 +0200 Subject: [PATCH 126/161] Add Never Ask Me For External Link Activated Need to remove [General] NeverAskMeForExternalLinkActivated key in RetroShare.conf to revert. --- retroshare-gui/src/gui/MainWindow.cpp | 34 +++++++++++++++++---------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/retroshare-gui/src/gui/MainWindow.cpp b/retroshare-gui/src/gui/MainWindow.cpp index 116b9637f..e00417080 100644 --- a/retroshare-gui/src/gui/MainWindow.cpp +++ b/retroshare-gui/src/gui/MainWindow.cpp @@ -733,16 +733,16 @@ void MainWindow::updateStatus() if (ratesstatus) ratesstatus->getRatesStatus(downKb, upKb); - if(torstatus) - torstatus->getTorStatus(); + if(torstatus) + torstatus->getTorStatus(); if(!hiddenmode) { - if (natstatus) - natstatus->getNATStatus(); - - if (dhtstatus) - dhtstatus->getDHTStatus(); + if (natstatus) + natstatus->getNATStatus(); + + if (dhtstatus) + dhtstatus->getDHTStatus(); } if (discstatus) { @@ -1434,27 +1434,37 @@ void MainWindow::settingsChanged() void MainWindow::externalLinkActivated(const QUrl &url) { static bool already_warned = false ; + bool never_ask_me = Settings->value("NeverAskMeForExternalLinkActivated",false).toBool(); - if(!already_warned) + if(!already_warned && !never_ask_me) { QMessageBox mb(QObject::tr("Confirmation"), QObject::tr("Do you want this link to be handled by your system?")+"

"+ url.toString()+"

"+tr("Make sure this link has not been forged to drag you to a malicious website."), QMessageBox::Question, QMessageBox::Yes,QMessageBox::No, 0); - QCheckBox *checkbox = new QCheckBox(tr("Don't ask me again")) ; + QCheckBox *dontAsk_CB = new QCheckBox(tr("Don't ask me again")); + QCheckBox *neverAsk_CB = new QCheckBox(tr("Never ask me again")); + dontAsk_CB->setToolTip(tr("This will be saved only for this session.")); + neverAsk_CB->setToolTip(tr("This will be saved permanently. You'll need to clean RetroShare.conf to revert.")); QGridLayout* layout = qobject_cast(mb.layout()); if (layout) { - layout->addWidget(checkbox,layout->rowCount(),0,1, layout->columnCount(), Qt::AlignLeft); + layout->addWidget(dontAsk_CB,layout->rowCount(),0,1, layout->columnCount(), Qt::AlignLeft); + layout->addWidget(neverAsk_CB,layout->rowCount(),0,1, layout->columnCount(), Qt::AlignLeft); } else { //Not QGridLayout so add at end - mb.layout()->addWidget(checkbox) ; + mb.layout()->addWidget(dontAsk_CB); + mb.layout()->addWidget(neverAsk_CB); } int res = mb.exec() ; if (res == QMessageBox::No) return ; - else if(checkbox->isChecked()) + + if(dontAsk_CB->isChecked()) already_warned = true ; + + if(neverAsk_CB->isChecked()) + Settings->setValue("NeverAskMeForExternalLinkActivated",true); } QDesktopServices::openUrl(url) ; From 92b21d73326359403ec399578af5387c05ed0d11 Mon Sep 17 00:00:00 2001 From: sehraf Date: Mon, 21 May 2018 14:26:46 +0200 Subject: [PATCH 127/161] compile fix wikipoos --- libretroshare/src/rsserver/rsinit.cc | 10 +++++----- retroshare-gui/src/gui/WikiPoos/WikiEditDialog.cpp | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 0d9300202..095dbaeaf 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -1351,11 +1351,11 @@ int RsServer::StartupRetroShare() p3Wiki *mWiki = new p3Wiki(wiki_ds, NULL, mGxsIdService); // create GXS wiki service - RsGxsNetService* wiki_ns = new RsGxsNetService( - RS_SERVICE_GXS_TYPE_WIKI, wiki_ds, nxsMgr, - mWiki, mWiki->getServiceInfo(), - mGxsIdService, mGxsCircles, - pgpAuxUtils); + RsGxsNetService* wiki_ns = new RsGxsNetService( + RS_SERVICE_GXS_TYPE_WIKI, wiki_ds, nxsMgr, + mWiki, mWiki->getServiceInfo(), + mReputations, mGxsCircles, mGxsIdService, + pgpAuxUtils); mWiki->setNetworkExchangeService(wiki_ns) ; #endif diff --git a/retroshare-gui/src/gui/WikiPoos/WikiEditDialog.cpp b/retroshare-gui/src/gui/WikiPoos/WikiEditDialog.cpp index 709d0262c..667e55fa7 100644 --- a/retroshare-gui/src/gui/WikiPoos/WikiEditDialog.cpp +++ b/retroshare-gui/src/gui/WikiPoos/WikiEditDialog.cpp @@ -715,7 +715,7 @@ void WikiEditDialog::loadBaseHistory(const uint32_t &token) std::cerr << " ParentId: " << page.mMeta.mParentId; std::cerr << std::endl; - GxsIdRSTreeWidgetItem *modItem = new GxsIdRSTreeWidgetItem(mThreadCompareRole); + GxsIdRSTreeWidgetItem *modItem = new GxsIdRSTreeWidgetItem(mThreadCompareRole, GxsIdDetails::ICON_TYPE_AVATAR); modItem->setData(WET_DATA_COLUMN, WET_ROLE_ORIGPAGEID, QString::fromStdString(page.mMeta.mOrigMsgId.toStdString())); modItem->setData(WET_DATA_COLUMN, WET_ROLE_PAGEID, QString::fromStdString(page.mMeta.mMsgId.toStdString())); @@ -830,7 +830,7 @@ void WikiEditDialog::loadEditTreeData(const uint32_t &token) } /* create an Entry */ - GxsIdRSTreeWidgetItem *modItem = new GxsIdRSTreeWidgetItem(mThreadCompareRole); + GxsIdRSTreeWidgetItem *modItem = new GxsIdRSTreeWidgetItem(mThreadCompareRole, GxsIdDetails::ICON_TYPE_AVATAR); modItem->setData(WET_DATA_COLUMN, WET_ROLE_ORIGPAGEID, QString::fromStdString(snapshot.mMeta.mOrigMsgId.toStdString())); modItem->setData(WET_DATA_COLUMN, WET_ROLE_PAGEID, QString::fromStdString(snapshot.mMeta.mMsgId.toStdString())); modItem->setData(WET_DATA_COLUMN, WET_ROLE_PARENTID, QString::fromStdString(snapshot.mMeta.mParentId.toStdString())); From 0c19a5640ec12ef7845e1da222247dfc727aa6d8 Mon Sep 17 00:00:00 2001 From: sehraf Date: Mon, 21 May 2018 14:55:10 +0200 Subject: [PATCH 128/161] compile fix gxsphoto --- libretroshare/src/rsitems/rsphotoitems.cc | 27 +++++++++++------------ libretroshare/src/rsitems/rsphotoitems.h | 2 +- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/libretroshare/src/rsitems/rsphotoitems.cc b/libretroshare/src/rsitems/rsphotoitems.cc index 9c9ff2d02..3a5ad1ead 100644 --- a/libretroshare/src/rsitems/rsphotoitems.cc +++ b/libretroshare/src/rsitems/rsphotoitems.cc @@ -61,24 +61,23 @@ void RsGxsPhotoAlbumItem::serial_process(RsGenericSerializer::SerializeJob j,RsG RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_LOCATION, album.mWhere, "mWhere"); RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_PIC_TYPE, album.mThumbnail.type,"mThumbnail.type"); - RsTlvBinaryDataRef b(RS_SERVICE_GXS_TYPE_PHOTO, album.mThumbnail.data,album.mThumbnail.size); - - RsTypeSerializer::serial_process(j,ctx,b,"thumbnail binary data") ; + RsTlvBinaryDataRef b(RS_SERVICE_GXS_TYPE_PHOTO, album.mThumbnail.data, album.mThumbnail.size); + RsTypeSerializer::serial_process(j, ctx, b, "thumbnail binary data") ; } void RsGxsPhotoPhotoItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_CAPTION, photo.mCaption); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_CATEGORY, photo.mCategory); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_DESCR, photo.mDescription); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_HASH_TAG, photo.mHashTags); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_MSG, photo.mOther); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_PIC_AUTH, photo.mPhotographer); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_DATE, photo.mWhen); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_LOCATION, photo.mWhere); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_PIC_TYPE, photo.mThumbnail.type); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_CAPTION, photo.mCaption, "mCaption"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_CATEGORY, photo.mCategory, "mCategory"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_DESCR, photo.mDescription, "mDescription"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_HASH_TAG, photo.mHashTags, "mHashTags"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_MSG, photo.mOther, "mOther"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_PIC_AUTH, photo.mPhotographer, "mPhotographer"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_DATE, photo.mWhen, "mWhen"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_LOCATION, photo.mWhere, "mWhere"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_PIC_TYPE, photo.mThumbnail.type, "mThumbnail.type"); - RsTlvBinaryDataRef b(RS_SERVICE_GXS_TYPE_PHOTO,photo.mThumbnail.data, photo.mThumbnail.size); - RsTypeSerializer::serial_process(j,ctx, b, "mThumbnail") ; + RsTlvBinaryDataRef b(RS_SERVICE_GXS_TYPE_PHOTO, photo.mThumbnail.data, photo.mThumbnail.size); + RsTypeSerializer::serial_process(j, ctx, b, "mThumbnail") ; } void RsGxsPhotoCommentItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { diff --git a/libretroshare/src/rsitems/rsphotoitems.h b/libretroshare/src/rsitems/rsphotoitems.h index 3819edb88..cf809ce8e 100644 --- a/libretroshare/src/rsitems/rsphotoitems.h +++ b/libretroshare/src/rsitems/rsphotoitems.h @@ -50,7 +50,7 @@ public: virtual ~RsGxsPhotoAlbumItem() { return;} void clear(); - std::ostream &print(std::ostream &out, uint16_t indent = 0); +// std::ostream &print(std::ostream &out, uint16_t indent = 0); virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx); From 0bf02e2bc39b1cc43b282a913e8f128fb7e2ff39 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Tue, 22 May 2018 19:14:25 +0200 Subject: [PATCH 129/161] pqissludp::Initiate_Connection() check remote_addr to be IPv4 before use --- libretroshare/src/pqi/pqissludp.cc | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/libretroshare/src/pqi/pqissludp.cc b/libretroshare/src/pqi/pqissludp.cc index 8d7e45fd1..dbde145df 100644 --- a/libretroshare/src/pqi/pqissludp.cc +++ b/libretroshare/src/pqi/pqissludp.cc @@ -227,6 +227,15 @@ int pqissludp::Initiate_Connection() return -1; } + if(!sockaddr_storage_ipv6_to_ipv4(remote_addr)) + { + std::cerr << __PRETTY_FUNCTION__ << "Error: remote_addr is not " + << "valid IPv4!" << std::endl; + sockaddr_storage_dump(remote_addr); + print_stacktrace(); + return -EINVAL; + } + mTimeoutTS = time(NULL) + mConnectTimeout; //std::cerr << "Setting Connect Timeout " << mConnectTimeout << " Seconds into Future " << std::endl; //std::cerr << " Connect Period is:" << mConnectPeriod << std::endl; @@ -254,32 +263,22 @@ int pqissludp::Initiate_Connection() struct sockaddr_in proxyaddr; struct sockaddr_in remoteaddr; - bool nonIpV4 = false; - if(!sockaddr_storage_ipv6_to_ipv4(remote_addr)) - { - nonIpV4 = true; - std::cerr << __PRETTY_FUNCTION__ << "Error: remote_addr is not " - << "valid IPv4!" << std::endl; - sockaddr_storage_dump(remote_addr); - } if(!sockaddr_storage_ipv6_to_ipv4(mConnectSrcAddr)) { - nonIpV4 = true; std::cerr << __PRETTY_FUNCTION__ << "Error: mConnectSrcAddr is " << "not valid IPv4!" << std::endl; sockaddr_storage_dump(mConnectSrcAddr); + print_stacktrace(); + return -EINVAL; } if(!sockaddr_storage_ipv6_to_ipv4(mConnectProxyAddr)) { - nonIpV4 = true; std::cerr << __PRETTY_FUNCTION__ << "Error: mConnectProxyAddr " << "is not valid IPv4!" << std::endl; sockaddr_storage_dump(mConnectProxyAddr); - } - if(!nonIpV4) - { print_stacktrace(); return -EINVAL; + } struct sockaddr_in *rap = (struct sockaddr_in *) &remote_addr; @@ -301,7 +300,6 @@ int pqissludp::Initiate_Connection() err = tou_connect_via_relay(sockfd, &srcaddr, &proxyaddr, &remoteaddr); } - /*** It seems that the UDP Layer sees x 1.2 the traffic of the SSL layer. * We need to compensate somewhere... we drop the maximum traffic to 75% of limit From 1ad1fdc7bee356bf9502989842639f5e41376eda Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Tue, 22 May 2018 19:17:37 +0200 Subject: [PATCH 130/161] p3NetMgrIMPL::checkNetAddress() notify if port change Plus cleanups and proper sockaddr_storage copy --- libretroshare/src/pqi/p3netmgr.cc | 15 +++++---- libretroshare/src/pqi/p3peermgr.cc | 32 +++++++++++-------- libretroshare/src/rsserver/p3peers.cc | 4 +-- .../src/gui/settings/ServerPage.cpp | 13 +++++--- 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/libretroshare/src/pqi/p3netmgr.cc b/libretroshare/src/pqi/p3netmgr.cc index 7ca1713f7..3381705cd 100644 --- a/libretroshare/src/pqi/p3netmgr.cc +++ b/libretroshare/src/pqi/p3netmgr.cc @@ -987,8 +987,8 @@ bool p3NetMgrIMPL::checkNetAddress() bool addrChanged = false; bool validAddr = false; - struct sockaddr_storage prefAddr; - struct sockaddr_storage oldAddr; + sockaddr_storage prefAddr; + sockaddr_storage oldAddr; if (mNetMode & RS_NET_MODE_TRY_LOOPBACK) { @@ -996,7 +996,7 @@ bool p3NetMgrIMPL::checkNetAddress() std::cerr << "p3NetMgrIMPL::checkNetAddress() LOOPBACK ... forcing to 127.0.0.1"; std::cerr << std::endl; #endif - sockaddr_storage_ipv4_aton(prefAddr, "127.0.0.1"); + sockaddr_storage_ipv4_aton(prefAddr, "127.0.0.1"); validAddr = true; } else @@ -1012,7 +1012,7 @@ bool p3NetMgrIMPL::checkNetAddress() std::vector addrs; if (getLocalAddresses(addrs)) { - for (auto it = addrs.begin(); it!=addrs.end(); ++it) + for (auto it = addrs.begin(); it != addrs.end(); ++it) { sockaddr_storage& addr(*it); if( sockaddr_storage_isValidNet(addr) && @@ -1060,8 +1060,8 @@ bool p3NetMgrIMPL::checkNetAddress() { RS_STACK_MUTEX(mNetMtx); - - oldAddr = mLocalAddr; + + sockaddr_storage_copy(mLocalAddr, oldAddr); addrChanged = !sockaddr_storage_sameip(prefAddr, mLocalAddr); #ifdef NETMGR_DEBUG_TICK @@ -1087,7 +1087,7 @@ bool p3NetMgrIMPL::checkNetAddress() // update address. sockaddr_storage_copyip(mLocalAddr, prefAddr); - mNetFlags.mLocalAddr = mLocalAddr; + sockaddr_storage_copy(mLocalAddr, mNetFlags.mLocalAddr); if(sockaddr_storage_isLoopbackNet(mLocalAddr)) { @@ -1137,6 +1137,7 @@ bool p3NetMgrIMPL::checkNetAddress() if (sockaddr_storage_sameip(mLocalAddr, mExtAddr)) { sockaddr_storage_setport(mExtAddr, sockaddr_storage_port(mLocalAddr)); + addrChanged = true; } // ensure that address family is set, otherwise windows Barfs. diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index ba3ecbf84..d1e9e4e97 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -1239,11 +1239,11 @@ bool p3PeerMgrIMPL::UpdateOwnAddress( const sockaddr_storage& pLocalAddr, sockaddr_storage_copy(pExtAddr, extAddr); sockaddr_storage_ipv6_to_ipv4(extAddr); -#ifdef PEER_DEBUG +//#ifdef PEER_DEBUG std::cerr << "p3PeerMgrIMPL::UpdateOwnAddress(" << sockaddr_storage_tostring(localAddr) << ", " << sockaddr_storage_tostring(extAddr) << ")" << std::endl; -#endif +//#endif if( rsBanList && !rsBanList->isAddressAccepted(localAddr, @@ -1428,21 +1428,25 @@ bool p3PeerMgrIMPL::setLocalAddress(const RsPeerId &id, const struct sockaddr return changed; } -bool p3PeerMgrIMPL::setExtAddress(const RsPeerId &id, const struct sockaddr_storage &addr) +bool p3PeerMgrIMPL::setExtAddress( const RsPeerId &id, + const sockaddr_storage &addr ) { - bool changed = false; - uint32_t check_res = 0 ; + bool changed = false; + uint32_t check_res = 0; - if(rsBanList!=NULL && !rsBanList->isAddressAccepted(addr,RSBANLIST_CHECKING_FLAGS_BLACKLIST,&check_res)) - { - std::cerr << "(SS) trying to set external contact address for peer " << id << " to a banned address " << sockaddr_storage_iptostring(addr )<< std::endl; - return false ; - } + if( rsBanList!=NULL && !rsBanList->isAddressAccepted( + addr, RSBANLIST_CHECKING_FLAGS_BLACKLIST, &check_res) ) + { + std::cerr << "(SS) trying to set external contact address for peer " + << id << " to a banned address " + << sockaddr_storage_iptostring(addr) << std::endl; + return false; + } if (id == AuthSSL::getAuthSSL()->OwnId()) { { - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + RS_STACK_MUTEX(mPeerMtx); if (!sockaddr_storage_same(mOwnState.serveraddr, addr)) { mOwnState.serveraddr = addr; @@ -1455,7 +1459,7 @@ bool p3PeerMgrIMPL::setExtAddress(const RsPeerId &id, const struct sockaddr_s return changed; } - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + RS_STACK_MUTEX(mPeerMtx); /* check if it is a friend */ std::map::iterator it; if (mFriendList.end() == (it = mFriendList.find(id))) @@ -1463,7 +1467,9 @@ bool p3PeerMgrIMPL::setExtAddress(const RsPeerId &id, const struct sockaddr_s if (mOthersList.end() == (it = mOthersList.find(id))) { #ifdef PEER_DEBUG - std::cerr << "p3PeerMgrIMPL::setLocalAddress() cannot add addres info : peer id not found in friend list id: " << id << std::endl; + std::cerr << "p3PeerMgrIMPL::setLocalAddress() cannot add addres " + << "info : peer id not found in friend list id: " << id + << std::endl; #endif return false; } diff --git a/libretroshare/src/rsserver/p3peers.cc b/libretroshare/src/rsserver/p3peers.cc index 52f6c3dba..c6d686469 100644 --- a/libretroshare/src/rsserver/p3peers.cc +++ b/libretroshare/src/rsserver/p3peers.cc @@ -336,8 +336,8 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d) if(!sockaddr_storage_isnull(ps.localaddr)) { sockaddr_storage_ipv6_to_ipv4(ps.localaddr); - d.localAddr = sockaddr_storage_iptostring(ps.localaddr); - d.localPort = sockaddr_storage_port(ps.localaddr); + d.localAddr = sockaddr_storage_iptostring(ps.localaddr); + d.localPort = sockaddr_storage_port(ps.localaddr); } else { diff --git a/retroshare-gui/src/gui/settings/ServerPage.cpp b/retroshare-gui/src/gui/settings/ServerPage.cpp index 042a379cb..dcfe29426 100755 --- a/retroshare-gui/src/gui/settings/ServerPage.cpp +++ b/retroshare-gui/src/gui/settings/ServerPage.cpp @@ -846,10 +846,15 @@ void ServerPage::updateStatus() return; } - /* load up configuration from rsPeers */ - RsPeerDetails detail; - if (!rsPeers->getPeerDetails(rsPeers->getOwnId(), detail)) - return; + /* load up configuration from rsPeers */ + RsPeerDetails detail; + if (!rsPeers->getPeerDetails(rsPeers->getOwnId(), detail)) + { + std::cerr << __PRETTY_FUNCTION__ << " getPeerDetails(...) failed!" + << " This is unexpected report to developers please." + << std::endl; + return; + } /* only update if can't edit */ if (!ui.localPort->isEnabled()) From 7c77e93ff4ebbc7c826de44b54cac8704b408605 Mon Sep 17 00:00:00 2001 From: Phenom Date: Fri, 25 May 2018 17:32:36 +0200 Subject: [PATCH 131/161] Fix AppVeyor Compilation --- appveyor.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 4878efe71..47b3c1ef3 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -63,7 +63,8 @@ clone_depth: 1 # clone entire repository history if not de environment: global: #Qt: https://www.appveyor.com/docs/installed-software#qt - QTDIR: C:\Qt\5.4\mingw491_32 + # (C:\Qt\5.10 mapped to C:\Qt\5.10.1 for backward compatibility) + QTDIR: C:\Qt\5.10\mingw53_32 MSYS2_ARCH: i686 TARGET: i686_32-pc-msys From 428b331d8efede1e2f39f2fc49216c675d081030 Mon Sep 17 00:00:00 2001 From: sehraf Date: Fri, 25 May 2018 23:12:35 +0200 Subject: [PATCH 132/161] fix for Qt 5.11 Quote from Arch mailing list: - there's been a huge header cleanup in Qt modules. Expect build failures for applications that rely on transitive includes instead of declaring all required headers. Those need to be fixed upstream by explicitely adding the missing includes. --- retroshare-gui/src/gui/Posted/PostedItem.cpp | 1 + retroshare-gui/src/gui/chat/ChatTabWidget.cpp | 2 ++ retroshare-gui/src/gui/feeds/GxsChannelPostItem.cpp | 1 + retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp | 1 + 4 files changed, 5 insertions(+) diff --git a/retroshare-gui/src/gui/Posted/PostedItem.cpp b/retroshare-gui/src/gui/Posted/PostedItem.cpp index 7d70b3e15..8fc1cc686 100644 --- a/retroshare-gui/src/gui/Posted/PostedItem.cpp +++ b/retroshare-gui/src/gui/Posted/PostedItem.cpp @@ -22,6 +22,7 @@ */ #include +#include #include "rshare.h" #include "PostedItem.h" diff --git a/retroshare-gui/src/gui/chat/ChatTabWidget.cpp b/retroshare-gui/src/gui/chat/ChatTabWidget.cpp index a965bbb32..6dd9c2757 100644 --- a/retroshare-gui/src/gui/chat/ChatTabWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatTabWidget.cpp @@ -20,6 +20,8 @@ * Boston, MA 02110-1301, USA. ****************************************************************/ +#include + #include "ChatTabWidget.h" #include "ui_ChatTabWidget.h" #include "ChatDialog.h" diff --git a/retroshare-gui/src/gui/feeds/GxsChannelPostItem.cpp b/retroshare-gui/src/gui/feeds/GxsChannelPostItem.cpp index d154408b7..72317640c 100644 --- a/retroshare-gui/src/gui/feeds/GxsChannelPostItem.cpp +++ b/retroshare-gui/src/gui/feeds/GxsChannelPostItem.cpp @@ -23,6 +23,7 @@ #include #include +#include #include "rshare.h" #include "GxsChannelPostItem.h" diff --git a/retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp b/retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp index 97ac9dd75..e9773aba9 100644 --- a/retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp +++ b/retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp @@ -23,6 +23,7 @@ #include #include +#include #include "rshare.h" #include "GxsForumMsgItem.h" From f53e5e8468e1f59a08dac6a1d71f0fb859be6c99 Mon Sep 17 00:00:00 2001 From: sehraf Date: Fri, 25 May 2018 23:59:02 +0200 Subject: [PATCH 133/161] Fix displayed chat link name in id dialog before (decimal): Message in chat room 5327517029776505601 after (proper id): Message in chat room LFD6E08C33A98C658 --- retroshare-gui/src/gui/Identity/IdDialog.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/Identity/IdDialog.cpp b/retroshare-gui/src/gui/Identity/IdDialog.cpp index 71bf7be40..17e62d3ca 100644 --- a/retroshare-gui/src/gui/Identity/IdDialog.cpp +++ b/retroshare-gui/src/gui/Identity/IdDialog.cpp @@ -1994,7 +1994,8 @@ QString IdDialog::createUsageString(const RsIdentityUsage& u) const } case RsIdentityUsage::CHAT_LOBBY_MSG_VALIDATION: // Chat lobby msgs are signed, so each time one comes, or a chat lobby event comes, a signature verificaiton happens. { - RetroShareLink l = RetroShareLink::createChatRoom(ChatId(ChatLobbyId(u.mAdditionalId)),QString::number(u.mAdditionalId)); + ChatId id = ChatId(ChatLobbyId(u.mAdditionalId)); + RetroShareLink l = RetroShareLink::createChatRoom(id, QString::fromStdString(id.toStdString())); return tr("Message in chat room %1").arg(l.toHtml()) ; } case RsIdentityUsage::GLOBAL_ROUTER_SIGNATURE_CHECK: // Global router message validation From 9b16e3338f7a3e144dfb66919ed962a6783ce5f7 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 11 May 2018 20:53:29 +0200 Subject: [PATCH 134/161] removed conflicts to retroshare06 in debian control file --- build_scripts/Debian+Ubuntu/debian/control | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build_scripts/Debian+Ubuntu/debian/control b/build_scripts/Debian+Ubuntu/debian/control index 68b7e8a68..f93af67e9 100644 --- a/build_scripts/Debian+Ubuntu/debian/control +++ b/build_scripts/Debian+Ubuntu/debian/control @@ -8,7 +8,7 @@ Homepage: http://retroshare.sourceforge.net Package: retroshare-voip-plugin Architecture: any -Conflicts: retroshare06-voip-plugin +Conflicts: Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare, libspeex1, libspeexdsp1, libqt5multimedia5 Description: RetroShare VOIP plugin This package provides a plugin for RetroShare, a secured Friend-to-Friend communication @@ -17,7 +17,7 @@ Description: RetroShare VOIP plugin Package: retroshare-feedreader-plugin Architecture: any -Conflicts: retroshare06-feedreader-plugin +Conflicts: Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare Description: RetroShare FeedReader plugin Plugin for Retroshare, adding a RSS feed reader tab to retroshare. @@ -25,14 +25,14 @@ Description: RetroShare FeedReader plugin Package: retroshare-nogui Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, gnome-keyring -Conflicts: retroshare,retroshare06-nogui +Conflicts: retroshare Description: headless version of Retroshare Headless version of the Retroshare platform. Package: retroshare Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, gnome-keyring -Conflicts: retroshare-nogui,retroshare06 +Conflicts: retroshare-nogui Description: Secure communication with friends RetroShare is a Open Source, private and secure decentralised commmunication platform. It creates mesh of computers linked with TLS connections, From f663600be55cf49e3d8b5d517f0b48babb0f91d4 Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 1 Jun 2018 22:27:59 +0200 Subject: [PATCH 135/161] fixed makeSourcePackage.sh --- build_scripts/Debian+Ubuntu/makeSourcePackage.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh index 2cb83425d..007a52790 100755 --- a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh +++ b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh @@ -2,8 +2,8 @@ ###################### PARAMETERS #################### gitpath="https://github.com/RetroShare/RetroShare.git" -#branch="master" -branch="v0.6.4-official_release" +branch="master" +#branch="v0.6.4-official_release" #bubba3="Y" # comment out to compile for bubba3 ###################################################### @@ -33,7 +33,7 @@ gpgkey="0932399B" date=`git log --pretty=format:"%ai" | head -1 | cut -d\ -f1 | sed -e s/-//g` time=`git log --pretty=format:"%aD" | head -1 | cut -d\ -f5 | sed -e s/://g` -hhsh=`git log --pretty=format:"%H" | head -1 | cut -c1-8` +hhsh=`git log --pretty=format:"%H" | head -1 | cut -c1-8` rev=${date}.${hhsh} useretrotor="false" From 6bb1d3c509aa6bbbdb7e8322118de8c39330f98b Mon Sep 17 00:00:00 2001 From: csoler Date: Fri, 1 Jun 2018 22:35:06 +0200 Subject: [PATCH 136/161] updated ubuntu changelog --- build_scripts/Debian+Ubuntu/changelog | 77 +++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/build_scripts/Debian+Ubuntu/changelog b/build_scripts/Debian+Ubuntu/changelog index 4886eb5f0..7bdac642a 100644 --- a/build_scripts/Debian+Ubuntu/changelog +++ b/build_scripts/Debian+Ubuntu/changelog @@ -1,5 +1,82 @@ retroshare (ZZZZZZ-1.XXXXXX~YYYYYY) YYYYYY; urgency=low + f663600 csoler Fri, 1 Jun 2018 22:27:59 +0200 fixed makeSourcePackage.sh + 9b16e33 csoler Fri, 11 May 2018 20:53:29 +0200 removed conflicts to retroshare06 in debian control file + 57dde55 csoler Sat, 26 May 2018 11:23:33 +0200 Merge pull request #1269 from PhenomRetroShare/Add_NeverAskMeForExternalLinkActivated + 33fe823 csoler Sat, 26 May 2018 11:22:47 +0200 Merge pull request #1271 from sehraf/pr_compile-fix-unused-services + 65beb2c csoler Sat, 26 May 2018 11:09:41 +0200 Merge pull request #1275 from sehraf/pr_fix-IDdialog-chat-link + d8b3fc0 csoler Sat, 26 May 2018 11:08:46 +0200 Merge pull request #1274 from PhenomRetroShare/Fix_AppVeyorCompil + f53e5e8 sehraf Fri, 25 May 2018 23:59:02 +0200 Fix displayed chat link name in id dialog before (decimal): Message in chat room 5327517029776505601 after (proper id): Message in chat room LFD6E08C33A98C658 + 428b331 sehraf Fri, 25 May 2018 23:12:35 +0200 fix for Qt 5.11 + 7c77e93 Phenom Fri, 25 May 2018 17:32:36 +0200 Fix AppVeyor Compilation + de65cb6 csoler Tue, 22 May 2018 23:59:26 +0200 Merge pull request #1272 from G10h4ck/net_little_fixes + 1ad1fdc Gioacc Tue, 22 May 2018 19:17:37 +0200 p3NetMgrIMPL::checkNetAddress() notify if port change + 0bf02e2 Gioacc Tue, 22 May 2018 19:14:25 +0200 pqissludp::Initiate_Connection() check remote_addr to be IPv4 before use + 0c19a56 sehraf Mon, 21 May 2018 14:55:10 +0200 compile fix gxsphoto + 92b21d7 sehraf Mon, 21 May 2018 14:26:46 +0200 compile fix wikipoos + 8f107cc Phenom Sat, 19 May 2018 16:37:29 +0200 Add Never Ask Me For External Link Activated + 186617f Gioacc Thu, 17 May 2018 16:43:14 +0200 Fix missing include + 28981b6 csoler Wed, 16 May 2018 13:37:02 +0200 Merge pull request #1251 from PhenomRetroShare/Fix_Cppcheck(duplInheritedMember)InRsItem + d11f88c csoler Wed, 16 May 2018 13:33:25 +0200 Merge pull request #1252 from PhenomRetroShare/Fix_Cppcheck(duplInheritedMember)InbdNode + 2145911 csoler Wed, 16 May 2018 13:30:56 +0200 Merge pull request #1262 from PhenomRetroShare/Fix_PartialDirCheck + e232000 csoler Wed, 16 May 2018 13:28:23 +0200 Merge pull request #1266 from csoler/v0.6-FT + 3f88e3e csoler Wed, 16 May 2018 13:24:52 +0200 added check to avoid hashing files that are currently being modified + 8451550 csoler Wed, 16 May 2018 11:40:39 +0200 added missing licence to file_tree.h + 291c86e csoler Wed, 16 May 2018 11:17:22 +0200 Merge pull request #1265 from csoler/v0.6-GxsFix + 4661329 csoler Wed, 16 May 2018 11:15:18 +0200 fixed bug causing new forums and identities to not show up immediately + 9f37b63 G10h4c Wed, 16 May 2018 10:49:28 +0200 Merge pull request #1264 from G10h4ck/channel_json_api_v2 + 181d99e Gioacc Wed, 16 May 2018 10:38:47 +0200 Give proper error message if Qt version is too old + f12cd57 Phenom Sat, 12 May 2018 14:31:17 +0200 Fix Partial Dir Check + 57cff61 csoler Fri, 11 May 2018 20:28:53 +0200 fixed merge + ecbd115 csoler Fri, 11 May 2018 20:24:24 +0200 added missing include path for libretroshare/src in libresapi + fe5ea9e csoler Fri, 11 May 2018 17:37:15 +0200 additional fixes to debian packaging files + 2173aab csoler Fri, 11 May 2018 17:02:58 +0200 fixed rules file again + 84a02cb csoler Fri, 11 May 2018 16:39:49 +0200 added missing targets in rules file + 4b6304e csoler Fri, 11 May 2018 16:33:19 +0200 few fixes according to lintian complaints + c2492fe csoler Thu, 10 May 2018 23:10:16 +0200 fixed a few lintian errors in debian/control + 9f5409b csoler Thu, 10 May 2018 22:04:11 +0200 fixed email to match mentors login + 45fdbb4 csoler Thu, 10 May 2018 22:03:39 +0200 fixed email to match mentors login + b4ada80 csoler Wed, 9 May 2018 13:54:40 +0200 removed > 3.4.1 in debian/control, because pbuilder would not build + e81c82e csoler Wed, 9 May 2018 13:37:25 +0200 added notes file for debian release + 4d287d6 csoler Wed, 9 May 2018 13:27:16 +0200 fixed weird mistake in grouter which causes an issue only in gcc > 8 + 889c277 csoler Wed, 9 May 2018 10:15:32 +0200 fixed packaging script for debian release + f406d9a csoler Wed, 9 May 2018 10:15:02 +0200 updated debian/control to be the control file for stretch. Updated copyright notice + 186ff0f csoler Wed, 9 May 2018 10:12:34 +0200 created proper debian changelog using dch and updated project url + 6d67936 csoler Tue, 8 May 2018 15:34:44 +0200 Merge pull request #1253 from PhenomRetroShare/Add_LastPostColInGroupTreeWidget + 2ebacf3 Phenom Tue, 8 May 2018 12:08:08 +0200 Add Last Post Column in GroupTreeWidget. + 4d748bd Phenom Mon, 7 May 2018 18:05:07 +0200 Fix CppCheck duplInheritedMember warning in bdNode + f39fd06 Phenom Mon, 7 May 2018 17:29:11 +0200 Fix CppCheck duplInheritedMember warning in RsItem + 1352631 thunde Mon, 7 May 2018 06:42:52 +0200 Updated Windows build environment + 38ac234 thunde Mon, 7 May 2018 07:30:29 +0200 Fixed Windows compile with pre-compiled libraries Added new variable EXTERNAL_LIB_DIR to specify path of external libraries + d9a75a9 thunde Sun, 29 Apr 2018 20:40:05 +0200 Added RapidJSON to Windows build environment + 5b607ad csoler Sun, 6 May 2018 23:53:45 +0200 Merge pull request #1245 from PhenomRetroShare/AddContextMenuForGxsIdInTextChatBrowser + a6821f4 csoler Sun, 6 May 2018 23:02:18 +0200 Merge pull request #1249 from sehraf/pr_libsecret + c20436e csoler Sun, 6 May 2018 22:59:20 +0200 Merge pull request #1250 from PhenomRetroShare/Fix_CppCheckerWarningInFtController + d8dc2a8 csoler Sun, 6 May 2018 22:57:57 +0200 Merge pull request #1246 from PhenomRetroShare/Add_GxsIdInRestoredChatMsg + dca33da Phenom Sun, 6 May 2018 18:53:29 +0200 Fix CppCheck in ftcontroller.cc + c89e36a sehraf Fri, 4 May 2018 20:52:31 +0200 add auto selection of libsecret with fallback to libgnome-keyring + 1129bcb sehraf Fri, 4 May 2018 20:47:41 +0200 Add support for libsecret + 10badf5 sehraf Fri, 4 May 2018 20:43:40 +0200 fix retroshare-nogui.pro autologin + 676c070 csoler Thu, 3 May 2018 15:55:21 +0200 extended the rapid_json trick to plugins + 5f12b60 csoler Thu, 3 May 2018 15:22:39 +0200 added -lgnome-keyring to unix LIBS when rs_autologin is set + bfe8e40 csoler Thu, 3 May 2018 15:22:03 +0200 updated ubuntu changelog + 98f0052 Phenom Thu, 3 May 2018 15:04:22 +0200 Add GxsId in Restored Chat Message. + ecba4c2 Phenom Wed, 2 May 2018 20:31:52 +0200 Add Context Menu for GxsId in lobby textBrowser. + 91ed367 csoler Thu, 3 May 2018 13:45:44 +0200 added packaging for ubuntu bionic + 56e8134 cyril soler Thu, 3 May 2018 11:32:24 +0200 removed sqlite3 lib from ld when using sqlcipher. + 1366f61 csoler Wed, 2 May 2018 23:51:46 +0200 Merge pull request #1241 from RetroPooh/chatimgattprev1 + 8e111c2 csoler Wed, 2 May 2018 22:46:27 +0200 added rapidjson-1.1.0 code hard-coded in the source directory to allow everyone to compile without the need to tweak too much. When v1.1.0 is mainstream (espcially on ubuntu) we can revert back to an external dependency + 48c4b4c G10h4c Sat, 28 Apr 2018 09:34:14 +0200 Merge pull request #1242 from beardog108/master + ccede9d G10h4c Sat, 28 Apr 2018 09:33:14 +0200 Merge pull request #1243 from sehraf/pr_fix-make-install + 10daf3b sehraf Sat, 28 Apr 2018 09:06:10 +0200 Fix 'make install' + 82a00c2 KevinF Fri, 27 Apr 2018 15:34:43 -0500 added rapidjson to package installation command on Debian/OpenSUSE/Arch + be75e89 Gioacc Fri, 27 Apr 2018 20:55:38 +0200 Fix compialtion after merge + f3ae61b Gioacc Fri, 27 Apr 2018 18:17:09 +0200 Merge branch 'json_experiments' + + -- Retroshare Dev Team Thu, 03 May 2018 12:00:00 +0100 + +retroshare (0.6.4-1.20180503.676c0701~xenial) xenial; urgency=low + 2dc69cb RetroP Fri, 27 Apr 2018 16:50:00 +0300 embed preview for images on file attach in chat c199199 Gioacc Sun, 8 Apr 2018 12:37:41 +0200 pqissl silence extra debug message 8245d74 csoler Sat, 7 Apr 2018 14:33:58 +0200 Merge pull request #1230 from csoler/master From d7b366de2315a70a614faca326f49ce1b65acb75 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Thu, 7 Jun 2018 14:54:58 +0200 Subject: [PATCH 137/161] RS-gui Solve unused parameter warning --- retroshare-gui/src/gui/RemoteDirModel.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/RemoteDirModel.cpp b/retroshare-gui/src/gui/RemoteDirModel.cpp index 6f7608e77..22fb0af5b 100644 --- a/retroshare-gui/src/gui/RemoteDirModel.cpp +++ b/retroshare-gui/src/gui/RemoteDirModel.cpp @@ -374,7 +374,7 @@ const QIcon& RetroshareDirModel::getFlagsIcon(FileStorageFlags flags) return *static_icons[n] ; } -QVariant RetroshareDirModel::filterRole(const DirDetails& details,int coln) const +QVariant RetroshareDirModel::filterRole(const DirDetails& details, int /*coln*/) const { if(mFilteredPointers.empty() || mFilteredPointers.find(details.ref) != mFilteredPointers.end()) return QString(RETROSHARE_DIR_MODEL_FILTER_STRING); From 3eff851cdb8a738a9ebda9c180a569a3ec9a4ad9 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Thu, 7 Jun 2018 14:57:31 +0200 Subject: [PATCH 138/161] Improve deprecation of RS_TOKREQ_ANSTYPE_* --- libretroshare/src/retroshare/rstokenservice.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libretroshare/src/retroshare/rstokenservice.h b/libretroshare/src/retroshare/rstokenservice.h index bcb273dd5..079f34023 100644 --- a/libretroshare/src/retroshare/rstokenservice.h +++ b/libretroshare/src/retroshare/rstokenservice.h @@ -61,14 +61,17 @@ /* TODO CLEANUP: RS_TOKREQ_ANSTYPE_* values are meaningless and not used by * RsTokenService or its implementation, and may be arbitrarly defined by each - * GXS client as they are of no usage, their use is deprecated */ + * GXS client as they are of no usage, their use is deprecated, up until the + * definitive cleanup is done new code must use RS_DEPRECATED_TOKREQ_ANSTYPE for + * easier cleanup. */ #ifndef RS_NO_WARN_DEPRECATED # warning RS_TOKREQ_ANSTYPE_* macros are deprecated! #endif -#define RS_TOKREQ_ANSTYPE_LIST 0x0001 -#define RS_TOKREQ_ANSTYPE_SUMMARY 0x0002 -#define RS_TOKREQ_ANSTYPE_DATA 0x0003 -#define RS_TOKREQ_ANSTYPE_ACK 0x0004 +#define RS_DEPRECATED_TOKREQ_ANSTYPE 0x0000 +#define RS_TOKREQ_ANSTYPE_LIST 0x0001 +#define RS_TOKREQ_ANSTYPE_SUMMARY 0x0002 +#define RS_TOKREQ_ANSTYPE_DATA 0x0003 +#define RS_TOKREQ_ANSTYPE_ACK 0x0004 /*! From e81b81dff137e468cbf164811a736863074f00aa Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Thu, 7 Jun 2018 14:59:00 +0200 Subject: [PATCH 139/161] libresapi improve qmake optional builds --- libresapi/src/libresapi.pro | 2 -- libresapi/src/use_libresapi.pri | 5 +++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index 885d430f5..6f6db46a4 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -16,8 +16,6 @@ INCLUDEPATH += ../../rapidjson-1.1.0 INCLUDEPATH += ../../libretroshare/src libresapilocalserver { - CONFIG *= qt - QT *= network SOURCES *= api/ApiServerLocal.cpp HEADERS *= api/ApiServerLocal.h } diff --git a/libresapi/src/use_libresapi.pri b/libresapi/src/use_libresapi.pri index e9b1753a2..c84103f1b 100644 --- a/libresapi/src/use_libresapi.pri +++ b/libresapi/src/use_libresapi.pri @@ -10,6 +10,11 @@ sLibs = mLibs = dLibs = +libresapilocalserver { + CONFIG *= qt + QT *= network +} + libresapihttpserver { mLibs *= microhttpd } From 508951d26f502fdd7265871df9a77629d1ec3be3 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Thu, 7 Jun 2018 15:00:11 +0200 Subject: [PATCH 140/161] Improve channels JSON API v2 list_channels return only with metadata (including the data produced very big JSON saturating some client buffer implementation) add get_channel_info to get full channel data with optional thumbnail thumbnail is sent by default but can be avoided adding "want_thumbnail":false to the JSON query rename get_channel to get_channel_content --- libresapi/src/api/ChannelsHandler.cpp | 117 ++++++++++++++++++++------ libresapi/src/api/ChannelsHandler.h | 3 +- 2 files changed, 94 insertions(+), 26 deletions(-) diff --git a/libresapi/src/api/ChannelsHandler.cpp b/libresapi/src/api/ChannelsHandler.cpp index 3c5084578..c79d693b1 100644 --- a/libresapi/src/api/ChannelsHandler.cpp +++ b/libresapi/src/api/ChannelsHandler.cpp @@ -51,7 +51,8 @@ ChannelsHandler::ChannelsHandler(RsGxsChannels& channels): mChannels(channels) { addResourceHandler("list_channels", this, &ChannelsHandler::handleListChannels); - addResourceHandler("get_channel", this, &ChannelsHandler::handleGetChannel); + addResourceHandler("get_channel_info", this, &ChannelsHandler::handleGetChannelInfo); + addResourceHandler("get_channel_content", this, &ChannelsHandler::handleGetChannelContent); addResourceHandler("toggle_subscribe", this, &ChannelsHandler::handleToggleSubscription); addResourceHandler("toggle_auto_download", this, &ChannelsHandler::handleToggleAutoDownload); addResourceHandler("toggle_read", this, &ChannelsHandler::handleTogglePostRead); @@ -62,12 +63,77 @@ ChannelsHandler::ChannelsHandler(RsGxsChannels& channels): mChannels(channels) void ChannelsHandler::handleListChannels(Request& /*req*/, Response& resp) { RsTokReqOptions opts; - opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA; + opts.mReqType = GXS_REQUEST_TYPE_GROUP_META; uint32_t token; RsTokenService& tChannels = *mChannels.getTokenService(); - tChannels.requestGroupInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts); + tChannels.requestGroupInfo(token, RS_DEPRECATED_TOKREQ_ANSTYPE, opts); + + time_t start = time(NULL); + while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + std::list grps; + if( tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE + && mChannels.getGroupSummary(token, grps) ) + { + for( RsGroupMetaData& grp : grps ) + { + KeyValueReference id("channel_id", grp.mGroupId); + KeyValueReference vis_msg("visible_msg_count", grp.mVisibleMsgCount); + bool own = (grp.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN); + bool subscribed = IS_GROUP_SUBSCRIBED(grp.mSubscribeFlags); + std::string lastPostTsStr = std::to_string(grp.mLastPost); + std::string publishTsStr = std::to_string(grp.mPublishTs); + resp.mDataStream.getStreamToMember() + << id + << makeKeyValueReference("name", grp.mGroupName) + << makeKeyValueReference("last_post_ts", lastPostTsStr) + << makeKeyValueReference("popularity", grp.mPop) + << makeKeyValueReference("publish_ts", publishTsStr) + << vis_msg + << makeKeyValueReference("group_status", grp.mGroupStatus) + << makeKeyValueReference("author_id", grp.mAuthorId) + << makeKeyValueReference("parent_grp_id", grp.mParentGrpId) + << makeKeyValueReference("own", own) + << makeKeyValueReference("subscribed", subscribed); + } + + resp.setOk(); + } + else resp.setFail("Cant get data from GXS!"); +} + +void ChannelsHandler::handleGetChannelInfo(Request& req, Response& resp) +{ + std::string chanIdStr; + req.mStream << makeKeyValueReference("channel_id", chanIdStr); + if(chanIdStr.empty()) + { + resp.setFail("channel_id required!"); + return; + } + + RsGxsGroupId chanId(chanIdStr); + if(chanId.isNull()) + { + resp.setFail("Invalid channel_id:" + chanIdStr); + return; + } + + bool wantThumbnail = true; + req.mStream << makeKeyValueReference("want_thumbnail", wantThumbnail); + + std::list groupIds; groupIds.push_back(chanId); + RsTokReqOptions opts; + opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA; + uint32_t token; + + RsTokenService& tChannels = *mChannels.getTokenService(); + tChannels.requestGroupInfo( token, RS_DEPRECATED_TOKREQ_ANSTYPE, + opts, groupIds ); time_t start = time(NULL); while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) @@ -78,33 +144,35 @@ void ChannelsHandler::handleListChannels(Request& /*req*/, Response& resp) if( tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE && mChannels.getGroupData(token, grps) ) { - for( std::vector::iterator vit = grps.begin(); - vit != grps.end(); ++vit ) + for( RsGxsChannelGroup& grp : grps ) { - RsGxsChannelGroup& grp = *vit; KeyValueReference id("channel_id", grp.mMeta.mGroupId); KeyValueReference vis_msg("visible_msg_count", grp.mMeta.mVisibleMsgCount); bool own = (grp.mMeta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN); bool subscribed = IS_GROUP_SUBSCRIBED(grp.mMeta.mSubscribeFlags); std::string lastPostTsStr = std::to_string(grp.mMeta.mLastPost); std::string publishTsStr = std::to_string(grp.mMeta.mPublishTs); - std::string thumbnail_base64; - Radix64::encode(grp.mImage.mData, grp.mImage.mSize, thumbnail_base64); - resp.mDataStream.getStreamToMember() - << id - << makeKeyValueReference("name", grp.mMeta.mGroupName) - << makeKeyValueReference("last_post_ts", lastPostTsStr) - << makeKeyValueReference("popularity", grp.mMeta.mPop) - << makeKeyValueReference("publish_ts", publishTsStr) - << vis_msg - << makeKeyValueReference("group_status", grp.mMeta.mGroupStatus) - << makeKeyValueReference("author_id", grp.mMeta.mAuthorId) - << makeKeyValueReference("parent_grp_id", grp.mMeta.mParentGrpId) - << makeKeyValueReference("description", grp.mDescription) - << makeKeyValueReference("own", own) - << makeKeyValueReference("subscribed", subscribed) - << makeKeyValueReference("thumbnail_base64_png", thumbnail_base64) - << makeKeyValueReference("auto_download", grp.mAutoDownload); + StreamBase& rgrp(resp.mDataStream.getStreamToMember()); + rgrp << id + << makeKeyValueReference("name", grp.mMeta.mGroupName) + << makeKeyValueReference("last_post_ts", lastPostTsStr) + << makeKeyValueReference("popularity", grp.mMeta.mPop) + << makeKeyValueReference("publish_ts", publishTsStr) + << vis_msg + << makeKeyValueReference("group_status", grp.mMeta.mGroupStatus) + << makeKeyValueReference("author_id", grp.mMeta.mAuthorId) + << makeKeyValueReference("parent_grp_id", grp.mMeta.mParentGrpId) + << makeKeyValueReference("description", grp.mDescription) + << makeKeyValueReference("own", own) + << makeKeyValueReference("subscribed", subscribed) + << makeKeyValueReference("auto_download", grp.mAutoDownload); + + if(wantThumbnail) + { + std::string thumbnail_base64; + Radix64::encode(grp.mImage.mData, grp.mImage.mSize, thumbnail_base64); + rgrp << makeKeyValueReference("thumbnail_base64_png", thumbnail_base64); + } } resp.setOk(); @@ -112,7 +180,7 @@ void ChannelsHandler::handleListChannels(Request& /*req*/, Response& resp) else resp.setFail("Cant get data from GXS!"); } -void ChannelsHandler::handleGetChannel(Request& req, Response& resp) +void ChannelsHandler::handleGetChannelContent(Request& req, Response& resp) { std::string chanIdStr; req.mStream << makeKeyValueReference("channel_id", chanIdStr); @@ -120,7 +188,6 @@ void ChannelsHandler::handleGetChannel(Request& req, Response& resp) { resp.setFail("channel_id required!"); return; - } RsGxsGroupId chanId(chanIdStr); diff --git a/libresapi/src/api/ChannelsHandler.h b/libresapi/src/api/ChannelsHandler.h index b40f23b73..55c5fec2d 100644 --- a/libresapi/src/api/ChannelsHandler.h +++ b/libresapi/src/api/ChannelsHandler.h @@ -30,7 +30,8 @@ struct ChannelsHandler : ResourceRouter private: void handleListChannels(Request& req, Response& resp); - void handleGetChannel(Request& req, Response& resp); + void handleGetChannelInfo(Request& req, Response& resp); + void handleGetChannelContent(Request& req, Response& resp); void handleToggleSubscription(Request& req, Response& resp); void handleCreateChannel(Request& req, Response& resp); void handleToggleAutoDownload(Request& req, Response& resp); From 8edb1f7535f13f359f11b11089cfa1fd6f12d0d8 Mon Sep 17 00:00:00 2001 From: cyril soler Date: Fri, 8 Jun 2018 14:32:25 +0200 Subject: [PATCH 141/161] fixed problem causing infinite loop of net reset in p3NetMgr --- libretroshare/src/pqi/p3netmgr.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libretroshare/src/pqi/p3netmgr.cc b/libretroshare/src/pqi/p3netmgr.cc index 3381705cd..be119fd45 100644 --- a/libretroshare/src/pqi/p3netmgr.cc +++ b/libretroshare/src/pqi/p3netmgr.cc @@ -1134,8 +1134,11 @@ bool p3NetMgrIMPL::checkNetAddress() * are the same (modify server)... this mismatch can * occur when the local port is changed.... */ - if (sockaddr_storage_sameip(mLocalAddr, mExtAddr)) + if (sockaddr_storage_sameip(mLocalAddr, mExtAddr) && sockaddr_storage_port(mLocalAddr) != sockaddr_storage_port(mExtAddr)) { +#ifdef NETMGR_DEBUG_RESET + std::cerr << "p3NetMgrIMPL::checkNetAddress() local and external ports are not the same. Setting external port to " << sockaddr_storage_port(mLocalAddr) << std::endl; +#endif sockaddr_storage_setport(mExtAddr, sockaddr_storage_port(mLocalAddr)); addrChanged = true; } From 4eda2779222812d3ffedaacf1fc5b6e526e6b8e5 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Thu, 31 May 2018 17:59:55 +0200 Subject: [PATCH 142/161] Added missing declare of metatype --- retroshare-gui/src/gui/chat/ChatLobbyDialog.h | 1 + 1 file changed, 1 insertion(+) diff --git a/retroshare-gui/src/gui/chat/ChatLobbyDialog.h b/retroshare-gui/src/gui/chat/ChatLobbyDialog.h index f70eb27a6..6f3f52096 100644 --- a/retroshare-gui/src/gui/chat/ChatLobbyDialog.h +++ b/retroshare-gui/src/gui/chat/ChatLobbyDialog.h @@ -28,6 +28,7 @@ #include "ChatDialog.h" Q_DECLARE_METATYPE(RsGxsId) +Q_DECLARE_METATYPE(QList) class GxsIdChooser ; class QToolButton; From 5953e7bbe892654fd96fdd77d9d0c998cff8fb7a Mon Sep 17 00:00:00 2001 From: thunder2 Date: Thu, 31 May 2018 19:19:41 +0200 Subject: [PATCH 143/161] Windows build environment - Added "-spec win32-g++" to build.bat - Added "--no-check-certificate" to download-file.bat - Fixed build installer --- build_scripts/Windows/build/build-installer.bat | 2 +- build_scripts/Windows/build/build.bat | 2 +- build_scripts/Windows/tools/download-file.bat | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build_scripts/Windows/build/build-installer.bat b/build_scripts/Windows/build/build-installer.bat index 1063d8680..c986adbba 100644 --- a/build_scripts/Windows/build/build-installer.bat +++ b/build_scripts/Windows/build/build-installer.bat @@ -9,7 +9,7 @@ call "%EnvPath%\env.bat" if errorlevel 1 goto error_env :: Initialize environment -call "%~dp0env.bat" %* +call "%~dp0env.bat" standard if errorlevel 2 exit /B 2 if errorlevel 1 goto error_env diff --git a/build_scripts/Windows/build/build.bat b/build_scripts/Windows/build/build.bat index 30d20c59d..3fa8fb49a 100644 --- a/build_scripts/Windows/build/build.bat +++ b/build_scripts/Windows/build/build.bat @@ -52,7 +52,7 @@ title Build - %SourceName%%RsType%-%RsBuildConfig% [qmake] set RS_QMAKE_CONFIG=%RsBuildConfig% version_detail_bash_script rs_autologin retroshare_plugins if "%RsRetroTor%"=="1" set RS_QMAKE_CONFIG=%RS_QMAKE_CONFIG% retrotor -qmake "%SourcePath%\RetroShare.pro" -r "CONFIG+=%RS_QMAKE_CONFIG%" "EXTERNAL_LIB_DIR=%BuildLibsPath%\libs" +qmake "%SourcePath%\RetroShare.pro" -r -spec win32-g++ "CONFIG+=%RS_QMAKE_CONFIG%" "EXTERNAL_LIB_DIR=%BuildLibsPath%\libs" if errorlevel 1 goto error echo. diff --git a/build_scripts/Windows/tools/download-file.bat b/build_scripts/Windows/tools/download-file.bat index d42349ccb..b0e93be5f 100644 --- a/build_scripts/Windows/tools/download-file.bat +++ b/build_scripts/Windows/tools/download-file.bat @@ -8,6 +8,6 @@ if "%~2"=="" ( ) ::"%EnvCurlExe%" -L -k "%~1" -o "%~2" -"%EnvWgetExe%" --continue "%~1" --output-document="%~2" +"%EnvWgetExe%" --no-check-certificate --continue "%~1" --output-document="%~2" exit /B %ERRORLEVEL% From 5d237c8753d94443e5f8e715b077e8500ef26640 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 9 Jun 2018 17:08:13 +0200 Subject: [PATCH 144/161] Improve libresapi qmake files --- libresapi/src/libresapi.pro | 3 --- libresapi/src/use_libresapi.pri | 5 +++++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index 6f6db46a4..161ce9bf7 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -21,9 +21,6 @@ libresapilocalserver { } libresapi_settings { - CONFIG *= qt - QT *= core - SOURCES += api/SettingsHandler.cpp HEADERS += api/SettingsHandler.h } diff --git a/libresapi/src/use_libresapi.pri b/libresapi/src/use_libresapi.pri index c84103f1b..ee4c87d46 100644 --- a/libresapi/src/use_libresapi.pri +++ b/libresapi/src/use_libresapi.pri @@ -15,6 +15,11 @@ libresapilocalserver { QT *= network } +libresapi_settings { + CONFIG *= qt + QT *= core +} + libresapihttpserver { mLibs *= microhttpd } From 1825b263f00eef69f39616e1cf13a4c1c35d8d0c Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 9 Jun 2018 17:21:59 +0200 Subject: [PATCH 145/161] ChunkMap::reAskPendingChunk fix unused parameter warning --- libretroshare/src/ft/ftchunkmap.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libretroshare/src/ft/ftchunkmap.cc b/libretroshare/src/ft/ftchunkmap.cc index 64411ac58..800408ec6 100644 --- a/libretroshare/src/ft/ftchunkmap.cc +++ b/libretroshare/src/ft/ftchunkmap.cc @@ -257,7 +257,9 @@ void ChunkMap::setChunkCheckingResult(uint32_t chunk_number,bool check_succeeded } } -bool ChunkMap::reAskPendingChunk(const RsPeerId& peer_id,uint32_t size_hint,uint64_t& offset,uint32_t& size) +bool ChunkMap::reAskPendingChunk( const RsPeerId& peer_id, + uint32_t /*size_hint*/, + uint64_t& offset, uint32_t& size) { // make sure that we're at the end of the file. No need to be too greedy in the middle of it. From 9886840b79d617a32764ad52ef4747f5fd999250 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 9 Jun 2018 17:59:11 +0200 Subject: [PATCH 146/161] Provide proper constructor for SerializeContext Deprecate constructor that depends on deprecated declarations. --- libretroshare/src/serialiser/rsserializer.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/libretroshare/src/serialiser/rsserializer.h b/libretroshare/src/serialiser/rsserializer.h index dcc4d089f..9640694e0 100644 --- a/libretroshare/src/serialiser/rsserializer.h +++ b/libretroshare/src/serialiser/rsserializer.h @@ -225,6 +225,13 @@ struct RsGenericSerializer : RsSerialType /** Allow shared allocator usage to avoid costly JSON deepcopy for * nested RsSerializable */ SerializeContext( + uint8_t *data, uint32_t size, + SerializationFlags flags = SERIALIZATION_FLAG_NONE, + RsJson::AllocatorType* allocator = nullptr) : + mData(data), mSize(size), mOffset(0), mOk(true), mFlags(flags), + mJson(rapidjson::kObjectType, allocator) {} + + RS_DEPRECATED SerializeContext( uint8_t *data, uint32_t size, SerializationFormat format, SerializationFlags flags, RsJson::AllocatorType* allocator = nullptr) : From 6805875333fe4c2e64457b89f193fbf27c84c816 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sun, 10 Jun 2018 15:10:13 +0200 Subject: [PATCH 147/161] Finally get rid of the annoying MASQUARADING typo --- libbitdht/src/bitdht/bdnode.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libbitdht/src/bitdht/bdnode.cc b/libbitdht/src/bitdht/bdnode.cc index f7ae5e220..95d1d1c7e 100644 --- a/libbitdht/src/bitdht/bdnode.cc +++ b/libbitdht/src/bitdht/bdnode.cc @@ -518,7 +518,7 @@ void bdNode::checkPotentialPeer(bdId *id, bdId *src) #ifndef DISABLE_BAD_PEER_FILTER std::cerr << "bdNode::checkPotentialPeer("; mFns->bdPrintId(std::cerr, id); - std::cerr << ") MASQARADING AS KNOWN PEER - FLAGGING AS BAD"; + std::cerr << ") MASQUERADING AS KNOWN PEER - FLAGGING AS BAD"; std::cerr << std::endl; // Stores in queue for later callback and desemination around the network. @@ -603,7 +603,7 @@ void bdNode::addPeer(const bdId *id, uint32_t peerflags) std::cerr << "bdNode::addPeer("; mFns->bdPrintId(std::cerr, id); std::cerr << ", " << std::hex << peerflags << std::dec; - std::cerr << ") MASQARADING AS KNOWN PEER - FLAGGING AS BAD"; + std::cerr << ") MASQUERADING AS KNOWN PEER - FLAGGING AS BAD"; std::cerr << std::endl; From 1f76108a9e3ec5dacc1cd107ed57848f1a92eb0e Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 11 Jun 2018 14:21:28 +0200 Subject: [PATCH 148/161] Remove some cruft from build files --- libretroshare/src/libretroshare.pro | 14 -------------- retroshare-gui/src/gui/settings/rsharesettings.cpp | 4 ++-- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index e401ed8ee..729c9186d 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -267,8 +267,6 @@ win32-g++ { DEFINES += USE_CMD_ARGS - CONFIG += upnp_miniupnpc - wLibs = ws2_32 gdi32 uuid iphlpapi crypt32 ole32 winmm LIBS += $$linkDynamicLibs(wLibs) } @@ -279,22 +277,10 @@ mac { QMAKE_CC = $${QMAKE_CXX} OBJECTS_DIR = temp/obj MOC_DIR = temp/moc - #DEFINES = WINDOWS_SYS WIN32 STATICLIB MINGW - #DEFINES *= MINIUPNPC_VERSION=13 - - CONFIG += upnp_miniupnpc - CONFIG += c++11 - - # zeroconf disabled at the end of libretroshare.pro (but need the code) - #CONFIG += zeroconf - #CONFIG += zcnatassist # Beautiful Hack to fix 64bit file access. QMAKE_CXXFLAGS *= -Dfseeko64=fseeko -Dftello64=ftello -Dfopen64=fopen -Dvstatfs64=vstatfs - #GPG_ERROR_DIR = ../../../../libgpg-error-1.7 - #GPGME_DIR = ../../../../gpgme-1.1.8 - for(lib, LIB_DIR):LIBS += -L"$$lib" for(bin, BIN_DIR):LIBS += -L"$$bin" diff --git a/retroshare-gui/src/gui/settings/rsharesettings.cpp b/retroshare-gui/src/gui/settings/rsharesettings.cpp index 91b306cc0..e37216e39 100644 --- a/retroshare-gui/src/gui/settings/rsharesettings.cpp +++ b/retroshare-gui/src/gui/settings/rsharesettings.cpp @@ -34,8 +34,8 @@ #include #include -#if defined(Q_OS_WIN) -#include +#ifdef Q_OS_WIN +# include #endif /* Retroshare's Settings */ From 8a901e658cd42a27a5738e08fe51528e2090b717 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 11 Jun 2018 14:29:07 +0200 Subject: [PATCH 149/161] Improve appveyor CI --- appveyor.yml | 155 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 117 insertions(+), 38 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 47b3c1ef3..6f26a1c67 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -52,7 +52,7 @@ on_finish: #- ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1')) # clone directory -clone_folder: c:\projects\RetroShare +clone_folder: C:\projects\RetroShare # fetch repository as zip archive #shallow_clone: true # default is "false" @@ -62,20 +62,18 @@ clone_depth: 1 # clone entire repository history if not de environment: global: - #Qt: https://www.appveyor.com/docs/installed-software#qt - # (C:\Qt\5.10 mapped to C:\Qt\5.10.1 for backward compatibility) - QTDIR: C:\Qt\5.10\mingw53_32 +## Qt: https://www.appveyor.com/docs/installed-software#qt +# QTDIR: C:\Qt\5.10.1\mingw53_32 MSYS2_ARCH: i686 TARGET: i686_32-pc-msys + MINGW_PREFIX: C:\msys64\mingw32 + RS_DEPLOY: RetroShare_deploy # build cache to preserve files/folders between builds -cache: - - c:\projects\libs -# - packages -> **\packages.config # preserve "packages" directory in the root of build folder but will reset it if packages.config is modified -# - projectA\libs -# - node_modules # local npm modules -# - %APPDATA%\npm-cache # npm cache +#cache: +# Disabled because it's bigger then supported by appveyor free plan +# - C:\msys64\var\cache\pacman\pkg # scripts that run after cloning repository #install: @@ -90,24 +88,12 @@ install: # Configuring MSys2 - set PATH=C:\msys64\usr\bin;%PATH% - set PATH=C:\msys64\mingw32\bin;%PATH% + - pacman --noconfirm -S mingw-w64-i686-qt5 mingw-w64-i686-miniupnpc mingw-w64-i686-sqlcipher mingw-w64-i686-libmicrohttpd + #- pacman --noconfirm -S mingw-w64-i686-qt5-static mingw-w64-i686-miniupnpc mingw-w64-i686-sqlcipher mingw-w64-i686-libmicrohttpd + #- set PATH=C:\msys64\mingw32\qt5-static\bin\;%PATH% + # Configuring Qt - - set PATH=%QTDIR%\bin;C:\Qt\Tools\mingw491_32\bin;%PATH% - # Install all default programms - #- C:\msys64\usr\bin\bash -lc "pacman --noconfirm -Sy base-devel git mercurial cvs wget p7zip gcc perl ruby python2" #Already installed - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -Sy openssl-devel" - # Install toolchain - #- C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-toolchain mingw-w64-x86_64-toolchain" #Already installed - # Install other binutils - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-curl mingw-w64-x86_64-curl" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-miniupnpc mingw-w64-x86_64-miniupnpc" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-sqlite3 mingw-w64-x86_64-sqlite3" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-speex mingw-w64-x86_64-speex" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-speexdsp mingw-w64-x86_64-speexdsp" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-opencv mingw-w64-x86_64-opencv" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-ffmpeg mingw-w64-x86_64-ffmpeg" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-libmicrohttpd mingw-w64-x86_64-libmicrohttpd" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-libxslt mingw-w64-x86_64-libxslt" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-rapidjson mingw-w64-x86_64-rapidjson" +# - set PATH=%QTDIR%\bin;C:\Qt\Tools\mingw491_32\bin;%PATH% # Hack for new MSys2 - copy C:\msys64\mingw32\i686-w64-mingw32\bin\ar.exe C:\msys64\mingw32\bin\i686-w64-mingw32-ar.exe @@ -116,10 +102,6 @@ install: - copy C:\msys64\mingw64\x86_64-w64-mingw32\bin\ar.exe C:\msys64\mingw64\bin\x86_64-w64-mingw32-ar.exe - copy C:\msys64\mingw64\x86_64-w64-mingw32\bin\ranlib.exe C:\msys64\mingw64\bin\x86_64-w64-mingw32-ranlib.exe - copy C:\msys64\mingw64\bin\windres.exe C:\msys64\mingw64\bin\x86_64-w64-mingw32-windres.exe - # Build missing Libs - #- C:\msys64\mingw32.exe -lc "cd /c/projects/RetroShare/msys2_build_libs/ && make" - # Clone RetroShare - #- git clone -q --branch={branch} https://github.com/RetroShare/RetroShare.git C:\projects\RetroShare #---------------------------------# @@ -144,18 +126,112 @@ configuration: Release # scripts to run before build before_build: + - cd C:\projects\RetroShare +# - find C:\ > filelist.txt # scripts to run *after* solution is built and *before* automatic packaging occurs (web apps, NuGet packages, Azure Cloud Services) before_package: -# scripts to run after build -after_build: - # to run your custom scripts instead of automatic MSBuild build_script: - - cd C:\projects\RetroShare - - qmake CONFIG+=no_sqlcipher - - make + - qmake -Wall -spec win32-g++ "CONFIG=debug" + - mingw32-make -j3 + +# scripts to run after build +after_build: + - mkdir %RS_DEPLOY% + - copy retroshare-nogui\src\retroshare-nogui.exe %RS_DEPLOY%\ + - copy retroshare-gui\src\retroshare.exe %RS_DEPLOY%\ + +## In Debug build winedeplyqt forget the non debug Qt libs + - copy C:\msys64\mingw32\bin\Qt5Svg.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5Core.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5Multimedia.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5Widgets.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5Xml.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5PrintSupport.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5Gui.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5Network.dll %RS_DEPLOY%\ + + - mkdir %RS_DEPLOY%\playlistformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\playlistformats\qtmultimedia_m3u.dll %RS_DEPLOY%\playlistformats + + - mkdir %RS_DEPLOY%\printsupport\ + - copy C:\msys64\mingw32\share\qt5\plugins\printsupport\windowsprintersupport.dll %RS_DEPLOY%\printsupport\ + + - mkdir %RS_DEPLOY%\iconengines\ + - copy C:\msys64\mingw32\share\qt5\plugins\iconengines\qsvgicon.dll %RS_DEPLOY%\iconengines\ + + - mkdir %RS_DEPLOY%\bearer\ + - copy C:\msys64\mingw32\share\qt5\plugins\bearer\qgenericbearer.dll %RS_DEPLOY%\bearer\ + + - mkdir %RS_DEPLOY%\mediaservice\ + - copy C:\msys64\mingw32\share\qt5\plugins\mediaservice\qtmedia_audioengine.dll %RS_DEPLOY%\mediaservice\ + - copy C:\msys64\mingw32\share\qt5\plugins\mediaservice\dsengine.dll %RS_DEPLOY%\mediaservice\ + + - mkdir %RS_DEPLOY%\styles\ + - copy C:\msys64\mingw32\share\qt5\plugins\styles\qwindowsvistastyle.dll %RS_DEPLOY%\styles\ + + - mkdir %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qwebp.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qicns.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qjpeg.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qtiff.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qtga.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qjp2.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qico.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qwbmp.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qicns.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qgif.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qsvg.dll %RS_DEPLOY%\imageformats\ + + - mkdir %RS_DEPLOY%\platforms\ + - copy C:\msys64\mingw32\share\qt5\plugins\platforms\qwindows.dll %RS_DEPLOY%\platforms\ + + - mkdir %RS_DEPLOY%\audio\ + - copy C:\msys64\mingw32\share\qt5\plugins\audio\qtaudio_windows.dll %RS_DEPLOY%\audio\ + + - windeployqt %RS_DEPLOY%\retroshare.exe + + - copy C:\msys64\mingw32\bin\libbz2*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libeay32.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libminiupnpc.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libsqlcipher*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libsqlite3*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\ssleay32.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\zlib*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libgcc_s_dw2*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libstdc*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libwinpthread*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libicu*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libpcre*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libharfbuzz*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libpng*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libfreetype*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libglib*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libgraphite2.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libintl*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libiconv*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libjasper*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libjpeg*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libtiff*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libwebp*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libwebpdemux*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\liblzma*.dll %RS_DEPLOY%\ + +## Needed for libresapi http + - copy C:\msys64\mingw32\bin\libmicrohttpd*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libgnutls*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libgmp*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libhogweed*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libidn2*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libnettle*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libp11-kit*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libtasn1*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libunistring*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libffi*.dll %RS_DEPLOY%\ + + - find C:\projects\RetroShare >> filelist.txt # to disable automatic builds #build: off @@ -164,7 +240,10 @@ build_script: # artifacts configuration # #---------------------------------# -#artifacts: +artifacts: + - path: $(RS_DEPLOY) + - path: '**\*.exe' + - path: filelist.txt # # # pushing a single file # - path: test.zip From 3c678f2a282c9356741e4e1337bd8adf2b83074b Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Mon, 11 Jun 2018 16:05:25 +0200 Subject: [PATCH 150/161] Fix windows build if MINGW_PREFIX is defined --- retroshare.pri | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/retroshare.pri b/retroshare.pri index 6b8fdd870..111530a39 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -400,13 +400,11 @@ win32-g++ { PREFIX_MSYS2=$${TEMPTATIVE_MSYS2} } - !isEmpty(PREFIX_MSYS2) { - message(Found MSYS2: $${PREFIX_MSYS2}) + !isEmpty(PREFIX_MSYS2):message(Found MSYS2: $${PREFIX_MSYS2}) + } - isEmpty(PREFIX) { - PREFIX = $$system_path($${PREFIX_MSYS2}) - } - } + isEmpty(PREFIX):!isEmpty(PREFIX_MSYS2) { + PREFIX = $$system_path($${PREFIX_MSYS2}) } isEmpty(PREFIX) { From 79e676edbe646ea3c8f3257ffbbd335437310208 Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 18 Jun 2018 22:27:05 +0200 Subject: [PATCH 151/161] fixed re-load of GXS groups (forums/channels) when the read flag is changed --- libretroshare/src/gxs/rsgenexchange.cc | 2 +- libretroshare/src/services/p3gxschannels.cc | 3 +-- .../src/gui/gxs/RsGxsUpdateBroadcastBase.cpp | 8 +++++++- retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp | 13 +++++++------ 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 15d0aa9e9..e219821f2 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -2010,7 +2010,7 @@ void RsGenExchange::processMsgMetaChanges() if (!msgIds.empty()) { RS_STACK_MUTEX(mGenMtx); - RsGxsMsgChange* c = new RsGxsMsgChange(RsGxsNotify::TYPE_PROCESSED, true); + RsGxsMsgChange* c = new RsGxsMsgChange(RsGxsNotify::TYPE_PROCESSED, false); c->msgChangeMap = msgIds; mNotifications.push_back(c); } diff --git a/libretroshare/src/services/p3gxschannels.cc b/libretroshare/src/services/p3gxschannels.cc index e70769c7a..beb828fdf 100644 --- a/libretroshare/src/services/p3gxschannels.cc +++ b/libretroshare/src/services/p3gxschannels.cc @@ -218,8 +218,7 @@ RsGenExchange::ServiceCreate_Return p3GxsChannels::service_CreateGroup(RsGxsGrpI void p3GxsChannels::notifyChanges(std::vector &changes) { #ifdef GXSCHANNELS_DEBUG - std::cerr << "p3GxsChannels::notifyChanges()"; - std::cerr << std::endl; + std::cerr << "p3GxsChannels::notifyChanges() : " << changes.size() << "changes to notify" << std::endl; #endif p3Notify *notify = NULL; diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp index bf092c3cc..a657959a9 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp @@ -68,7 +68,13 @@ void RsGxsUpdateBroadcastBase::updateBroadcastChanged() /* Update only update when the widget is visible. */ if (mUpdateWhenInvisible || !widget || widget->isVisible()) { - if (!mGrpIds.empty() || !mGrpIdsMeta.empty() || !mMsgIds.empty() || !mMsgIdsMeta.empty()) + // (cyril) Re-load the entire group is new messages are here, or if group metadata has changed (e.g. visibility permissions, admin rights, etc). + // Do not re-load if Msg data has changed, which means basically the READ flag has changed, because this action is done in the UI in the + // first place so there's no need to re-update the UI once this is done. + // + // The question to whether we should re=load when mGrpIds is not empty is still open. It's not harmful anyway. + + if (!mGrpIds.empty() || !mGrpIdsMeta.empty() /*|| !mMsgIds.empty()*/ || !mMsgIdsMeta.empty()) mFillComplete = true ; securedUpdateDisplay(); diff --git a/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp b/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp index cbda167cb..35da468da 100644 --- a/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp +++ b/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp @@ -45,20 +45,21 @@ RsGxsUpdateBroadcast *RsGxsUpdateBroadcast::get(RsGxsIfaceHelper *ifaceImpl) void RsGxsUpdateBroadcast::onChangesReceived(const RsGxsChanges& changes) { -#ifdef DEBUG_GXS_BROADCAST +#ifndef DEBUG_GXS_BROADCAST std::cerr << "onChangesReceived()" << std::endl; { std::cerr << "Received changes for service " << (void*)changes.mService << ", expecting service " << (void*)mIfaceImpl->getTokenService() << std::endl; - std::cerr << " changes content: " << std::endl; - for(std::list::const_iterator it(changes.mGrps.begin());it!=changes.mGrps.end();++it) std::cerr << " grp id: " << *it << std::endl; - for(std::list::const_iterator it(changes.mGrpsMeta.begin());it!=changes.mGrpsMeta.end();++it) std::cerr << " grp meta: " << *it << std::endl; + for(std::list::const_iterator it(changes.mGrps.begin());it!=changes.mGrps.end();++it) + std::cerr << "[GRP CHANGE] grp id: " << *it << std::endl; + for(std::list::const_iterator it(changes.mGrpsMeta.begin());it!=changes.mGrpsMeta.end();++it) + std::cerr << "[GRP CHANGE] grp meta: " << *it << std::endl; for(std::map >::const_iterator it(changes.mMsgs.begin());it!=changes.mMsgs.end();++it) for(uint32_t i=0;isecond.size();++i) - std::cerr << " grp id: " << it->first << ". Msg ID " << it->second[i] << std::endl; + std::cerr << "[MSG CHANGE] grp id: " << it->first << ". Msg ID " << it->second[i] << std::endl; for(std::map >::const_iterator it(changes.mMsgsMeta.begin());it!=changes.mMsgsMeta.end();++it) for(uint32_t i=0;isecond.size();++i) - std::cerr << " grp id: " << it->first << ". Msg Meta " << it->second[i] << std::endl; + std::cerr << "[MSG CHANGE] grp id: " << it->first << ". Msg Meta " << it->second[i] << std::endl; } #endif if(changes.mService != mIfaceImpl->getTokenService()) From 2e7398ac9bbbf94aad1df46a53dcd304a039cbe7 Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 18 Jun 2018 22:35:22 +0200 Subject: [PATCH 152/161] removed debug info --- .../src/util/RsGxsUpdateBroadcast.cpp | 166 +++++++++--------- 1 file changed, 83 insertions(+), 83 deletions(-) diff --git a/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp b/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp index 35da468da..48c13d6db 100644 --- a/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp +++ b/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp @@ -1,83 +1,83 @@ -#include - -#include "RsGxsUpdateBroadcast.h" -#include "gui/notifyqt.h" - -#include - -//#define DEBUG_GXS_BROADCAST 1 - -// previously gxs allowed only one event consumer to poll for changes -// this required a single broadcast instance per service -// now the update notify works through rsnotify and notifyqt -// so the single instance per service is not really needed anymore - -QMap updateBroadcastMap; - -RsGxsUpdateBroadcast::RsGxsUpdateBroadcast(RsGxsIfaceHelper *ifaceImpl) : - QObject(NULL), mIfaceImpl(ifaceImpl) -{ - connect(NotifyQt::getInstance(), SIGNAL(gxsChange(RsGxsChanges)), this, SLOT(onChangesReceived(RsGxsChanges))); -} - -void RsGxsUpdateBroadcast::cleanup() -{ - QMap::iterator it; - for (it = updateBroadcastMap.begin(); it != updateBroadcastMap.end(); ++it) { - delete(it.value()); - } - - updateBroadcastMap.clear(); -} - -RsGxsUpdateBroadcast *RsGxsUpdateBroadcast::get(RsGxsIfaceHelper *ifaceImpl) -{ - QMap::iterator it = updateBroadcastMap.find(ifaceImpl); - if (it != updateBroadcastMap.end()) { - return it.value(); - } - - RsGxsUpdateBroadcast *updateBroadcast = new RsGxsUpdateBroadcast(ifaceImpl); - updateBroadcastMap.insert(ifaceImpl, updateBroadcast); - - return updateBroadcast; -} - -void RsGxsUpdateBroadcast::onChangesReceived(const RsGxsChanges& changes) -{ -#ifndef DEBUG_GXS_BROADCAST - std::cerr << "onChangesReceived()" << std::endl; - - { - std::cerr << "Received changes for service " << (void*)changes.mService << ", expecting service " << (void*)mIfaceImpl->getTokenService() << std::endl; - for(std::list::const_iterator it(changes.mGrps.begin());it!=changes.mGrps.end();++it) - std::cerr << "[GRP CHANGE] grp id: " << *it << std::endl; - for(std::list::const_iterator it(changes.mGrpsMeta.begin());it!=changes.mGrpsMeta.end();++it) - std::cerr << "[GRP CHANGE] grp meta: " << *it << std::endl; - for(std::map >::const_iterator it(changes.mMsgs.begin());it!=changes.mMsgs.end();++it) - for(uint32_t i=0;isecond.size();++i) - std::cerr << "[MSG CHANGE] grp id: " << it->first << ". Msg ID " << it->second[i] << std::endl; - for(std::map >::const_iterator it(changes.mMsgsMeta.begin());it!=changes.mMsgsMeta.end();++it) - for(uint32_t i=0;isecond.size();++i) - std::cerr << "[MSG CHANGE] grp id: " << it->first << ". Msg Meta " << it->second[i] << std::endl; - } -#endif - if(changes.mService != mIfaceImpl->getTokenService()) - { - // std::cerr << "(EE) Incorrect service. Dropping." << std::endl; - - return; - } - - if (!changes.mMsgs.empty() || !changes.mMsgsMeta.empty()) - { - emit msgsChanged(changes.mMsgs, changes.mMsgsMeta); - } - - if (!changes.mGrps.empty() || !changes.mGrpsMeta.empty()) - { - emit grpsChanged(changes.mGrps, changes.mGrpsMeta); - } - - emit changed(); -} +#include + +#include "RsGxsUpdateBroadcast.h" +#include "gui/notifyqt.h" + +#include + +//#define DEBUG_GXS_BROADCAST 1 + +// previously gxs allowed only one event consumer to poll for changes +// this required a single broadcast instance per service +// now the update notify works through rsnotify and notifyqt +// so the single instance per service is not really needed anymore + +QMap updateBroadcastMap; + +RsGxsUpdateBroadcast::RsGxsUpdateBroadcast(RsGxsIfaceHelper *ifaceImpl) : + QObject(NULL), mIfaceImpl(ifaceImpl) +{ + connect(NotifyQt::getInstance(), SIGNAL(gxsChange(RsGxsChanges)), this, SLOT(onChangesReceived(RsGxsChanges))); +} + +void RsGxsUpdateBroadcast::cleanup() +{ + QMap::iterator it; + for (it = updateBroadcastMap.begin(); it != updateBroadcastMap.end(); ++it) { + delete(it.value()); + } + + updateBroadcastMap.clear(); +} + +RsGxsUpdateBroadcast *RsGxsUpdateBroadcast::get(RsGxsIfaceHelper *ifaceImpl) +{ + QMap::iterator it = updateBroadcastMap.find(ifaceImpl); + if (it != updateBroadcastMap.end()) { + return it.value(); + } + + RsGxsUpdateBroadcast *updateBroadcast = new RsGxsUpdateBroadcast(ifaceImpl); + updateBroadcastMap.insert(ifaceImpl, updateBroadcast); + + return updateBroadcast; +} + +void RsGxsUpdateBroadcast::onChangesReceived(const RsGxsChanges& changes) +{ +#ifdef DEBUG_GXS_BROADCAST + std::cerr << "onChangesReceived()" << std::endl; + + { + std::cerr << "Received changes for service " << (void*)changes.mService << ", expecting service " << (void*)mIfaceImpl->getTokenService() << std::endl; + for(std::list::const_iterator it(changes.mGrps.begin());it!=changes.mGrps.end();++it) + std::cerr << "[GRP CHANGE] grp id: " << *it << std::endl; + for(std::list::const_iterator it(changes.mGrpsMeta.begin());it!=changes.mGrpsMeta.end();++it) + std::cerr << "[GRP CHANGE] grp meta: " << *it << std::endl; + for(std::map >::const_iterator it(changes.mMsgs.begin());it!=changes.mMsgs.end();++it) + for(uint32_t i=0;isecond.size();++i) + std::cerr << "[MSG CHANGE] grp id: " << it->first << ". Msg ID " << it->second[i] << std::endl; + for(std::map >::const_iterator it(changes.mMsgsMeta.begin());it!=changes.mMsgsMeta.end();++it) + for(uint32_t i=0;isecond.size();++i) + std::cerr << "[MSG CHANGE] grp id: " << it->first << ". Msg Meta " << it->second[i] << std::endl; + } +#endif + if(changes.mService != mIfaceImpl->getTokenService()) + { + // std::cerr << "(EE) Incorrect service. Dropping." << std::endl; + + return; + } + + if (!changes.mMsgs.empty() || !changes.mMsgsMeta.empty()) + { + emit msgsChanged(changes.mMsgs, changes.mMsgsMeta); + } + + if (!changes.mGrps.empty() || !changes.mGrpsMeta.empty()) + { + emit grpsChanged(changes.mGrps, changes.mGrpsMeta); + } + + emit changed(); +} From 6139632378b59091ef67ec4438b748d718f0c77d Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 18 Jun 2018 22:37:31 +0200 Subject: [PATCH 153/161] changed back RsGxsUpdateBroadCast.cpp to dos line ending --- .../src/util/RsGxsUpdateBroadcast.cpp | 166 +++++++++--------- 1 file changed, 83 insertions(+), 83 deletions(-) diff --git a/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp b/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp index 48c13d6db..0fdf63d65 100644 --- a/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp +++ b/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp @@ -1,83 +1,83 @@ -#include - -#include "RsGxsUpdateBroadcast.h" -#include "gui/notifyqt.h" - -#include - -//#define DEBUG_GXS_BROADCAST 1 - -// previously gxs allowed only one event consumer to poll for changes -// this required a single broadcast instance per service -// now the update notify works through rsnotify and notifyqt -// so the single instance per service is not really needed anymore - -QMap updateBroadcastMap; - -RsGxsUpdateBroadcast::RsGxsUpdateBroadcast(RsGxsIfaceHelper *ifaceImpl) : - QObject(NULL), mIfaceImpl(ifaceImpl) -{ - connect(NotifyQt::getInstance(), SIGNAL(gxsChange(RsGxsChanges)), this, SLOT(onChangesReceived(RsGxsChanges))); -} - -void RsGxsUpdateBroadcast::cleanup() -{ - QMap::iterator it; - for (it = updateBroadcastMap.begin(); it != updateBroadcastMap.end(); ++it) { - delete(it.value()); - } - - updateBroadcastMap.clear(); -} - -RsGxsUpdateBroadcast *RsGxsUpdateBroadcast::get(RsGxsIfaceHelper *ifaceImpl) -{ - QMap::iterator it = updateBroadcastMap.find(ifaceImpl); - if (it != updateBroadcastMap.end()) { - return it.value(); - } - - RsGxsUpdateBroadcast *updateBroadcast = new RsGxsUpdateBroadcast(ifaceImpl); - updateBroadcastMap.insert(ifaceImpl, updateBroadcast); - - return updateBroadcast; -} - -void RsGxsUpdateBroadcast::onChangesReceived(const RsGxsChanges& changes) -{ -#ifdef DEBUG_GXS_BROADCAST - std::cerr << "onChangesReceived()" << std::endl; - - { - std::cerr << "Received changes for service " << (void*)changes.mService << ", expecting service " << (void*)mIfaceImpl->getTokenService() << std::endl; - for(std::list::const_iterator it(changes.mGrps.begin());it!=changes.mGrps.end();++it) - std::cerr << "[GRP CHANGE] grp id: " << *it << std::endl; - for(std::list::const_iterator it(changes.mGrpsMeta.begin());it!=changes.mGrpsMeta.end();++it) - std::cerr << "[GRP CHANGE] grp meta: " << *it << std::endl; - for(std::map >::const_iterator it(changes.mMsgs.begin());it!=changes.mMsgs.end();++it) - for(uint32_t i=0;isecond.size();++i) - std::cerr << "[MSG CHANGE] grp id: " << it->first << ". Msg ID " << it->second[i] << std::endl; - for(std::map >::const_iterator it(changes.mMsgsMeta.begin());it!=changes.mMsgsMeta.end();++it) - for(uint32_t i=0;isecond.size();++i) - std::cerr << "[MSG CHANGE] grp id: " << it->first << ". Msg Meta " << it->second[i] << std::endl; - } -#endif - if(changes.mService != mIfaceImpl->getTokenService()) - { - // std::cerr << "(EE) Incorrect service. Dropping." << std::endl; - - return; - } - - if (!changes.mMsgs.empty() || !changes.mMsgsMeta.empty()) - { - emit msgsChanged(changes.mMsgs, changes.mMsgsMeta); - } - - if (!changes.mGrps.empty() || !changes.mGrpsMeta.empty()) - { - emit grpsChanged(changes.mGrps, changes.mGrpsMeta); - } - - emit changed(); -} +#include + +#include "RsGxsUpdateBroadcast.h" +#include "gui/notifyqt.h" + +#include + +//#define DEBUG_GXS_BROADCAST 1 + +// previously gxs allowed only one event consumer to poll for changes +// this required a single broadcast instance per service +// now the update notify works through rsnotify and notifyqt +// so the single instance per service is not really needed anymore + +QMap updateBroadcastMap; + +RsGxsUpdateBroadcast::RsGxsUpdateBroadcast(RsGxsIfaceHelper *ifaceImpl) : + QObject(NULL), mIfaceImpl(ifaceImpl) +{ + connect(NotifyQt::getInstance(), SIGNAL(gxsChange(RsGxsChanges)), this, SLOT(onChangesReceived(RsGxsChanges))); +} + +void RsGxsUpdateBroadcast::cleanup() +{ + QMap::iterator it; + for (it = updateBroadcastMap.begin(); it != updateBroadcastMap.end(); ++it) { + delete(it.value()); + } + + updateBroadcastMap.clear(); +} + +RsGxsUpdateBroadcast *RsGxsUpdateBroadcast::get(RsGxsIfaceHelper *ifaceImpl) +{ + QMap::iterator it = updateBroadcastMap.find(ifaceImpl); + if (it != updateBroadcastMap.end()) { + return it.value(); + } + + RsGxsUpdateBroadcast *updateBroadcast = new RsGxsUpdateBroadcast(ifaceImpl); + updateBroadcastMap.insert(ifaceImpl, updateBroadcast); + + return updateBroadcast; +} + +void RsGxsUpdateBroadcast::onChangesReceived(const RsGxsChanges& changes) +{ +#ifdef DEBUG_GXS_BROADCAST + std::cerr << "onChangesReceived()" << std::endl; + + { + std::cerr << "Received changes for service " << (void*)changes.mService << ", expecting service " << (void*)mIfaceImpl->getTokenService() << std::endl; + for(std::list::const_iterator it(changes.mGrps.begin());it!=changes.mGrps.end();++it) + std::cerr << "[GRP CHANGE] grp id: " << *it << std::endl; + for(std::list::const_iterator it(changes.mGrpsMeta.begin());it!=changes.mGrpsMeta.end();++it) + std::cerr << "[GRP CHANGE] grp meta: " << *it << std::endl; + for(std::map >::const_iterator it(changes.mMsgs.begin());it!=changes.mMsgs.end();++it) + for(uint32_t i=0;isecond.size();++i) + std::cerr << "[MSG CHANGE] grp id: " << it->first << ". Msg ID " << it->second[i] << std::endl; + for(std::map >::const_iterator it(changes.mMsgsMeta.begin());it!=changes.mMsgsMeta.end();++it) + for(uint32_t i=0;isecond.size();++i) + std::cerr << "[MSG CHANGE] grp id: " << it->first << ". Msg Meta " << it->second[i] << std::endl; + } +#endif + if(changes.mService != mIfaceImpl->getTokenService()) + { + // std::cerr << "(EE) Incorrect service. Dropping." << std::endl; + + return; + } + + if (!changes.mMsgs.empty() || !changes.mMsgsMeta.empty()) + { + emit msgsChanged(changes.mMsgs, changes.mMsgsMeta); + } + + if (!changes.mGrps.empty() || !changes.mGrpsMeta.empty()) + { + emit grpsChanged(changes.mGrps, changes.mGrpsMeta); + } + + emit changed(); +} From 84699db744abcf67842aeface3880fdfaffe00b6 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 21 Jun 2018 15:46:59 +0200 Subject: [PATCH 154/161] changed std::vector into std::set in notification system, hence removing lots of std::find(std::vector::begin(),std::vector::end(),T), causing quadratic costs in multiple places. --- libretroshare/src/gxs/rsdataservice.cc | 18 ++--- libretroshare/src/gxs/rsdataservice.h | 2 +- libretroshare/src/gxs/rsgds.h | 2 +- libretroshare/src/gxs/rsgenexchange.cc | 53 ++++++------ libretroshare/src/gxs/rsgxsdataaccess.cc | 28 +++---- libretroshare/src/gxs/rsgxsnetservice.cc | 8 +- libretroshare/src/gxs/rsgxsutil.cc | 14 ++-- libretroshare/src/gxs/rsgxsutil.h | 4 +- libretroshare/src/gxstrans/p3gxstrans.cc | 2 +- libretroshare/src/retroshare/rsgxsiface.h | 4 +- .../src/retroshare/rsgxsifacetypes.h | 6 +- libretroshare/src/retroshare/rsgxsservice.h | 2 +- libretroshare/src/services/p3gxschannels.cc | 20 ++--- libretroshare/src/services/p3gxscircles.cc | 6 +- libretroshare/src/services/p3gxscommon.cc | 4 +- libretroshare/src/services/p3gxsforums.cc | 11 +-- libretroshare/src/services/p3idservice.cc | 4 +- libretroshare/src/services/p3postbase.cc | 16 ++-- retroshare-gui/src/gui/Identity/IdDialog.cpp | 4 +- retroshare-gui/src/gui/NewsFeed.cpp | 4 +- .../src/gui/feeds/GxsForumMsgItem.cpp | 4 +- retroshare-gui/src/gui/gxs/GxsFeedItem.cpp | 16 ++-- .../src/gui/gxs/GxsGroupFeedItem.cpp | 5 +- .../src/gui/gxs/GxsGroupFrameDialog.cpp | 5 +- .../src/gui/gxs/GxsMessageFramePostWidget.cpp | 19 ++--- .../src/gui/gxs/GxsMessageFramePostWidget.h | 2 +- .../src/gui/gxs/RsGxsUpdateBroadcastBase.cpp | 81 +++++-------------- .../src/gui/gxs/RsGxsUpdateBroadcastBase.h | 22 ++--- .../src/gui/gxs/RsGxsUpdateBroadcastPage.cpp | 12 +-- .../src/gui/gxs/RsGxsUpdateBroadcastPage.h | 12 +-- .../gui/gxs/RsGxsUpdateBroadcastWidget.cpp | 12 +-- .../src/gui/gxs/RsGxsUpdateBroadcastWidget.h | 12 +-- .../gui/gxschannels/CreateGxsChannelMsg.cpp | 2 +- .../src/gui/gxsforums/CreateGxsForumMsg.cpp | 8 +- .../gui/gxsforums/GxsForumThreadWidget.cpp | 54 ++++++------- .../src/util/RsGxsUpdateBroadcast.h | 2 +- 36 files changed, 208 insertions(+), 272 deletions(-) diff --git a/libretroshare/src/gxs/rsdataservice.cc b/libretroshare/src/gxs/rsdataservice.cc index bebcee21d..0b4d0f1da 100644 --- a/libretroshare/src/gxs/rsdataservice.cc +++ b/libretroshare/src/gxs/rsdataservice.cc @@ -1217,7 +1217,7 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b const RsGxsGroupId& grpId = mit->first; // if vector empty then request all messages - const std::vector& msgIdV = mit->second; + const std::set& msgIdV = mit->second; std::vector msgSet; if(msgIdV.empty()){ @@ -1235,7 +1235,7 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b }else{ // request each grp - std::vector::const_iterator sit = msgIdV.begin(); + std::set::const_iterator sit = msgIdV.begin(); for(; sit!=msgIdV.end();++sit){ const RsGxsMessageId& msgId = *sit; @@ -1305,7 +1305,7 @@ int RsDataService::retrieveGxsMsgMetaData(const GxsMsgReq& reqIds, GxsMsgMetaRes const RsGxsGroupId& grpId = mit->first; // if vector empty then request all messages - const std::vector& msgIdV = mit->second; + const std::set& msgIdV = mit->second; std::vector metaSet; if(msgIdV.empty()){ @@ -1321,7 +1321,7 @@ int RsDataService::retrieveGxsMsgMetaData(const GxsMsgReq& reqIds, GxsMsgMetaRes }else{ // request each grp - std::vector::const_iterator sit = msgIdV.begin(); + std::set::const_iterator sit = msgIdV.begin(); for(; sit!=msgIdV.end(); ++sit){ const RsGxsMessageId& msgId = *sit; @@ -1561,7 +1561,7 @@ int RsDataService::removeMsgs(const GxsMsgReq& msgIds) for(; mit != msgIds.end(); ++mit) { - const std::vector& msgIdV = mit->second; + const std::set& msgIdV = mit->second; const RsGxsGroupId& grpId = mit->first; // delete messages @@ -1622,7 +1622,7 @@ int RsDataService::retrieveGroupIds(std::vector &grpIds) return 1; } -int RsDataService::retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_vector& msgIds) +int RsDataService::retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_set& msgIds) { #ifdef RS_DATA_SERVICE_DEBUG_TIME rstime::RsScopeTimer timer(""); @@ -1643,7 +1643,7 @@ int RsDataService::retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std if(c->columnCount() != 1) std::cerr << "(EE) ********* not retrieving all columns!!" << std::endl; - msgIds.push_back(RsGxsMessageId(msgId)); + msgIds.insert(RsGxsMessageId(msgId)); valid = c->moveToNext(); #ifdef RS_DATA_SERVICE_DEBUG_TIME @@ -1674,8 +1674,8 @@ bool RsDataService::locked_removeMessageEntries(const GxsMsgReq& msgIds) for(; mit != msgIds.end(); ++mit) { const RsGxsGroupId& grpId = mit->first; - const std::vector& msgsV = mit->second; - std::vector::const_iterator vit = msgsV.begin(); + const std::set& msgsV = mit->second; + std::set::const_iterator vit = msgsV.begin(); for(; vit != msgsV.end(); ++vit) { diff --git a/libretroshare/src/gxs/rsdataservice.h b/libretroshare/src/gxs/rsdataservice.h index c669ee016..cc137b320 100644 --- a/libretroshare/src/gxs/rsdataservice.h +++ b/libretroshare/src/gxs/rsdataservice.h @@ -110,7 +110,7 @@ public: * @param msgId msgsids retrieved * @return error code */ - int retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_vector& msgId); + int retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_set& msgId); /*! * @return the cache size set for this RsGeneralDataService in bytes diff --git a/libretroshare/src/gxs/rsgds.h b/libretroshare/src/gxs/rsgds.h index e20d0ed04..78987ac66 100644 --- a/libretroshare/src/gxs/rsgds.h +++ b/libretroshare/src/gxs/rsgds.h @@ -208,7 +208,7 @@ public: * @param msgId msgsids retrieved * @return error code */ - virtual int retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_vector& msgId) = 0; + virtual int retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_set& msgId) = 0; /*! * @return the cache size set for this RsGeneralDataService in bytes diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index e219821f2..ad7e02dcb 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -217,7 +217,7 @@ void RsGenExchange::tick() RS_STACK_MUTEX(mGenMtx) ; std::list grpIds; - std::map > msgIds; + std::map > msgIds; mIntegrityCheck->getDeletedIds(grpIds, msgIds); if (!grpIds.empty()) @@ -1078,23 +1078,19 @@ bool RsGenExchange::checkAuthenFlag(const PrivacyBitPos& pos, const uint8_t& fla } } -static void addMessageChanged(std::map > &msgs, const std::map > &msgChanged) +static void addMessageChanged(std::map > &msgs, const std::map > &msgChanged) { if (msgs.empty()) { msgs = msgChanged; } else { - std::map >::const_iterator mapIt; - for (mapIt = msgChanged.begin(); mapIt != msgChanged.end(); ++mapIt) { + for (auto mapIt = msgChanged.begin(); mapIt != msgChanged.end(); ++mapIt) + { const RsGxsGroupId &grpId = mapIt->first; - const std::vector &srcMsgIds = mapIt->second; - std::vector &destMsgIds = msgs[grpId]; + const std::set &srcMsgIds = mapIt->second; + std::set &destMsgIds = msgs[grpId]; - std::vector::const_iterator msgIt; - for (msgIt = srcMsgIds.begin(); msgIt != srcMsgIds.end(); ++msgIt) { - if (std::find(destMsgIds.begin(), destMsgIds.end(), *msgIt) == destMsgIds.end()) { - destMsgIds.push_back(*msgIt); - } - } + for (auto msgIt = srcMsgIds.begin(); msgIt != srcMsgIds.end(); ++msgIt) + destMsgIds.insert(*msgIt) ; } } } @@ -1789,8 +1785,8 @@ void RsGenExchange::deleteMsgs(uint32_t& token, const GxsMsgReq& msgs) if(mNetService != NULL) for(GxsMsgReq::const_iterator it(msgs.begin());it!=msgs.end();++it) - for(uint32_t i=0;isecond.size();++i) - mNetService->rejectMessage(it->second[i]) ; + for(auto it2(it->second.begin());it2!=it->second.end();++it2) + mNetService->rejectMessage(*it2); } void RsGenExchange::publishMsg(uint32_t& token, RsGxsMsgItem *msgItem) @@ -1961,8 +1957,8 @@ void RsGenExchange::processMsgMetaChanges() if(m.val.getAsInt32(RsGeneralDataService::MSG_META_STATUS+GXS_MASK, mask)) { GxsMsgReq req; - std::vector msgIdV; - msgIdV.push_back(m.msgId.second); + std::set msgIdV; + msgIdV.insert(m.msgId.second); req.insert(std::make_pair(m.msgId.first, msgIdV)); GxsMsgMetaResult result; mDataStore->retrieveGxsMsgMetaData(req, result); @@ -1994,7 +1990,7 @@ void RsGenExchange::processMsgMetaChanges() mDataAccess->updatePublicRequestStatus(token, RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE); if (changed) { - msgIds[m.msgId.first].push_back(m.msgId.second); + msgIds[m.msgId.first].insert(m.msgId.second); } } else @@ -2145,7 +2141,7 @@ void RsGenExchange::publishMsgs() mMsgsToPublish.insert(std::make_pair(sign_it->first, item.mItem)); } - std::map > msgChangeMap; + std::map > msgChangeMap; std::map::iterator mit = mMsgsToPublish.begin(); for(; mit != mMsgsToPublish.end(); ++mit) @@ -2273,7 +2269,7 @@ void RsGenExchange::publishMsgs() mDataAccess->addMsgData(msg); delete msg ; - msgChangeMap[grpId].push_back(msgId); + msgChangeMap[grpId].insert(msgId); delete[] metaDataBuff; @@ -2972,10 +2968,10 @@ void RsGenExchange::processRecvdMessages() msg->metaData->mMsgStatus = GXS_SERV::GXS_MSG_STATUS_UNPROCESSED | GXS_SERV::GXS_MSG_STATUS_GUI_NEW | GXS_SERV::GXS_MSG_STATUS_GUI_UNREAD; msgs_to_store.push_back(msg); - std::vector &msgv = msgIds[msg->grpId]; - - if (std::find(msgv.begin(), msgv.end(), msg->msgId) == msgv.end()) - msgv.push_back(msg->msgId); + msgIds[msg->grpId].insert(msg->msgId); + // std::vector &msgv = msgIds[msg->grpId]; + // if (std::find(msgv.begin(), msgv.end(), msg->msgId) == msgv.end()) + // msgv.push_back(msg->msgId); computeHash(msg->msg, msg->metaData->mHash); msg->metaData->recvTS = time(NULL); @@ -3330,7 +3326,7 @@ void RsGenExchange::removeDeleteExistingMessages( std::list& msgs, Gx //RsGxsGroupId::std_list grpIds(mGrpIdsUnique.begin(), mGrpIdsUnique.end()); //RsGxsGroupId::std_list::const_iterator it = grpIds.begin(); - typedef std::map MsgIdReq; + typedef std::map MsgIdReq; MsgIdReq msgIdReq; // now get a list of all msgs ids for each group @@ -3350,7 +3346,7 @@ void RsGenExchange::removeDeleteExistingMessages( std::list& msgs, Gx // now for each msg to be stored that exist in the retrieved msg/grp "index" delete and erase from map for(std::list::iterator cit2 = msgs.begin(); cit2 != msgs.end();) { - const RsGxsMessageId::std_vector& msgIds = msgIdReq[(*cit2)->metaData->mGroupId]; + const RsGxsMessageId::std_set& msgIds = msgIdReq[(*cit2)->metaData->mGroupId]; #ifdef GEN_EXCH_DEBUG std::cerr << " grpid=" << (*cit2)->grpId << ", msgid=" << (*cit2)->msgId ; @@ -3358,12 +3354,13 @@ void RsGenExchange::removeDeleteExistingMessages( std::list& msgs, Gx // Avoid storing messages that are already in the database, as well as messages that are too old (or generally do not pass the database storage test) // - if(std::find(msgIds.begin(), msgIds.end(), (*cit2)->metaData->mMsgId) != msgIds.end() || !messagePublicationTest( *(*cit2)->metaData)) + if(msgIds.find((*cit2)->metaData->mMsgId) != msgIds.end() || !messagePublicationTest( *(*cit2)->metaData)) { // msg exist in retrieved index. We should use a std::set here instead of a vector. - RsGxsMessageId::std_vector& notifyIds = msgIdsNotify[ (*cit2)->metaData->mGroupId]; - RsGxsMessageId::std_vector::iterator it2 = std::find(notifyIds.begin(), notifyIds.end(), (*cit2)->metaData->mMsgId); + RsGxsMessageId::std_set& notifyIds = msgIdsNotify[ (*cit2)->metaData->mGroupId]; + RsGxsMessageId::std_set::iterator it2 = notifyIds.find((*cit2)->metaData->mMsgId); + if(it2 != notifyIds.end()) { notifyIds.erase(it2); diff --git a/libretroshare/src/gxs/rsgxsdataaccess.cc b/libretroshare/src/gxs/rsgxsdataaccess.cc index 5d36bd909..77c0d05d9 100644 --- a/libretroshare/src/gxs/rsgxsdataaccess.cc +++ b/libretroshare/src/gxs/rsgxsdataaccess.cc @@ -225,7 +225,7 @@ bool RsGxsDataAccess::requestMsgInfo(uint32_t &token, uint32_t ansType, MsgMetaReq* mmr = new MsgMetaReq(); for(; lit != grpIds.end(); ++lit) - mmr->mMsgIds[*lit] = std::vector(); + mmr->mMsgIds[*lit] = std::set(); req = mmr; }else if(reqType & GXS_REQUEST_TYPE_MSG_DATA) @@ -233,7 +233,7 @@ bool RsGxsDataAccess::requestMsgInfo(uint32_t &token, uint32_t ansType, MsgDataReq* mdr = new MsgDataReq(); for(; lit != grpIds.end(); ++lit) - mdr->mMsgIds[*lit] = std::vector(); + mdr->mMsgIds[*lit] = std::set(); req = mdr; }else if(reqType & GXS_REQUEST_TYPE_MSG_IDS) @@ -241,7 +241,7 @@ bool RsGxsDataAccess::requestMsgInfo(uint32_t &token, uint32_t ansType, MsgIdReq* mir = new MsgIdReq(); for(; lit != grpIds.end(); ++lit) - mir->mMsgIds[*lit] = std::vector(); + mir->mMsgIds[*lit] = std::set(); req = mir; } @@ -1191,7 +1191,7 @@ bool RsGxsDataAccess::getMsgList(const GxsMsgReq& msgIds, const RsTokReqOptions& // Add the discovered Latest Msgs. for(oit = origMsgTs.begin(); oit != origMsgTs.end(); ++oit) { - msgIdsOut[grpId].push_back(oit->second.first); + msgIdsOut[grpId].insert(oit->second.first); } } @@ -1228,7 +1228,7 @@ bool RsGxsDataAccess::getMsgList(const GxsMsgReq& msgIds, const RsTokReqOptions& if (add) { - msgIdsOut[grpId].push_back(msgMeta->mMsgId); + msgIdsOut[grpId].insert(msgMeta->mMsgId); metaFilter[grpId].insert(std::make_pair(msgMeta->mMsgId, msgMeta)); } @@ -1373,7 +1373,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) // get meta data for all in group GxsMsgMetaResult result; GxsMsgReq msgIds; - msgIds.insert(std::make_pair(grpMsgIdPair.first, std::vector())); + msgIds.insert(std::make_pair(grpMsgIdPair.first, std::set())); mDataStore->retrieveGxsMsgMetaData(msgIds, result); std::vector& metaV = result[grpMsgIdPair.first]; std::vector::iterator vit_meta; @@ -1382,7 +1382,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) const RsGxsMessageId& msgId = grpMsgIdPair.second; const RsGxsGroupId& grpId = grpMsgIdPair.first; - std::vector outMsgIds; + std::set outMsgIds; RsGxsMsgMetaData* origMeta = NULL; for(vit_meta = metaV.begin(); vit_meta != metaV.end(); ++vit_meta) @@ -1477,7 +1477,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) // Add the discovered Latest Msgs. for(oit = origMsgTs.begin(); oit != origMsgTs.end(); ++oit) { - outMsgIds.push_back(oit->second.first); + outMsgIds.insert(oit->second.first); } } else @@ -1502,7 +1502,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) } } } - outMsgIds.push_back(latestMsgId); + outMsgIds.insert(latestMsgId); metaMap.insert(std::make_pair(latestMsgId, latestMeta)); } } @@ -1514,7 +1514,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) if (meta->mOrigMsgId == origMsgId) { - outMsgIds.push_back(meta->mMsgId); + outMsgIds.insert(meta->mMsgId); metaMap.insert(std::make_pair(meta->mMsgId, meta)); } } @@ -1556,7 +1556,7 @@ bool RsGxsDataAccess::getGroupStatistic(GroupStatisticRequest *req) { // filter based on options GxsMsgIdResult metaReq; - metaReq[req->mGrpId] = std::vector(); + metaReq[req->mGrpId] = std::set(); GxsMsgMetaResult metaResult; mDataStore->retrieveGxsMsgMetaData(metaReq, metaResult); @@ -1672,7 +1672,7 @@ bool RsGxsDataAccess::getMsgList(MsgIdReq* req) for(; vit != vit_end; ++vit) { RsGxsMsgMetaData* meta = *vit; - req->mMsgIdResult[grpId].push_back(meta->mMsgId); + req->mMsgIdResult[grpId].insert(meta->mMsgId); delete meta; // discard meta data mem } } @@ -1718,8 +1718,8 @@ void RsGxsDataAccess::filterMsgList(GxsMsgIdResult& msgIds, const RsTokReqOption if(cit == msgMetas.end()) continue; - std::vector& msgs = mit->second; - std::vector::iterator vit = msgs.begin(); + std::set& msgs = mit->second; + std::set::iterator vit = msgs.begin(); const std::map& meta = cit->second; std::map::const_iterator cit2; diff --git a/libretroshare/src/gxs/rsgxsnetservice.cc b/libretroshare/src/gxs/rsgxsnetservice.cc index 48a9e9234..0f61c5a74 100644 --- a/libretroshare/src/gxs/rsgxsnetservice.cc +++ b/libretroshare/src/gxs/rsgxsnetservice.cc @@ -784,7 +784,7 @@ void RsGxsNetService::handleRecvSyncGrpStatistics(RsNxsSyncGrpStatsItem *grs) // now count available messages GxsMsgReq reqIds; - reqIds[grs->grpId] = std::vector(); + reqIds[grs->grpId] = std::set(); GxsMsgMetaResult result; #ifdef NXS_NET_DEBUG_6 @@ -2757,7 +2757,7 @@ void RsGxsNetService::locked_genReqMsgTransaction(NxsTransaction* tr) #endif GxsMsgReq reqIds; - reqIds[grpId] = std::vector(); + reqIds[grpId] = std::set(); GxsMsgMetaResult result; mDataStore->retrieveGxsMsgMetaData(reqIds, result); std::vector &msgMetaV = result[grpId]; @@ -3296,7 +3296,7 @@ void RsGxsNetService::locked_genSendMsgsTransaction(NxsTransaction* tr) RsNxsSyncMsgItem* item = dynamic_cast(*lit); if (item) { - msgIds[item->grpId].push_back(item->msgId); + msgIds[item->grpId].insert(item->msgId); if(grpId.isNull()) grpId = item->grpId; @@ -4127,7 +4127,7 @@ void RsGxsNetService::handleRecvSyncMessage(RsNxsSyncMsgReqItem *item,bool item_ } GxsMsgReq req; - req[item->grpId] = std::vector(); + req[item->grpId] = std::set(); GxsMsgMetaResult metaResult; mDataStore->retrieveGxsMsgMetaData(req, metaResult); diff --git a/libretroshare/src/gxs/rsgxsutil.cc b/libretroshare/src/gxs/rsgxsutil.cc index abf02aef2..bb0b1fb05 100644 --- a/libretroshare/src/gxs/rsgxsutil.cc +++ b/libretroshare/src/gxs/rsgxsutil.cc @@ -67,7 +67,7 @@ bool RsGxsMessageCleanUp::clean() GxsMsgReq req; GxsMsgMetaResult result; - req[grpId] = std::vector(); + req[grpId] = std::set(); mDs->retrieveGxsMsgMetaData(req, result); GxsMsgMetaResult::iterator mit = result.begin(); @@ -117,7 +117,7 @@ bool RsGxsMessageCleanUp::clean() if( remove ) { - req[grpId].push_back(meta->mMsgId); + req[grpId].insert(meta->mMsgId); #ifdef DEBUG_GXSUTIL std::cerr << " Scheduling for removal." << std::endl; @@ -241,9 +241,9 @@ bool RsGxsIntegrityCheck::check() for (msgIdsIt = msgIds.begin(); msgIdsIt != msgIds.end(); ++msgIdsIt) { const RsGxsGroupId& grpId = msgIdsIt->first; - std::vector &msgIdV = msgIdsIt->second; + std::set &msgIdV = msgIdsIt->second; - std::vector::iterator msgIdIt; + std::set::iterator msgIdIt; for (msgIdIt = msgIdV.begin(); msgIdIt != msgIdV.end(); ++msgIdIt) { const RsGxsMessageId& msgId = *msgIdIt; @@ -261,7 +261,7 @@ bool RsGxsIntegrityCheck::check() if (nxsMsgIt == nxsMsgV.end()) { - msgsToDel[grpId].push_back(msgId); + msgsToDel[grpId].insert(msgId); } } } @@ -284,7 +284,7 @@ bool RsGxsIntegrityCheck::check() if(msg->metaData == NULL || currHash != msg->metaData->mHash) { std::cerr << "(EE) deleting message data with wrong hash or null meta data. meta=" << (void*)msg->metaData << std::endl; - msgsToDel[msg->grpId].push_back(msg->msgId); + msgsToDel[msg->grpId].insert(msg->msgId); } else if(!msg->metaData->mAuthorId.isNull() && subscribed_groups.find(msg->metaData->mGroupId)!=subscribed_groups.end()) { @@ -377,7 +377,7 @@ bool RsGxsIntegrityCheck::isDone() return mDone; } -void RsGxsIntegrityCheck::getDeletedIds(std::list& grpIds, std::map >& msgIds) +void RsGxsIntegrityCheck::getDeletedIds(std::list& grpIds, std::map >& msgIds) { RsStackMutex stack(mIntegrityMutex); diff --git a/libretroshare/src/gxs/rsgxsutil.h b/libretroshare/src/gxs/rsgxsutil.h index b169e00b5..70e832c3e 100644 --- a/libretroshare/src/gxs/rsgxsutil.h +++ b/libretroshare/src/gxs/rsgxsutil.h @@ -208,7 +208,7 @@ public: void run(); - void getDeletedIds(std::list& grpIds, std::map >& msgIds); + void getDeletedIds(std::list& grpIds, std::map > &msgIds); private: @@ -217,7 +217,7 @@ private: bool mDone; RsMutex mIntegrityMutex; std::list mDeletedGrps; - std::map > mDeletedMsgs; + std::map > mDeletedMsgs; RsGixs *mGixs ; }; diff --git a/libretroshare/src/gxstrans/p3gxstrans.cc b/libretroshare/src/gxstrans/p3gxstrans.cc index d0dbb61b0..5b3834ea8 100644 --- a/libretroshare/src/gxstrans/p3gxstrans.cc +++ b/libretroshare/src/gxstrans/p3gxstrans.cc @@ -455,7 +455,7 @@ void p3GxsTrans::GxsTransIntegrityCleanupThread::run() if(stored_msgs.end() != it2) { - msgsToDel[it2->second.first].push_back(it2->second.second); + msgsToDel[it2->second.first].insert(it2->second.second); #ifdef DEBUG_GXSTRANS std::cerr << " scheduling msg " << std::hex << it2->second.first << "," << it2->second.second << " for deletion." << std::endl; diff --git a/libretroshare/src/retroshare/rsgxsiface.h b/libretroshare/src/retroshare/rsgxsiface.h index 3e2ae55ae..36c660ef8 100644 --- a/libretroshare/src/retroshare/rsgxsiface.h +++ b/libretroshare/src/retroshare/rsgxsiface.h @@ -40,8 +40,8 @@ class RsGxsChanges public: RsGxsChanges(): mService(0){} RsTokenService *mService; - std::map > mMsgs; - std::map > mMsgsMeta; + std::map > mMsgs; + std::map > mMsgsMeta; std::list mGrps; std::list mGrpsMeta; }; diff --git a/libretroshare/src/retroshare/rsgxsifacetypes.h b/libretroshare/src/retroshare/rsgxsifacetypes.h index 18637f1b1..071b6bd46 100644 --- a/libretroshare/src/retroshare/rsgxsifacetypes.h +++ b/libretroshare/src/retroshare/rsgxsifacetypes.h @@ -43,10 +43,10 @@ typedef Sha1CheckSum RsGxsMessageId; typedef GXSId RsGxsId; typedef GXSCircleId RsGxsCircleId; -typedef std::map > GxsMsgIdResult; +typedef std::map > GxsMsgIdResult; typedef std::pair RsGxsGrpMsgIdPair; -typedef std::map > MsgRelatedIdResult; -typedef std::map > GxsMsgReq; +typedef std::map > MsgRelatedIdResult; +typedef std::map > GxsMsgReq; struct RsMsgMetaData; diff --git a/libretroshare/src/retroshare/rsgxsservice.h b/libretroshare/src/retroshare/rsgxsservice.h index 6afed31c6..b8b98940a 100644 --- a/libretroshare/src/retroshare/rsgxsservice.h +++ b/libretroshare/src/retroshare/rsgxsservice.h @@ -46,7 +46,7 @@ class RsGxsMsgChange : public RsGxsNotify { public: RsGxsMsgChange(NotifyType type, bool metaChange) : NOTIFY_TYPE(type), mMetaChange(metaChange) {} - std::map > msgChangeMap; + std::map > msgChangeMap; NotifyType getType(){ return NOTIFY_TYPE;} bool metaChange() { return mMetaChange; } private: diff --git a/libretroshare/src/services/p3gxschannels.cc b/libretroshare/src/services/p3gxschannels.cc index beb828fdf..dd284a4f1 100644 --- a/libretroshare/src/services/p3gxschannels.cc +++ b/libretroshare/src/services/p3gxschannels.cc @@ -241,16 +241,12 @@ void p3GxsChannels::notifyChanges(std::vector &changes) /* message received */ if (notify) { - std::map > &msgChangeMap = msgChange->msgChangeMap; - std::map >::iterator mit; - for (mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) - { - std::vector::iterator mit1; - for (mit1 = mit->second.begin(); mit1 != mit->second.end(); ++mit1) + std::map > &msgChangeMap = msgChange->msgChangeMap; + for (auto mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) + for (auto mit1 = mit->second.begin(); mit1 != mit->second.end(); ++mit1) { notify->AddFeedItem(RS_FEED_ITEM_CHANNEL_MSG, mit->first.toStdString(), mit1->toStdString()); } - } } } @@ -261,9 +257,8 @@ void p3GxsChannels::notifyChanges(std::vector &changes) std::cerr << std::endl; #endif - std::map > &msgChangeMap = msgChange->msgChangeMap; - std::map >::iterator mit; - for(mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) + std::map > &msgChangeMap = msgChange->msgChangeMap; + for(auto mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) { #ifdef GXSCHANNELS_DEBUG std::cerr << "p3GxsChannels::notifyChanges() Msgs for Group: " << mit->first; @@ -807,10 +802,7 @@ void p3GxsChannels::request_SpecificUnprocessedPosts(std::list >::iterator it; for(it = ids.begin(); it != ids.end(); ++it) - { - std::vector &vect_msgIds = msgIds[it->first]; - vect_msgIds.push_back(it->second); - } + msgIds[it->first].insert(it->second); RsGenExchange::getTokenService()->requestMsgInfo(token, ansType, opts, msgIds); GxsTokenQueue::queueRequest(token, GXSCHANNELS_UNPROCESSED_SPECIFIC); diff --git a/libretroshare/src/services/p3gxscircles.cc b/libretroshare/src/services/p3gxscircles.cc index 5e27e378d..063361121 100644 --- a/libretroshare/src/services/p3gxscircles.cc +++ b/libretroshare/src/services/p3gxscircles.cc @@ -212,14 +212,14 @@ void p3GxsCircles::notifyChanges(std::vector &changes) #ifdef DEBUG_CIRCLES std::cerr << " Found circle Message Change Notification" << std::endl; #endif - for(std::map >::iterator mit = msgChange->msgChangeMap.begin(); mit != msgChange->msgChangeMap.end(); ++mit) + for(auto mit = msgChange->msgChangeMap.begin(); mit != msgChange->msgChangeMap.end(); ++mit) { #ifdef DEBUG_CIRCLES std::cerr << " Msgs for Group: " << mit->first << std::endl; #endif force_cache_reload(RsGxsCircleId(mit->first)); if (notify && (c->getType() == RsGxsNotify::TYPE_RECEIVE) ) - for (std::vector::const_iterator msgIdIt(mit->second.begin()), end(mit->second.end()); msgIdIt != end; ++msgIdIt) + for (auto msgIdIt(mit->second.begin()), end(mit->second.end()); msgIdIt != end; ++msgIdIt) { const RsGxsMessageId& msgId = *msgIdIt; notify->AddFeedItem(RS_FEED_ITEM_CIRCLE_MEMB_REQ,RsGxsCircleId(mit->first).toStdString(),msgId.toStdString()); @@ -2120,7 +2120,7 @@ bool p3GxsCircles::processMembershipRequests(uint32_t token) #ifdef DEBUG_CIRCLES std::cerr << " Older than last known (" << time(NULL)-info.last_subscription_TS << " seconds ago): deleting." << std::endl; #endif - messages_to_delete[RsGxsGroupId(cid)].push_back(it->second[i]->meta.mMsgId) ; + messages_to_delete[RsGxsGroupId(cid)].insert(it->second[i]->meta.mMsgId) ; } } diff --git a/libretroshare/src/services/p3gxscommon.cc b/libretroshare/src/services/p3gxscommon.cc index 5793e58ff..e1db6750d 100644 --- a/libretroshare/src/services/p3gxscommon.cc +++ b/libretroshare/src/services/p3gxscommon.cc @@ -502,8 +502,8 @@ bool p3GxsCommentService::createGxsVote(uint32_t &token, RsGxsVote &vote) opts.mReqType = GXS_REQUEST_TYPE_MSG_META; GxsMsgReq msgIds; - std::vector &vect_msgIds = msgIds[parentId.first]; - vect_msgIds.push_back(parentId.second); + std::set &vect_msgIds = msgIds[parentId.first]; + vect_msgIds.insert(parentId.second); uint32_t int_token; mExchange->getTokenService()->requestMsgInfo(int_token, RS_TOKREQ_ANSTYPE_SUMMARY, opts, msgIds); diff --git a/libretroshare/src/services/p3gxsforums.cc b/libretroshare/src/services/p3gxsforums.cc index 7f1e8dfcf..1be91dc00 100644 --- a/libretroshare/src/services/p3gxsforums.cc +++ b/libretroshare/src/services/p3gxsforums.cc @@ -205,15 +205,12 @@ void p3GxsForums::notifyChanges(std::vector &changes) RsGxsMsgChange *msgChange = dynamic_cast(c); if (msgChange) { - std::map > &msgChangeMap = msgChange->msgChangeMap; - std::map >::iterator mit; - for (mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) + std::map > &msgChangeMap = msgChange->msgChangeMap; + + for (auto mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) { - std::vector::iterator mit1; - for (mit1 = mit->second.begin(); mit1 != mit->second.end(); ++mit1) - { + for (auto mit1 = mit->second.begin(); mit1 != mit->second.end(); ++mit1) notify->AddFeedItem(RS_FEED_ITEM_FORUM_MSG, mit->first.toStdString(), mit1->toStdString()); - } } break; } diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index 5ea8f898e..8a3f08195 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -562,8 +562,8 @@ void p3IdService::notifyChanges(std::vector &changes) std::cerr << std::endl; #endif - std::map > &msgChangeMap = msgChange->msgChangeMap; - std::map >::iterator mit; + std::map > &msgChangeMap = msgChange->msgChangeMap; + std::map >::iterator mit; for(mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) { #ifdef DEBUG_IDS diff --git a/libretroshare/src/services/p3postbase.cc b/libretroshare/src/services/p3postbase.cc index 0c37db1f1..9d1df6bdc 100644 --- a/libretroshare/src/services/p3postbase.cc +++ b/libretroshare/src/services/p3postbase.cc @@ -110,9 +110,8 @@ void p3PostBase::notifyChanges(std::vector &changes) std::cerr << std::endl; #endif - std::map > &msgChangeMap = msgChange->msgChangeMap; - std::map >::iterator mit; - for(mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) + std::map > &msgChangeMap = msgChange->msgChangeMap; + for(auto mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) { #ifdef POSTBASE_DEBUG std::cerr << "p3PostBase::notifyChanges() Msgs for Group: " << mit->first; @@ -125,8 +124,7 @@ void p3PostBase::notifyChanges(std::vector &changes) if (notify && msgChange->getType() == RsGxsNotify::TYPE_RECEIVE) { - std::vector::iterator mit1; - for (mit1 = mit->second.begin(); mit1 != mit->second.end(); ++mit1) + for (auto mit1 = mit->second.begin(); mit1 != mit->second.end(); ++mit1) { notify->AddFeedItem(RS_FEED_ITEM_POSTED_MSG, mit->first.toStdString(), mit1->toStdString()); } @@ -431,7 +429,7 @@ void p3PostBase::background_loadMsgs(const uint32_t &token, bool unprocessed) mBgIncremental = unprocessed; } - std::map > postMap; + std::map > postMap; // generate vector of changes to push to the GUI. std::vector changes; @@ -487,7 +485,7 @@ void p3PostBase::background_loadMsgs(const uint32_t &token, bool unprocessed) #endif /* but we need to notify GUI about them */ - msgChanges->msgChangeMap[mit->first].push_back((*vit)->meta.mMsgId); + msgChanges->msgChangeMap[mit->first].insert((*vit)->meta.mMsgId); } else if (NULL != (commentItem = dynamic_cast(*vit))) { @@ -546,7 +544,7 @@ void p3PostBase::background_loadMsgs(const uint32_t &token, bool unprocessed) if (sit == mBgStatsMap.end()) { // add to map of ones to update. - postMap[groupId].push_back(threadId); + postMap[groupId].insert(threadId); mBgStatsMap[threadId] = PostStats(0,0,0); sit = mBgStatsMap.find(threadId); @@ -704,7 +702,7 @@ void p3PostBase::background_updateVoteCounts(const uint32_t &token) #endif stats.increment(it->second); - msgChanges->msgChangeMap[mit->first].push_back(vit->mMsgId); + msgChanges->msgChangeMap[mit->first].insert(vit->mMsgId); } else { diff --git a/retroshare-gui/src/gui/Identity/IdDialog.cpp b/retroshare-gui/src/gui/Identity/IdDialog.cpp index 17e62d3ca..7f644be5f 100644 --- a/retroshare-gui/src/gui/Identity/IdDialog.cpp +++ b/retroshare-gui/src/gui/Identity/IdDialog.cpp @@ -2132,12 +2132,12 @@ void IdDialog::updateDisplay(bool complete) } requestCircleGroupMeta(); - std::list grpIds; + std::set grpIds; getAllGrpIds(grpIds); if (!getGrpIds().empty()) { requestIdList(); - if (!mId.isNull() && std::find(grpIds.begin(), grpIds.end(), mId) != grpIds.end()) { + if (!mId.isNull() && grpIds.find(mId)!=grpIds.end()) { requestIdDetails(); requestRepList(); } diff --git a/retroshare-gui/src/gui/NewsFeed.cpp b/retroshare-gui/src/gui/NewsFeed.cpp index 45c8c1cb6..a9c599389 100644 --- a/retroshare-gui/src/gui/NewsFeed.cpp +++ b/retroshare-gui/src/gui/NewsFeed.cpp @@ -366,8 +366,8 @@ void NewsFeed::updateDisplay() opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; GxsMsgReq msgIds; - std::vector &vect_msgIds = msgIds[grpId]; - vect_msgIds.push_back(msgId); + std::set &vect_msgIds = msgIds[grpId]; + vect_msgIds.insert(msgId); uint32_t token; mTokenQueueCircle->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, TOKEN_TYPE_MESSAGE); diff --git a/retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp b/retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp index e9773aba9..b02afa336 100644 --- a/retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp +++ b/retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp @@ -438,8 +438,8 @@ void GxsForumMsgItem::requestParentMessage(const RsGxsMessageId &msgId) opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; GxsMsgReq msgIds; - std::vector &vect_msgIds = msgIds[groupId()]; - vect_msgIds.push_back(msgId); + std::set &vect_msgIds = msgIds[groupId()]; + vect_msgIds.insert(msgId); uint32_t token; mLoadQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeParentMessage); diff --git a/retroshare-gui/src/gui/gxs/GxsFeedItem.cpp b/retroshare-gui/src/gui/gxs/GxsFeedItem.cpp index f795768f6..2f036f900 100644 --- a/retroshare-gui/src/gui/gxs/GxsFeedItem.cpp +++ b/retroshare-gui/src/gui/gxs/GxsFeedItem.cpp @@ -93,19 +93,15 @@ void GxsFeedItem::fillDisplay(RsGxsUpdateBroadcastBase *updateBroadcastBase, boo { GxsGroupFeedItem::fillDisplay(updateBroadcastBase, complete); - std::map > msgs; + std::map > msgs; updateBroadcastBase->getAllMsgIds(msgs); if (!msgs.empty()) { - std::map >::const_iterator mit = msgs.find(groupId()); - if (mit != msgs.end()) - { - const std::vector &msgIds = mit->second; - if (std::find(msgIds.begin(), msgIds.end(), messageId()) != msgIds.end()) { + std::map >::const_iterator mit = msgs.find(groupId()); + + if (mit != msgs.end() && mit->second.find(messageId()) != mit->second.end()) requestMessage(); - } - } } } @@ -129,8 +125,8 @@ void GxsFeedItem::requestMessage() opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; GxsMsgReq msgIds; - std::vector &vect_msgIds = msgIds[groupId()]; - vect_msgIds.push_back(mMessageId); + std::set &vect_msgIds = msgIds[groupId()]; + vect_msgIds.insert(mMessageId); uint32_t token; mLoadQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeMessage); diff --git a/retroshare-gui/src/gui/gxs/GxsGroupFeedItem.cpp b/retroshare-gui/src/gui/gxs/GxsGroupFeedItem.cpp index 9682ab987..1b3d0990e 100644 --- a/retroshare-gui/src/gui/gxs/GxsGroupFeedItem.cpp +++ b/retroshare-gui/src/gui/gxs/GxsGroupFeedItem.cpp @@ -170,12 +170,11 @@ void GxsGroupFeedItem::fillDisplaySlot(bool complete) void GxsGroupFeedItem::fillDisplay(RsGxsUpdateBroadcastBase *updateBroadcastBase, bool /*complete*/) { - std::list grpIds; + std::set grpIds; updateBroadcastBase->getAllGrpIds(grpIds); - if (std::find(grpIds.begin(), grpIds.end(), groupId()) != grpIds.end()) { + if (grpIds.find(groupId()) != grpIds.end()) requestGroup(); - } } /***********************************************************/ diff --git a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp index 3bae96cfd..33c6a04e5 100644 --- a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp +++ b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp @@ -236,11 +236,10 @@ void GxsGroupFrameDialog::updateDisplay(bool complete) requestGroupSummary(); } else { /* Update all groups of changed messages */ - std::map > msgIds; + std::map > msgIds; getAllMsgIds(msgIds); - std::map >::iterator msgIt; - for (msgIt = msgIds.begin(); msgIt != msgIds.end(); ++msgIt) { + for (auto msgIt = msgIds.begin(); msgIt != msgIds.end(); ++msgIt) { updateMessageSummaryList(msgIt->first); } } diff --git a/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.cpp b/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.cpp index df5442bcd..f23dc83f2 100644 --- a/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.cpp +++ b/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.cpp @@ -109,21 +109,22 @@ void GxsMessageFramePostWidget::updateDisplay(bool complete) } bool updateGroup = false; - const std::list &grpIdsMeta = getGrpIdsMeta(); - if (std::find(grpIdsMeta.begin(), grpIdsMeta.end(), groupId()) != grpIdsMeta.end()) { - updateGroup = true; - } + const std::set &grpIdsMeta = getGrpIdsMeta(); - const std::list &grpIds = getGrpIds(); - if (!groupId().isNull() && std::find(grpIds.begin(), grpIds.end(), groupId()) != grpIds.end()) { + if(grpIdsMeta.find(groupId())!=grpIdsMeta.end()) + updateGroup = true; + + const std::set &grpIds = getGrpIds(); + if (!groupId().isNull() && grpIds.find(groupId())!=grpIds.end()) + { updateGroup = true; /* Do we need to fill all posts? */ requestAllPosts(); } else { - std::map > msgs; + std::map > msgs; getAllMsgIds(msgs); if (!msgs.empty()) { - std::map >::const_iterator mit = msgs.find(groupId()); + auto mit = msgs.find(groupId()); if (mit != msgs.end()) { requestPosts(mit->second); } @@ -341,7 +342,7 @@ void GxsMessageFramePostWidget::loadAllPosts(const uint32_t &token) emit groupChanged(this); } -void GxsMessageFramePostWidget::requestPosts(const std::vector &msgIds) +void GxsMessageFramePostWidget::requestPosts(const std::set &msgIds) { #ifdef ENABLE_DEBUG std::cerr << "GxsMessageFramePostWidget::requestPosts()"; diff --git a/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.h b/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.h index d01a0ca42..2a42cb772 100644 --- a/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.h +++ b/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.h @@ -72,7 +72,7 @@ protected: void loadAllPosts(const uint32_t &token); virtual void insertAllPosts(const uint32_t &token, GxsMessageFramePostThread *thread) = 0; - void requestPosts(const std::vector &msgIds); + void requestPosts(const std::set &msgIds); void loadPosts(const uint32_t &token); virtual void insertPosts(const uint32_t &token) = 0; diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp index a657959a9..8d4bc0fc3 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp @@ -15,7 +15,7 @@ RsGxsUpdateBroadcastBase::RsGxsUpdateBroadcastBase(RsGxsIfaceHelper *ifaceImpl, mUpdateBroadcast = RsGxsUpdateBroadcast::get(ifaceImpl); connect(mUpdateBroadcast, SIGNAL(changed()), this, SLOT(updateBroadcastChanged())); connect(mUpdateBroadcast, SIGNAL(grpsChanged(std::list, std::list)), this, SLOT(updateBroadcastGrpsChanged(std::list,std::list))); - connect(mUpdateBroadcast, SIGNAL(msgsChanged(std::map >, std::map >)), this, SLOT(updateBroadcastMsgsChanged(std::map >,std::map >))); + connect(mUpdateBroadcast, SIGNAL(msgsChanged(std::map >, std::map >)), this, SLOT(updateBroadcastMsgsChanged(std::map >,std::map >))); } RsGxsUpdateBroadcastBase::~RsGxsUpdateBroadcastBase() @@ -84,79 +84,36 @@ void RsGxsUpdateBroadcastBase::updateBroadcastChanged() void RsGxsUpdateBroadcastBase::updateBroadcastGrpsChanged(const std::list &grpIds, const std::list &grpIdsMeta) { std::list::const_iterator it; - for (it = grpIds.begin(); it != grpIds.end(); ++it) { - if (std::find(mGrpIds.begin(), mGrpIds.end(), *it) == mGrpIds.end()) { - mGrpIds.push_back(*it); - } - } - for (it = grpIdsMeta.begin(); it != grpIdsMeta.end(); ++it) { - if (std::find(mGrpIdsMeta.begin(), mGrpIdsMeta.end(), *it) == mGrpIdsMeta.end()) { - mGrpIdsMeta.push_back(*it); - } - } + for (it = grpIds.begin(); it != grpIds.end(); ++it) + mGrpIds.insert(*it) ; + + for (it = grpIdsMeta.begin(); it != grpIdsMeta.end(); ++it) + mGrpIdsMeta.insert(*it); } -void RsGxsUpdateBroadcastBase::updateBroadcastMsgsChanged(const std::map > &msgIds, const std::map > &msgIdsMeta) +template void merge(std::set& dst,const std::set& src) { for(auto it(src.begin());it!=src.end();++it) dst.insert(*it) ; } + +void RsGxsUpdateBroadcastBase::updateBroadcastMsgsChanged(const std::map > &msgIds, const std::map > &msgIdsMeta) { - std::map >::const_iterator mapIt; - for (mapIt = msgIds.begin(); mapIt != msgIds.end(); ++mapIt) { - const RsGxsGroupId &grpId = mapIt->first; - const std::vector &srcMsgIds = mapIt->second; - std::vector &destMsgIds = mMsgIds[grpId]; + for (auto mapIt = msgIds.begin(); mapIt != msgIds.end(); ++mapIt) + merge(mMsgIds[mapIt->first],mapIt->second) ; - std::vector::const_iterator msgIt; - for (msgIt = srcMsgIds.begin(); msgIt != srcMsgIds.end(); ++msgIt) { - if (std::find(destMsgIds.begin(), destMsgIds.end(), *msgIt) == destMsgIds.end()) { - destMsgIds.push_back(*msgIt); - } - } - } - for (mapIt = msgIdsMeta.begin(); mapIt != msgIdsMeta.end(); ++mapIt) { - const RsGxsGroupId &grpId = mapIt->first; - const std::vector &srcMsgIds = mapIt->second; - std::vector &destMsgIds = mMsgIdsMeta[grpId]; - - std::vector::const_iterator msgIt; - for (msgIt = srcMsgIds.begin(); msgIt != srcMsgIds.end(); ++msgIt) { - if (std::find(destMsgIds.begin(), destMsgIds.end(), *msgIt) == destMsgIds.end()) { - destMsgIds.push_back(*msgIt); - } - } - } + for (auto mapIt = msgIdsMeta.begin(); mapIt != msgIdsMeta.end(); ++mapIt) + merge(mMsgIdsMeta[mapIt->first],mapIt->second) ; } -void RsGxsUpdateBroadcastBase::getAllGrpIds(std::list &grpIds) +void RsGxsUpdateBroadcastBase::getAllGrpIds(std::set &grpIds) { - std::list::const_iterator it; - for (it = mGrpIds.begin(); it != mGrpIds.end(); ++it) { - if (std::find(grpIds.begin(), grpIds.end(), *it) == grpIds.end()) { - grpIds.push_back(*it); - } - } - for (it = mGrpIdsMeta.begin(); it != mGrpIdsMeta.end(); ++it) { - if (std::find(grpIds.begin(), grpIds.end(), *it) == grpIds.end()) { - grpIds.push_back(*it); - } - } + grpIds = mGrpIds; + merge(grpIds,mGrpIdsMeta) ; } -void RsGxsUpdateBroadcastBase::getAllMsgIds(std::map > &msgIds) +void RsGxsUpdateBroadcastBase::getAllMsgIds(std::map > &msgIds) { /* Copy first map */ msgIds = mMsgIds; /* Append second map */ - std::map >::const_iterator mapIt; - for (mapIt = mMsgIdsMeta.begin(); mapIt != mMsgIdsMeta.end(); ++mapIt) { - const RsGxsGroupId &grpId = mapIt->first; - const std::vector &srcMsgIds = mapIt->second; - std::vector &destMsgIds = msgIds[grpId]; - - std::vector::const_iterator msgIt; - for (msgIt = srcMsgIds.begin(); msgIt != srcMsgIds.end(); ++msgIt) { - if (std::find(destMsgIds.begin(), destMsgIds.end(), *msgIt) == destMsgIds.end()) { - destMsgIds.push_back(*msgIt); - } - } - } + for (auto mapIt = mMsgIdsMeta.begin(); mapIt != mMsgIdsMeta.end(); ++mapIt) + merge(msgIds[mapIt->first],mapIt->second); } diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.h b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.h index 9ffa8ff72..90dd3f507 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.h +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.h @@ -19,12 +19,12 @@ public: RsGxsUpdateBroadcastBase(RsGxsIfaceHelper* ifaceImpl, QWidget *parent = NULL); virtual ~RsGxsUpdateBroadcastBase(); - const std::list &getGrpIds() { return mGrpIds; } - const std::list &getGrpIdsMeta() { return mGrpIdsMeta; } - void getAllGrpIds(std::list &grpIds); - const std::map > &getMsgIds() { return mMsgIds; } - const std::map > &getMsgIdsMeta() { return mMsgIdsMeta; } - void getAllMsgIds(std::map > &msgIds); + const std::set &getGrpIds() { return mGrpIds; } + const std::set &getGrpIdsMeta() { return mGrpIdsMeta; } + void getAllGrpIds(std::set &grpIds); + const std::map > &getMsgIds() { return mMsgIds; } + const std::map > &getMsgIdsMeta() { return mMsgIdsMeta; } + void getAllMsgIds(std::map > &msgIds); protected: void fillComplete(); @@ -38,15 +38,15 @@ signals: private slots: void updateBroadcastChanged(); void updateBroadcastGrpsChanged(const std::list& grpIds, const std::list &grpIdsMeta); - void updateBroadcastMsgsChanged(const std::map >& msgIds, const std::map >& msgIdsMeta); + void updateBroadcastMsgsChanged(const std::map >& msgIds, const std::map >& msgIdsMeta); void securedUpdateDisplay(); private: RsGxsUpdateBroadcast *mUpdateBroadcast; bool mFillComplete; bool mUpdateWhenInvisible; // Update also when not visible - std::list mGrpIds; - std::list mGrpIdsMeta; - std::map > mMsgIds; - std::map > mMsgIdsMeta; + std::set mGrpIds; + std::set mGrpIdsMeta; + std::map > mMsgIds; + std::map > mMsgIdsMeta; }; diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.cpp b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.cpp index 5eba14b89..629148688 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.cpp +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.cpp @@ -22,32 +22,32 @@ void RsGxsUpdateBroadcastPage::setUpdateWhenInvisible(bool update) mBase->setUpdateWhenInvisible(update); } -const std::list &RsGxsUpdateBroadcastPage::getGrpIdsMeta() +const std::set &RsGxsUpdateBroadcastPage::getGrpIdsMeta() { return mBase->getGrpIdsMeta(); } -void RsGxsUpdateBroadcastPage::getAllGrpIds(std::list &grpIds) +void RsGxsUpdateBroadcastPage::getAllGrpIds(std::set &grpIds) { mBase->getAllGrpIds(grpIds); } -const std::list &RsGxsUpdateBroadcastPage::getGrpIds() +const std::set &RsGxsUpdateBroadcastPage::getGrpIds() { return mBase->getGrpIds(); } -const std::map > &RsGxsUpdateBroadcastPage::getMsgIdsMeta() +const std::map > &RsGxsUpdateBroadcastPage::getMsgIdsMeta() { return mBase->getMsgIdsMeta(); } -void RsGxsUpdateBroadcastPage::getAllMsgIds(std::map > &msgIds) +void RsGxsUpdateBroadcastPage::getAllMsgIds(std::map > &msgIds) { mBase->getAllMsgIds(msgIds); } -const std::map > &RsGxsUpdateBroadcastPage::getMsgIds() +const std::map > &RsGxsUpdateBroadcastPage::getMsgIds() { return mBase->getMsgIds(); } diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h index ed03bb15c..0c67f58ff 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h @@ -24,12 +24,12 @@ public: void fillComplete(); void setUpdateWhenInvisible(bool update); - const std::list &getGrpIds(); - const std::list &getGrpIdsMeta(); - void getAllGrpIds(std::list &grpIds); - const std::map > &getMsgIds(); - const std::map > &getMsgIdsMeta(); - void getAllMsgIds(std::map > &msgIds); + const std::set &getGrpIds(); + const std::set &getGrpIdsMeta(); + void getAllGrpIds(std::set &grpIds); + const std::map > &getMsgIds(); + const std::map > &getMsgIdsMeta(); + void getAllMsgIds(std::map > &msgIds); protected: virtual void showEvent(QShowEvent *event); diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp index d2450e719..a6e3e5705 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp @@ -24,32 +24,32 @@ void RsGxsUpdateBroadcastWidget::setUpdateWhenInvisible(bool update) mBase->setUpdateWhenInvisible(update); } -const std::list &RsGxsUpdateBroadcastWidget::getGrpIds() +const std::set &RsGxsUpdateBroadcastWidget::getGrpIds() { return mBase->getGrpIds(); } -const std::list &RsGxsUpdateBroadcastWidget::getGrpIdsMeta() +const std::set &RsGxsUpdateBroadcastWidget::getGrpIdsMeta() { return mBase->getGrpIdsMeta(); } -void RsGxsUpdateBroadcastWidget::getAllGrpIds(std::list &grpIds) +void RsGxsUpdateBroadcastWidget::getAllGrpIds(std::set &grpIds) { mBase->getAllGrpIds(grpIds); } -const std::map > &RsGxsUpdateBroadcastWidget::getMsgIds() +const std::map > &RsGxsUpdateBroadcastWidget::getMsgIds() { return mBase->getMsgIds(); } -const std::map > &RsGxsUpdateBroadcastWidget::getMsgIdsMeta() +const std::map > &RsGxsUpdateBroadcastWidget::getMsgIdsMeta() { return mBase->getMsgIdsMeta(); } -void RsGxsUpdateBroadcastWidget::getAllMsgIds(std::map > &msgIds) +void RsGxsUpdateBroadcastWidget::getAllMsgIds(std::map > &msgIds) { mBase->getAllMsgIds(msgIds); } diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.h b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.h index 33fcd06bf..f0ab07742 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.h +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.h @@ -24,12 +24,12 @@ public: void fillComplete(); void setUpdateWhenInvisible(bool update); - const std::list &getGrpIds(); - const std::list &getGrpIdsMeta(); - void getAllGrpIds(std::list &grpIds); - const std::map > &getMsgIds(); - const std::map > &getMsgIdsMeta(); - void getAllMsgIds(std::map > &msgIds); + const std::set &getGrpIds(); + const std::set &getGrpIdsMeta(); + void getAllGrpIds(std::set &grpIds); + const std::map > &getMsgIds(); + const std::map > &getMsgIdsMeta(); + void getAllMsgIds(std::map > &msgIds); RsGxsIfaceHelper *interfaceHelper() { return mInterfaceHelper; } diff --git a/retroshare-gui/src/gui/gxschannels/CreateGxsChannelMsg.cpp b/retroshare-gui/src/gui/gxschannels/CreateGxsChannelMsg.cpp index cc93d5627..b4470ac5c 100644 --- a/retroshare-gui/src/gui/gxschannels/CreateGxsChannelMsg.cpp +++ b/retroshare-gui/src/gui/gxschannels/CreateGxsChannelMsg.cpp @@ -604,7 +604,7 @@ void CreateGxsChannelMsg::newChannelMsg() GxsMsgReq message_ids; opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; - message_ids[mChannelId].push_back(mOrigPostId); + message_ids[mChannelId].insert(mOrigPostId); mChannelQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_SUMMARY, opts, message_ids, CREATEMSG_CHANNEL_POST_INFO); } } diff --git a/retroshare-gui/src/gui/gxsforums/CreateGxsForumMsg.cpp b/retroshare-gui/src/gui/gxsforums/CreateGxsForumMsg.cpp index 60f34221f..23d827ac3 100644 --- a/retroshare-gui/src/gui/gxsforums/CreateGxsForumMsg.cpp +++ b/retroshare-gui/src/gui/gxsforums/CreateGxsForumMsg.cpp @@ -185,8 +185,8 @@ void CreateGxsForumMsg::newMsg() opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; GxsMsgReq msgIds; - std::vector &vect = msgIds[mForumId]; - vect.push_back(mParentId); + std::set &vect = msgIds[mForumId]; + vect.insert(mParentId); //std::cerr << "ForumsV2Dialog::newMsg() Requesting Parent Summary(" << mParentId << ")"; //std::cerr << std::endl; @@ -205,8 +205,8 @@ void CreateGxsForumMsg::newMsg() opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; GxsMsgReq msgIds; - std::vector &vect = msgIds[mForumId]; - vect.push_back(mOrigMsgId); + std::set &vect = msgIds[mForumId]; + vect.insert(mOrigMsgId); //std::cerr << "ForumsV2Dialog::newMsg() Requesting Parent Summary(" << mParentId << ")"; //std::cerr << std::endl; diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp index 5209ca184..7b9ff9183 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp @@ -404,25 +404,25 @@ void GxsForumThreadWidget::changeEvent(QEvent *e) } } -static void removeMessages(std::map > &msgIds, QList &removeMsgId) +static void removeMessages(std::map > &msgIds, QList &removeMsgId) { QList removedMsgId; - std::map >::iterator grpIt; - for (grpIt = msgIds.begin(); grpIt != msgIds.end(); ) { - std::vector &msgs = grpIt->second; + for (auto grpIt = msgIds.begin(); grpIt != msgIds.end(); ) + { + std::set &msgs = grpIt->second; QList::const_iterator removeMsgIt; for (removeMsgIt = removeMsgId.begin(); removeMsgIt != removeMsgId.end(); ++removeMsgIt) { - std::vector::iterator msgIt = std::find(msgs.begin(), msgs.end(), *removeMsgIt); - if (msgIt != msgs.end()) { + if(msgs.find(*removeMsgIt) != msgs.end()) + { removedMsgId.push_back(*removeMsgIt); - msgs.erase(msgIt); + msgs.erase(*removeMsgIt); } } if (msgs.empty()) { - std::map >::iterator grpItErase = grpIt++; + std::map >::iterator grpItErase = grpIt++; msgIds.erase(grpItErase); } else { ++grpIt; @@ -452,18 +452,18 @@ void GxsForumThreadWidget::updateDisplay(bool complete) } bool updateGroup = false; - const std::list &grpIdsMeta = getGrpIdsMeta(); - if (std::find(grpIdsMeta.begin(), grpIdsMeta.end(), groupId()) != grpIdsMeta.end()) { - updateGroup = true; - } + const std::set &grpIdsMeta = getGrpIdsMeta(); - const std::list &grpIds = getGrpIds(); - if (std::find(grpIds.begin(), grpIds.end(), groupId()) != grpIds.end()) { + if(grpIdsMeta.find(groupId())!=grpIdsMeta.end()) + updateGroup = true; + + const std::set &grpIds = getGrpIds(); + if (grpIds.find(groupId())!=grpIds.end()){ updateGroup = true; /* Update threads */ insertThreads(); } else { - std::map > msgIds; + std::map > msgIds; getAllMsgIds(msgIds); if (!mIgnoredMsgId.empty()) { @@ -2111,8 +2111,8 @@ void GxsForumThreadWidget::flagperson() #endif GxsMsgReq msgIds; - std::vector &vect = msgIds[postId.first]; - vect.push_back(postId.second); + std::set &vect = msgIds[postId.first]; + vect.insert(postId.second); uint32_t token; mTokenQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, token_type); @@ -2413,8 +2413,8 @@ void GxsForumThreadWidget::requestMessageData(const RsGxsGrpMsgIdPair &msgId) #endif GxsMsgReq msgIds; - std::vector &vect = msgIds[msgId.first]; - vect.push_back(msgId.second); + std::set &vect = msgIds[msgId.first]; + vect.insert(msgId.second); uint32_t token; mTokenQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeMessageData); @@ -2464,8 +2464,8 @@ void GxsForumThreadWidget::requestMsgData_ReplyWithPrivateMessage(const RsGxsGrp #endif GxsMsgReq msgIds; - std::vector &vect = msgIds[msgId.first]; - vect.push_back(msgId.second); + std::set &vect = msgIds[msgId.first]; + vect.insert(msgId.second); uint32_t token; mTokenQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeReplyMessage); @@ -2482,8 +2482,8 @@ void GxsForumThreadWidget::requestMsgData_ShowAuthorInPeople(const RsGxsGrpMsgId #endif GxsMsgReq msgIds; - std::vector &vect = msgIds[msgId.first]; - vect.push_back(msgId.second); + std::set &vect = msgIds[msgId.first]; + vect.insert(msgId.second); uint32_t token; mTokenQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeShowAuthorInPeople); @@ -2499,8 +2499,8 @@ void GxsForumThreadWidget::requestMsgData_EditForumMessage(const RsGxsGrpMsgIdPa #endif GxsMsgReq msgIds; - std::vector &vect = msgIds[msgId.first]; - vect.push_back(msgId.second); + std::set &vect = msgIds[msgId.first]; + vect.insert(msgId.second); uint32_t token; mTokenQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeEditForumMessage); @@ -2516,8 +2516,8 @@ void GxsForumThreadWidget::requestMsgData_ReplyForumMessage(const RsGxsGrpMsgIdP #endif GxsMsgReq msgIds; - std::vector &vect = msgIds[msgId.first]; - vect.push_back(msgId.second); + std::set &vect = msgIds[msgId.first]; + vect.insert(msgId.second); uint32_t token; mTokenQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeReplyForumMessage); diff --git a/retroshare-gui/src/util/RsGxsUpdateBroadcast.h b/retroshare-gui/src/util/RsGxsUpdateBroadcast.h index d9d90db1f..1622ca606 100644 --- a/retroshare-gui/src/util/RsGxsUpdateBroadcast.h +++ b/retroshare-gui/src/util/RsGxsUpdateBroadcast.h @@ -19,7 +19,7 @@ public: signals: void changed(); - void msgsChanged(const std::map >& msgIds, const std::map >& msgIdsMeta); + void msgsChanged(const std::map >& msgIds, const std::map >& msgIdsMeta); void grpsChanged(const std::list& grpIds, const std::list& grpIdsMeta); private slots: From cadc6978825c65009bd337da4dbccd620de25470 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 21 Jun 2018 17:27:15 +0200 Subject: [PATCH 155/161] removed reload of currently selected identity during full reload of Id list in People as it caused some blinking --- retroshare-gui/src/gui/Identity/IdDialog.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/retroshare-gui/src/gui/Identity/IdDialog.cpp b/retroshare-gui/src/gui/Identity/IdDialog.cpp index 7f644be5f..5c9bce46b 100644 --- a/retroshare-gui/src/gui/Identity/IdDialog.cpp +++ b/retroshare-gui/src/gui/Identity/IdDialog.cpp @@ -2125,7 +2125,7 @@ void IdDialog::updateDisplay(bool complete) if (complete) { /* Fill complete */ requestIdList(); - requestIdDetails(); + //requestIdDetails(); requestRepList(); return; From a7f1adc49adc9d2f1ed625d410e0ea8b0fe69c91 Mon Sep 17 00:00:00 2001 From: csoler Date: Thu, 21 Jun 2018 18:55:29 +0200 Subject: [PATCH 156/161] added comment --- retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp index 8d4bc0fc3..ffbd6147e 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp @@ -73,6 +73,7 @@ void RsGxsUpdateBroadcastBase::updateBroadcastChanged() // first place so there's no need to re-update the UI once this is done. // // The question to whether we should re=load when mGrpIds is not empty is still open. It's not harmful anyway. + // This should probably be decided by the service itself. if (!mGrpIds.empty() || !mGrpIdsMeta.empty() /*|| !mMsgIds.empty()*/ || !mMsgIdsMeta.empty()) mFillComplete = true ; From 502459a25a01e3136c0e8c1b05fb3b8dd72f8f4d Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Tue, 26 Jun 2018 15:12:16 +0200 Subject: [PATCH 157/161] Cleanup rapidjson inclusion Fix compilation error of android service --- libresapi/src/libresapi.pro | 4 ---- libretroshare/src/libretroshare.pro | 5 ----- libretroshare/src/use_libretroshare.pri | 5 +++++ retroshare-gui/src/retroshare-gui.pro | 5 ----- retroshare-nogui/src/retroshare-nogui.pro | 4 ---- 5 files changed, 5 insertions(+), 18 deletions(-) diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index 161ce9bf7..5f4c973e9 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -9,10 +9,6 @@ DESTDIR = lib !include(use_libresapi.pri):error("Including") -# when rapidjson is mainstream on all distribs, we will not need the sources anymore -# in the meantime, they are part of the RS directory so that it is always possible to find them - -INCLUDEPATH += ../../rapidjson-1.1.0 INCLUDEPATH += ../../libretroshare/src libresapilocalserver { diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 729c9186d..999cf30eb 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -17,11 +17,6 @@ DESTDIR = lib #QMAKE_CFLAGS += -Werror #QMAKE_CXXFLAGS += -Werror -# when rapidjson is mainstream on all distribs, we will not need the sources anymore -# in the meantime, they are part of the RS directory so that it is always possible to find them - -INCLUDEPATH += ../../rapidjson-1.1.0 - debug { # DEFINES *= DEBUG # DEFINES *= OPENDHT_DEBUG DHT_DEBUG CONN_DEBUG DEBUG_UDP_SORTER P3DISC_DEBUG DEBUG_UDP_LAYER FT_DEBUG EXTADDRSEARCH_DEBUG diff --git a/libretroshare/src/use_libretroshare.pri b/libretroshare/src/use_libretroshare.pri index d17e36739..3a3d1acb7 100644 --- a/libretroshare/src/use_libretroshare.pri +++ b/libretroshare/src/use_libretroshare.pri @@ -13,6 +13,11 @@ bitdht { !include("../../libbitdht/src/use_libbitdht.pri"):error("Including") } +# when rapidjson is mainstream on all distribs, we will not need the sources +# anymore in the meantime, they are part of the RS directory so that it is +# always possible to find them +INCLUDEPATH *= $$system_path($$clean_path($${PWD}/../../rapidjson-1.1.0)) + sLibs = mLibs = $$RS_SQL_LIB ssl crypto $$RS_THREAD_LIB $$RS_UPNP_LIB dLibs = diff --git a/retroshare-gui/src/retroshare-gui.pro b/retroshare-gui/src/retroshare-gui.pro index 48e10a377..30b2af946 100644 --- a/retroshare-gui/src/retroshare-gui.pro +++ b/retroshare-gui/src/retroshare-gui.pro @@ -10,11 +10,6 @@ DEFINES += TARGET=\\\"$${TARGET}\\\" DEPENDPATH *= $${PWD} $${RS_INCLUDE_DIR} retroshare-gui INCLUDEPATH *= $${PWD} retroshare-gui -# when rapidjson is mainstream on all distribs, we will not need the sources anymore -# in the meantime, they are part of the RS directory so that it is always possible to find them - -INCLUDEPATH += ../../rapidjson-1.1.0 - libresapihttpserver { !include("../../libresapi/src/use_libresapi.pri"):error("Including") HEADERS *= gui/settings/WebuiPage.h diff --git a/retroshare-nogui/src/retroshare-nogui.pro b/retroshare-nogui/src/retroshare-nogui.pro index 6c95d278c..05f5e99e1 100644 --- a/retroshare-nogui/src/retroshare-nogui.pro +++ b/retroshare-nogui/src/retroshare-nogui.pro @@ -16,10 +16,6 @@ libresapihttpserver { !include("../../libretroshare/src/use_libretroshare.pri"):error("Including") -# when rapidjson is mainstream on all distribs, we will not need the sources anymore -# in the meantime, they are part of the RS directory so that it is always possible to find them - -INCLUDEPATH += ../../rapidjson-1.1.0 ################################# Linux ########################################## linux-* { From f7625e35261b11e8ce3f391a9963fb0ec69802e5 Mon Sep 17 00:00:00 2001 From: sehraf Date: Thu, 28 Jun 2018 20:25:10 +0200 Subject: [PATCH 158/161] fix json uint64 --- libretroshare/src/serialiser/rstypeserializer.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libretroshare/src/serialiser/rstypeserializer.cc b/libretroshare/src/serialiser/rstypeserializer.cc index e7faa5b52..72d20ecc2 100644 --- a/libretroshare/src/serialiser/rstypeserializer.cc +++ b/libretroshare/src/serialiser/rstypeserializer.cc @@ -291,8 +291,8 @@ bool RsTypeSerializer::from_JSON( const std::string& memberName, uint64_t& member, RsJson& jDoc ) { SAFE_GET_JSON_V(); - ret = ret && v.IsUint(); - if(ret) member = v.GetUint(); + ret = ret && v.IsUint64(); + if(ret) member = v.GetUint64(); return ret; } From abc5b840d265e6316d2d3efb5b3ef1dbf42629de Mon Sep 17 00:00:00 2001 From: cyril soler Date: Mon, 2 Jul 2018 09:36:21 +0200 Subject: [PATCH 159/161] added queuedConnection type in fillDisplay() between RsGxsBroadcastWidget and RsGxsBroadCastBase. --- retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp index a6e3e5705..2f1ddf4d6 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp @@ -4,8 +4,9 @@ RsGxsUpdateBroadcastWidget::RsGxsUpdateBroadcastWidget(RsGxsIfaceHelper *ifaceImpl, QWidget *parent, Qt::WindowFlags flags) : QWidget(parent, flags) { - mBase = new RsGxsUpdateBroadcastBase(ifaceImpl, this); - connect(mBase, SIGNAL(fillDisplay(bool)), this, SLOT(fillDisplay(bool))); + mBase = new RsGxsUpdateBroadcastBase(ifaceImpl, this); + // The Queued connection is here to circumvent an apparent mutex problem in Qt + connect(mBase, SIGNAL(fillDisplay(bool)), this, SLOT(fillDisplay(bool)),Qt::QueuedConnection); mInterfaceHelper = ifaceImpl; } @@ -14,6 +15,7 @@ RsGxsUpdateBroadcastWidget::~RsGxsUpdateBroadcastWidget() { } + void RsGxsUpdateBroadcastWidget::fillComplete() { mBase->fillComplete(); From e6db04e2b538b7e6ef01013beaf0aae1a8ce0132 Mon Sep 17 00:00:00 2001 From: cyril soler Date: Mon, 2 Jul 2018 09:45:17 +0200 Subject: [PATCH 160/161] cancelled previous commit, because it sort of breaks the update of forum lists. --- .../gui/gxs/RsGxsUpdateBroadcastWidget.cpp | 44 +++++++++---------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp index 2f1ddf4d6..19ae2c3d0 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp @@ -2,68 +2,66 @@ #include "RsGxsUpdateBroadcastBase.h" RsGxsUpdateBroadcastWidget::RsGxsUpdateBroadcastWidget(RsGxsIfaceHelper *ifaceImpl, QWidget *parent, Qt::WindowFlags flags) - : QWidget(parent, flags) + : QWidget(parent, flags) { mBase = new RsGxsUpdateBroadcastBase(ifaceImpl, this); - // The Queued connection is here to circumvent an apparent mutex problem in Qt - connect(mBase, SIGNAL(fillDisplay(bool)), this, SLOT(fillDisplay(bool)),Qt::QueuedConnection); + connect(mBase, SIGNAL(fillDisplay(bool)), this, SLOT(fillDisplay(bool))); - mInterfaceHelper = ifaceImpl; + mInterfaceHelper = ifaceImpl; } RsGxsUpdateBroadcastWidget::~RsGxsUpdateBroadcastWidget() { } - void RsGxsUpdateBroadcastWidget::fillComplete() { - mBase->fillComplete(); + mBase->fillComplete(); } void RsGxsUpdateBroadcastWidget::setUpdateWhenInvisible(bool update) { - mBase->setUpdateWhenInvisible(update); + mBase->setUpdateWhenInvisible(update); } -const std::set &RsGxsUpdateBroadcastWidget::getGrpIds() +const std::list &RsGxsUpdateBroadcastWidget::getGrpIds() { - return mBase->getGrpIds(); + return mBase->getGrpIds(); } -const std::set &RsGxsUpdateBroadcastWidget::getGrpIdsMeta() +const std::list &RsGxsUpdateBroadcastWidget::getGrpIdsMeta() { - return mBase->getGrpIdsMeta(); + return mBase->getGrpIdsMeta(); } -void RsGxsUpdateBroadcastWidget::getAllGrpIds(std::set &grpIds) +void RsGxsUpdateBroadcastWidget::getAllGrpIds(std::list &grpIds) { - mBase->getAllGrpIds(grpIds); + mBase->getAllGrpIds(grpIds); } -const std::map > &RsGxsUpdateBroadcastWidget::getMsgIds() +const std::map > &RsGxsUpdateBroadcastWidget::getMsgIds() { - return mBase->getMsgIds(); + return mBase->getMsgIds(); } -const std::map > &RsGxsUpdateBroadcastWidget::getMsgIdsMeta() +const std::map > &RsGxsUpdateBroadcastWidget::getMsgIdsMeta() { - return mBase->getMsgIdsMeta(); + return mBase->getMsgIdsMeta(); } -void RsGxsUpdateBroadcastWidget::getAllMsgIds(std::map > &msgIds) +void RsGxsUpdateBroadcastWidget::getAllMsgIds(std::map > &msgIds) { - mBase->getAllMsgIds(msgIds); + mBase->getAllMsgIds(msgIds); } void RsGxsUpdateBroadcastWidget::fillDisplay(bool complete) { - updateDisplay(complete); - update(); // Qt flush + updateDisplay(complete); + update(); // Qt flush } void RsGxsUpdateBroadcastWidget::showEvent(QShowEvent *event) { - mBase->showEvent(event); - QWidget::showEvent(event); + mBase->showEvent(event); + QWidget::showEvent(event); } From 1b2b3113ca80cfb8b6dff648928f9a4a42f25542 Mon Sep 17 00:00:00 2001 From: cyril soler Date: Mon, 2 Jul 2018 10:21:38 +0200 Subject: [PATCH 161/161] fixed previous commit caused by an apparent bug in qtcreator when updating code --- .../src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp index 19ae2c3d0..c20809937 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp @@ -24,32 +24,32 @@ void RsGxsUpdateBroadcastWidget::setUpdateWhenInvisible(bool update) mBase->setUpdateWhenInvisible(update); } -const std::list &RsGxsUpdateBroadcastWidget::getGrpIds() +const std::set &RsGxsUpdateBroadcastWidget::getGrpIds() { return mBase->getGrpIds(); } -const std::list &RsGxsUpdateBroadcastWidget::getGrpIdsMeta() +const std::set &RsGxsUpdateBroadcastWidget::getGrpIdsMeta() { return mBase->getGrpIdsMeta(); } -void RsGxsUpdateBroadcastWidget::getAllGrpIds(std::list &grpIds) +void RsGxsUpdateBroadcastWidget::getAllGrpIds(std::set &grpIds) { mBase->getAllGrpIds(grpIds); } -const std::map > &RsGxsUpdateBroadcastWidget::getMsgIds() +const std::map > &RsGxsUpdateBroadcastWidget::getMsgIds() { return mBase->getMsgIds(); } -const std::map > &RsGxsUpdateBroadcastWidget::getMsgIdsMeta() +const std::map > &RsGxsUpdateBroadcastWidget::getMsgIdsMeta() { return mBase->getMsgIdsMeta(); } -void RsGxsUpdateBroadcastWidget::getAllMsgIds(std::map > &msgIds) +void RsGxsUpdateBroadcastWidget::getAllMsgIds(std::map > &msgIds) { mBase->getAllMsgIds(msgIds); }