1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
|
// ==========================================================================
// SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2026, Knut Reinert, FU Berlin
// 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 Knut Reinert or the FU Berlin 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 COPYRIGHT HOLDERS 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 KNUT REINERT OR THE FU BERLIN 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.
//
// ==========================================================================
// Author: David Weese <david.weese@fu-berlin.de>
// ==========================================================================
// Base class for high level access to formatted files. It supports different
// file formats, compression, auto detection from file extensions and content
// as well as reading/writing from/to stdin/stdout.
// ==========================================================================
#ifndef SEQAN_STREAM_SMART_FILE_H_
#define SEQAN_STREAM_SMART_FILE_H_
namespace seqan2 {
// ============================================================================
// Forwards
// ============================================================================
/*!
* @defgroup FileFormats File Formats
* @brief Tags for identifying file formats.
*/
/*!
* @mfn FormattedFile#FileFormat
* @brief Metafunction for retrieving the file format type of a formatted file.
*
* @signature FileFormat<TFormattedFile>::Type;
*
* @tparam TFormattedFile The formatted file type to query for its file format type.
* @return Type The resulting @link FileFormats @endlink file formats type.
*/
template <typename TFormattedFile>
struct FileFormat;
template <typename TFormattedFile, typename TStorageSpec>
struct FormattedFileContext;
template <typename TObject, typename TStorageSpec>
struct StorageSwitch;
// ============================================================================
// Concepts
// ============================================================================
// ----------------------------------------------------------------------------
// Concept FormattedFileHeaderConcept
// ----------------------------------------------------------------------------
/*!
* @concept FormattedFileHeaderConcept
* @extends DefaultConstructibleConcept
* @extends CopyConstructibleConcept
* @extends AssignableConcept
* @headerfile <seqan/stream.h>
* @brief Concept for header of formatted files.
* @signature concept FormattedFileHeaderConcept;
*/
// ----------------------------------------------------------------------------
// Concept FormattedFileRecordConcept
// ----------------------------------------------------------------------------
/*!
* @concept FormattedFileRecordConcept
* @extends DefaultConstructibleConcept
* @extends CopyConstructibleConcept
* @extends AssignableConcept
* @headerfile <seqan/stream.h>
* @brief Concept for record of formatted files.
* @signature concept FormattedFileRecordConcept;
*/
// ============================================================================
// Classes
// ============================================================================
// ----------------------------------------------------------------------------
// Class FormattedFile
// ----------------------------------------------------------------------------
/*!
* @class FormattedFile
* @headerfile <seqan/stream.h>
* @brief Base class for formatted file I/O.
*
* @signature template <typename TFileFormat, typename TDirection[, typename TSpec]>
* struct FormattedFile;
*
* @tparam TFileFormat A type specifying the file format.
* @tparam TDirection The direction of the file, one of @link DirectionTags#Input Input
* @endlink or @link DirectionTags#Output @endlink.
* @tparam TSpec A tag for the specialization, defauls to <tt>void</tt>.
*
* FormattedFile provides the following basic I/O operations on formatted files:
*
* <ul>
* <li>Open a file given its filename or attach to an existing stream like stdin/stdout.</li>
* <li>Guess the file format from the file content or filename extension.</li>
* <li>Set the file format manually.</li>
* <li>Access compressed or uncompressed files transparently.</li>
* </ul>
*
* FormattedFile encapsulates a VirtualStream and provides access to its @link StreamConcept#DirectionIterator @endlink.
* Each instance of FormattedFile keeps a @link FormattedFile#FormattedFileContext @endlink while reading or writing the formatted file.
*/
/*!
* @class FormattedFileIn
* @headerfile <seqan/stream.h>
* @brief Base class for reading formatted files.
* @signature typedef FormattedFile<TFileFormat, Input, TSpec> FormattedFileIn;
* @extends FormattedFile
*
* A formatted input file can write @link FormattedFileHeaderConcept Header @endlink
* and @link FormattedFileRecordConcept Records @endlink.
*/
/*!
* @fn FormattedFileIn#readHeader
* @brief Read one @link FormattedFileHeaderConcept @endlink from a @link FormattedFileIn @endlink object.
*
* @signature void readHeader(fileIn, header);
*
* @param[in,out] fileIn The @link FormattedFileIn @endlink object to read from.
* @param[in] header The @link FormattedFileHeaderConcept @endlink to read.
*
* @throw IOError On low-level I/O errors.
* @throw ParseError On high-level file format errors.
*/
/*!
* @fn FormattedFileIn#readRecord
* @brief Read one @link FormattedFileRecordConcept @endlink from a @link FormattedFileIn @endlink object.
*
* @signature void readRecord(fileIn, record);
*
* @param[in,out] fileIn The @link FormattedFileIn @endlink object to read from.
* @param[in] record The @link FormattedFileRecordConcept @endlink to read.
*
* @throw IOError On low-level I/O errors.
* @throw ParseError On high-level file format errors.
*/
/*!
* @class FormattedFileOut
* @headerfile <seqan/stream.h>
* @brief Base class for writing formatted files.
* @signature typedef FormattedFile<TFileFormat, Output, TSpec> FormattedFileOut;
* @extends FormattedFile
*
* A formatted output file can write @link FormattedFileHeaderConcept Header @endlink
* and @link FormattedFileRecordConcept Records @endlink.
*/
/*!
* @fn FormattedFileOut#writeHeader
* @brief Write one @link FormattedFileHeaderConcept @endlink to a @link FormattedFileOut @endlink object.
*
* @signature void writeHeader(fileOut, header);
*
* @param[in,out] fileOut The @link FormattedFileOut @endlink object to write into.
* @param[in] header The @link FormattedFileHeaderConcept @endlink to write.
*
* @throw IOError On low-level I/O errors.
* @throw ParseError On high-level file format errors.
*/
/*!
* @fn FormattedFileOut#writeRecord
* @brief Write one @link FormattedFileRecordConcept @endlink to a @link FormattedFileOut @endlink object.
*
* @signature void writeRecord(fileOut, record);
*
* @param[in,out] fileOut The @link FormattedFileOut @endlink object to write into.
* @param[in] record The @link FormattedFileRecordConcept @endlink to write.
*
* @throw IOError On low-level I/O errors.
* @throw ParseError On high-level file format errors.
*/
template <typename TFileFormat, typename TDirection, typename TSpec = void>
struct FormattedFile
{
typedef VirtualStream<char, TDirection> TStream;
typedef typename Iterator<TStream, TDirection>::Type TIter;
typedef typename FileFormat<FormattedFile>::Type TFileFormats;
typedef typename FormattedFileContext<FormattedFile, Owner<> >::Type TOwnerContext;
typedef typename FormattedFileContext<FormattedFile, Dependent<> >::Type TDependentContext;
TStream stream;
TIter iter;
TFileFormats format;
TOwnerContext data_context;
TDependentContext context;
/*!
* @fn FormattedFile#FormattedFile
* @brief Provides default construction.
*
* @signature FormattedFile::FormattedFile();
* @signature FormattedFile::FormattedFile(fileName[, openMode]);
* @signature FormattedFile::FormattedFile(stream);
* @signature FormattedFile::FormattedFile(other);
* @signature FormattedFile::FormattedFile(otherContext);
* @signature FormattedFile::FormattedFile(otherContext, fileName[, openMode]);
* @signature FormattedFile::FormattedFile(otherContext, stream);
*
* @param[in] fileName Path to file to open, <tt>char const *</tt>.
* @param[in] openMode Optionally, the file open mode, default obtained from <tt>TDirection</tt>.
* @param[in] stream A <tt>std::basic_istream<></tt> to read from or <tt>std::basic_ostream<></tt>
* to write to, depending on <tt>TDirection</tt>.
* @param[in] other A second FormattedFile, this FormattedFile's dependent context will depend on <tt>other</tt>'s
* dependent context.
* @param[in] otherContext The dependent context of another FormattedFile, this FormattedFile's dependent context will depend on <tt>otherContext</tt>.
*
* @throw IOError The variants that accept the <tt>fileName</tt> parameter throw an exception of this
* type in case opening the file fails.
*/
FormattedFile() : context(data_context)
{}
// filename based c'tors
explicit FormattedFile(const char * fileName, int openMode = DefaultOpenMode<FormattedFile>::VALUE) :
context(data_context)
{
_open(*this, fileName, openMode, True());
}
// stream based c'tors
template <typename TValue>
explicit
FormattedFile(std::basic_istream<TValue> &istream,
SEQAN_CTOR_ENABLE_IF(And<IsSameType<TDirection, Input>, IsSameType<TValue, char> >)) :
context(data_context)
{
_open(*this, istream, _mapFileFormatToCompressionFormat(format), True());
ignoreUnusedVariableWarning(dummy);
}
template <typename TValue, typename TFormat>
FormattedFile(std::basic_ostream<TValue> &ostream,
TFormat const &format,
SEQAN_CTOR_ENABLE_IF(And<IsSameType<TDirection, Output>, IsSameType<TValue, char> >)) :
context(data_context)
{
bool success = open(*this, ostream, format);
ignoreUnusedVariableWarning(dummy);
ignoreUnusedVariableWarning(success);
SEQAN_ASSERT(success);
}
// now everything given another context
explicit
FormattedFile(TDependentContext &otherCtx) :
context(otherCtx)
{}
FormattedFile(FormattedFile const & other) :
context(other.context)
{}
FormattedFile(TDependentContext &otherCtx, const char *fileName, int openMode = DefaultOpenMode<FormattedFile>::VALUE) :
context(otherCtx)
{
_open(*this, fileName, openMode, True());
}
template <typename TValue>
explicit
FormattedFile(TDependentContext &otherCtx,
std::basic_istream<TValue> &istream,
SEQAN_CTOR_ENABLE_IF(And<IsSameType<TDirection, Input>, IsSameType<TValue, char> >)) :
context(otherCtx)
{
_open(*this, istream, _mapFileFormatToCompressionFormat(format), True());
ignoreUnusedVariableWarning(dummy);
}
template <typename TValue, typename TFormat>
FormattedFile(TDependentContext &otherCtx,
std::basic_ostream<TValue> &ostream,
TFormat const &format,
SEQAN_CTOR_ENABLE_IF(And<IsSameType<TDirection, Output>, IsSameType<TValue, char> >)) :
context(otherCtx)
{
bool success = open(*this, ostream, format);
ignoreUnusedVariableWarning(dummy);
ignoreUnusedVariableWarning(success);
SEQAN_ASSERT(success);
}
~FormattedFile()
{
close(*this);
}
/*!
* @fn FormattedFile::operatorTDependentContext FormattedFile::operator TDependentContext
* @brief Allows conversion to a dependent context for the FormattedFile
* @signature TDependentContext & FormattedFile::operator TDependentContext();
*
* @return TDependentContext The dependent context of this FormattedFile.
*/
operator TDependentContext & ()
{
return context;
}
/*!
* @fn FormattedFile::getFileExtensions
* @brief Static function that returns a list of allowed file format extension.
*
* @signature TExtensionVector getFileExtensions()
*
* @return TExtensionVector A <tt>std::vector<std::string></tt> with the allowed file extensions.
*/
static std::vector<std::string>
getFileExtensions()
{
std::vector<std::string> extensions;
_getCompressionExtensions(extensions,
TFileFormats(),
CompressedFileTypes(),
false);
return extensions;
}
};
// ============================================================================
// Metafunctions
// ============================================================================
// ----------------------------------------------------------------------------
// Metafunction IsInputFile
// ----------------------------------------------------------------------------
template <typename T>
struct IsInputFile : False
{};
template <typename TFormat, typename TSpec>
struct IsInputFile<FormattedFile<TFormat, Input, TSpec> > : True
{};
// ----------------------------------------------------------------------------
// Metafunction IsOutputFile
// ----------------------------------------------------------------------------
template <typename T>
struct IsOutputFile : False
{};
template <typename TFormat, typename TSpec>
struct IsOutputFile<FormattedFile<TFormat, Output, TSpec> > : True
{};
// ----------------------------------------------------------------------------
// Metafunction DirectionIterator
// ----------------------------------------------------------------------------
template <typename TFileFormat, typename TDirection, typename TSpec>
struct DirectionIterator<FormattedFile<TFileFormat, TDirection, TSpec>, TDirection>
{
typedef typename FormattedFile<TFileFormat, TDirection, TSpec>::TIter Type;
};
// ----------------------------------------------------------------------------
// Metafunction FormattedFileContext
// ----------------------------------------------------------------------------
// FormattedFile holds a Context either as owner or dependent (see StorageSwitch below).
// Note that the FormattedFile class contains twice the context: both as owner and dependent.
// NOTE(esiragusa): A statically-typed Holder should abstract this pattern.
/*!
* @mfn FormattedFile#FormattedFileContext
* @brief Returns the context type for a FormattedFile.
*
* @signature FormattedFileContext<TFormattedFile, TStorageSpec>::Type
*
* @tparam TFormattedFile The FormattedFile to query.
* @tparam TStorageSpec The storage specification, passed as specialization to any @link StringSet @endlink
* contained in the context.
* @tparam Type The resulting FormattedFile context type.
*/
template <typename TFormattedFile, typename TStorageSpec>
struct FormattedFileContext
{
typedef Nothing Type;
};
template <typename TFormattedFile, typename TStorageSpec>
struct FormattedFileContext<TFormattedFile const, TStorageSpec>
{
typedef typename FormattedFileContext<TFormattedFile, TStorageSpec>::Type const Type;
};
// ----------------------------------------------------------------------------
// Metafunction StorageSwitch
// ----------------------------------------------------------------------------
// This metafunction is used to switch the relationship between the FormattedFile
// and its Context among aggregation and composition.
// NOTE(esiragusa): there was a more generic metafunction Member<> to do this.
// Composition (owner).
template <typename TObject, typename TStorageSpec>
struct StorageSwitch
{
typedef TObject Type;
};
// Aggregation (dependent).
template <typename TObject, typename TSpec>
struct StorageSwitch<TObject, Dependent<TSpec> >
{
typedef TObject * Type;
};
// ----------------------------------------------------------------------------
// Metafunction Value
// ----------------------------------------------------------------------------
template <typename TFileFormat, typename TDirection, typename TSpec>
struct Value<FormattedFile<TFileFormat, TDirection, TSpec> > :
Value<typename FormattedFile<TFileFormat, TDirection, TSpec>::TStream> {};
// ----------------------------------------------------------------------------
// Metafunction Position
// ----------------------------------------------------------------------------
template <typename TFileFormat, typename TDirection, typename TSpec>
struct Position<FormattedFile<TFileFormat, TDirection, TSpec> > :
Position<typename FormattedFile<TFileFormat, TDirection, TSpec>::TStream> {};
// ----------------------------------------------------------------------------
// Metafunction DefaultOpenMode
// ----------------------------------------------------------------------------
template <typename TFileFormat, typename TDirection, typename TSpec, typename TDummy>
struct DefaultOpenMode<FormattedFile<TFileFormat, TDirection, TSpec>, TDummy> :
DefaultOpenMode<typename FormattedFile<TFileFormat, TDirection, TSpec>::TStream, TDummy> {};
// ============================================================================
// Functions
// ============================================================================
// ----------------------------------------------------------------------------
// Helper Function _throwIf().
// ----------------------------------------------------------------------------
// Helper functions that reduce number of "uncaught exception" false positives in static analysis tools.
template <typename TException> void _throwIf(TException const & e, True const & /*tag*/) { SEQAN_THROW(e); }
template <typename TException> void _throwIf(TException const & /*e*/, False const & /*tag*/) { /*nop*/ }
// ----------------------------------------------------------------------------
// Function directionIterator()
// ----------------------------------------------------------------------------
template <typename TFileFormat, typename TDirection, typename TSpec>
inline typename FormattedFile<TFileFormat, TDirection, TSpec>::TIter &
directionIterator(FormattedFile<TFileFormat, TDirection, TSpec> & file, TDirection const &)
{
return file.iter;
}
// ----------------------------------------------------------------------------
// Function format()
// ----------------------------------------------------------------------------
/*!
* @fn FormattedFile#format
* @brief Return the format of a FormattedFile.
*
* @signature TFormat format(file);
*
* @param[in] file The FormattedFile to check.
* @return TFormat The type as returned from @link FormattedFile#FileFormat @endlink.
*/
template <typename TFileFormat, typename TDirection, typename TSpec>
inline typename FileFormat<FormattedFile<TFileFormat, TDirection, TSpec> >::Type &
format(FormattedFile<TFileFormat, TDirection, TSpec> & file)
{
return file.format;
}
template <typename TFileFormat, typename TDirection, typename TSpec>
inline typename FileFormat<FormattedFile<TFileFormat, TDirection, TSpec> >::Type const &
format(FormattedFile<TFileFormat, TDirection, TSpec> const & file)
{
return file.format;
}
// ----------------------------------------------------------------------------
// Function setFormat()
// ----------------------------------------------------------------------------
/*!
* @fn FormattedFile#setFormat
* @brief Set the format of a FormattedFile.
*
* @signature void setFormat(file, format);
*
* @param[in,out] file The FormattedFile to change.
* @param[in] format The @link FormattedFile#FileFormat @endlink to set.
*/
template <typename TFileFormat, typename TDirection, typename TSpec, typename TFormat>
inline void
setFormat(FormattedFile<TFileFormat, TDirection, TSpec> & file, TFormat format)
{
assign(file.format, format);
}
// ----------------------------------------------------------------------------
// Function guessFormat()
// ----------------------------------------------------------------------------
/*!
* @fn FormattedFile#guessFormat
* @brief Guess the format of an open FormattedFile.
*
* @signature bool guessFormat(file);
*
* @param[in,out] file The open FormattedFile for which the format is to be guessed.
*/
template <typename TFileFormat, typename TSpec>
inline bool guessFormat(FormattedFile<TFileFormat, Input, TSpec> & file)
{
return guessFormatFromStream(file.stream, file.format);
}
template <typename TFileFormat, typename TSpec>
inline bool guessFormat(FormattedFile<TFileFormat, Output, TSpec> &)
{
return true;
}
// ----------------------------------------------------------------------------
// _mapFileFormatToCompressionFormat
// ----------------------------------------------------------------------------
template <typename TFormat>
inline Nothing
_mapFileFormatToCompressionFormat(TFormat)
{
return Nothing();
}
template <typename TCompressedFileTypes>
inline void
_mapFileFormatToCompressionFormat(TagSelector<TCompressedFileTypes> &,
TagSelector<void> const &)
{}
template <typename TCompressedFileTypes, typename TTagList>
inline void
_mapFileFormatToCompressionFormat(TagSelector<TCompressedFileTypes> & result,
TagSelector<TTagList> const & format)
{
typedef typename TTagList::Type TFormat;
typedef typename TagSelector<TTagList>::Base TBase;
if (isEqual(format, TFormat()))
assign(result, _mapFileFormatToCompressionFormat(TFormat()));
else
_mapFileFormatToCompressionFormat(result, static_cast<TBase const &>(format));
}
template <typename TFileFormatList>
inline TagSelector<CompressedFileTypes>
_mapFileFormatToCompressionFormat(TagSelector<TFileFormatList> format)
{
TagSelector<CompressedFileTypes> compressionType;
_mapFileFormatToCompressionFormat(compressionType, format);
return compressionType;
}
template <typename TFormattedFile, typename TFormat>
inline void
_checkThatStreamOutputFormatIsSet(TFormattedFile const &, TFormat const &)
{}
template <typename TFileFormat, typename TSpec, typename TFileFormatList>
inline void
_checkThatStreamOutputFormatIsSet(FormattedFile<TFileFormat, Output, TSpec> const &, TagSelector<TFileFormatList> const &format)
{
if (value(format) < 0)
SEQAN_FAIL("FormattedFile: File format not specified, use setFormat().");
}
// ----------------------------------------------------------------------------
// Function open()
// ----------------------------------------------------------------------------
/*!
* @fn FormattedFile#open
* @brief Open a FormattedFile.
*
* @signature bool open(file, fileName, mode);
*
* @param[in,out] file The FormattedFile to open.
* @param[in] fileName The name of the file open.
* @param[in] mode The open mode: @link FileOpenMode @endlink.
* @return bool <tt>true</tt> in the case of success, <tt>false</tt> otherwise.
*/
template <typename TFileFormat, typename TDirection, typename TSpec,
typename TStream, typename TCompressionFormat, typename TThrowExceptions>
inline bool _open(FormattedFile<TFileFormat, TDirection, TSpec> & file,
TStream &stream,
TCompressionFormat const &compressionFormat,
TThrowExceptions = True())
{
if (!open(file.stream, stream, compressionFormat))
{
_throwIf(UnknownFileFormat(), TThrowExceptions());
return false;
}
if (!guessFormat(file))
{
_throwIf(UnknownFileFormat(), TThrowExceptions());
return false;
}
file.iter = directionIterator(file.stream, TDirection());
return true;
}
template <typename TFileFormat, typename TDirection, typename TSpec, typename TStream>
inline SEQAN_FUNC_ENABLE_IF(Is<StreamConcept<TStream> >, bool)
open(FormattedFile<TFileFormat, TDirection, TSpec> & file,
TStream &stream)
{
return _open(file, stream, _mapFileFormatToCompressionFormat(file.format), False());
}
template <typename TFileFormat, typename TSpec, typename TStream, typename TFormat_>
inline bool open(FormattedFile<TFileFormat, Output, TSpec> & file,
TStream &stream,
Tag<TFormat_> format)
{
setFormat(file, format);
return _open(file, stream, _mapFileFormatToCompressionFormat(file.format), False());
}
template <typename TFileFormat, typename TSpec, typename TStream, typename TFormats>
inline bool open(FormattedFile<TFileFormat, Output, TSpec> & file,
TStream &stream,
TagSelector<TFormats> format)
{
setFormat(file, format);
return _open(file, stream, _mapFileFormatToCompressionFormat(file.format), False());
}
// ----------------------------------------------------------------------------
// Function open(fileName)
// ----------------------------------------------------------------------------
template <typename TFileFormat, typename TDirection, typename TSpec, typename TThrowExceptions>
inline bool _open(FormattedFile<TFileFormat, TDirection, TSpec> & file,
const char *fileName,
int openMode = DefaultOpenMode<FormattedFile<TFileFormat, TDirection, TSpec> >::VALUE,
TThrowExceptions = True())
{
if (!open(file.stream, fileName, openMode))
{
_throwIf(FileOpenError(fileName), TThrowExceptions());
return false;
}
if (IsSameType<TDirection, Input>::VALUE && _isPipe(fileName))
{
if (!guessFormat(file))
{
// read from a pipe (without file extension)
_throwIf(UnknownFileFormat(), TThrowExceptions());
return false;
}
}
else
{
Prefix<const char *>::Type basename = _getUncompressedBasename(fileName, format(file.stream));
if (!guessFormatFromFilename(basename, file.format)) // read/write from/to a file (with extension)
{
close(file.stream);
_throwIf(UnknownExtensionError(fileName), TThrowExceptions());
return false;
}
}
file.iter = directionIterator(file.stream, TDirection());
return true;
}
template <typename TFileFormat, typename TDirection, typename TSpec>
inline bool open(FormattedFile<TFileFormat, TDirection, TSpec> & file,
const char *fileName,
int openMode = DefaultOpenMode<FormattedFile<TFileFormat, TDirection, TSpec> >::VALUE)
{
return _open(file, fileName, openMode, False());
}
// ----------------------------------------------------------------------------
// Function isOpen()
// ----------------------------------------------------------------------------
/*!
* @fn FormattedFile#isOpen
* @brief Determines whether a FormattedFile is currently open.
*
* @signature bool isOpen(file);
*
* @param[in] file The FormattedFile to check.
* @return bool <tt>true</tt> if file is currently open, <tt>false</tt> otherwise.
*/
template <typename TFileFormat, typename TDirection, typename TSpec>
inline bool isOpen(FormattedFile<TFileFormat, TDirection, TSpec> const & file)
{
return file.stream.file.is_open();
}
// ----------------------------------------------------------------------------
// Function close()
// ----------------------------------------------------------------------------
/*!
* @fn FormattedFile#close
* @brief Close a FormattedFile.
*
* @signature bool close(file);
*
* @param[in,out] file The FormattedFile to close.
* @return bool <tt>true</tt> in the case of success, <tt>false</tt> otherwise.
*/
template <typename TFileFormat, typename TDirection, typename TSpec>
inline bool close(FormattedFile<TFileFormat, TDirection, TSpec> & file)
{
setFormat(file, typename FileFormat<FormattedFile<TFileFormat, TDirection, TSpec> >::Type());
file.iter = typename DirectionIterator<FormattedFile<TFileFormat, TDirection, TSpec>, TDirection>::Type();
return close(file.stream);
}
// ----------------------------------------------------------------------------
// Function atEnd()
// ----------------------------------------------------------------------------
/*!
* @fn FormattedFile#atEnd
* @brief Determines whether a FormattedFile is at the end.
*
* @signature bool atEnd(file);
*
* @param[in,out] file The FormattedFile to check.
* @return bool <tt>true</tt> in the case of success, <tt>false</tt> otherwise.
*
* @datarace Not thread safe.
*/
template <typename TFileFormat, typename TDirection, typename TSpec>
inline SEQAN_FUNC_ENABLE_IF(Is<InputStreamConcept<typename FormattedFile<TFileFormat, TDirection, TSpec>::TStream> >, bool)
atEnd(FormattedFile<TFileFormat, TDirection, TSpec> const & file)
{
return atEnd(file.iter);
}
// ----------------------------------------------------------------------------
// Function position()
// ----------------------------------------------------------------------------
template <typename TFileFormat, typename TSpec>
inline typename Position<FormattedFile<TFileFormat, Output, TSpec> >::Type
position(FormattedFile<TFileFormat, Output, TSpec> & file)
{
return file.stream.tellp();
}
template <typename TFileFormat, typename TSpec>
inline typename Position<FormattedFile<TFileFormat, Input, TSpec> >::Type
position(FormattedFile<TFileFormat, Input, TSpec> & file)
{
return file.stream.tellg();
}
// ----------------------------------------------------------------------------
// Function setPosition()
// ----------------------------------------------------------------------------
template <typename TFileFormat, typename TSpec, typename TPosition>
inline bool
setPosition(FormattedFile<TFileFormat, Output, TSpec> & file, TPosition pos)
{
return (TPosition)file.stream.rdbuf()->pubseekpos(pos, std::ios_base::out) == pos;
}
template <typename TFileFormat, typename TSpec, typename TPosition>
inline bool
setPosition(FormattedFile<TFileFormat, Input, TSpec> & file, TPosition pos)
{
return (TPosition)file.stream.rdbuf()->pubseekpos(pos, std::ios_base::in) == pos;
}
// ----------------------------------------------------------------------------
// Function context()
// ----------------------------------------------------------------------------
/*!
* @fn FormattedFile#context
* @brief Return the FormattedFile's dependent context object.
*
* @signature TDependentContext & context(file);
*
* @param[in,out] file The FormattedFile to query for its context.
*
* @return TDependentContext The dependent context, type as returned from @link FormattedFile#FormattedFileContext @endlink.
*/
template <typename TFileFormat, typename TDirection, typename TSpec>
inline typename FormattedFileContext<FormattedFile<TFileFormat, TDirection, TSpec>, Dependent<> >::Type &
context(FormattedFile<TFileFormat, TDirection, TSpec> & file)
{
return file.context;
}
template <typename TFileFormat, typename TDirection, typename TSpec>
inline typename FormattedFileContext<FormattedFile<TFileFormat, TDirection, TSpec>, Dependent<> >::Type const &
context(FormattedFile<TFileFormat, TDirection, TSpec> const & file)
{
return file.context;
}
// ----------------------------------------------------------------------------
// Function _getCompressionFormatExtensions()
// ----------------------------------------------------------------------------
template <typename TStringSet, typename TFormat_, typename TCompressionFormats>
inline void
_getCompressionExtensions(
TStringSet &stringSet,
Tag<TFormat_> const & /*formatTag*/,
TCompressionFormats const & compress,
bool primaryExtensionOnly,
Nothing)
{
typedef Tag<TFormat_> TFormat;
std::vector<std::string> compressionExtensions;
_getFileExtensions(compressionExtensions, compress, true);
unsigned len = (primaryExtensionOnly)? 1 : sizeof(FileExtensions<TFormat>::VALUE) / sizeof(char*);
for (unsigned i = 0; i < len; ++i)
for (unsigned j = 0; j < compressionExtensions.size(); ++j)
{
size_t jj = (j == 0)? compressionExtensions.size() - 1 : j - 1; // swap first and last compression extension
appendValue(stringSet, (std::string)FileExtensions<TFormat>::VALUE[i] + compressionExtensions[jj]);
}
}
template <typename TStringSet, typename TFormat_, typename TCompressionFormats, typename TCompression_>
inline void
_getCompressionExtensions(
TStringSet &stringSet,
Tag<TFormat_> const & formatTag,
TCompressionFormats const &,
bool primaryExtensionOnly,
Tag<TCompression_>)
{
_getFileExtensions(stringSet, formatTag, primaryExtensionOnly);
}
template <typename TStringSet, typename TFormat_, typename TCompressionFormats>
inline void
_getCompressionExtensions(
TStringSet &stringSet,
Tag<TFormat_> const & formatTag,
TCompressionFormats const & compress,
bool primaryExtensionOnly)
{
_getCompressionExtensions(stringSet, formatTag, compress, primaryExtensionOnly, _mapFileFormatToCompressionFormat(formatTag));
}
template <typename TStringSet, typename TTag, typename TCompressionFormats>
inline void
_getCompressionExtensions(
TStringSet &stringSet,
TagList<TTag, void> const & /*formatTag*/,
TCompressionFormats const & compress,
bool primaryExtensionOnly = false)
{
_getCompressionExtensions(stringSet, TTag(), compress, primaryExtensionOnly);
}
template <typename TStringSet, typename TTag, typename TSubList, typename TCompressionFormats>
inline void
_getCompressionExtensions(
TStringSet &stringSet,
TagList<TTag, TSubList> const & /*formatTag*/,
TCompressionFormats const & compress,
bool primaryExtensionOnly = false)
{
_getCompressionExtensions(stringSet, TTag(), compress, primaryExtensionOnly);
_getCompressionExtensions(stringSet, TSubList(), compress, primaryExtensionOnly);
}
template <typename TStringSet, typename TTagList, typename TCompressionFormats>
inline void
_getCompressionExtensions(
TStringSet &stringSet,
TagSelector<TTagList> const & /*formatTag*/,
TCompressionFormats const & compress,
bool primaryExtensionOnly = false)
{
_getCompressionExtensions(stringSet, TTagList(), compress, primaryExtensionOnly);
}
// ----------------------------------------------------------------------------
// Function getFileExtensions()
// ----------------------------------------------------------------------------
/*!
* @fn FormattedFile#getFileExtensions
* @brief Static function that returns a list of allowed file format extension.
*
* @signature TExtensionVector getFileExtensions(file)
*
* @param[in] file The FormattedFile to query.
* @return TExtensionVector A <tt>std::vector<std::string></tt> with the allowed file extensions.
*
* This is a shortcut to @link FormattedFile#getFileExtensions @endlink.
*/
template <typename TFileFormat, typename TDirection, typename TSpec>
static std::vector<std::string>
getFileExtensions(FormattedFile<TFileFormat, TDirection, TSpec> const & file)
{
return file.getFileExtensions();
}
} // namespace seqan2
#endif // SEQAN_STREAM_SMART_FILE_H_
|