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
10#include <pdfsdk/cxx/exception.h>
11#include <pdfsdk/core/types.h>
12
13namespace PDF {
14
15inline std::chrono::system_clock::time_point PDDateTimeToStdTimePoint(const PDDateTime& pddt) {
16 std::tm tm = {
17 pddt.second, pddt.minute, pddt.hour, pddt.day, pddt.month - 1, pddt.year - 1900, 0, 0, 0};
18
19 auto time_point = std::chrono::system_clock::from_time_t(std::mktime(&tm));
20 if (pddt.tz_sign == 0)
21 return time_point;
22
23 auto utc_offset = std::chrono::hours(pddt.tz_hour) + std::chrono::minutes(pddt.tz_minute);
24 return pddt.tz_sign > 0
25 ? time_point - utc_offset
26 : time_point + utc_offset;
27}
28
29namespace detail {
30
31template<typename CallableGetter, typename... Args>
32std::wstring GetWstringProperty(CallableGetter getter, Args&&... args) {
33 size_t valueSize = 0;
34 auto ec = std::invoke(getter, args..., nullptr, 0, &valueSize);
35 if (ec == kPDErrSuccess)
36 return std::wstring{};
37 if (ec != kPDErrBufferTooSmall)
38 PDF_CHECK_SUCCESS_X(ec);
39 std::wstring valueString(valueSize, L'\0');
40 PDF_CHECK_SUCCESS_X(std::invoke(getter, args..., &valueString[0], valueSize, &valueSize));
41 return valueString;
42}
43
44template<typename CallableGetter, typename... Args>
45std::string GetStringProperty(CallableGetter getter, Args&&... args) {
46 size_t valueSize = 0;
47 auto ec = std::invoke(getter, args..., nullptr, 0, &valueSize);
48 if (ec == kPDErrSuccess)
49 return std::string{};
50 if (ec != kPDErrBufferTooSmall)
51 PDF_CHECK_SUCCESS_X(ec);
52 std::string valueString(valueSize, '\0');
53 PDF_CHECK_SUCCESS_X(std::invoke(getter, args..., &valueString[0], valueSize, &valueSize));
54 return valueString;
55}
56
57} // namespace detail
58} // namespace PDF
59
60#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
Definition types.h:23
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.