helper.h

Go to the documentation of this file.
00001 
00007 /*
00008  *  Copyright (c) 2003, Matt Beard
00009  *
00010  *  This software is provided 'as-is', without any express or implied warranty.
00011  *  In no event will the authors be held liable for any damages arising from
00012  *  the use of this software.
00013  *
00014  *  Permission is granted to anyone to use this software for any purpose,
00015  *  including commercial applications, and to alter it and redistribute it
00016  *  freely, subject to the following restrictions:
00017  *
00018  *    1. The origin of this software must not be misrepresented; you must
00019  *       not claim that you wrote the original software. If you use this
00020  *       software in a product, an acknowledgment in the product
00021  *       documentation would be appreciated but is not required.
00022  *  
00023  *    2. Altered source versions must be plainly marked as such, and must
00024  *       not be misrepresented as being the original software.
00025  *  
00026  *    3. This notice may not be removed or altered from any source
00027  *       distribution.
00028  */
00029 
00030 #ifndef MXFLIB__HELPER_H
00031 #define MXFLIB__HELPER_H
00032 
00033 
00034 #include <time.h>
00035 #include <string>
00036 
00037 namespace mxflib
00038 {
00039     // Declare the global null-ul which is 16 zero bytes
00040     extern const UL Null_UL;
00041     
00043     inline std::string Int2String(int Num, int Digits = 0)
00044     {
00045         char Buffer[18];
00046         if(Digits > 16) Digits = 16;
00047         sprintf(Buffer, "%0*d", Digits, Num);
00048         return std::string(Buffer);
00049     }
00050 
00052     inline std::string UInt2String(int Num, int Digits = 0)
00053     {
00054         char Buffer[18];
00055         if(Digits > 16) Digits = 16;
00056         sprintf(Buffer, "%0*u", Digits, Num);
00057         return std::string(Buffer);
00058     }
00059 
00060     // Support old capitalization of unsigned integers
00061     inline std::string Uint2String(int Num, int Digits = 0) { return UInt2String(Num, Digits); }
00062 
00064     inline std::string Int2HexString(int Num, int Digits = 0)
00065     {
00066         char Buffer[18];
00067         if(Digits > 16) Digits = 16;
00068         sprintf(Buffer, "%0*x", Digits, Num);
00069         return std::string(Buffer);
00070     }
00071 
00073 
00076     inline std::string Time2String(full_time Time, bool StrictISO = false)
00077     {
00078         char Buffer[32];
00079         
00080         if(StrictISO)
00081             strftime(Buffer, 31, "%Y-%m-%dT%H:%M:%S.", gmtime( &Time.time ));
00082         else
00083             strftime(Buffer, 31, "%Y-%m-%d %H:%M:%S.", gmtime( &Time.time ));
00084 
00085         // Append the milliseconds
00086         sprintf(&Buffer[strlen(Buffer)], "%03d", Time.msBy4 * 4);
00087 
00088         return std::string(Buffer);
00089     }
00090 
00092 
00094     inline std::string Now2String(bool StrictISO = false)
00095     {
00096         full_time now = GetTime();
00097         
00098         return Time2String(now, StrictISO);
00099     }
00100 
00102 
00110     UInt32 MakeBER(UInt8 *Data, int MaxSize, UInt64 Length, UInt32 Size = 0);
00111 
00112 
00114 
00119     inline DataChunkPtr MakeBER(UInt64 Length, UInt32 Size = 0)
00120     {
00121         // Buffer for building BER
00122         UInt8 Buff[9];
00123 
00124         UInt32 Bytes = MakeBER(Buff, 9, Length, Size);
00125 
00126         // Return as a DataChunk
00127         return new DataChunk(Bytes, Buff);
00128     }
00129 
00131     Length ReadBER(UInt8 **Data, int MaxSize);
00132 
00133 
00139     int EncodeOID( UInt8* presult, UInt64 subid, int length );
00140 
00142     UMIDPtr MakeUMID(int Type, const UUIDPtr AssetID = NULL);
00143 
00145     DataChunkPtr FileReadChunk(FileHandle InFile, UInt64 Size);
00146 
00147 
00149 
00154     U32Pair ReadIFFHeader(FileHandle InFile, bool BigEndian = true);
00155 
00156 
00158 
00163     inline U32Pair ReadRIFFHeader(FileHandle InFile)
00164     {
00165         return ReadIFFHeader(InFile, false);
00166     }
00167 
00168 
00170 
00175     inline U32Pair ReadAIFFHeader(FileHandle InFile)
00176     {
00177         return ReadIFFHeader(InFile, true);
00178     }
00179 
00180 
00182 
00189     std::pair<UInt32, Length> ReadAtomHeader(FileHandle InFile, bool SkipWide = true);
00190 
00191 
00193     DataChunkPtr Hex2DataChunk(std::string Hex);
00194 
00196     void SetDictionaryPath(std::string NewPath);
00197 
00199     inline void SetDictionaryPath(const char *NewPath) { SetDictionaryPath(std::string(NewPath)); }
00200 
00202 
00206     std::string LookupDictionaryPath(const char *Filename);
00207     
00209     inline std::string LookupDictionaryPath(std::string Filename) { return LookupDictionaryPath(Filename.c_str()); }
00210 
00212 
00216     std::string SearchPath(const char *Path, const char *Filename);
00217 
00219     inline std::string SearchPath(std::string Path, std::string Filename) { return SearchPath(Path.c_str(), Filename.c_str()); }
00220 
00221 
00222     // File read primitives
00223 
00225     inline UInt8 ReadU8(FileHandle Handle) { unsigned char Buffer[1]; if(FileRead(Handle, Buffer, 1) == 1) return GetU8(Buffer); else return 0; }
00226 
00228     inline UInt16 ReadU16(FileHandle Handle) { unsigned char Buffer[2]; if(FileRead(Handle, Buffer, 2) == 2) return GetU16(Buffer); else return 0; }
00229 
00231     inline UInt32 ReadU32(FileHandle Handle) { unsigned char Buffer[4]; if(FileRead(Handle, Buffer, 4) == 4) return GetU32(Buffer); else return 0; }
00232 
00234     inline UInt64 ReadU64(FileHandle Handle) { unsigned char Buffer[8]; if(FileRead(Handle, Buffer, 8) == 8) return GetU64(Buffer); else return 0; }
00235 
00237     inline Int8 ReadI8(FileHandle Handle) { return (Int8)ReadU8(Handle); }
00238 
00240     inline Int16 ReadI16(FileHandle Handle) { return (Int16)ReadU16(Handle); }
00241     
00243     inline Int32 ReadI32(FileHandle Handle) { return (Int32)ReadU32(Handle); }
00244     
00246     inline Int64 ReadI64(FileHandle Handle) { return (Int64)ReadU64(Handle); }
00247 
00249     inline UInt8 ReadU8_LE(FileHandle Handle) { unsigned char Buffer[1]; if(FileRead(Handle, Buffer, 1) == 1) return GetU8_LE(Buffer); else return 0; }
00250 
00252     inline UInt16 ReadU16_LE(FileHandle Handle) { unsigned char Buffer[2]; if(FileRead(Handle, Buffer, 2) == 2) return GetU16_LE(Buffer); else return 0; }
00253 
00255     inline UInt32 ReadU32_LE(FileHandle Handle) { unsigned char Buffer[4]; if(FileRead(Handle, Buffer, 4) == 4) return GetU32_LE(Buffer); else return 0; }
00256 
00258     inline UInt64 ReadU64_LE(FileHandle Handle) { unsigned char Buffer[8]; if(FileRead(Handle, Buffer, 8) == 8) return GetU64_LE(Buffer); else return 0; }
00259 
00261     inline Int8 ReadI8_LE(FileHandle Handle) { return (Int8)ReadU8_LE(Handle); }
00262 
00264     inline Int16 ReadI16_LE(FileHandle Handle) { return (Int16)ReadU16_LE(Handle); }
00265     
00267     inline Int32 ReadI32_LE(FileHandle Handle) { return (Int32)ReadU32_LE(Handle); }
00268     
00270     inline Int64 ReadI64_LE(FileHandle Handle) { return (Int64)ReadU64_LE(Handle); }
00271 
00272 
00273 
00275     bool IsPartitionKey(const UInt8 *Key);
00276 
00278 
00279     bool IsWideString(std::string &String);
00280 
00282 
00284     int ReadHexString(const char **Source, int Max, UInt8 *Dest, const char *Sep);
00285 
00287 
00289     inline int ReadHexString(const char *Source, int Max, UInt8 *Dest, const char *Sep)
00290     {
00291         const char *p = Source;
00292         return ReadHexString(&p, Max, Dest, Sep);
00293     }
00294 
00296 
00298     bool StringToUL(UInt8 *Data, std::string Val);
00299     
00301 
00303     inline ULPtr StringToUL(std::string Val)
00304     {
00305         UInt8 Data[16];
00306         if(!StringToUL(Data, Val)) return NULL;
00307         return new UL(Data);
00308     }
00309 }
00310 
00311 #endif // MXFLIB__HELPER_H

Generated on Mon Apr 2 15:20:53 2007 for MXFLib by  doxygen 1.5.1-p1