1 /*****************************************************************************/ 2 // Copyright 2006-2007 Adobe Systems Incorporated 3 // All Rights Reserved. 4 // 5 // NOTICE: Adobe permits you to use, modify, and distribute this file in 6 // accordance with the terms of the Adobe license agreement accompanying it. 7 /*****************************************************************************/ 8 9 /* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_file_stream.cpp#2 $ */ 10 /* $DateTime: 2012/06/01 07:28:57 $ */ 11 /* $Change: 832715 $ */ 12 /* $Author: tknoll $ */ 13 14 /*****************************************************************************/ 15 16 #include "dng_file_stream.h" 17 18 #include "dng_exceptions.h" 19 20 /*****************************************************************************/ 21 22 dng_file_stream::dng_file_stream (const char *filename, 23 bool output, 24 uint32 bufferSize) 25 26 : dng_stream ((dng_abort_sniffer *) NULL, 27 bufferSize, 28 0) 29 30 , fFile (NULL) 31 32 { 33 34 fFile = fopen (filename, output ? "wb" : "rb"); 35 36 if (!fFile) 37 { 38 39 #if qDNGValidate 40 41 ReportError ("Unable to open file", 42 filename); 43 44 ThrowSilentError (); 45 46 #else 47 48 ThrowOpenFile (); 49 50 #endif 51 52 } 53 54 } 55 56 /*****************************************************************************/ 57 58 dng_file_stream::~dng_file_stream () 59 { 60 61 if (fFile) 62 { 63 fclose (fFile); 64 fFile = NULL; 65 } 66 67 } 68 69 /*****************************************************************************/ 70 71 uint64 dng_file_stream::DoGetLength () 72 { 73 74 if (fseek (fFile, 0, SEEK_END) != 0) 75 { 76 77 ThrowReadFile (); 78 79 } 80 81 return (uint64) ftell (fFile); 82 83 } 84 85 /*****************************************************************************/ 86 87 void dng_file_stream::DoRead (void *data, 88 uint32 count, 89 uint64 offset) 90 { 91 92 if (fseek (fFile, (long) offset, SEEK_SET) != 0) 93 { 94 95 ThrowReadFile (); 96 97 } 98 99 uint32 bytesRead = (uint32) fread (data, 1, count, fFile); 100 101 if (bytesRead != count) 102 { 103 104 ThrowReadFile (); 105 106 } 107 108 } 109 110 /*****************************************************************************/ 111 112 void dng_file_stream::DoWrite (const void *data, 113 uint32 count, 114 uint64 offset) 115 { 116 117 if (fseek (fFile, (uint32) offset, SEEK_SET) != 0) 118 { 119 120 ThrowWriteFile (); 121 122 } 123 124 uint32 bytesWritten = (uint32) fwrite (data, 1, count, fFile); 125 126 if (bytesWritten != count) 127 { 128 129 ThrowWriteFile (); 130 131 } 132 133 } 134 135 /*****************************************************************************/ 136