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 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 ****************************************************************************/
29
30#ifndef COMMONUTILS_H_INCLUDED
31#define COMMONUTILS_H_INCLUDED
32
33#include "cpl_port.h"
34
35#ifdef __cplusplus
36
37#if defined(_WIN32) && (defined(_MSC_VER) || defined(SUPPORTS_WMAIN))
38
39#include <wchar.h>
40#include <stdlib.h>
41#include "cpl_conv.h"
42#include "cpl_string.h"
43
44class ARGVDestroyer
45{
46 char **m_papszList = nullptr;
47 ARGVDestroyer(const ARGVDestroyer &) = delete;
48 ARGVDestroyer &operator=(const ARGVDestroyer &) = delete;
49
50 public:
51 explicit ARGVDestroyer(char **papszList) : m_papszList(papszList)
52 {
53 }
54
55 ~ARGVDestroyer()
56 {
57 CSLDestroy(m_papszList);
58 }
59};
60
61extern "C" int wmain(int argc, wchar_t **argv_w, wchar_t ** /* envp */);
62
63#define MAIN_START(argc, argv) \
64 extern "C" int wmain(int argc, wchar_t **argv_w, wchar_t ** /* envp */) \
65 { \
66 char **argv = \
67 static_cast<char **>(CPLCalloc(argc + 1, sizeof(char *))); \
68 for (int i = 0; i < argc; i++) \
69 { \
70 argv[i] = \
71 CPLRecodeFromWChar(argv_w[i], CPL_ENC_UCS2, CPL_ENC_UTF8); \
72 } \
73 ARGVDestroyer argvDestroyer(argv); \
74 try \
75 {
76
77#else // defined(_WIN32)
78
79#define MAIN_START(argc, argv) \
80 int main(int argc, char **argv) \
81 { \
82 try \
83 {
84
85#endif // defined(_WIN32)
86
87#define MAIN_END \
88 } \
89 catch (const std::exception &e) \
90 { \
91 fprintf(stderr, "Unexpected exception: %s", e.what()); \
92 return -1; \
93 } \
94 }
95
96#endif // defined(__cplusplus)
97
98 CPL_C_START
99
100void CPL_DLL EarlySetConfigOptions(int argc, char **argv);
101
102 CPL_C_END
103
104#ifdef __cplusplus
105
106#include "cpl_string.h"
107#include <vector>
108
109std::vector<std::string> CPL_DLL
110GetOutputDriversFor(const char *pszDestFilename, int nFlagRasterVector);
111 CPLString CPL_DLL GetOutputDriverForRaster(const char *pszDestFilename);
112void GDALRemoveBOM(GByte *pabyData);
113std::string GDALRemoveSQLComments(const std::string &osInput);
114
115int ArgIsNumeric(const char *pszArg);
116
117// those values shouldn't be changed, because overview levels >= 0 are meant
118// to be overview indices, and ovr_level < OVR_LEVEL_AUTO mean overview level
119// automatically selected minus (OVR_LEVEL_AUTO - ovr_level)
120constexpr int OVR_LEVEL_AUTO = -2;
121constexpr int OVR_LEVEL_NONE = -1;
122
123#endif /* __cplusplus */
124
125#endif /* COMMONUTILS_H_INCLUDED */
CPLString
Convenient string class based on std::string.
Definition: cpl_string.h:320
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:299
CPL_C_START
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:295
GByte
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:185
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:200