[go: up one dir, main page]

File: common.cpp

package info (click to toggle)
sfftobmp 3.1.3-7
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 23,452 kB
  • sloc: cpp: 148,005; ansic: 85,028; sh: 14,606; makefile: 587; asm: 284
file content (283 lines) | stat: -rw-r--r-- 7,459 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Utility classes
//
// This file is part of sfftobmp, a program to convert
// structured fax files (sff) to windows bitmap files (bmp),
// portable bitmap graphics (pbm), tagged image file format (tiff)
// or JPEG (jpg).
//
// Copyright (C) 1998-2008 Peter Schaefer-Hutter and contributors ("THE AUTHORS")
//
// Permission to use, copy, modify, distribute, and sell this software and
// its documentation for any purpose is hereby granted without fee.
//
// THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
// WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
// INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY
// DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY
// THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE
// OR PERFORMANCE OF THIS SOFTWARE.
//
// Contributor(s):
//    Ulf Zibis <ulf.zibis@gmx.de> (Prepared for inheritance,
//       destructor handling, preserving filetime)
//
// You can contact the original author by email at peter.schaefer@gmx.de.
//
// I'm always pleased to hear that somebody is actually using my software.
// If you can manage it, e-mail me a quick notice. Thanks!
//
/*-RCS-Info----------------------------------------------------

 $Id: common.cpp,v 1.5 2009/04/28 19:02:08 pschaefer Exp $

---RCS-Info--------------------------------------------------*/

#include "sfftypes.h"
#include "common.h"

#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef WIN32
#include <utime.h>
#else
#include <sys/utime.h>
#endif

#include <cassert>
#include <cstdio>
#include <iostream>

#ifdef WIN32
#include <io.h>   // _mktemp
#endif

using namespace std;

//-----------------------------------------------------------------

const std::string CSimpleException::m_aReasons[err_count+1] =
{
//  err_invalidfile
    "Not a valid sff file.",
//    err_corruptfile,
    "File seems corrupt. Reading abandoned.",
//    err_lastpageread,
    "Last page read.",
//    err_notsupported,
    "Operation not supported.",
//    err_openfile,
    "Can't open file.",
//    err_closedfile,
    "Operation on closed file.",
//    err_findPath,
    "Path not found.",
//    err_nowhitestart,
    "Line doesn't begin with white code.",
//    err_noblackcode,
    "White code not followed by black code.",
//    err_noblackterm,
    "Black MUC not followed by black TERM.",
//    err_nowhiteterm,
    "White MUC not followed by white TERM.",
//    err_invalidversion,
    "OOps. Don't know how to handle this Fileversion.",
//    err_unknowncoding,
    "Oh my dear. Don't know how to handle this encoding.",
//    err_toomuchformats,
    "Please specify only one output format.",
//    err_cmdline,
    "Error in commandline.",
//    err_noformat,
    "No output format specified.",
//    err_outfileexists,
    "Output file already exists, use -f to force overwrite.",
//    err_outfileisdir,
    "Given output file is directory.",
//    err_outdir,
    "Cannot create output directory.",
//    err_stdoutnotallowed
    "Output on stdout is not available with this output format.",
//    err_stdoutonlyonefile
    "Output on stdout is not available with more than one input file.",
//
// ------------------- INSERT HERE
//
//    err_count
    "Unknown error."
};

const string& CSimpleException::what()
{
  if (m_nError < err_count) {
    return m_aReasons[m_nError];
  }
  return m_aReasons[err_count];
}

//-----------------------------------------------------------------

CFile::CFile(const std::string& strPath) :
  m_hFile(NULL)
{
  Open( strPath, "rb");
}

CFile::~CFile()
{
  Close();
}

void CFile::Open(const std::string& strPath, const char *pszMode)
{
  m_strPath = strPath;
  if (( m_hFile ) || !( m_hFile = fopen(strPath.c_str(), pszMode))) {
    throw CSimpleException(CSimpleException::err_openfile);
  }
  m_nFileNo = fileno( m_hFile);
}

