LCOV - code coverage report
Current view: top level - ogr/ogrsf_frmts/dxf - ogrdxfdriver.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 69 70 98.6 %
Date: 2025-01-18 02:53:07 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  DXF Translator
       4             :  * Purpose:  Implements OGRDXFDriver.
       5             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 2009, Frank Warmerdam <warmerdam@pobox.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "ogr_dxf.h"
      14             : #include "cpl_conv.h"
      15             : 
      16             : /************************************************************************/
      17             : /*                       OGRDXFDriverIdentify()                         */
      18             : /************************************************************************/
      19             : 
      20       46346 : static int OGRDXFDriverIdentify(GDALOpenInfo *poOpenInfo)
      21             : 
      22             : {
      23       46346 :     if (poOpenInfo->fpL == nullptr || poOpenInfo->nHeaderBytes == 0)
      24       43372 :         return FALSE;
      25        2974 :     if (poOpenInfo->IsExtensionEqualToCI("dxf"))
      26         256 :         return TRUE;
      27        2718 :     const char *pszIter = (const char *)poOpenInfo->pabyHeader;
      28        2718 :     bool bFoundZero = false;
      29        2718 :     int i = 0;  // Used after for.
      30     1066920 :     for (; pszIter[i]; i++)
      31             :     {
      32     1064210 :         if (pszIter[i] == '0')
      33             :         {
      34       45194 :             int j = i - 1;  // Used after for.
      35       46910 :             for (; j >= 0; j--)
      36             :             {
      37       46902 :                 if (pszIter[j] != ' ')
      38       45186 :                     break;
      39             :             }
      40       45194 :             if (j < 0 || pszIter[j] == '\n' || pszIter[j] == '\r')
      41             :             {
      42          15 :                 bFoundZero = true;
      43          15 :                 break;
      44             :             }
      45             :         }
      46             :     }
      47        2718 :     if (!bFoundZero)
      48        2703 :         return FALSE;
      49          15 :     i++;
      50          22 :     while (pszIter[i] == ' ')
      51           7 :         i++;
      52          18 :     while (pszIter[i] == '\n' || pszIter[i] == '\r')
      53           3 :         i++;
      54          15 :     if (!STARTS_WITH_CI(pszIter + i, "SECTION"))
      55          13 :         return FALSE;
      56           2 :     i += static_cast<int>(strlen("SECTION"));
      57           2 :     return pszIter[i] == '\n' || pszIter[i] == '\r';
      58             : }
      59             : 
      60             : /************************************************************************/
      61             : /*                                Open()                                */
      62             : /************************************************************************/
      63             : 
      64         124 : static GDALDataset *OGRDXFDriverOpen(GDALOpenInfo *poOpenInfo)
      65             : 
      66             : {
      67         124 :     if (!OGRDXFDriverIdentify(poOpenInfo))
      68           0 :         return nullptr;
      69             : 
      70         124 :     OGRDXFDataSource *poDS = new OGRDXFDataSource();
      71             : 
      72         124 :     if (!poDS->Open(poOpenInfo->pszFilename, false,
      73         124 :                     poOpenInfo->papszOpenOptions))
      74             :     {
      75           1 :         delete poDS;
      76           1 :         poDS = nullptr;
      77             :     }
      78             : 
      79         124 :     return poDS;
      80             : }
      81             : 
      82             : /************************************************************************/
      83             : /*                              Create()                                */
      84             : /************************************************************************/
      85             : 
      86             : static GDALDataset *
      87          57 : OGRDXFDriverCreate(const char *pszName, CPL_UNUSED int nBands,
      88             :                    CPL_UNUSED int nXSize, CPL_UNUSED int nYSize,
      89             :                    CPL_UNUSED GDALDataType eDT, char **papszOptions)
      90             : {
      91          57 :     OGRDXFWriterDS *poDS = new OGRDXFWriterDS();
      92             : 
      93          57 :     if (poDS->Open(pszName, papszOptions))
      94          56 :         return poDS;
      95             :     else
      96             :     {
      97           1 :         delete poDS;
      98           1 :         return nullptr;
      99             :     }
     100             : }
     101             : 
     102             : /************************************************************************/
     103             : /*                           RegisterOGRDXF()                           */
     104             : /************************************************************************/
     105             : 
     106        1688 : void RegisterOGRDXF()
     107             : 
     108             : {
     109        1688 :     if (GDALGetDriverByName("DXF") != nullptr)
     110         304 :         return;
     111             : 
     112        1384 :     GDALDriver *poDriver = new GDALDriver();
     113             : 
     114        1384 :     poDriver->SetDescription("DXF");
     115        1384 :     poDriver->SetMetadataItem(GDAL_DCAP_VECTOR, "YES");
     116        1384 :     poDriver->SetMetadataItem(GDAL_DCAP_CREATE_LAYER, "YES");
     117        1384 :     poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, "AutoCAD DXF");
     118        1384 :     poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "dxf");
     119        1384 :     poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/vector/dxf.html");
     120        1384 :     poDriver->SetMetadataItem(GDAL_DCAP_Z_GEOMETRIES, "YES");
     121        1384 :     poDriver->SetMetadataItem(GDAL_DMD_SUPPORTED_SQL_DIALECTS, "OGRSQL SQLITE");
     122             : 
     123        1384 :     poDriver->SetMetadataItem(
     124             :         GDAL_DMD_CREATIONOPTIONLIST,
     125             :         "<CreationOptionList>"
     126             :         "  <Option name='HEADER' type='string' description='Template header "
     127             :         "file' default='header.dxf'/>"
     128             :         "  <Option name='TRAILER' type='string' description='Template trailer "
     129             :         "file' default='trailer.dxf'/>"
     130             :         "  <Option name='FIRST_ENTITY' type='int' description='Identifier of "
     131             :         "first entity'/>"
     132             :         "  <Option name='INSUNITS' type='string-select' "
     133             :         "description='Drawing units for the model space ($INSUNITS system "
     134             :         "variable)' default='AUTO'>"
     135             :         "    <Value>AUTO</Value>"
     136             :         "    <Value>HEADER_VALUE</Value>"
     137             :         "    <Value alias='0'>UNITLESS</Value>"
     138             :         "    <Value alias='1'>INCHES</Value>"
     139             :         "    <Value alias='2'>FEET</Value>"
     140             :         "    <Value alias='4'>MILLIMETERS</Value>"
     141             :         "    <Value alias='5'>CENTIMETERS</Value>"
     142             :         "    <Value alias='6'>METERS</Value>"
     143             :         "    <Value alias='21'>US_SURVEY_FEET</Value>"
     144             :         "  </Option>"
     145             :         "  <Option name='MEASUREMENT' type='string-select' "
     146             :         "description='Whether the current drawing uses imperial or metric "
     147             :         "hatch "
     148             :         "pattern and linetype ($MEASUREMENT system variable)' "
     149             :         "default='HEADER_VALUE'>"
     150             :         "    <Value>HEADER_VALUE</Value>"
     151             :         "    <Value alias='0'>IMPERIAL</Value>"
     152             :         "    <Value alias='1'>METRIC</Value>"
     153             :         "  </Option>"
     154        1384 :         "</CreationOptionList>");
     155             : 
     156        1384 :     poDriver->SetMetadataItem(
     157             :         GDAL_DMD_OPENOPTIONLIST,
     158             :         "<OpenOptionList>"
     159             :         "  <Option name='CLOSED_LINE_AS_POLYGON' type='boolean' description="
     160             :         "'Whether to expose closed POLYLINE/LWPOLYLINE as polygons' "
     161             :         "default='NO'/>"
     162             :         "  <Option name='INLINE_BLOCKS' type='boolean' description="
     163             :         "'Whether INSERT entities are exploded with the geometry of the BLOCK "
     164             :         "they reference' default='YES'/>"
     165             :         "  <Option name='MERGE_BLOCK_GEOMETRIES' type='boolean' description="
     166             :         "'Whether blocks should be merged into a compound geometry' "
     167             :         "default='YES'/>"
     168             :         "  <Option name='TRANSLATE_ESCAPE_SEQUENCES' type='boolean' "
     169             :         "description="
     170             :         "'Whether character escapes are honored where applicable, and MTEXT "
     171             :         "control sequences are stripped' default='YES'/>"
     172             :         "  <Option name='INCLUDE_RAW_CODE_VALUES' type='boolean' description="
     173             :         "'Whether a RawCodeValues field should be added to contain all group "
     174             :         "codes and values' default='NO'/>"
     175             :         "  <Option name='3D_EXTENSIBLE_MODE' type='boolean' description="
     176             :         "'Whether to include ASM entities with the raw ASM data stored in a "
     177             :         "field' default='NO'/>"
     178             :         "  <Option name='HATCH_TOLEARNCE' type='float' description="
     179             :         "'Tolerance used when looking for the next component to add to the "
     180             :         "hatch boundary.'/>"
     181             :         "  <Option name='ENCODING' type='string' description="
     182             :         "'Encoding name, as supported by iconv, to override $DWGCODEPAGE'/>"
     183        1384 :         "</OpenOptionList>");
     184             : 
     185        1384 :     poDriver->SetMetadataItem(GDAL_DS_LAYER_CREATIONOPTIONLIST,
     186        1384 :                               "<LayerCreationOptionList/>");
     187             : 
     188        1384 :     poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES");
     189        1384 :     poDriver->SetMetadataItem(GDAL_DCAP_FEATURE_STYLES, "YES");
     190        1384 :     poDriver->SetMetadataItem(GDAL_DCAP_FEATURE_STYLES_READ, "YES");
     191        1384 :     poDriver->SetMetadataItem(GDAL_DCAP_FEATURE_STYLES_WRITE, "YES");
     192        1384 :     poDriver->SetMetadataItem(GDAL_DCAP_MULTIPLE_VECTOR_LAYERS, "YES");
     193             : 
     194        1384 :     poDriver->pfnOpen = OGRDXFDriverOpen;
     195        1384 :     poDriver->pfnIdentify = OGRDXFDriverIdentify;
     196        1384 :     poDriver->pfnCreate = OGRDXFDriverCreate;
     197             : 
     198        1384 :     GetGDALDriverManager()->RegisterDriver(poDriver);
     199             : }

Generated by: LCOV version 1.14