BasicFileIO.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: BasicFileIO.cpp
37 // Author: Kai Welke
38 // Date: 25.03.2005
39 // ****************************************************************************
40 // Changes: 20.03.2009, Moritz Hassert:
41 // * ReadInt etc.: replaced type-cast-loads with memcpy() to avoid
42 // problems on platforms that expect aligned pointers
43 // ****************************************************************************
44 
45 
46 // ****************************************************************************
47 // Includes
48 // ****************************************************************************
49 
50 #include <new> // for explicitly using correct new/delete operators on VC DSPs
51 
52 #include "BasicFileIO.h"
53 #include <string.h>
54 #include <stdlib.h>
55 
56 
57 
58 // ****************************************************************************
59 // CBasicFileIO reading
60 // ****************************************************************************
61 
62 bool CBasicFileIO::ReadBool(char*& pchBuffer)
63 {
64  bool bReturn = (*((char *) pchBuffer)) ? true : false;
65  pchBuffer += sizeof(char);
66  return bReturn;
67 }
68 
69 char CBasicFileIO::ReadChar(char*& pchBuffer)
70 {
71  char chReturn = *((char*)pchBuffer);
72  pchBuffer += sizeof(char);
73  return chReturn;
74 }
75 
77 {
78  int nLength = ReadInt(pchBuffer);
79 
80  if(nLength == 0) return "";
81 
82  std::string szReturn(pchBuffer, nLength);
83  pchBuffer += nLength;
84 
85  return szReturn;
86 }
87 
88 short CBasicFileIO::ReadShort(char*& pchBuffer)
89 {
90  short nReturn;
91  memcpy(&nReturn, pchBuffer, sizeof(short));
92  pchBuffer += sizeof(short);
93  return nReturn;
94 }
95 
96 int CBasicFileIO::ReadInt(char*& pchBuffer)
97 {
98  int nReturn;
99  memcpy(&nReturn, pchBuffer, sizeof(int));
100  pchBuffer += sizeof(int);
101  return nReturn;
102 }
103 
104 float CBasicFileIO::ReadFloat(char*& pchBuffer)
105 {
106  float fReturn;
107  memcpy(&fReturn, pchBuffer, sizeof(float));
108  pchBuffer += sizeof(float);
109  return fReturn;
110 }
111 
112 double CBasicFileIO::ReadDouble(char*& pchBuffer)
113 {
114  double dReturn;
115  memcpy(&dReturn, pchBuffer, sizeof(double));
116  pchBuffer += sizeof(double);
117  return dReturn;
118 }
119 
120 void CBasicFileIO::ReadBytes(char*& pchBuffer, void* pDest, int nSize)
121 {
122  memcpy(pDest,(void*) pchBuffer,nSize);
123  pchBuffer += nSize;
124 }
125 
126 
127 // ****************************************************************************
128 // CBasicFileIO writing
129 // ****************************************************************************
130 
131 bool CBasicFileIO::WriteBool(FILE *fp, bool bValue)
132 {
133  char chValue = (char) bValue;
134  return fwrite(&chValue, 1, 1, fp) == 1;
135 }
136 
137 bool CBasicFileIO::WriteChar(FILE *fp, char chValue)
138 {
139  return fwrite(&chValue, 1, 1, fp) == 1;
140 }
141 
142 bool CBasicFileIO::WriteString(FILE *fp, std::string szValue)
143 {
144  if (szValue == "")
145  return WriteInt(fp,0);
146 
147  return WriteInt(fp, (int) szValue.length()) && fwrite(szValue.data(), szValue.length(), 1, fp) == 1;
148 }
149 
150 bool CBasicFileIO::WriteInt(FILE *fp, int nValue)
151 {
152  return fwrite(&nValue, sizeof(int), 1, fp) == 1;
153 }
154 
155 bool CBasicFileIO::WriteFloat(FILE *fp, float fValue)
156 {
157  return fwrite(&fValue, sizeof(float), 1, fp) == 1;
158 }
159 
160 bool CBasicFileIO::WriteDouble(FILE *fp, double dValue)
161 {
162  return fwrite(&dValue, sizeof(double), 1, fp) == 1;
163 }
164 
165 bool CBasicFileIO::WriteBytes(FILE *fp, void* pSrc, int nSize)
166 {
167  return fwrite(pSrc, nSize, 1, fp) == 1;
168 }
169 
170 
172 {
173  const int nCurrentFilePos = ftell(fp);
174  fseek(fp, 0, SEEK_END);
175  const int nFileSize = ftell(fp);
176  fseek(fp, nCurrentFilePos, SEEK_SET);
177  return nFileSize;
178 }
static void ReadBytes(char *&pchBuffer, void *pDest, int nSize)
static std::string ReadString(char *&pchBuffer)
Definition: BasicFileIO.cpp:76
static short ReadShort(char *&pchBuffer)
Definition: BasicFileIO.cpp:88
static int GetFileSize(FILE *fp)
static float ReadFloat(char *&pchBuffer)
static double ReadDouble(char *&pchBuffer)
static bool ReadBool(char *&pchBuffer)
Definition: BasicFileIO.cpp:62
static bool WriteFloat(FILE *fp, float fValue)
static char ReadChar(char *&pchBuffer)
Definition: BasicFileIO.cpp:69
static bool WriteString(FILE *fp, std::string szValue)
static bool WriteInt(FILE *fp, int nValue)
GLsizei const GLchar ** string
Definition: glext.h:3528
static bool WriteDouble(FILE *fp, double dValue)
static bool WriteBool(FILE *fp, bool bValue)
static int ReadInt(char *&pchBuffer)
Definition: BasicFileIO.cpp:96
static bool WriteBytes(FILE *fp, void *pSrc, int nSize)
static bool WriteChar(FILE *fp, char chValue)


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:27