Line data Source code
1 : /*******************************************************************************
2 : * Project: OGR CAD Driver
3 : * Purpose: Implements driver based on libopencad
4 : * Author: Alexandr Borzykh, mush3d at gmail.com
5 : * Author: Dmitry Baryshnikov, polimax@mail.ru
6 : * Language: C++
7 : *******************************************************************************
8 : * The MIT License (MIT)
9 : *
10 : * Copyright (c) 2016 Alexandr Borzykh
11 : * Copyright (c) 2016, NextGIS
12 : *
13 : * SPDX-License-Identifier: MIT
14 : *******************************************************************************/
15 : #include "ogr_cad.h"
16 : #include "vsilfileio.h"
17 : #include "ogrcaddrivercore.h"
18 :
19 : /************************************************************************/
20 : /* OGRCADDriverOpen() */
21 : /************************************************************************/
22 :
23 9 : static GDALDataset *OGRCADDriverOpen(GDALOpenInfo *poOpenInfo)
24 : {
25 9 : long nSubRasterLayer = -1;
26 9 : long nSubRasterFID = -1;
27 :
28 : CADFileIO *pFileIO;
29 9 : if (STARTS_WITH_CI(poOpenInfo->pszFilename, "CAD:"))
30 : {
31 : char **papszTokens =
32 0 : CSLTokenizeString2(poOpenInfo->pszFilename, ":", 0);
33 0 : int nTokens = CSLCount(papszTokens);
34 0 : if (nTokens < 4)
35 : {
36 0 : CSLDestroy(papszTokens);
37 0 : return nullptr;
38 : }
39 :
40 0 : CPLString osFilename;
41 0 : for (int i = 1; i < nTokens - 2; ++i)
42 : {
43 0 : if (osFilename.empty())
44 0 : osFilename += ":";
45 0 : osFilename += papszTokens[i];
46 : }
47 :
48 0 : pFileIO = new VSILFileIO(osFilename);
49 0 : nSubRasterLayer = atol(papszTokens[nTokens - 2]);
50 0 : nSubRasterFID = atol(papszTokens[nTokens - 1]);
51 :
52 0 : CSLDestroy(papszTokens);
53 : }
54 : else
55 : {
56 9 : pFileIO = new VSILFileIO(poOpenInfo->pszFilename);
57 : }
58 :
59 9 : if (IdentifyCADFile(pFileIO, false) == FALSE)
60 : {
61 0 : delete pFileIO;
62 0 : return nullptr;
63 : }
64 :
65 : /* -------------------------------------------------------------------- */
66 : /* Confirm the requested access is supported. */
67 : /* -------------------------------------------------------------------- */
68 9 : if (poOpenInfo->eAccess == GA_Update)
69 : {
70 0 : CPLError(CE_Failure, CPLE_NotSupported,
71 : "The CAD driver does not support update access to existing"
72 : " datasets.\n");
73 0 : delete pFileIO;
74 0 : return nullptr;
75 : }
76 :
77 9 : GDALCADDataset *poDS = new GDALCADDataset();
78 9 : if (!poDS->Open(poOpenInfo, pFileIO, nSubRasterLayer, nSubRasterFID))
79 : {
80 1 : delete poDS;
81 1 : return nullptr;
82 : }
83 : else
84 8 : return poDS;
85 : }
86 :
87 : /************************************************************************/
88 : /* RegisterOGRCAD() */
89 : /************************************************************************/
90 :
91 8 : void RegisterOGRCAD()
92 : {
93 8 : if (GDALGetDriverByName(DRIVER_NAME) == nullptr)
94 : {
95 8 : auto poDriver = new GDALDriver();
96 8 : OGRCADDriverSetCommonMetadata(poDriver);
97 :
98 8 : poDriver->pfnOpen = OGRCADDriverOpen;
99 :
100 8 : GetGDALDriverManager()->RegisterDriver(poDriver);
101 : }
102 8 : }
103 :
104 17 : CPLString CADRecode(const CPLString &sString, int CADEncoding)
105 : {
106 17 : const char *const apszSource[] = {/* 0 UNDEFINED */ "",
107 : /* 1 ASCII */ "US-ASCII",
108 : /* 2 8859_1 */ "ISO-8859-1",
109 : /* 3 8859_2 */ "ISO-8859-2",
110 : /* 4 UNDEFINED */ "",
111 : /* 5 8859_4 */ "ISO-8859-4",
112 : /* 6 8859_5 */ "ISO-8859-5",
113 : /* 7 8859_6 */ "ISO-8859-6",
114 : /* 8 8859_7 */ "ISO-8859-7",
115 : /* 9 8859_8 */ "ISO-8859-8",
116 : /* 10 8859_9 */ "ISO-8859-9",
117 : /* 11 DOS437 */ "CP437",
118 : /* 12 DOS850 */ "CP850",
119 : /* 13 DOS852 */ "CP852",
120 : /* 14 DOS855 */ "CP855",
121 : /* 15 DOS857 */ "CP857",
122 : /* 16 DOS860 */ "CP860",
123 : /* 17 DOS861 */ "CP861",
124 : /* 18 DOS863 */ "CP863",
125 : /* 19 DOS864 */ "CP864",
126 : /* 20 DOS865 */ "CP865",
127 : /* 21 DOS869 */ "CP869",
128 : /* 22 DOS932 */ "CP932",
129 : /* 23 MACINTOSH */ "MACINTOSH",
130 : /* 24 BIG5 */ "BIG5",
131 : /* 25 KSC5601 */ "CP949",
132 : /* 26 JOHAB */ "JOHAB",
133 : /* 27 DOS866 */ "CP866",
134 : /* 28 ANSI_1250 */ "CP1250",
135 : /* 29 ANSI_1251 */ "CP1251",
136 : /* 30 ANSI_1252 */ "CP1252",
137 : /* 31 GB2312 */ "GB2312",
138 : /* 32 ANSI_1253 */ "CP1253",
139 : /* 33 ANSI_1254 */ "CP1254",
140 : /* 34 ANSI_1255 */ "CP1255",
141 : /* 35 ANSI_1256 */ "CP1256",
142 : /* 36 ANSI_1257 */ "CP1257",
143 : /* 37 ANSI_874 */ "CP874",
144 : /* 38 ANSI_932 */ "CP932",
145 : /* 39 ANSI_936 */ "CP936",
146 : /* 40 ANSI_949 */ "CP949",
147 : /* 41 ANSI_950 */ "CP950",
148 : /* 42 ANSI_1361 */ "CP1361",
149 : /* 43 ANSI_1200 */ "UTF-16",
150 : /* 44 ANSI_1258 */ "CP1258"};
151 :
152 17 : if (CADEncoding > 0 &&
153 17 : CADEncoding < static_cast<int>(CPL_ARRAYSIZE(apszSource)) &&
154 : CADEncoding != 4)
155 : {
156 : char *pszRecoded =
157 17 : CPLRecode(sString, apszSource[CADEncoding], CPL_ENC_UTF8);
158 34 : CPLString soRecoded(pszRecoded);
159 17 : CPLFree(pszRecoded);
160 17 : return soRecoded;
161 : }
162 0 : CPLError(CE_Failure, CPLE_NotSupported,
163 : "CADRecode() function does not support provided CADEncoding.");
164 0 : return CPLString("");
165 : }
|