void CFile::OpenTemp()
{
  char tmp_name[] = "sfftobmp-tmpXXXXXX";
#ifndef WIN32
  if (( m_nFileNo ) || !(( m_nFileNo = mkstemp(tmp_name)) > 0)) {
    m_nFileNo = 0;
    cerr << "temp file couldn't be created: error " << errno << endl;
    throw CSimpleException(CSimpleException::err_openfile);
  }
  unlink(tmp_name);
#else
  _mktemp(tmp_name);
  if (( m_nFileNo ) ||
      (( m_nFileNo = (int)::CreateFile(tmp_name,
                     GENERIC_READ|GENERIC_WRITE, 0, NULL,
                     CREATE_NEW,FILE_ATTRIBUTE_TEMPORARY|
                     FILE_FLAG_DELETE_ON_CLOSE, NULL)) == 0))
  {
    cerr << "temp file couldn't be created: error " << errno << endl;
    throw CSimpleException(CSimpleException::err_openfile);
  }
#endif
  m_strPath = tmp_name;
}

void CFile::Close()
{
  if (m_hFile) {
    fclose(m_hFile);
  } else if (m_nFileNo) {
#ifndef WIN32
    close(m_nFileNo);
#else
    ::CloseHandle((HANDLE)m_nFileNo);
#endif
  }
  m_nFileNo = 0;
  m_hFile = NULL;
}

time_t CFile::GetModificationTime()
{
  struct stat buf;
  if (::fstat( m_nFileNo, &buf ) == 0)
    return buf.st_mtime ;
  else
    throw CSimpleException(CSimpleException::err_notsupported);
}

void CFile::SetModificationTime(const time_t &modtime)
{
  time_t ltime;
  time( &ltime);
  utimbuf filetime;
  filetime.actime = ltime;
  filetime.modtime = modtime;
  if (::utime( m_strPath.string().c_str(), &filetime) != 0) {
    cerr << "CFile::SetModificationTime(): ErrorNr.: " << errno << endl;
  }
}

sff_byte CFile::GetC()
{
  return ::fgetc(m_hFile);
}

void CFile::Read(void *pTarget, int nLen)
{
	size_t rc = ::fread(pTarget, 1, nLen, m_hFile);
}

void CFile::Write(void *pSource, int nLen)
{
  ::fwrite(pSource, nLen, 1, m_hFile);
}

sff_dword CFile::Tell()
{
  return ::ftell(m_hFile);
}

void CFile::Seek(int pos, CFile::seek_offset dir)
{
  int whence;
  if (dir == sk_from_start) {
    whence = SEEK_SET;
  } else if (dir == sk_current) {
    whence = SEEK_CUR;
  } else {
    return;
  }
  ::fseek(m_hFile, pos, whence);
}

bool CFile::Eof()
{
  return (feof(m_hFile) != 0);
}

void CFile::DumpToStdOut()
{
  char buf[1024];
  sff_dword count;

#ifndef WIN32
  int result=lseek(m_nFileNo,0,SEEK_SET);
  if (result!=0) {
    cerr << "seeking to temp file start failed " << endl;
    throw CSimpleException(CSimpleException::err_openfile);
  }
  while ( (count = read(m_nFileNo, buf, 1024)) > 0) {
    result=write(STDOUT_FILENO, buf, count);
    if (result<0) {
      cerr << "writing to stdout failed with error " << errno << endl;
      throw CSimpleException(CSimpleException::err_openfile);
    }
  }
  if (count<0) {
    cerr << "reading from temp file failed with error " << errno << endl;
    throw CSimpleException(CSimpleException::err_openfile);
  }
#else
  SetFilePointer((HANDLE)m_nFileNo,0,0,FILE_BEGIN);
  HANDLE hStdOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
  sff_dword written;
  for (;;) {
    ::ReadFile((HANDLE)m_nFileNo, buf, 1024, (DWORD*)&count, NULL);
    if (count <= 0) break;
    if (!::WriteFile(hStdOut, buf, count, (DWORD *)&written, NULL)) {
      cerr << "writing to stdout failed with error " << errno << endl;
      throw CSimpleException(CSimpleException::err_openfile);
    }
  }
  if (count<0) {
    cerr << "reading from temp file failed with error " << errno << endl;
    throw CSimpleException(CSimpleException::err_openfile);
  }
#endif
}