/[svn]/libgig/trunk/src/RIFF.cpp
ViewVC logotype

Diff of /libgig/trunk/src/RIFF.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1885 by persson, Thu Apr 16 18:25:31 2009 UTC revision 2450 by persson, Wed May 8 17:53:07 2013 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file access library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2009 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2013 by Christian Schoenebeck                      *
6   *                              <cuse@users.sourceforge.net>               *   *                              <cuse@users.sourceforge.net>               *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
# Line 22  Line 22 
22   ***************************************************************************/   ***************************************************************************/
23    
24  #include <algorithm>  #include <algorithm>
25    #include <set>
26  #include <string.h>  #include <string.h>
27    
28  #include "RIFF.h"  #include "RIFF.h"
# Line 594  namespace RIFF { Line 595  namespace RIFF {
595      }      }
596    
597      /**      /**
598         * Reads a null-padded string of size characters and copies it
599         * into the string \a s. The position within the chunk will
600         * automatically be incremented.
601         *
602         * @param s                 destination string
603         * @param size              number of characters to read
604         * @throws RIFF::Exception  if an error occured or less than
605         *                          \a size characters could be read!
606         */
607        void Chunk::ReadString(String& s, int size) {
608            char* buf = new char[size];
609            ReadSceptical(buf, 1, size);
610            s.assign(buf, std::find(buf, buf + size, '\0'));
611            delete[] buf;
612        }
613    
614        /**
615       * Writes \a WordCount number of 32 Bit unsigned integer words from the       * Writes \a WordCount number of 32 Bit unsigned integer words from the
616       * buffer pointed by \a pData to the chunk's body, directly to the       * buffer pointed by \a pData to the chunk's body, directly to the
617       * actual "physical" file. The position within the chunk will       * actual "physical" file. The position within the chunk will
# Line 860  namespace RIFF { Line 878  namespace RIFF {
878              #else              #else
879              int iBytesMoved = 1;              int iBytesMoved = 1;
880              #endif              #endif
881              for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) {              for (unsigned long ulOffset = 0; ulToMove > 0 && iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) {
882                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;                  iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;
883                  #if POSIX                  #if POSIX
884                  lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);                  lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);
# Line 1383  namespace RIFF { Line 1401  namespace RIFF {
1401  // *************** File ***************  // *************** File ***************
1402  // *  // *
1403    
1404    //HACK: to avoid breaking DLL compatibility to older versions of libgig we roll the new std::set<Chunk*> into the old std::list<Chunk*> container, should be replaced on member variable level soon though
1405    #define _GET_RESIZED_CHUNKS() \
1406            (reinterpret_cast<std::set<Chunk*>*>(ResizedChunks.front()))
1407    
1408      /** @brief Create new RIFF file.      /** @brief Create new RIFF file.
1409       *       *
1410       * Use this constructor if you want to create a new RIFF file completely       * Use this constructor if you want to create a new RIFF file completely
# Line 1398  namespace RIFF { Line 1420  namespace RIFF {
1420       * @see AddSubChunk(), AddSubList(), SetByteOrder()       * @see AddSubChunk(), AddSubList(), SetByteOrder()
1421       */       */
1422      File::File(uint32_t FileType) : List(this) {      File::File(uint32_t FileType) : List(this) {
1423            //HACK: see _GET_RESIZED_CHUNKS() comment
1424            ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>));
1425          #if defined(WIN32)          #if defined(WIN32)
1426          hFileRead = hFileWrite = INVALID_HANDLE_VALUE;          hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1427          #else          #else
# Line 1418  namespace RIFF { Line 1442  namespace RIFF {
1442       *                         given RIFF file       *                         given RIFF file
1443       */       */
1444      File::File(const String& path) : List(this), Filename(path) {      File::File(const String& path) : List(this), Filename(path) {
1445        #if DEBUG         #if DEBUG
1446        std::cout << "File::File("<<path<<")" << std::endl;         std::cout << "File::File("<<path<<")" << std::endl;
1447        #endif // DEBUG         #endif // DEBUG
1448          bEndianNative = true;          try {
1449          #if POSIX              bEndianNative = true;
1450          hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK);              //HACK: see _GET_RESIZED_CHUNKS() comment
1451          if (hFileRead <= 0) {              ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>));
1452              hFileRead = hFileWrite = 0;              #if POSIX
1453              throw RIFF::Exception("Can't open \"" + path + "\"");              hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK);
1454          }              if (hFileRead <= 0) {
1455          #elif defined(WIN32)                  hFileRead = hFileWrite = 0;
1456          hFileRead = hFileWrite = CreateFile(                  throw RIFF::Exception("Can't open \"" + path + "\"");
1457                                       path.c_str(), GENERIC_READ,              }
1458                                       FILE_SHARE_READ | FILE_SHARE_WRITE,              #elif defined(WIN32)
1459                                       NULL, OPEN_EXISTING,              hFileRead = hFileWrite = CreateFile(
1460                                       FILE_ATTRIBUTE_NORMAL |                                           path.c_str(), GENERIC_READ,
1461                                       FILE_FLAG_RANDOM_ACCESS, NULL                                           FILE_SHARE_READ | FILE_SHARE_WRITE,
1462                                   );                                           NULL, OPEN_EXISTING,
1463          if (hFileRead == INVALID_HANDLE_VALUE) {                                           FILE_ATTRIBUTE_NORMAL |
1464              hFileRead = hFileWrite = INVALID_HANDLE_VALUE;                                           FILE_FLAG_RANDOM_ACCESS, NULL
1465              throw RIFF::Exception("Can't open \"" + path + "\"");                                       );
1466                if (hFileRead == INVALID_HANDLE_VALUE) {
1467                    hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1468                    throw RIFF::Exception("Can't open \"" + path + "\"");
1469                }
1470                #else
1471                hFileRead = hFileWrite = fopen(path.c_str(), "rb");
1472                if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\"");
1473                #endif // POSIX
1474                Mode = stream_mode_read;
1475                ulStartPos = RIFF_HEADER_SIZE;
1476                ReadHeader(0);
1477                if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) {
1478                    throw RIFF::Exception("Not a RIFF file");
1479                }
1480          }          }
1481          #else          catch (...) {
1482          hFileRead = hFileWrite = fopen(path.c_str(), "rb");              Cleanup();
1483          if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\"");              throw;
         #endif // POSIX  
         Mode = stream_mode_read;  
         ulStartPos = RIFF_HEADER_SIZE;  
         ReadHeader(0);  
         if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) {  
             throw RIFF::Exception("Not a RIFF file");  
1484          }          }
1485      }      }
1486    
# Line 1607  namespace RIFF { Line 1639  namespace RIFF {
1639    
1640          // first we sum up all positive chunk size changes (and skip all negative ones)          // first we sum up all positive chunk size changes (and skip all negative ones)
1641          unsigned long ulPositiveSizeDiff = 0;          unsigned long ulPositiveSizeDiff = 0;
1642          for (std::set<Chunk*>::const_iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) {          std::set<Chunk*>* resizedChunks = _GET_RESIZED_CHUNKS();
1643            for (std::set<Chunk*>::const_iterator iter = resizedChunks->begin(), end = resizedChunks->end(); iter != end; ++iter) {
1644              if ((*iter)->GetNewSize() == 0) {              if ((*iter)->GetNewSize() == 0) {
1645                  throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter));                  throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter));
1646              }              }
# Line 1663  namespace RIFF { Line 1696  namespace RIFF {
1696          if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);          if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);
1697    
1698          // forget all resized chunks          // forget all resized chunks
1699          ResizedChunks.clear();          resizedChunks->clear();
1700      }      }
1701    
1702      /** @brief Save changes to another file.      /** @brief Save changes to another file.
# Line 1720  namespace RIFF { Line 1753  namespace RIFF {
1753          if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);          if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);
1754    
1755          // forget all resized chunks          // forget all resized chunks
1756          ResizedChunks.clear();          _GET_RESIZED_CHUNKS()->clear();
1757    
1758          #if POSIX          #if POSIX
1759          if (hFileWrite) close(hFileWrite);          if (hFileWrite) close(hFileWrite);
# Line 1756  namespace RIFF { Line 1789  namespace RIFF {
1789         #if DEBUG         #if DEBUG
1790         std::cout << "File::~File()" << std::endl;         std::cout << "File::~File()" << std::endl;
1791         #endif // DEBUG         #endif // DEBUG
1792            Cleanup();
1793        }
1794    
1795        void File::Cleanup() {
1796          #if POSIX          #if POSIX
1797          if (hFileRead) close(hFileRead);          if (hFileRead) close(hFileRead);
1798          #elif defined(WIN32)          #elif defined(WIN32)
# Line 1765  namespace RIFF { Line 1802  namespace RIFF {
1802          #endif // POSIX          #endif // POSIX
1803          DeleteChunkList();          DeleteChunkList();
1804          pFile = NULL;          pFile = NULL;
1805            //HACK: see _GET_RESIZED_CHUNKS() comment
1806            delete _GET_RESIZED_CHUNKS();
1807      }      }
1808    
1809      void File::LogAsResized(Chunk* pResizedChunk) {      void File::LogAsResized(Chunk* pResizedChunk) {
1810          ResizedChunks.insert(pResizedChunk);          _GET_RESIZED_CHUNKS()->insert(pResizedChunk);
1811      }      }
1812    
1813      void File::UnlogResized(Chunk* pResizedChunk) {      void File::UnlogResized(Chunk* pResizedChunk) {
1814          ResizedChunks.erase(pResizedChunk);          _GET_RESIZED_CHUNKS()->erase(pResizedChunk);
1815      }      }
1816    
1817      unsigned long File::GetFileSize() {      unsigned long File::GetFileSize() {

Legend:
Removed from v.1885  
changed lines
  Added in v.2450

  ViewVC Help
Powered by ViewVC