PDF Toolbox
Loading...
Searching...
No Matches
PdfToolbox_PtxSys.h
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * File: PdfToolbox_PtxSys.h
4 *
5 * Description: Sytem declaration file
6 *
7 * Author: PDF Tools AG
8 *
9 * Copyright: Copyright (C) 2015 - 2024 PDF Tools AG, Switzerland
10 * All rights reserved.
11 *
12 * Classification: CONFIDENTIAL
13 *
14 *****************************************************************************/
15
16#ifndef PDFTOOLBOX_PTXSYS_H__
17#define PDFTOOLBOX_PTXSYS_H__
18
19#include <stdarg.h>
20#ifndef NO_FILE_STREAM_DESCRIPTOR
21#include <stdio.h>
22#endif
23
24#ifdef __cplusplus
25extern "C"
26{
27#endif
28
29#ifdef _UNICODE
30#define PtxSys_PathStreamDescriptor_Create PtxSys_PathStreamDescriptor_CreateW
31#else
32#define PtxSys_PathStreamDescriptor_Create PtxSys_PathStreamDescriptor_CreateA
33#endif
34
35typedef pos_t(STDCALL* TGetLength)(void* handle);
36typedef int(STDCALL* TSeek)(void* handle, pos_t iPos);
37typedef pos_t(STDCALL* TTell)(void* handle);
38typedef size_t(STDCALL* TRead)(void* handle, void* pData, size_t nSize);
39typedef size_t(STDCALL* TWrite)(void* handle, const void* pData, size_t nSize);
40typedef void(STDCALL* TRelease)(void* handle);
41
75
76#ifndef NO_FILE_STREAM_DESCRIPTOR
77/******************************************************************************
78 * Stream descriptor for FILE*
79 * (always use binary mode to fopen file!)
80 *****************************************************************************/
81
88{
89 pos_t iPos, nLen;
90 iPos = ftell((FILE*)handle);
91 if (iPos == -1)
92 return -1;
93 if (fseek((FILE*)handle, 0L, SEEK_END) != 0)
94 return -1;
95 nLen = ftell((FILE*)handle);
96 if (fseek((FILE*)handle, (long)iPos, SEEK_SET) != 0)
97 return -1;
98 return nLen;
99}
100
108static int STDCALL PtxSysFILEPtrSeek__(void* handle, pos_t iPos)
109{
110 return fseek((FILE*)handle, (long)iPos, SEEK_SET) == 0 ? 1 : 0;
111}
112
119static pos_t STDCALL PtxSysFILEPtrTell__(void* handle) { return ftell((FILE*)handle); }
120
129static size_t STDCALL PtxSysFILEPtrRead__(void* handle, void* pData, size_t nSize)
130{
131 size_t nRead = fread(pData, 1, nSize, (FILE*)handle);
132 if (nRead != nSize && ferror((FILE*)handle) != 0)
133 return (size_t)-1;
134 return nRead;
135}
136
145static size_t STDCALL PtxSysFILEPtrWrite__(void* handle, const void* pData, size_t nSize)
146{
147 if (fwrite(pData, 1, nSize, (FILE*)handle) != nSize)
148 return (size_t)-1;
149 return nSize;
150}
151
157static void STDCALL PtxSysFILEPtrRelease__(void* handle) { fclose((FILE*)handle); }
158
167static void PtxSysCreateFILEStreamDescriptor(TPtxSys_StreamDescriptor* pDescriptor, FILE* handle, int bCloseOnRelease)
168{
169 pDescriptor->m_handle = handle;
171 pDescriptor->pfSeek = &PtxSysFILEPtrSeek__;
172 pDescriptor->pfTell = &PtxSysFILEPtrTell__;
173 pDescriptor->pfRead = &PtxSysFILEPtrRead__;
174 pDescriptor->pfWrite = &PtxSysFILEPtrWrite__;
175 pDescriptor->pfRelease = bCloseOnRelease ? &PtxSysFILEPtrRelease__ : NULL; // NOLINT(modernize-use-nullptr)
176}
177#endif // NO_FILE_STREAM_DESCRIPTOR
178
179/******************************************************************************
180 * Path Stream Descriptor convenience function
181 *****************************************************************************/
182
192 const char* szPath, BOOL bIsReadOnly);
202 const WCHAR* szPath, BOOL bIsReadOnly);
208
209/******************************************************************************
210 * Stream Descriptor for memory streams
211 *****************************************************************************/
212
219
225
226/******************************************************************************
227 * Stream Descriptor
228 *****************************************************************************/
231 pos_t iPos);
234 void* pData, size_t nSize);
236 const void* pData, size_t nSize);
238
239#ifdef __cplusplus
240}
241#endif
242
243#endif /* PDFTOOLBOX_PTXSYS_H__ */
#define PDFTOOLBOX_CALL
Definition PdfToolbox_Platform.h:26
#define STDCALL
Definition PdfToolbox_Platform.h:96
unsigned short WCHAR
Definition PdfToolbox_Platform.h:90
#define PDFTOOLBOX_EXPORT
Definition PdfToolbox_Platform.h:138
long long pos_t
Definition PdfToolbox_Platform.h:98
static int STDCALL PtxSysFILEPtrSeek__(void *handle, pos_t iPos)
Callback implementation for TPtxSys_StreamDescriptor::pfSeek for FILE*.
Definition PdfToolbox_PtxSys.h:108
size_t(STDCALL * TWrite)(void *handle, const void *pData, size_t nSize)
Definition PdfToolbox_PtxSys.h:39
static pos_t STDCALL PtxSysFILEPtrTell__(void *handle)
Callback implementation for TPtxSys_StreamDescriptor::pfTell for FILE*.
Definition PdfToolbox_PtxSys.h:119
PDFTOOLBOX_EXPORT pos_t PDFTOOLBOX_CALL PtxSys_StreamDescriptor_GetLength(TPtxSys_StreamDescriptor *pStreamDescriptor)
struct TPtxSys_StreamDescriptor TPtxSys_StreamDescriptor
Structure that groups a set of callbacks that model streams.
size_t(STDCALL * TRead)(void *handle, void *pData, size_t nSize)
Definition PdfToolbox_PtxSys.h:38
int(STDCALL * TSeek)(void *handle, pos_t iPos)
Definition PdfToolbox_PtxSys.h:36
static void PtxSysCreateFILEStreamDescriptor(TPtxSys_StreamDescriptor *pDescriptor, FILE *handle, int bCloseOnRelease)
Initialization function for TPtxSys_StreamDescriptor for FILE*.
Definition PdfToolbox_PtxSys.h:167
static void STDCALL PtxSysFILEPtrRelease__(void *handle)
Callback implementation for TPtxSys_StreamDescriptor::pfRelease for FILE*.
Definition PdfToolbox_PtxSys.h:157
void(STDCALL * TRelease)(void *handle)
Definition PdfToolbox_PtxSys.h:40
static pos_t STDCALL PtxSysFILEPtrGetLength__(void *handle)
Callback implementation for TPtxSys_StreamDescriptor::pfGetLength for FILE*.
Definition PdfToolbox_PtxSys.h:87
PDFTOOLBOX_EXPORT BOOL PDFTOOLBOX_CALL PtxSys_MemoryStreamDescriptor_Create(TPtxSys_StreamDescriptor *pDescriptor)
Initialization function for TPtxSys_StreamDescriptor for the usage as memory streams.
PDFTOOLBOX_EXPORT pos_t PDFTOOLBOX_CALL PtxSys_StreamDescriptor_Tell(TPtxSys_StreamDescriptor *pStreamDescriptor)
pos_t(STDCALL * TTell)(void *handle)
Definition PdfToolbox_PtxSys.h:37
PDFTOOLBOX_EXPORT void PDFTOOLBOX_CALL PtxSys_StreamDescriptor_Release(TPtxSys_StreamDescriptor *pStreamDescriptor)
static size_t STDCALL PtxSysFILEPtrRead__(void *handle, void *pData, size_t nSize)
Callback implementation for TPtxSys_StreamDescriptor::pfRead for FILE*.
Definition PdfToolbox_PtxSys.h:129
PDFTOOLBOX_EXPORT size_t PDFTOOLBOX_CALL PtxSys_StreamDescriptor_Read(TPtxSys_StreamDescriptor *pStreamDescriptor, void *pData, size_t nSize)
pos_t(STDCALL * TGetLength)(void *handle)
Definition PdfToolbox_PtxSys.h:35
PDFTOOLBOX_EXPORT BOOL PDFTOOLBOX_CALL PtxSys_PathStreamDescriptor_CreateA(TPtxSys_StreamDescriptor *pDescriptor, const char *szPath, BOOL bIsReadOnly)
Initialization function for TPtxSys_StreamDescriptor for a given path.
PDFTOOLBOX_EXPORT size_t PDFTOOLBOX_CALL PtxSys_StreamDescriptor_Write(TPtxSys_StreamDescriptor *pStreamDescriptor, const void *pData, size_t nSize)
PDFTOOLBOX_EXPORT BOOL PDFTOOLBOX_CALL PtxSys_StreamDescriptor_Seek(TPtxSys_StreamDescriptor *pStreamDescriptor, pos_t iPos)
PDFTOOLBOX_EXPORT BOOL PDFTOOLBOX_CALL PtxSys_PathStreamDescriptor_CreateW(TPtxSys_StreamDescriptor *pDescriptor, const WCHAR *szPath, BOOL bIsReadOnly)
Initialization function for TPtxSys_StreamDescriptor for a given path.
PDFTOOLBOX_EXPORT void PDFTOOLBOX_CALL PtxSys_MemoryStreamDescriptor_Close(TPtxSys_StreamDescriptor *pDescriptor)
Delete the underlying buffer.
PDFTOOLBOX_EXPORT void PDFTOOLBOX_CALL PtxSys_PathStreamDescriptor_Close(TPtxSys_StreamDescriptor *pDescriptor)
Close the underlying file.
static size_t STDCALL PtxSysFILEPtrWrite__(void *handle, const void *pData, size_t nSize)
Callback implementation for TPtxSys_StreamDescriptor::pfWrite for FILE*.
Definition PdfToolbox_PtxSys.h:145
#define BOOL
Definition PdfToolbox_Types.h:18
Structure that groups a set of callbacks that model streams.
Definition PdfToolbox_PtxSys.h:44
TTell pfTell
Get current byte position.
Definition PdfToolbox_PtxSys.h:57
void * m_handle
Stream handle.
Definition PdfToolbox_PtxSys.h:73
TRelease pfRelease
Release handle.
Definition PdfToolbox_PtxSys.h:70
TGetLength pfGetLength
Get length of stream in bytes.
Definition PdfToolbox_PtxSys.h:46
TSeek pfSeek
Set position.
Definition PdfToolbox_PtxSys.h:52
TRead pfRead
Read nSize bytes from stream.
Definition PdfToolbox_PtxSys.h:62
TWrite pfWrite
Write nSize bytes to stream.
Definition PdfToolbox_PtxSys.h:67