DSF2FLAC
fstream_plus.cpp
00001 /*
00002  * dsf2flac - http://code.google.com/p/dsf2flac/
00003  * 
00004  * A file conversion tool for translating dsf dsd audio files into
00005  * flac pcm audio files.
00006  *
00007  * Copyright (c) 2013 by respective authors.
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  * 
00024  * Acknowledgements
00025  * 
00026  * Many thanks to the following authors and projects whose work has greatly
00027  * helped the development of this tool.
00028  * 
00029  * 
00030  * Sebastian Gesemann - dsd2pcm (http://code.google.com/p/dsd2pcm/)
00031  * SACD Ripper (http://code.google.com/p/sacd-ripper/)
00032  * Maxim V.Anisiutkin - foo_input_sacd (http://sourceforge.net/projects/sacddecoder/files/)
00033  * Vladislav Goncharov - foo_input_sacd_hq (http://vladgsound.wordpress.com)
00034  * Jesus R - www.sonore.us
00035  * 
00036  */
00037  
00048 #include "fstream_plus.h"
00049 
00050 fstreamPlus::fstreamPlus() : std::fstream()
00051 {
00052 }
00053 
00054 fstreamPlus::~fstreamPlus()
00055 {
00056 }
00057 
00059 bool fstreamPlus::seekg(streampos pos)
00060 {
00061         std::fstream::seekg(pos);
00062         return !good();
00063 }
00064 bool fstreamPlus::seekg(streamoff pos, ios_base::seekdir way)
00065 {
00066         std::fstream::seekg(pos,way);
00067         return !good();
00068 }
00069 
00071 bool fstreamPlus::read_int8   (dsf2flac_int8*   b,stream_size n) {return read_helper(b,n);}
00072 bool fstreamPlus::read_uint8  (dsf2flac_uint8*  b,stream_size n) {return read_helper(b,n);}
00073 bool fstreamPlus::read_uint16 (dsf2flac_uint16* b,stream_size n) {return read_helper(b,n);}
00074 bool fstreamPlus::read_uint32 (dsf2flac_uint32* b,stream_size n) {return read_helper(b,n);}
00075 bool fstreamPlus::read_uint64 (dsf2flac_uint64* b,stream_size n) {return read_helper(b,n);}
00076 
00078 bool fstreamPlus::read_int32_rev        (dsf2flac_int32*  b,stream_size n) {return read_helper_rev(b,n);}
00079 bool fstreamPlus::read_uint16_rev       (dsf2flac_uint16* b,stream_size n) {return read_helper_rev(b,n);}
00080 bool fstreamPlus::read_uint32_rev       (dsf2flac_uint32* b,stream_size n) {return read_helper_rev(b,n);}
00081 bool fstreamPlus::read_uint64_rev       (dsf2flac_uint64* b,stream_size n) {return read_helper_rev(b,n);}
00082 
00084 template<typename rType> bool fstreamPlus::read_helper(rType* b, stream_size n) {
00085         read( reinterpret_cast<char*>(b), sizeof(rType)*n);
00086         return bad();
00087 }
00088 template<typename rType> bool fstreamPlus::read_helper_rev(rType* b, stream_size n) {
00089         read( reinterpret_cast<char*>(b), sizeof(rType)*n);
00090         reverseByteOrder(b,n);
00091         return bad();
00092 }
00093 
00094 
00096 template<typename rType> void fstreamPlus::reverseByteOrder(rType* b,stream_size n)
00097 {
00098         for (stream_size i=0; i<n; i++)
00099                 b[i] = reverseByteOrder(b[i]);
00100 }
00101 template<typename rType> rType fstreamPlus::reverseByteOrder(rType b)
00102 {
00103         int n = sizeof(rType);
00104         rType b2;
00105         unsigned char* dst = reinterpret_cast<unsigned char*>(&b2);
00106         unsigned char* src = reinterpret_cast<unsigned char*>(&b);
00107         for (int i=0;i<n;i++)
00108                 dst[i] = src[n-1-i];
00109         return b2;
00110 }
 All Classes Files Functions Variables