Pdftools SDK
Loading...
Searching...
No Matches
PdfTools_PdfToolsSys.h
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * File: PdfTools_PdfToolsSys.h
4 *
5 * Description: Sytem declaration file
6 *
7 * Author: PDF Tools AG
8 *
9 * Copyright: Copyright (C) 2023 - 2024 PDF Tools AG, Switzerland
10 * All rights reserved.
11 *
12 * Notice: By downloading and using this artifact, you accept PDF Tools AG's
13 * [license agreement](https://www.pdf-tools.com/license-agreement/),
14 * [privacy policy](https://www.pdf-tools.com/privacy-policy/),
15 * and allow PDF Tools AG to track your usage data.
16 *
17 * Classification: CONFIDENTIAL
18 *
19 *****************************************************************************/
20
21#ifndef PDFTOOLS_PDFTOOLSSYS_H__
22#define PDFTOOLS_PDFTOOLSSYS_H__
23
24#include <stdarg.h>
25#ifndef NO_FILE_STREAM_DESCRIPTOR
26#include <stdio.h>
27#endif
28
29#ifdef __cplusplus
30extern "C"
31{
32#endif
33
34#ifdef _UNICODE
35#define PdfToolsSys_PathStreamDescriptor_Create PdfToolsSys_PathStreamDescriptor_CreateW
36#else
37#define PdfToolsSys_PathStreamDescriptor_Create PdfToolsSys_PathStreamDescriptor_CreateA
38#endif
39
40typedef pos_t(STDCALL* TGetLength)(void* handle);
41typedef int(STDCALL* TSeek)(void* handle, pos_t iPos);
42typedef pos_t(STDCALL* TTell)(void* handle);
43typedef size_t(STDCALL* TRead)(void* handle, void* pData, size_t nSize);
44typedef size_t(STDCALL* TWrite)(void* handle, const void* pData, size_t nSize);
45typedef void(STDCALL* TRelease)(void* handle);
46
80
81#ifndef NO_FILE_STREAM_DESCRIPTOR
82/******************************************************************************
83 * Stream descriptor for FILE*
84 * (always use binary mode to fopen file!)
85 *****************************************************************************/
86
93{
94 pos_t iPos, nLen;
95 iPos = ftell((FILE*)handle);
96 if (iPos == -1)
97 return -1;
98 if (fseek((FILE*)handle, 0L, SEEK_END) != 0)
99 return -1;
100 nLen = ftell((FILE*)handle);
101 if (fseek((FILE*)handle, (long)iPos, SEEK_SET) != 0)
102 return -1;
103 return nLen;
104}
105
113static int STDCALL PdfToolsSysFILEPtrSeek__(void* handle, pos_t iPos)
114{
115 return fseek((FILE*)handle, (long)iPos, SEEK_SET) == 0 ? 1 : 0;
116}
117
124static pos_t STDCALL PdfToolsSysFILEPtrTell__(void* handle) { return ftell((FILE*)handle); }
125
134static size_t STDCALL PdfToolsSysFILEPtrRead__(void* handle, void* pData, size_t nSize)
135{
136 size_t nRead = fread(pData, 1, nSize, (FILE*)handle);
137 if (nRead != nSize && ferror((FILE*)handle) != 0)
138 return (size_t)-1;
139 return nRead;
140}
141
150static size_t STDCALL PdfToolsSysFILEPtrWrite__(void* handle, const void* pData, size_t nSize)
151{
152 if (fwrite(pData, 1, nSize, (FILE*)handle) != nSize)
153 return (size_t)-1;
154 return nSize;
155}
156
162static void STDCALL PdfToolsSysFILEPtrRelease__(void* handle) { fclose((FILE*)handle); }
163
173 int bCloseOnRelease)
174{
175 pDescriptor->m_handle = handle;
177 pDescriptor->pfSeek = &PdfToolsSysFILEPtrSeek__;
178 pDescriptor->pfTell = &PdfToolsSysFILEPtrTell__;
179 pDescriptor->pfRead = &PdfToolsSysFILEPtrRead__;
180 pDescriptor->pfWrite = &PdfToolsSysFILEPtrWrite__;
181 pDescriptor->pfRelease = bCloseOnRelease ? &PdfToolsSysFILEPtrRelease__ : NULL; // NOLINT(modernize-use-nullptr)
182}
183#endif // NO_FILE_STREAM_DESCRIPTOR
184
185/******************************************************************************
186 * Path Stream Descriptor convenience function
187 *****************************************************************************/
188
198 const char* szPath, BOOL bIsReadOnly);
208 const WCHAR* szPath, BOOL bIsReadOnly);
214
215/******************************************************************************
216 * Stream Descriptor for memory streams
217 *****************************************************************************/
218
226
232
233#ifdef __cplusplus
234}
235#endif
236
237#endif /* PDFTOOLS_PDFTOOLSSYS_H__ */
#define PDFTOOLS_CALL
Definition PdfTools_PdfTools.h:26
PDFTOOLS_EXPORT void PDFTOOLS_CALL PdfToolsSys_PathStreamDescriptor_Close(TPdfToolsSys_StreamDescriptor *pDescriptor)
Close the underlying file.
static pos_t STDCALL PdfToolsSysFILEPtrGetLength__(void *handle)
Callback implementation for TPdfToolsSys_StreamDescriptor::pfGetLength for FILE*.
Definition PdfTools_PdfToolsSys.h:92
static int STDCALL PdfToolsSysFILEPtrSeek__(void *handle, pos_t iPos)
Callback implementation for TPdfToolsSys_StreamDescriptor::pfSeek for FILE*.
Definition PdfTools_PdfToolsSys.h:113
size_t(STDCALL * TWrite)(void *handle, const void *pData, size_t nSize)
Definition PdfTools_PdfToolsSys.h:44
size_t(STDCALL * TRead)(void *handle, void *pData, size_t nSize)
Definition PdfTools_PdfToolsSys.h:43
int(STDCALL * TSeek)(void *handle, pos_t iPos)
Definition PdfTools_PdfToolsSys.h:41
void(STDCALL * TRelease)(void *handle)
Definition PdfTools_PdfToolsSys.h:45
PDFTOOLS_EXPORT BOOL PDFTOOLS_CALL PdfToolsSys_PathStreamDescriptor_CreateW(TPdfToolsSys_StreamDescriptor *pDescriptor, const WCHAR *szPath, BOOL bIsReadOnly)
Initialization function for TPdfToolsSys_StreamDescriptor for a given path.
static size_t STDCALL PdfToolsSysFILEPtrRead__(void *handle, void *pData, size_t nSize)
Callback implementation for TPdfToolsSys_StreamDescriptor::pfRead for FILE*.
Definition PdfTools_PdfToolsSys.h:134
pos_t(STDCALL * TTell)(void *handle)
Definition PdfTools_PdfToolsSys.h:42
PDFTOOLS_EXPORT void PDFTOOLS_CALL PdfToolsSys_MemoryStreamDescriptor_Close(TPdfToolsSys_StreamDescriptor *pDescriptor)
Delete the underlying buffer.
struct TPdfToolsSys_StreamDescriptor TPdfToolsSys_StreamDescriptor
Structure that groups a set of callbacks that model streams.
pos_t(STDCALL * TGetLength)(void *handle)
Definition PdfTools_PdfToolsSys.h:40
static pos_t STDCALL PdfToolsSysFILEPtrTell__(void *handle)
Callback implementation for TPdfToolsSys_StreamDescriptor::pfTell for FILE*.
Definition PdfTools_PdfToolsSys.h:124
static void STDCALL PdfToolsSysFILEPtrRelease__(void *handle)
Callback implementation for TPdfToolsSys_StreamDescriptor::pfRelease for FILE*.
Definition PdfTools_PdfToolsSys.h:162
static void PdfToolsSysCreateFILEStreamDescriptor(TPdfToolsSys_StreamDescriptor *pDescriptor, FILE *handle, int bCloseOnRelease)
Initialization function for TPdfToolsSys_StreamDescriptor for FILE*.
Definition PdfTools_PdfToolsSys.h:172
PDFTOOLS_EXPORT BOOL PDFTOOLS_CALL PdfToolsSys_MemoryStreamDescriptor_Create(TPdfToolsSys_StreamDescriptor *pDescriptor)
Initialization function for TPdfToolsSys_StreamDescriptor for the usage as memory streams.
PDFTOOLS_EXPORT BOOL PDFTOOLS_CALL PdfToolsSys_PathStreamDescriptor_CreateA(TPdfToolsSys_StreamDescriptor *pDescriptor, const char *szPath, BOOL bIsReadOnly)
Initialization function for TPdfToolsSys_StreamDescriptor for a given path.
static size_t STDCALL PdfToolsSysFILEPtrWrite__(void *handle, const void *pData, size_t nSize)
Callback implementation for TPdfToolsSys_StreamDescriptor::pfWrite for FILE*.
Definition PdfTools_PdfToolsSys.h:150
#define PDFTOOLS_EXPORT
Definition PdfTools_Platform.h:143
#define STDCALL
Definition PdfTools_Platform.h:101
unsigned short WCHAR
Definition PdfTools_Platform.h:95
long long pos_t
Definition PdfTools_Platform.h:103
#define BOOL
Definition PdfTools_Types.h:23
Structure that groups a set of callbacks that model streams.
Definition PdfTools_PdfToolsSys.h:49
TTell pfTell
Get current byte position.
Definition PdfTools_PdfToolsSys.h:62
TWrite pfWrite
Write nSize bytes to stream.
Definition PdfTools_PdfToolsSys.h:72
TRead pfRead
Read nSize bytes from stream.
Definition PdfTools_PdfToolsSys.h:67
TSeek pfSeek
Set position.
Definition PdfTools_PdfToolsSys.h:57
TRelease pfRelease
Release handle.
Definition PdfTools_PdfToolsSys.h:75
TGetLength pfGetLength
Get length of stream in bytes.
Definition PdfTools_PdfToolsSys.h:51
void * m_handle
Stream handle.
Definition PdfTools_PdfToolsSys.h:78