diff --git a/Common/cppcf/Stream.h b/Common/cppcf/Stream.h deleted file mode 100644 index d093fe8dc3..0000000000 --- a/Common/cppcf/Stream.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - - -#include -#include -#include -#include "../../DesktopEditor/common/Types.h" - -namespace CFCPP -{ -using Stream = std::shared_ptr; -std::streamsize Length(const Stream& st) -{ - if (st.get() == nullptr) - return 0; - - auto curPos = st->tellg(); - st->seekg(std::ios_base::end); - auto ssize = st->tellg(); - st->seekg(curPos); - - return ssize; -} -} diff --git a/Common/cppcf/cfcpp.pro b/Common/cppcf/cfcpp.pro index 3ab4d5054f..711ecf92c5 100644 --- a/Common/cppcf/cfcpp.pro +++ b/Common/cppcf/cfcpp.pro @@ -9,9 +9,6 @@ CONFIG += c++11 # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -#BOOST -include(../3dParty/boost/boost.pri) - SOURCES += \ RBTree/rbtree.cpp \ cfitem.cpp \ @@ -22,6 +19,7 @@ SOURCES += \ header.cpp \ sector.cpp \ sectorcollection.cpp \ + stream.cpp \ streamrw.cpp \ streamview.cpp @@ -29,7 +27,6 @@ HEADERS += \ RBTree/irbnode.h \ RBTree/rbtree.h \ RBTree/rbtreeexception.h \ - Stream.h \ cfexception.h \ cfitem.h \ cfstorage.h \ @@ -43,6 +40,7 @@ HEADERS += \ sector.h \ sectorcollection.h \ slist.h \ + stream.h \ streamrw.h \ streamview.h \ svector.h diff --git a/Common/cppcf/cfstream.h b/Common/cppcf/cfstream.h index c9882768f6..b382d96971 100644 --- a/Common/cppcf/cfstream.h +++ b/Common/cppcf/cfstream.h @@ -1,6 +1,6 @@ #pragma once -#include "Stream.h" +#include "stream.h" #include "cfitem.h" #include diff --git a/Common/cppcf/compoundfile.h b/Common/cppcf/compoundfile.h index 22b39516a2..6baf8629b9 100644 --- a/Common/cppcf/compoundfile.h +++ b/Common/cppcf/compoundfile.h @@ -7,12 +7,10 @@ #include "RBTree/rbtree.h" #include "idirectoryentry.h" #include - +#include "header.h" namespace CFCPP { - -class Header; class DirectoryEntry; enum CFSConfiguration diff --git a/Common/cppcf/idirectoryentry.h b/Common/cppcf/idirectoryentry.h index 643d92ba24..44ac93d135 100644 --- a/Common/cppcf/idirectoryentry.h +++ b/Common/cppcf/idirectoryentry.h @@ -1,7 +1,7 @@ #pragma once #include -#include "Stream.h" +#include "stream.h" #include "RBTree/irbnode.h" #include "guid.h" diff --git a/Common/cppcf/sector.h b/Common/cppcf/sector.h index ef8244d9aa..ac8923f7af 100644 --- a/Common/cppcf/sector.h +++ b/Common/cppcf/sector.h @@ -1,6 +1,6 @@ #pragma once -#include "Stream.h" +#include "stream.h" #include #include "../../DesktopEditor/common/Types.h" #include diff --git a/Common/cppcf/stream.cpp b/Common/cppcf/stream.cpp new file mode 100644 index 0000000000..a939af5f20 --- /dev/null +++ b/Common/cppcf/stream.cpp @@ -0,0 +1,15 @@ +#include "stream.h" + + +std::streamsize CFCPP::Length(const CFCPP::Stream& st) +{ + if (st.get() == nullptr) + return 0; + + auto curPos = st->tellg(); + st->seekg(0, std::ios_base::end); + auto ssize = st->tellg(); + st->seekg(curPos); + + return ssize; +} diff --git a/Common/cppcf/stream.h b/Common/cppcf/stream.h new file mode 100644 index 0000000000..727bf0466a --- /dev/null +++ b/Common/cppcf/stream.h @@ -0,0 +1,13 @@ +#pragma once + + +#include +#include +#include +#include "../../DesktopEditor/common/Types.h" + +namespace CFCPP +{ +using Stream = std::shared_ptr; +std::streamsize Length(const Stream& st); +} diff --git a/Common/cppcf/streamrw.cpp b/Common/cppcf/streamrw.cpp index d8ca9ea646..20a3301d17 100644 --- a/Common/cppcf/streamrw.cpp +++ b/Common/cppcf/streamrw.cpp @@ -11,8 +11,8 @@ StreamRW::StreamRW(const Stream &stream) T_LONG64 StreamRW::Seek(T_LONG64 offset) { - stream->seekg(offset); - return stream->tellg(); + stream->seekp(offset, std::ios::cur); + return stream->tellp(); } template @@ -48,21 +48,3 @@ void StreamRW::WriteArray(const char *arr, int lenght) { stream->write(arr, lenght); } - -template -T StreamRW::Read() -{ - T value; - char* asByteArr = reinterpret_cast(&value); - stream->read(asByteArr, sizeof (T)); - std::reverse(asByteArr, asByteArr + sizeof (T)); - return value; -} - -template -void StreamRW::Write(T value) -{ - char* asByteArr = reinterpret_cast(&value); - std::reverse(asByteArr, asByteArr + sizeof (T)); - stream->write(asByteArr, sizeof (T)); -} diff --git a/Common/cppcf/streamrw.h b/Common/cppcf/streamrw.h index 27c389b1d0..58e6ce55ba 100644 --- a/Common/cppcf/streamrw.h +++ b/Common/cppcf/streamrw.h @@ -3,7 +3,7 @@ #include #include #include -#include "Stream.h" +#include "stream.h" namespace CFCPP @@ -15,10 +15,20 @@ public: T_LONG64 Seek(T_LONG64 offset); template - T Read(); + T Read() + { + T value; + char* asByteArr = reinterpret_cast(&value); + stream->read(asByteArr, sizeof (T)); + return value; + } template - void Write(T value); + void Write(T value) + { + char* asByteArr = reinterpret_cast(&value); + stream->write(asByteArr, sizeof (T)); + } std::vector ReadArray(int lenght); void ReadArray(char* data, int lenght); @@ -31,7 +41,7 @@ public: inline void Close(){return;} private: - std::array buffer; + std::array buffer; Stream stream; }; diff --git a/Common/cppcf/test/.gitignore b/Common/cppcf/test/.gitignore new file mode 100644 index 0000000000..fab7372d79 --- /dev/null +++ b/Common/cppcf/test/.gitignore @@ -0,0 +1,73 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/Common/cppcf/test/gtest_dependency.pri b/Common/cppcf/test/gtest_dependency.pri new file mode 100644 index 0000000000..7da47727ea --- /dev/null +++ b/Common/cppcf/test/gtest_dependency.pri @@ -0,0 +1,41 @@ +isEmpty(GOOGLETEST_DIR):GOOGLETEST_DIR=$$(GOOGLETEST_DIR) + +isEmpty(GOOGLETEST_DIR) { + GOOGLETEST_DIR = /home/ivaz28/onlydesk/core/Common/3dParty/v8/v8/third_party/googletest/src + !isEmpty(GOOGLETEST_DIR) { + warning("Using googletest src dir specified at Qt Creator wizard") + message("set GOOGLETEST_DIR as environment variable or qmake variable to get rid of this message") + } +} + +!isEmpty(GOOGLETEST_DIR): { + GTEST_SRCDIR = $$GOOGLETEST_DIR/googletest + GMOCK_SRCDIR = $$GOOGLETEST_DIR/googlemock +} else: unix { + exists(/usr/src/gtest):GTEST_SRCDIR=/usr/src/gtest + exists(/usr/src/gmock):GMOCK_SRCDIR=/usr/src/gmock + !isEmpty(GTEST_SRCDIR): message("Using gtest from system") +} + +requires(exists($$GTEST_SRCDIR):exists($$GMOCK_SRCDIR)) + +DEFINES += \ + GTEST_LANG_CXX11 + +!isEmpty(GTEST_SRCDIR) { + INCLUDEPATH *= \ + $$GTEST_SRCDIR \ + $$GTEST_SRCDIR/include + + SOURCES += \ + $$GTEST_SRCDIR/src/gtest-all.cc +} + +!isEmpty(GMOCK_SRCDIR) { + INCLUDEPATH *= \ + $$GMOCK_SRCDIR \ + $$GMOCK_SRCDIR/include + + SOURCES += \ + $$GMOCK_SRCDIR/src/gmock-all.cc +} diff --git a/Common/cppcf/test/main.cpp b/Common/cppcf/test/main.cpp new file mode 100644 index 0000000000..2ece9040d2 --- /dev/null +++ b/Common/cppcf/test/main.cpp @@ -0,0 +1,12 @@ +#include "tst_test_stream.h" +#include "streamrw.h" +#include + +using namespace CFCPP; +using namespace std; + +int main(int argc, char *argv[]) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/Common/cppcf/test/test.pro b/Common/cppcf/test/test.pro new file mode 100644 index 0000000000..23be0ae782 --- /dev/null +++ b/Common/cppcf/test/test.pro @@ -0,0 +1,16 @@ +include(gtest_dependency.pri) + +TEMPLATE = app +CONFIG += console c++11 +CONFIG -= app_bundle +CONFIG += thread +CONFIG -= qt + +LIBS += -L$$PWD/../Debug -lcfcpp +INCLUDEPATH += $$PWD/../ + +HEADERS += \ + tst_test_stream.h + +SOURCES += \ + main.cpp diff --git a/Common/cppcf/test/tst_test_stream.h b/Common/cppcf/test/tst_test_stream.h new file mode 100644 index 0000000000..b6037bd76f --- /dev/null +++ b/Common/cppcf/test/tst_test_stream.h @@ -0,0 +1,55 @@ +#ifndef TST_TEST_STREAM_H +#define TST_TEST_STREAM_H + +#include +#include +#include "streamrw.h" + +using namespace testing; +using namespace std; +using namespace CFCPP; + +struct StreamRWTest : testing::Test +{ + Stream stream; + shared_ptr rw; + string filename = "../../data/types/types.bin"; + const char symbol = 'a'; + const int integer = 13; + + StreamRWTest() + { + stream.reset(new std::fstream(filename, ios::app | ios::in | ios::out)); + rw.reset(new StreamRW(stream)); + } + + ~StreamRWTest() + { + remove(filename.c_str()); + } +}; + + +TEST_F(StreamRWTest, test_stream_open) +{ + EXPECT_EQ(static_pointer_cast(stream)->is_open(), 1); +} + +TEST_F(StreamRWTest, test_stream_write) +{ + rw->Write(symbol); + rw->Write(integer); + stream->flush(); + EXPECT_EQ((int)Length(stream), 5); +} + + +TEST_F(StreamRWTest, test_stream_read) +{ + EXPECT_EQ(rw->Seek(-5), 0); + EXPECT_EQ(rw->Read(), symbol); + EXPECT_EQ(rw->Read(), integer); +} + + +#endif // TST_TEST_STREAM_H