GDAL

commonutils.h
1/******************************************************************************
2 * $Id$
3 *
4 * Project: GDAL Utilities
5 * Purpose: Common utility routines
6 * Author: Even Rouault, <even dot rouault at spatialys.com>
7 *
8 ******************************************************************************
9 * Copyright (c) 2011-2012, Even Rouault <even dot rouault at spatialys.com>
10 *
11 * SPDX-License-Identifier: MIT
12 ****************************************************************************/
13
14#ifndef COMMONUTILS_H_INCLUDED
15#define COMMONUTILS_H_INCLUDED
16
17#include "cpl_port.h"
18
19#ifdef __cplusplus
20
21#if defined(_WIN32) && (defined(_MSC_VER) || defined(SUPPORTS_WMAIN))
22
23#include <wchar.h>
24#include <stdlib.h>
25#include "cpl_conv.h"
26#include "cpl_string.h"
27
28 class ARGVDestroyer
29{
30 char **m_papszList = nullptr;
31 ARGVDestroyer(const ARGVDestroyer &) = delete;
32 ARGVDestroyer &operator=(const ARGVDestroyer &) = delete;
33
34 public:
35 explicit ARGVDestroyer(char **papszList) : m_papszList(papszList)
36 {
37 }
38
39 ~ARGVDestroyer()
40 {
41 CSLDestroy(m_papszList);
42 }
43};
44
45 extern "C" int wmain(int argc, wchar_t **argv_w, wchar_t ** /* envp */);
46
47#define MAIN_START(argc, argv) \
48 extern "C" int wmain(int argc, wchar_t **argv_w, wchar_t ** /* envp */) \
49 { \
50 char **argv = \
51 static_cast<char **>(CPLCalloc(argc + 1, sizeof(char *))); \
52 for (int i = 0; i < argc; i++) \
53 { \
54 argv[i] = \
55 CPLRecodeFromWChar(argv_w[i], CPL_ENC_UCS2, CPL_ENC_UTF8); \
56 } \
57 ARGVDestroyer argvDestroyer(argv); \
58 try \
59 {
60
61#else // defined(_WIN32)
62
63#define MAIN_START(argc, argv) \
64 int main(int argc, char **argv) \
65 { \
66 try \
67 {
68
69#endif // defined(_WIN32)
70
71#define MAIN_END \
72 } \
73 catch (const std::exception &e) \
74 { \
75 fprintf(stderr, "Unexpected exception: %s", e.what()); \
76 return -1; \
77 } \
78 }
79
80#endif // defined(__cplusplus)
81
82 CPL_C_START
83
84 void CPL_DLL EarlySetConfigOptions(int argc, char **argv);
85
86 CPL_C_END
87
88#ifdef __cplusplus
89
90#include "cpl_string.h"
91#include <vector>
92
93std::vector<std::string> CPL_DLL
94GetOutputDriversFor(const char *pszDestFilename, int nFlagRasterVector);
95 CPLString CPL_DLL GetOutputDriverForRaster(const char *pszDestFilename);
96 void GDALRemoveBOM(GByte *pabyData);
97std::string GDALRemoveSQLComments(const std::string &osInput);
98
99 int ArgIsNumeric(const char *pszArg);
100
101// those values shouldn't be changed, because overview levels >= 0 are meant
102// to be overview indices, and ovr_level < OVR_LEVEL_AUTO mean overview level
103// automatically selected minus (OVR_LEVEL_AUTO - ovr_level)
104 constexpr int OVR_LEVEL_AUTO = -2;
105 constexpr int OVR_LEVEL_NONE = -1;
106
107#endif /* __cplusplus */
108
109#endif /* COMMONUTILS_H_INCLUDED */
CPLString
Convenient string class based on std::string.
Definition: cpl_string.h:307
cpl_conv.h
Various convenience functions for CPL.
cpl_port.h
Core portability definitions for CPL.
CPL_C_END
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:283
CPL_C_START
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:279
GByte
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:169
cpl_string.h
Various convenience functions for working with strings and string lists.
CSLDestroy
void CSLDestroy(char **papszStrList)
Free string list.
Definition: cpl_string.cpp:185