From: <dke...@us...> - 2008-03-31 23:32:28
|
Revision: 74 http://proteowizard.svn.sourceforge.net/proteowizard/?rev=74&view=rev Author: dkessner Date: 2008-03-31 16:32:32 -0700 (Mon, 31 Mar 2008) Log Message: ----------- Reader refactoring Modified Paths: -------------- trunk/pwiz/pwiz/msdata/Jamfile trunk/pwiz/pwiz/msdata/MSDataFile.cpp trunk/pwiz/pwiz/msdata/MSDataFile.hpp trunk/pwiz/pwiz/msdata/MSDataFileTest.cpp trunk/pwiz/pwiz/msdata/Reader_RAW.cpp trunk/pwiz/pwiz/msdata/Reader_RAW.hpp trunk/pwiz/pwiz/msdata/Reader_RAW_Test.cpp Added Paths: ----------- trunk/pwiz/pwiz/msdata/DefaultReaderList.cpp trunk/pwiz/pwiz/msdata/DefaultReaderList.hpp trunk/pwiz/pwiz/msdata/Reader.cpp trunk/pwiz/pwiz/msdata/Reader.hpp trunk/pwiz/pwiz/msdata/ReaderTest.cpp Added: trunk/pwiz/pwiz/msdata/DefaultReaderList.cpp =================================================================== --- trunk/pwiz/pwiz/msdata/DefaultReaderList.cpp (rev 0) +++ trunk/pwiz/pwiz/msdata/DefaultReaderList.cpp 2008-03-31 23:32:32 UTC (rev 74) @@ -0,0 +1,173 @@ +// +// DefaultReaderList.cpp +// +// +// Original author: Darren Kessner <Dar...@cs...> +// +// Copyright 2008 Spielberg Family Center for Applied Proteomics +// Cedars-Sinai Medical Center, Los Angeles, California 90048 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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 "DefaultReaderList.hpp" +#include "SpectrumList_mzXML.hpp" +#include "Serializer_mzML.hpp" +#include "Serializer_mzXML.hpp" +#include "Reader_RAW.hpp" +#include <iostream> +#include <fstream> + + +namespace pwiz { +namespace msdata { + + +using namespace std; +using boost::shared_ptr; + + +namespace { + + +class Reader_mzML : public Reader +{ + public: + + virtual bool accept(const std::string& filename, const std::string& head) const + { + istringstream iss(head); + return type(iss) != Type_Unknown; + } + + virtual void read(const std::string& filename, const std::string& head, MSData& result) const + { + shared_ptr<istream> is(new ifstream(filename.c_str(), ios::binary)); + if (!is.get() || !*is) + throw runtime_error(("[MSDataFile::Reader_mzML] Unable to open file " + filename).c_str()); + + switch (type(*is)) + { + case Type_mzML: + { + Serializer_mzML::Config config; + config.indexed = false; + Serializer_mzML serializer(config); + serializer.read(is, result); + break; + } + case Type_mzML_Indexed: + { + Serializer_mzML serializer; + serializer.read(is, result); + break; + } + case Type_Unknown: + default: + { + throw runtime_error("[MSDataFile::Reader_mzML] This isn't happening."); + } + } + } + + private: + + enum Type { Type_mzML, Type_mzML_Indexed, Type_Unknown }; + + Type type(istream& is) const + { + is.seekg(0); + + string buffer; + is >> buffer; + + if (buffer != "<?xml") + return Type_Unknown; + + getline(is, buffer); + is >> buffer; + + if (buffer == "<indexedmzML") + return Type_mzML_Indexed; + else if (buffer == "<mzML") + return Type_mzML; + else + return Type_Unknown; + } +}; + + +class Reader_mzXML : public Reader +{ + virtual bool accept(const std::string& filename, const std::string& head) const + { + istringstream iss(head); + + string buffer; + iss >> buffer; + + if (buffer != "<?xml") return false; + + getline(iss, buffer); + iss >> buffer; + + return (buffer=="<mzXML" || buffer=="<msRun"); + } + + virtual void read(const std::string& filename, const std::string& head, MSData& result) const + { + shared_ptr<istream> is(new ifstream(filename.c_str(), ios::binary)); + if (!is.get() || !*is) + throw runtime_error(("[MSDataFile::Reader_mzXML] Unable to open file " + filename).c_str()); + + try + { + // assume there is a scan index + Serializer_mzXML serializer; + serializer.read(is, result); + return; + } + catch (SpectrumList_mzXML::index_not_found&) + {} + + // error looking for index -- try again, but generate index + is->seekg(0); + Serializer_mzXML::Config config; + config.indexed = false; + Serializer_mzXML serializer(config); + serializer.read(is, result); + return; + } +}; + + +} // namespace + + +/// default Reader list +DefaultReaderList::DefaultReaderList() +{ + push_back(ReaderPtr(new Reader_mzML)); + push_back(ReaderPtr(new Reader_mzXML)); + + #ifndef PWIZ_NO_READER_RAW + push_back(ReaderPtr(new Reader_RAW)); + #endif +}; + + +} // namespace msdata +} // namespace pwiz + + Property changes on: trunk/pwiz/pwiz/msdata/DefaultReaderList.cpp ___________________________________________________________________ Name: svn:executable + * Added: trunk/pwiz/pwiz/msdata/DefaultReaderList.hpp =================================================================== --- trunk/pwiz/pwiz/msdata/DefaultReaderList.hpp (rev 0) +++ trunk/pwiz/pwiz/msdata/DefaultReaderList.hpp 2008-03-31 23:32:32 UTC (rev 74) @@ -0,0 +1,48 @@ +// +// DefaultReaderList.hpp +// +// +// Original author: Darren Kessner <Dar...@cs...> +// +// Copyright 2008 Spielberg Family Center for Applied Proteomics +// Cedars-Sinai Medical Center, Los Angeles, California 90048 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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 _DEFAULTREADERLIST_HPP_ +#define _DEFAULTREADERLIST_HPP_ + + +#include "Reader.hpp" + + +namespace pwiz { +namespace msdata { + + +/// default Reader list +class DefaultReaderList : public ReaderList +{ + public: + DefaultReaderList(); +}; + + +} // namespace msdata +} // namespace pwiz + + +#endif// _DEFAULTREADERLIST_HPP_ + Property changes on: trunk/pwiz/pwiz/msdata/DefaultReaderList.hpp ___________________________________________________________________ Name: svn:executable + * Modified: trunk/pwiz/pwiz/msdata/Jamfile =================================================================== --- trunk/pwiz/pwiz/msdata/Jamfile 2008-03-27 23:36:24 UTC (rev 73) +++ trunk/pwiz/pwiz/msdata/Jamfile 2008-03-31 23:32:32 UTC (rev 74) @@ -35,6 +35,7 @@ BinaryDataEncoder.cpp CVParam.cpp CVTranslator.cpp + DefaultReaderList.cpp Diff.cpp IO.cpp LegacyAdapter.cpp @@ -46,6 +47,7 @@ SpectrumList_mzML.cpp SpectrumList_mzXML.cpp RAMPAdapter.cpp + Reader.cpp Reader_RAW.cpp References.cpp ../util @@ -107,6 +109,7 @@ unit-test Serializer_mzXML_Test : Serializer_mzXML_Test.cpp examples.cpp pwiz_msdata ; # Serializer_mzXML.cpp examples.cpp SpectrumList_mzXML.cpp Serializer_mzML.cpp SpectrumList_mzML.cpp IO.cpp References.cpp Diff.cpp BinaryDataEncoder.cpp LegacyAdapter.cpp CVTranslator.cpp MSData.cpp cv.cpp ../util ../minimxml ; unit-test MSDataFileTest : MSDataFileTest.cpp examples.cpp pwiz_msdata ; unit-test RAMPAdapterTest : RAMPAdapterTest.cpp examples.cpp pwiz_msdata ; +unit-test ReaderTest : ReaderTest.cpp pwiz_msdata ; # special run target for Reader_RAW_Test, which needs external data Modified: trunk/pwiz/pwiz/msdata/MSDataFile.cpp =================================================================== --- trunk/pwiz/pwiz/msdata/MSDataFile.cpp 2008-03-27 23:36:24 UTC (rev 73) +++ trunk/pwiz/pwiz/msdata/MSDataFile.cpp 2008-03-31 23:32:32 UTC (rev 74) @@ -25,10 +25,8 @@ #include "TextWriter.hpp" #include "Serializer_mzML.hpp" #include "Serializer_mzXML.hpp" -#include "SpectrumList_mzXML.hpp" -#include "Reader_RAW.hpp" -#include <iostream> -#include <fstream> +#include "DefaultReaderList.hpp" +#include <fstream> #include <stdexcept> @@ -43,163 +41,8 @@ namespace { -class Reader_mzML : public MSDataFile::Reader +void readFile(const string& filename, MSData& msd, const Reader& reader) { - public: - - virtual bool accept(const std::string& filename, const std::string& head) const - { - istringstream iss(head); - return type(iss) != Type_Unknown; - } - - virtual void read(const std::string& filename, MSData& result) const - { - shared_ptr<istream> is(new ifstream(filename.c_str(), ios::binary)); - if (!is.get() || !*is) - throw runtime_error(("[MSDataFile::Reader_mzML] Unable to open file " + filename).c_str()); - - switch (type(*is)) - { - case Type_mzML: - { - Serializer_mzML::Config config; - config.indexed = false; - Serializer_mzML serializer(config); - serializer.read(is, result); - break; - } - case Type_mzML_Indexed: - { - Serializer_mzML serializer; - serializer.read(is, result); - break; - } - case Type_Unknown: - default: - { - throw runtime_error("[MSDataFile::Reader_mzML] This isn't happening."); - } - } - } - - private: - - enum Type { Type_mzML, Type_mzML_Indexed, Type_Unknown }; - - Type type(istream& is) const - { - is.seekg(0); - - string buffer; - is >> buffer; - - if (buffer != "<?xml") - return Type_Unknown; - - getline(is, buffer); - is >> buffer; - - if (buffer == "<indexedmzML") - return Type_mzML_Indexed; - else if (buffer == "<mzML") - return Type_mzML; - else - return Type_Unknown; - } -}; - - -class Reader_mzXML : public MSDataFile::Reader -{ - virtual bool accept(const std::string& filename, const std::string& head) const - { - istringstream iss(head); - - string buffer; - iss >> buffer; - - if (buffer != "<?xml") return false; - - getline(iss, buffer); - iss >> buffer; - - return (buffer=="<mzXML" || buffer=="<msRun"); - } - - virtual void read(const std::string& filename, MSData& result) const - { - shared_ptr<istream> is(new ifstream(filename.c_str(), ios::binary)); - if (!is.get() || !*is) - throw runtime_error(("[MSDataFile::Reader_mzXML] Unable to open file " + filename).c_str()); - - try - { - // assume there is a scan index - Serializer_mzXML serializer; - serializer.read(is, result); - return; - } - catch (SpectrumList_mzXML::index_not_found&) - {} - - // error looking for index -- try again, but generate index - is->seekg(0); - Serializer_mzXML::Config config; - config.indexed = false; - Serializer_mzXML serializer(config); - serializer.read(is, result); - return; - } -}; - - -// static instances of default readers -Reader_mzML reader_mzML_; -Reader_mzXML reader_mzXML_; -#ifndef PWIZ_NO_READER_RAW -Reader_RAW reader_RAW_; -#endif - -vector<const MSDataFile::Reader*> readers_; - - -void initializeDefaultReaders() -{ - // initialize default Readers if we don't have anything registered - - if (readers_.empty()) - { - readers_.push_back(&reader_mzML_); - readers_.push_back(&reader_mzXML_); -#ifndef PWIZ_NO_READER_RAW - readers_.push_back(&reader_RAW_); -#endif - } -} - - -} // namespace - - -void MSDataFile::registerReader(const Reader& reader) -{ - readers_.clear(); - readers_.push_back(&reader); -} - - -void MSDataFile::clearReader() -{ - readers_.clear(); -} - - -namespace { - - -void readFile(const string& filename, MSData& msd) -{ // peek at head of file ifstream is(filename.c_str(), ios::binary); @@ -210,30 +53,31 @@ is.read(&head[0], (std::streamsize)head.size()); is.close(); - // delegate to Readers + if (!reader.accept(filename, head)) + throw runtime_error("[MSDataFile::readFile()] Unsupported file format."); - initializeDefaultReaders(); + reader.read(filename, head, msd); +} - for (vector<const MSDataFile::Reader*>::const_iterator it=readers_.begin(); - it!=readers_.end(); ++it) - { - if ((*it)->accept(filename, head)) - { - (*it)->read(filename, msd); - return; - } - } - throw runtime_error("Unsupported file format."); -} +shared_ptr<DefaultReaderList> defaultReaderList_; } // namespace -MSDataFile::MSDataFile(const string& filename) +MSDataFile::MSDataFile(const string& filename, const Reader* reader) { - readFile(filename, *this); + if (reader) + { + readFile(filename, *this, *reader); + } + else + { + if (!defaultReaderList_.get()) + defaultReaderList_ = shared_ptr<DefaultReaderList>(new DefaultReaderList); + readFile(filename, *this, *defaultReaderList_); + } } Modified: trunk/pwiz/pwiz/msdata/MSDataFile.hpp =================================================================== --- trunk/pwiz/pwiz/msdata/MSDataFile.hpp 2008-03-27 23:36:24 UTC (rev 73) +++ trunk/pwiz/pwiz/msdata/MSDataFile.hpp 2008-03-31 23:32:32 UTC (rev 74) @@ -26,12 +26,9 @@ #include "MSData.hpp" +#include "Reader.hpp" #include "BinaryDataEncoder.hpp" -#if (!defined(_MSC_VER) && !defined(PWIZ_NO_READER_RAW)) -// Xcalibur DLL usage is msvc only - mingw doesn't provide com support -#define PWIZ_NO_READER_RAW -#endif namespace pwiz { namespace msdata { @@ -40,32 +37,11 @@ /// MSData object plus file I/O struct MSDataFile : public MSData { - /// interface for file readers - class Reader - { - public: + /// constructs MSData object backed by file; + /// reader==0 -> use DefaultReaderList + MSDataFile(const std::string& filename, + const Reader* reader = 0); - /// return true iff Reader can handle the file; Reader may filter based on - /// filename or on the buffer holding the head of the file - virtual bool accept(const std::string& filename, const std::string& head) const = 0; - - /// fill in the MSData structure -- generally this will amount to: - /// 1) fill in the file-level metadata - /// 2) instantiate a SpectrumList (held by MSData::run.spectrumListPtr) - virtual void read(const std::string& filename, MSData& result) const = 0; - - virtual ~Reader(){} - }; - - /// runtime Reader registration (replaces default Readers) - static void registerReader(const Reader& reader); - - /// clear registered Reader (use default Readers) - static void clearReader(); - - /// constructs MSData object backed by file - MSDataFile(const std::string& filename); - /// data format for write() enum Format {Format_Text, Format_mzML, Format_mzXML}; Modified: trunk/pwiz/pwiz/msdata/MSDataFileTest.cpp =================================================================== --- trunk/pwiz/pwiz/msdata/MSDataFileTest.cpp 2008-03-27 23:36:24 UTC (rev 73) +++ trunk/pwiz/pwiz/msdata/MSDataFileTest.cpp 2008-03-31 23:32:32 UTC (rev 74) @@ -144,7 +144,7 @@ 'i', '\0', 'g', '\0', 'a', '\0', 'n', '\0'}; -class TestReader : public MSDataFile::Reader +class TestReader : public Reader { public: @@ -163,7 +163,7 @@ return true; } - virtual void read(const std::string& filename, MSData& result) const + virtual void read(const std::string& filename, const std::string& head, MSData& result) const { count++; } @@ -180,13 +180,10 @@ os.write(rawHeader_, 18); os.close(); - // register our reader + // open the file with our Reader TestReader reader; - MSDataFile::registerReader(reader); + MSDataFile msd(filename, &reader); - // open the file - MSDataFile msd(filename); - // verify that our reader got called properly unit_assert(reader.count == 2); Added: trunk/pwiz/pwiz/msdata/Reader.cpp =================================================================== --- trunk/pwiz/pwiz/msdata/Reader.cpp (rev 0) +++ trunk/pwiz/pwiz/msdata/Reader.cpp 2008-03-31 23:32:32 UTC (rev 74) @@ -0,0 +1,59 @@ +// +// Reader.cpp +// +// +// Original author: Darren Kessner <Dar...@cs...> +// +// Copyright 2008 Spielberg Family Center for Applied Proteomics +// Cedars-Sinai Medical Center, Los Angeles, California 90048 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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 "Reader.hpp" + + +namespace pwiz { +namespace msdata { + + +using namespace std; + + +bool ReaderList::accept(const string& filename, const string& head) const +{ + for (const_iterator it=begin(); it!=end(); ++it) + if ((*it)->accept(filename, head)) return true; + + return false; +} + + +void ReaderList::read(const string& filename, const string& head, MSData& result) const +{ + for (const_iterator it=begin(); it!=end(); ++it) + if ((*it)->accept(filename, head)) + { + (*it)->read(filename, head, result); + return; + } + + throw runtime_error(("[ReaderList::read()] No child accepted file " + + filename).c_str()); +} + + +} // namespace msdata +} // namespace pwiz + Property changes on: trunk/pwiz/pwiz/msdata/Reader.cpp ___________________________________________________________________ Name: svn:executable + * Added: trunk/pwiz/pwiz/msdata/Reader.hpp =================================================================== --- trunk/pwiz/pwiz/msdata/Reader.hpp (rev 0) +++ trunk/pwiz/pwiz/msdata/Reader.hpp 2008-03-31 23:32:32 UTC (rev 74) @@ -0,0 +1,105 @@ +// +// Reader.hpp +// +// +// Original author: Darren Kessner <Dar...@cs...> +// +// Copyright 2008 Spielberg Family Center for Applied Proteomics +// Cedars-Sinai Medical Center, Los Angeles, California 90048 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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 _READER_HPP_ +#define _READER_HPP_ + + +#include "MSData.hpp" +#include <string> + + +namespace pwiz { +namespace msdata { + + +/// interface for file readers +class Reader +{ + public: + + /// return true iff Reader can handle the file; + /// Reader may filter based on filename and/or head of the file + virtual bool accept(const std::string& filename, + const std::string& head) const = 0; + + /// fill in the MSData structure + virtual void read(const std::string& filename, + const std::string& head, + MSData& result) const = 0; + + virtual ~Reader(){} +}; + + +typedef boost::shared_ptr<Reader> ReaderPtr; + + +/// +/// Reader container (composite pattern). +/// +/// The template get<reader_type>() gives access to child Readers by type, to facilitate +/// Reader-specific configuration at runtime. +/// +class ReaderList : public Reader, + public std::vector<ReaderPtr> +{ + public: + + /// returns true iff some child accepts + virtual bool accept(const std::string& filename, + const std::string& head) const; + + /// delegates to first child that accepts + virtual void read(const std::string& filename, + const std::string& head, + MSData& result) const; + + /// returns pointer to Reader of the specified type + template <typename reader_type> + reader_type* get() + { + for (iterator it=begin(); it!=end(); ++it) + { + reader_type* p = dynamic_cast<reader_type*>(it->get()); + if (p) return p; + } + + return 0; + } + + /// returns const pointer to Reader of the specified type + template <typename reader_type> + const reader_type* get() const + { + return const_cast<ReaderList*>(this)->get<reader_type>(); + } +}; + + +} // namespace msdata +} // namespace pwiz + + +#endif // _READER_HPP_ + Property changes on: trunk/pwiz/pwiz/msdata/Reader.hpp ___________________________________________________________________ Name: svn:executable + * Added: trunk/pwiz/pwiz/msdata/ReaderTest.cpp =================================================================== --- trunk/pwiz/pwiz/msdata/ReaderTest.cpp (rev 0) +++ trunk/pwiz/pwiz/msdata/ReaderTest.cpp 2008-03-31 23:32:32 UTC (rev 74) @@ -0,0 +1,208 @@ +// +// ReaderTest.cpp +// +// +// Original author: Darren Kessner <Dar...@cs...> +// +// Copyright 2008 Spielberg Family Center for Applied Proteomics +// Cedars-Sinai Medical Center, Los Angeles, California 90048 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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 "Reader.hpp" +#include "util/unit.hpp" +#include <iostream> + + +using namespace std; +using namespace pwiz::util; +using namespace pwiz::msdata; + + +ostream* os_ = 0; + + +class Reader1 : public Reader +{ + public: + + struct Config + { + string name; + mutable bool done; + Config() : name("default"), done(false) {} + }; + + Config config; + + virtual bool accept(const std::string& filename, const std::string& head) const + { + bool result = (filename == "1"); + if (os_) *os_ << "Reader1::accept(): " << boolalpha << result << endl; + return result; + } + + virtual void read(const std::string& filename, + const std::string& head, + MSData& result) const + { + if (os_) *os_ << "Reader1::read()\n"; + config.done = true; + } +}; + + +class Reader2 : public Reader +{ + public: + + struct Config + { + string color; + mutable bool done; + Config() : color("orange"), done(false) {} + }; + + Config config; + + virtual bool accept(const std::string& filename, const std::string& head) const + { + bool result = (filename == "2"); + if (os_) *os_ << "Reader2::accept(): " << boolalpha << result << endl; + return result; + } + + virtual void read(const std::string& filename, + const std::string& head, + MSData& result) const + { + if (os_) *os_ << "Reader2::read()\n"; + config.done = true; + } +}; + + +void testGet() +{ + if (os_) *os_ << "testGet()\n"; + + ReaderList readers; + readers.push_back(ReaderPtr(new Reader1)); + readers.push_back(ReaderPtr(new Reader2)); + + unit_assert(readers.size() == 2); + + Reader1* reader1 = readers.get<Reader1>(); + unit_assert(reader1); + if (os_) *os_ << "reader1 config: " << reader1->config.name << endl; + unit_assert(reader1->config.name == "default"); + reader1->config.name = "raw"; + if (os_) *os_ << "reader1 config: " << reader1->config.name << endl; + unit_assert(reader1->config.name == "raw"); + + Reader2* reader2 = readers.get<Reader2>(); + unit_assert(reader2); + if (os_) *os_ << "reader2 config: " << reader2->config.color << endl; + unit_assert(reader2->config.color == "orange"); + reader2->config.color = "purple"; + if (os_) *os_ << "reader2 config: " << reader2->config.color << endl; + unit_assert(reader2->config.color == "purple"); + + const ReaderList& const_readers = readers; + const Reader2* constReader2 = const_readers.get<Reader2>(); + unit_assert(constReader2); + if (os_) *os_ << "constReader2 config: " << constReader2->config.color << endl; + + if (os_) *os_ << endl; +} + + +void testAccept() +{ + if (os_) *os_ << "testAccept()\n"; + + ReaderList readers; + readers.push_back(ReaderPtr(new Reader1)); + readers.push_back(ReaderPtr(new Reader2)); + + if (os_) *os_ << "accept 1:\n"; + unit_assert(readers.accept("1", "head")); + if (os_) *os_ << "accept 2:\n"; + unit_assert(readers.accept("2", "head")); + if (os_) *os_ << "accept 3:\n"; + unit_assert(!readers.accept("3", "head")); + + if (os_) *os_ << endl; +} + + +void testRead() +{ + if (os_) *os_ << "testRead()\n"; + + ReaderList readers; + readers.push_back(ReaderPtr(new Reader1)); + readers.push_back(ReaderPtr(new Reader2)); + + MSData msd; + + // note: composite pattern with accept/read will cause two calls + // to accept(); the alternative is to maintain state between accept() + // and read(), which opens possibility for misuse. + + unit_assert(readers.get<Reader1>()->config.done == false); + if (readers.accept("1", "head")) + readers.read("1", "head", msd); + unit_assert(readers.get<Reader1>()->config.done == true); + + readers.get<Reader1>()->config.done = false; + unit_assert(readers.get<Reader2>()->config.done == false); + if (readers.accept("2", "head")) + readers.read("2", "head", msd); + unit_assert(readers.get<Reader1>()->config.done == false); + unit_assert(readers.get<Reader2>()->config.done == true); + + if (os_) *os_ << endl; +} + + +void test() +{ + testGet(); + testAccept(); + testRead(); +} + + +int main(int argc, char* argv[]) +{ + try + { + if (argc==2 && !strcmp(argv[1],"-v")) os_ = &cout; + test(); + return 0; + } + catch (exception& e) + { + cerr << e.what() << endl; + } + catch (...) + { + cerr << "Caught unknown exception.\n"; + } + + return 1; +} + Property changes on: trunk/pwiz/pwiz/msdata/ReaderTest.cpp ___________________________________________________________________ Name: svn:executable + * Modified: trunk/pwiz/pwiz/msdata/Reader_RAW.cpp =================================================================== --- trunk/pwiz/pwiz/msdata/Reader_RAW.cpp 2008-03-27 23:36:24 UTC (rev 73) +++ trunk/pwiz/pwiz/msdata/Reader_RAW.cpp 2008-03-31 23:32:32 UTC (rev 74) @@ -23,6 +23,7 @@ #include "Reader_RAW.hpp" + namespace { // helper function used by both forms (real and stubbed) of ReadRAW bool _hasRAWHeader(const std::string& head) @@ -41,6 +42,8 @@ return true; } } // namespace + + #ifndef PWIZ_NO_READER_RAW #include "CVTranslator.hpp" #include "rawfile/RawFile.h" @@ -398,7 +401,9 @@ } -void Reader_RAW::read(const string& filename, MSData& result) const +void Reader_RAW::read(const string& filename, + const string& head, + MSData& result) const { // initialize RawFileLibrary Modified: trunk/pwiz/pwiz/msdata/Reader_RAW.hpp =================================================================== --- trunk/pwiz/pwiz/msdata/Reader_RAW.hpp 2008-03-27 23:36:24 UTC (rev 73) +++ trunk/pwiz/pwiz/msdata/Reader_RAW.hpp 2008-03-31 23:32:32 UTC (rev 74) @@ -25,19 +25,32 @@ #define _READER_RAW_HPP_ -#include "MSDataFile.hpp" +#include "Reader.hpp" +// Xcalibur DLL usage is msvc only - mingw doesn't provide com support +#if (!defined(_MSC_VER) && !defined(PWIZ_NO_READER_RAW)) +#define PWIZ_NO_READER_RAW +#endif + + namespace pwiz { namespace msdata { -class Reader_RAW : public MSDataFile::Reader +class Reader_RAW : public Reader { public: - virtual bool accept(const std::string& filename, const std::string& head) const; - virtual void read(const std::string& filename, MSData& result) const; - static bool hasRAWHeader(const std::string& head); // checks header for "Finnigan" wide char string + + virtual bool accept(const std::string& filename, + const std::string& head) const; + + virtual void read(const std::string& filename, + const std::string& head, + MSData& result) const; + + /// checks header for "Finnigan" wide char string + static bool hasRAWHeader(const std::string& head); }; Modified: trunk/pwiz/pwiz/msdata/Reader_RAW_Test.cpp =================================================================== --- trunk/pwiz/pwiz/msdata/Reader_RAW_Test.cpp 2008-03-27 23:36:24 UTC (rev 73) +++ trunk/pwiz/pwiz/msdata/Reader_RAW_Test.cpp 2008-03-31 23:32:32 UTC (rev 74) @@ -68,7 +68,7 @@ Reader_RAW reader; MSData msd; - reader.read(filename, msd); + reader.read(filename, "dummy", msd); // make assertions about msd This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |