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 * Classification: CONFIDENTIAL
13 *
14 *****************************************************************************/
15
16#ifndef PDFTOOLS_PDFTOOLSSYS_H__
17#define PDFTOOLS_PDFTOOLSSYS_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 PdfToolsSys_PathStreamDescriptor_Create PdfToolsSys_PathStreamDescriptor_CreateW
31#else
32#define PdfToolsSys_PathStreamDescriptor_Create PdfToolsSys_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 PdfToolsSysFILEPtrSeek__(void* handle, pos_t iPos)
109{
110 return fseek((FILE*)handle, (long)iPos, SEEK_SET) == 0 ? 1 : 0;
111}
112
119static pos_t STDCALL PdfToolsSysFILEPtrTell__(void* handle) { return ftell((FILE*)handle); }
120
129static size_t STDCALL PdfToolsSysFILEPtrRead__(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 PdfToolsSysFILEPtrWrite__(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 PdfToolsSysFILEPtrRelease__(void* handle) { fclose((FILE*)handle); }
158
168 int bCloseOnRelease)
169{
170 pDescriptor->m_handle = handle;
172 pDescriptor->pfSeek = &PdfToolsSysFILEPtrSeek__;
173 pDescriptor->pfTell = &PdfToolsSysFILEPtrTell__;
174 pDescriptor->pfRead = &PdfToolsSysFILEPtrRead__;
175 pDescriptor->pfWrite = &PdfToolsSysFILEPtrWrite__;
176 pDescriptor->pfRelease = bCloseOnRelease ? &PdfToolsSysFILEPtrRelease__ : NULL; // NOLINT(modernize-use-nullptr)
177}
178#endif // NO_FILE_STREAM_DESCRIPTOR
179
180/******************************************************************************
181 * Path Stream Descriptor convenience function
182 *****************************************************************************/
183
193 const char* szPath, BOOL bIsReadOnly);
203 const WCHAR* szPath, BOOL bIsReadOnly);
209
210#ifdef __cplusplus
211}
212#endif
213
214#endif /* PDFTOOLS_PDFTOOLSSYS_H__ */
#define PDFTOOLS_CALL
Definition PdfTools_PdfTools.h:21
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:87
static int STDCALL PdfToolsSysFILEPtrSeek__(void *handle, pos_t iPos)
Callback implementation for TPdfToolsSys_StreamDescriptor::pfSeek for FILE*.
Definition PdfTools_PdfToolsSys.h:108
size_t(STDCALL * TWrite)(void *handle, const void *pData, size_t nSize)
Definition PdfTools_PdfToolsSys.h:39
size_t(STDCALL * TRead)(void *handle, void *pData, size_t nSize)
Definition PdfTools_PdfToolsSys.h:38
int(STDCALL * TSeek)(void *handle, pos_t iPos)
Definition PdfTools_PdfToolsSys.h:36
void(STDCALL * TRelease)(void *handle)
Definition PdfTools_PdfToolsSys.h:40
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:129
pos_t(STDCALL * TTell)(void *handle)
Definition PdfTools_PdfToolsSys.h:37
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:35
static pos_t STDCALL PdfToolsSysFILEPtrTell__(void *handle)
Callback implementation for TPdfToolsSys_StreamDescriptor::pfTell for FILE*.
Definition PdfTools_PdfToolsSys.h:119
static void STDCALL PdfToolsSysFILEPtrRelease__(void *handle)
Callback implementation for TPdfToolsSys_StreamDescriptor::pfRelease for FILE*.
Definition PdfTools_PdfToolsSys.h:157
static void PdfToolsSysCreateFILEStreamDescriptor(TPdfToolsSys_StreamDescriptor *pDescriptor, FILE *handle, int bCloseOnRelease)
Initialization function for TPdfToolsSys_StreamDescriptor for FILE*.
Definition PdfTools_PdfToolsSys.h:167
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:145
#define PDFTOOLS_EXPORT
Definition PdfTools_Platform.h:138
#define STDCALL
Definition PdfTools_Platform.h:96
unsigned short WCHAR
Definition PdfTools_Platform.h:90
long long pos_t
Definition PdfTools_Platform.h:98
#define BOOL
Definition PdfTools_Types.h:18
Structure that groups a set of callbacks that model streams.
Definition PdfTools_PdfToolsSys.h:44
TTell pfTell
Get current byte position.
Definition PdfTools_PdfToolsSys.h:57
TWrite pfWrite
Write nSize bytes to stream.
Definition PdfTools_PdfToolsSys.h:67
TRead pfRead
Read nSize bytes from stream.
Definition PdfTools_PdfToolsSys.h:62
TSeek pfSeek
Set position.
Definition PdfTools_PdfToolsSys.h:52
TRelease pfRelease
Release handle.
Definition PdfTools_PdfToolsSys.h:70
TGetLength pfGetLength
Get length of stream in bytes.
Definition PdfTools_PdfToolsSys.h:46
void * m_handle
Stream handle.
Definition PdfTools_PdfToolsSys.h:73