BasicFileIO.cpp
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 // ****************************************************************************
00036 // Filename:  BasicFileIO.cpp
00037 // Author:    Kai Welke
00038 // Date:      25.03.2005
00039 // ****************************************************************************
00040 // Changes:   20.03.2009, Moritz Hassert:
00041 //            * ReadInt etc.: replaced type-cast-loads with memcpy() to avoid
00042 //              problems on platforms that expect aligned pointers
00043 // ****************************************************************************
00044 
00045 
00046 // ****************************************************************************
00047 // Includes
00048 // ****************************************************************************
00049 
00050 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00051 
00052 #include "BasicFileIO.h"
00053 #include <string.h>
00054 #include <stdlib.h>
00055 
00056 
00057 
00058 // ****************************************************************************
00059 // CBasicFileIO reading
00060 // ****************************************************************************
00061 
00062 bool CBasicFileIO::ReadBool(char*& pchBuffer)
00063 {
00064         bool bReturn = (*((char *) pchBuffer)) ? true : false;
00065         pchBuffer += sizeof(char);
00066         return bReturn;
00067 }
00068 
00069 char CBasicFileIO::ReadChar(char*& pchBuffer)
00070 {
00071         char chReturn = *((char*)pchBuffer);
00072         pchBuffer += sizeof(char);
00073         return chReturn;
00074 }
00075 
00076 std::string CBasicFileIO::ReadString(char*& pchBuffer)
00077 {
00078         int nLength = ReadInt(pchBuffer);
00079         
00080         if(nLength == 0) return "";
00081         
00082         std::string szReturn(pchBuffer, nLength);
00083         pchBuffer += nLength;
00084         
00085         return szReturn;
00086 }
00087 
00088 short CBasicFileIO::ReadShort(char*& pchBuffer)
00089 {
00090         short nReturn;
00091         memcpy(&nReturn, pchBuffer, sizeof(short));
00092         pchBuffer += sizeof(short);
00093         return nReturn;
00094 }
00095 
00096 int CBasicFileIO::ReadInt(char*& pchBuffer)
00097 {
00098         int nReturn;
00099         memcpy(&nReturn, pchBuffer, sizeof(int));
00100         pchBuffer += sizeof(int);
00101         return nReturn;
00102 }
00103 
00104 float CBasicFileIO::ReadFloat(char*& pchBuffer)
00105 {
00106         float fReturn;
00107         memcpy(&fReturn, pchBuffer, sizeof(float));
00108         pchBuffer += sizeof(float);
00109         return fReturn;
00110 }
00111 
00112 double CBasicFileIO::ReadDouble(char*& pchBuffer)
00113 {
00114         double dReturn;
00115         memcpy(&dReturn, pchBuffer, sizeof(double));
00116         pchBuffer += sizeof(double);
00117         return dReturn;
00118 }
00119 
00120 void CBasicFileIO::ReadBytes(char*& pchBuffer, void* pDest, int nSize)
00121 {
00122         memcpy(pDest,(void*) pchBuffer,nSize);
00123         pchBuffer += nSize;
00124 }
00125 
00126 
00127 // ****************************************************************************
00128 // CBasicFileIO writing
00129 // ****************************************************************************
00130 
00131 bool CBasicFileIO::WriteBool(FILE *fp, bool bValue)
00132 {
00133         char chValue = (char) bValue;
00134         return fwrite(&chValue, 1, 1, fp) == 1;
00135 }
00136 
00137 bool CBasicFileIO::WriteChar(FILE *fp, char chValue)
00138 {
00139         return fwrite(&chValue, 1, 1, fp) == 1;
00140 }
00141 
00142 bool CBasicFileIO::WriteString(FILE *fp, std::string szValue)
00143 {
00144         if (szValue == "")
00145                 return WriteInt(fp,0);
00146 
00147         return WriteInt(fp, (int) szValue.length()) && fwrite(szValue.data(), szValue.length(), 1, fp) == 1;
00148 }
00149 
00150 bool CBasicFileIO::WriteInt(FILE *fp, int nValue)
00151 {
00152         return fwrite(&nValue, sizeof(int), 1, fp) == 1;
00153 }
00154 
00155 bool CBasicFileIO::WriteFloat(FILE *fp, float fValue)
00156 {
00157         return fwrite(&fValue, sizeof(float), 1, fp) == 1;
00158 }
00159 
00160 bool CBasicFileIO::WriteDouble(FILE *fp, double dValue)
00161 {
00162         return fwrite(&dValue, sizeof(double), 1, fp) == 1;
00163 }
00164 
00165 bool CBasicFileIO::WriteBytes(FILE *fp, void* pSrc, int nSize)
00166 {
00167         return fwrite(pSrc, nSize, 1, fp) == 1;
00168 }
00169 
00170 
00171 int CBasicFileIO::GetFileSize(FILE *fp)
00172 {
00173         const int nCurrentFilePos = ftell(fp);
00174         fseek(fp, 0, SEEK_END);
00175         const int nFileSize = ftell(fp);
00176         fseek(fp, nCurrentFilePos, SEEK_SET);
00177         return nFileSize;
00178 }


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:57