PDF SDK Documentation

Comprehensive Guide for Developers: Features, Integration, and API Reference

Loading...
Searching...
No Matches
helpers.h
1// Copyright (c) 2009-2025 Avanquest Software. All rights reserved.
2
3#ifndef PDFSDK_CXX_TYPES_H_INCLUDED_
4#define PDFSDK_CXX_TYPES_H_INCLUDED_
5
6#include <chrono>
7#include <functional>
8#include <string>
9#include <vector>
10
11#include <pdfsdk/cxx/bytes.h>
12#include <pdfsdk/cxx/exception.h>
13#include <pdfsdk/core/types.h>
14
15namespace PDF {
16
17inline bool IsValidPDDateTime(const PDDateTime& pddt) {
18 if (pddt.year < 1900)
19 return false;
20 if (pddt.month < 1 || pddt.month > 12)
21 return false;
22 if (pddt.day < 1 || pddt.day > 31)
23 return false;
24 if (pddt.hour < 0 || pddt.hour > 23)
25 return false;
26 if (pddt.minute < 0 || pddt.minute > 59)
27 return false;
28 if (pddt.second < 0 || pddt.second > 59)
29 return false;
30
31 return true;
32}
33
34inline std::chrono::system_clock::time_point PDDateTimeToStdTimePoint(const PDDateTime& pddt) {
35 std::tm tm = {};
36 tm.tm_year = pddt.year - 1900;
37 tm.tm_mon = pddt.month - 1;
38 tm.tm_mday = pddt.day;
39 tm.tm_hour = pddt.hour;
40 tm.tm_min = pddt.minute;
41 tm.tm_sec = pddt.second;
42
43 auto time_point = std::chrono::system_clock::from_time_t(std::mktime(&tm));
44 if (pddt.tz_sign == 0)
45 return time_point;
46
47 auto utc_offset = std::chrono::hours(pddt.tz_hour) + std::chrono::minutes(pddt.tz_minute);
48 return pddt.tz_sign > 0
49 ? time_point - utc_offset
50 : time_point + utc_offset;
51}
52
53namespace detail {
54
55template<typename CallableGetter, typename... Args>
56std::wstring GetWstringProperty(CallableGetter getter, Args&&... args) {
57 size_t valueSize = 0;
58 auto ec = std::invoke(getter, args..., nullptr, 0, &valueSize);
59 if (ec == kPDErrSuccess)
60 return std::wstring{};
61 if (ec != kPDErrBufferTooSmall)
62 PDF_CHECK_SUCCESS_X(ec);
63 std::wstring valueString(valueSize, L'\0');
64 PDF_CHECK_SUCCESS_X(std::invoke(getter, args..., &valueString[0], valueSize, &valueSize));
65 return valueString;
66}
67
68template<typename CallableGetter, typename... Args>
69std::string GetStringProperty(CallableGetter getter, Args&&... args) {
70 size_t valueSize = 0;
71 auto ec = std::invoke(getter, args..., nullptr, 0, &valueSize);
72 if (ec == kPDErrSuccess)
73 return std::string{};
74 if (ec != kPDErrBufferTooSmall)
75 PDF_CHECK_SUCCESS_X(ec);
76 std::string valueString(valueSize, '\0');
77 PDF_CHECK_SUCCESS_X(std::invoke(getter, args..., &valueString[0], valueSize, &valueSize));
78 return valueString;
79}
80
81template<typename CallableGetter, typename... Args>
82std::vector<Byte> GetBytesProperty(CallableGetter getter, Args&&... args) {
83 size_t valueSize = 0;
84 auto ec = std::invoke(getter, args..., nullptr, 0, &valueSize);
85 if (ec == kPDErrSuccess)
86 return std::vector<Byte>{};
87 if (ec != kPDErrBufferTooSmall)
88 PDF_CHECK_SUCCESS_X(ec);
89 std::vector<Byte> value(valueSize);
90 PDF_CHECK_SUCCESS_X(std::invoke(getter, args..., value.data(), valueSize, &valueSize));
91 return value;
92}
93
94} // namespace detail
95} // namespace PDF
96
97#endif // PDFSDK_CXX_TYPES_H_INCLUDED_
@ kPDErrSuccess
Operation was successful.
Definition errors.h:18
@ kPDErrBufferTooSmall
Memory buffer too small, increase buffer size.
Definition errors.h:25
int month
The month in the range 1..12.
Definition types.h:25
int tz_sign
The "sign" of the time zone, (0) means UTC, (-1) is west, (+1) is east.
Definition types.h:30
int year
The year.
Definition types.h:24
int tz_hour
The time zone hour in the range 0..23.
Definition types.h:31
int day
The day of the month in the range 1..31.
Definition types.h:26
int hour
The hour in the range 0..23.
Definition types.h:27
int tz_minute
The time zone minute in the range 0..59.
Definition types.h:32
int second
The second in the range 0..59.
Definition types.h:29
int minute
The minute in the range 0..59.
Definition types.h:28
Common types.