LCOV - code coverage report
Current view: directory - ogr/ogrsf_frmts/ili/iom - iom_file.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 194 62 32.0 %
Date: 2012-12-26 Functions: 47 11 23.4 %

       1                 : /**********************************************************************
       2                 :  * $Id: iom_file.cpp 12582 2007-10-29 09:38:21Z pka $
       3                 :  *
       4                 :  * Project:  iom - The INTERLIS Object Model
       5                 :  * Purpose:  For more information, please see <http://iom.sourceforge.net>
       6                 :  * Author:   Claude Eisenhut
       7                 :  *
       8                 :  **********************************************************************
       9                 :  * Copyright (c) 2007, Claude Eisenhut
      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 OR
      22                 :  * 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                 : 
      31                 : /** @file
      32                 :  * implementation of object file
      33                 :  * @defgroup file file level functions
      34                 :  * @{
      35                 :  */
      36                 : 
      37                 : #include <iostream>
      38                 : #include <string>
      39                 : 
      40                 : #include <iom/iom_p.h>
      41                 : 
      42                 : 
      43                 : /** open an INTERLIS XML file.
      44                 :  * @see IOM_CREATE IOM_DONTREAD
      45                 :  */
      46               0 : extern "C" IOM_FILE iom_open(const char *filename,int flags,const char *model)
      47                 : {
      48                 :   //__asm { int 3 };
      49               0 :   IomFile ret(dbgnew iom_file());
      50               0 :   ret->setFilename(filename);
      51               0 :   if(iom_fileexists(filename)){
      52                 :     // read file?
      53               0 :     if((flags & IOM_DONTREAD)==0){
      54               0 :       ret->readHeader(model);
      55                 :     }
      56                 :   }else{
      57                 :     // file doesn't exist
      58                 :     // do not create file?
      59               0 :     if((flags & IOM_CREATE)==0){
      60               0 :       std::string msg="File '";
      61               0 :       msg+=filename;
      62               0 :       msg+="' doesn't exist";
      63               0 :       iom_issueerr(msg.c_str());
      64               0 :       return 0;
      65                 :     }
      66                 :   }
      67               0 :   return ret->getRef();
      68                 : }
      69                 : 
      70                 : /** saves data to an INTERLIS XML file.
      71                 :  *  Requires: call to iom_setmodel().
      72                 :  */
      73               0 : extern "C" int iom_save(IOM_FILE file)
      74                 : {
      75                 :   //__asm { int 3 };
      76               0 :   return file->save();
      77                 : }
      78                 : 
      79                 : /** closes an INTERLIS XML file.
      80                 :  *
      81                 :  */
      82               0 : extern "C" void iom_close(IOM_FILE file)
      83                 : {
      84               0 :   if(!file->freeRef()){
      85               0 :     delete file;
      86                 :   }
      87               0 : }
      88                 : 
      89                 : /** compiles an INTERLIS model file.
      90                 :  *  Returns 0 if failed.
      91                 :  *  Requirements: Installed JRE (Java Runtime Environment) and INTERLIS 2-Compiler. The programs 
      92                 :  *  java and ili2c.jar somewhere in the PATH.
      93                 :  */
      94              12 : extern "C" IOM_BASKET iom_compileIli(int filec,char *filename[])
      95                 : {
      96              12 :   char *ili2cout=iom_gettmpnam();
      97              12 :   char *ili2c=iom_searchenv("ili2c.jar","PATH");
      98              12 :   if(!ili2c){
      99               0 :     iom_issueerr("ili2c.jar not found");
     100               0 :     return 0;
     101                 :   }
     102                 :   // call compiler
     103              12 :   std::string cmdline="java -jar ";
     104              12 :   cmdline+=ili2c;
     105              12 :   cmdline+=" --without-warnings -oIOM";
     106                 :         int i;
     107              25 :   for(i=0;i<filec;i++){
     108              13 :     cmdline+=" \"";
     109              13 :     cmdline+=filename[i];
     110              13 :     cmdline+="\"";
     111                 :   }
     112              12 :   if(i==0){
     113               0 :     iom_issueerr("no ili-file given");
     114               0 :     return 0;
     115                 :   }
     116              12 :   cmdline+=" >\"";
     117              12 :   cmdline+=ili2cout;
     118              12 :   cmdline+="\"";
     119                 :   //std::cerr << cmdline << std::endl;
     120              12 :   system(cmdline.c_str());
     121                 :   // read xtf of models
     122              12 :   IomFile model(dbgnew iom_file());
     123              12 :   model->setFilename(ili2cout);
     124              12 :   if(model->readHeader("iom04")){
     125               0 :     return 0;
     126                 :   }
     127              12 :   IomIterator basketi(dbgnew iom_iterator(model));
     128              12 :         IomBasket nb = basketi->next_basket();
     129              12 :   return nb.isNull() ? 0 : nb->getRef();
     130                 : }
     131                 : 
     132                 : /** gets the INTERLIS model.
     133                 : */
     134               0 : extern "C" IOM_BASKET iom_getmodel(IOM_FILE file)
     135                 : {
     136               0 :   IomBasket ret=file->getModel();
     137               0 :   return ret.isNull() ? 0 : ret->getRef();
     138                 : }
     139                 : 
     140                 : /** sets the INTERLIS model.
     141                 :  */
     142               0 : extern "C" void iom_setmodel(IOM_FILE file,IOM_BASKET model)
     143                 : {
     144               0 :   file->setModel(model);
     145               0 : }
     146                 : 
     147                 : /** gets an iterator to list all baskets in a file.
     148                 :  */
     149               0 : extern "C" IOM_ITERATOR iom_iteratorbasket(IOM_FILE file)
     150                 : {
     151               0 :   return (dbgnew iom_iterator(file))->getRef();
     152                 : }
     153                 : 
     154                 : /** gets the next basket or 0.
     155                 :  */
     156               0 : extern "C" IOM_BASKET iom_nextbasket(IOM_ITERATOR iterator)
     157                 : {
     158               0 :   IomBasket ret=iterator->next_basket();
     159               0 :   return ret.isNull() ? 0 : ret->getRef();
     160                 : }
     161                 : 
     162                 : /** gets the basket with a given bid or 0.
     163                 :  */
     164               0 : extern "C" IOM_BASKET iom_getbasket(IOM_FILE file,IOM_OID oid)
     165                 : {
     166               0 :   IomBasket ret=file->getBasket(X(oid));
     167               0 :   return ret.isNull() ? 0 : ret->getRef();
     168                 : }
     169                 : 
     170                 : // Basket in eine andere Datei verschieben
     171                 : extern "C" int iom_relocatebasket(IOM_FILE file,IOM_BASKET basket);
     172                 : 
     173                 : /** creates a new basket.
     174                 :  */
     175               0 : extern "C" IOM_BASKET iom_newbasket(IOM_FILE file)
     176                 : {
     177               0 :   IomBasket basket=new iom_basket();
     178               0 :   file->addBasket(basket);
     179               0 :   return basket->getRef();
     180                 : }
     181                 : 
     182                 : /** gets the content of the VERSION element in the headersection.
     183                 :  */
     184               0 : extern "C" const char *iom_getheadversion(IOM_FILE file)
     185                 : {
     186               0 :   return file->getHeadSecVersion_c();
     187                 : }
     188                 : /** gets the content of the VERSION element in the headersection.
     189                 :  */
     190               0 : extern "C" const char *iom_getheadversionUTF8(IOM_FILE file)
     191                 : {
     192                 :   static char *value=0;
     193               0 :   if(value){
     194               0 :     XMLString::release(&value);
     195                 :   }
     196               0 :   const XMLCh *ret=file->getHeadSecVersion();
     197               0 :   if(!ret)return 0;
     198               0 :   value=iom_toUTF8(ret);
     199               0 :   return value;
     200                 : }
     201                 : 
     202                 : /** gets the content of the SENDER element in the headersection.
     203                 :  */
     204               0 : extern "C" const char *iom_getheadsender(IOM_FILE file)
     205                 : {
     206               0 :   return file->getHeadSecSender_c();
     207                 : }
     208                 : 
     209                 : /** gets the content of the SENDER element in the headersection.
     210                 :  */
     211               0 : extern "C" const char *iom_getheadsenderUTF8(IOM_FILE file)
     212                 : {
     213                 :   static char *value=0;
     214               0 :   if(value){
     215               0 :     XMLString::release(&value);
     216                 :   }
     217               0 :   const XMLCh *ret=file->getHeadSecSender();
     218               0 :   if(!ret)return 0;
     219               0 :   value=iom_toUTF8(ret);
     220               0 :   return value;
     221                 : }
     222                 : 
     223                 : /** sets the content of the SENDER element in the headersection.
     224                 :  */
     225               0 : extern "C" void iom_setheadsender(IOM_FILE file,const char *sender)
     226                 : {
     227               0 :   file->setHeadSecSender(X(sender));
     228               0 : }
     229                 : /** sets the content of the SENDER element in the headersection.
     230                 :  */
     231               0 : extern "C" void iom_setheadsenderUTF8(IOM_FILE file,const char *sender)
     232                 : {
     233               0 :   XMLCh *unicodeForm=iom_fromUTF8(sender);
     234               0 :   file->setHeadSecSender(unicodeForm);
     235               0 :   XMLString::release(&unicodeForm);
     236               0 : }
     237                 : 
     238                 : /** gets the content of the COMMENT element in the headersection.
     239                 :  */
     240               0 : extern "C" const char *iom_getheadcomment(IOM_FILE file)
     241                 : {
     242               0 :   return file->getHeadSecComment_c();
     243                 : }
     244                 : /** gets the content of the COMMENT element in the headersection.
     245                 :  */
     246               0 : extern "C" const char *iom_getheadcommentUTF8(IOM_FILE file)
     247                 : {
     248                 :   static char *value=0;
     249               0 :   if(value){
     250               0 :     XMLString::release(&value);
     251                 :   }
     252               0 :   const XMLCh *ret=file->getHeadSecComment();
     253               0 :   if(!ret)return 0;
     254               0 :   value=iom_toUTF8(ret);
     255               0 :   return value;
     256                 : }
     257                 : 
     258                 : /** sets the content of the COMMENT element in the headersection.
     259                 :  */
     260               0 : extern "C" void iom_setheadcomment(IOM_FILE file,const char *comment)
     261                 : {
     262               0 :   file->setHeadSecComment(X(comment));
     263               0 : }
     264                 : /** sets the content of the COMMENT element in the headersection.
     265                 :  */
     266               0 : extern "C" void iom_setheadcommentUTF8(IOM_FILE file,const char *comment)
     267                 : {
     268               0 :   XMLCh *unicodeForm=iom_fromUTF8(comment);
     269               0 :   file->setHeadSecComment(unicodeForm);
     270               0 :   XMLString::release(&unicodeForm);
     271               0 : }
     272                 : 
     273                 : /** @}
     274                 :  */
     275                 : 
     276              12 : iom_file::iom_file()
     277                 : : parser(0)
     278                 : , handler(0)
     279                 : , filename(0)
     280                 : , useCount(0)
     281                 : , headversion_w(0)
     282                 : , headversion_c(0)
     283                 : , headsender_w(0)
     284                 : , headsender_c(0)
     285                 : , headcomment_w(0)
     286              12 : , headcomment_c(0)
     287                 : 
     288                 : {
     289              12 : }
     290              12 : iom_file::~iom_file()
     291                 : {
     292              12 :   if(headversion_c)XMLString::release(&headversion_c);
     293              12 :   if(headversion_w)XMLString::release(&headversion_w);
     294              12 :   if(headsender_c)XMLString::release(&headsender_c);
     295              12 :   if(headsender_w)XMLString::release(&headsender_w);
     296              12 :   if(headcomment_c)XMLString::release(&headcomment_c);
     297              12 :   if(headcomment_w)XMLString::release(&headcomment_w);
     298              12 :   if(filename){
     299              12 :     free((void *)filename);
     300                 :   }
     301              12 :   if(parser){
     302              12 :     delete parser;
     303                 :   }
     304              12 :   if(handler){
     305              12 :     delete handler;
     306                 :   }
     307              12 : }
     308                 : 
     309                 : /** sets the model.
     310                 :  */
     311               0 : void iom_file::setModel(IomBasket model1)
     312                 : {
     313               0 :   ilibasket=model1;
     314               0 : }
     315                 : 
     316                 : /** gets the model.
     317                 :  */
     318               0 : IomBasket iom_file::getModel()
     319                 : {
     320               0 :   return ilibasket;
     321                 : }
     322                 : 
     323              12 : void iom_file::addBasket(IomBasket basket)
     324                 : {
     325              12 :   basketv.push_back(basket);
     326              12 : }
     327                 : 
     328                 : /** gets a basket with a given oid or null.
     329                 :  */
     330               0 : IomBasket iom_file::getBasket(const XMLCh *oid)
     331                 : {
     332               0 :   std::vector<IomBasket>::iterator it;
     333               0 :   for(it=basketv.begin();it!=basketv.end();it++){
     334               0 :     IomBasket obj=*it;
     335               0 :     if(!XMLString::compareString(oid,obj->getOid())){
     336               0 :       return obj;
     337                 :     }
     338                 :   }
     339               0 :   return IomBasket();
     340                 : }
     341                 : 
     342                 : 
     343              12 : void iom_file::setHeadSecVersion(const XMLCh *version)
     344                 : {
     345              12 :   if(headversion_c)XMLString::release(&headversion_c);
     346              12 :   if(headversion_w)XMLString::release(&headversion_w);
     347              12 :   headversion_w=XMLString::replicate(version);
     348              12 : }
     349                 : 
     350               0 : const char *iom_file::getHeadSecVersion_c()
     351                 : {
     352               0 :   if(!headversion_w){
     353               0 :     return 0;
     354                 :   }
     355               0 :   if(!headversion_c){
     356               0 :     headversion_c=XMLString::transcode(headversion_w);
     357                 :   }
     358               0 :   return headversion_c;
     359                 : }
     360               0 : const XMLCh *iom_file::getHeadSecVersion()
     361                 : {
     362               0 :   return headversion_w;
     363                 : }
     364                 : 
     365              12 : void iom_file::setHeadSecSender(const XMLCh *sender)
     366                 : {
     367              12 :   if(headsender_c)XMLString::release(&headsender_c);
     368              12 :   if(headsender_w)XMLString::release(&headsender_w);
     369              12 :   headsender_w=XMLString::replicate(sender);
     370              12 : }
     371                 : 
     372               0 : const char *iom_file::getHeadSecSender_c()
     373                 : {
     374               0 :   if(!headsender_w){
     375               0 :     return 0;
     376                 :   }
     377               0 :   if(!headsender_c){
     378               0 :     headsender_c=XMLString::transcode(headsender_w);
     379                 :   }
     380               0 :   return headsender_c;
     381                 : }
     382               0 : const XMLCh *iom_file::getHeadSecSender()
     383                 : {
     384               0 :   return headsender_w;
     385                 : }
     386                 : 
     387               0 : void iom_file::setHeadSecComment(const XMLCh *comment)
     388                 : {
     389               0 :   if(headcomment_c)XMLString::release(&headcomment_c);
     390               0 :   if(headcomment_w)XMLString::release(&headcomment_w);
     391               0 :   headcomment_w=XMLString::replicate(comment);
     392               0 : }
     393                 : 
     394               0 : const char *iom_file::getHeadSecComment_c()
     395                 : {
     396               0 :   if(!headcomment_w){
     397               0 :     return 0;
     398                 :   }
     399               0 :   if(!headcomment_c){
     400               0 :     headcomment_c=XMLString::transcode(headcomment_w);
     401                 :   }
     402               0 :   return headcomment_c;
     403                 : }
     404               0 : const XMLCh *iom_file::getHeadSecComment()
     405                 : {
     406               0 :   return headcomment_w;
     407                 : }
     408                 : 
     409                 : 
     410              12 : IomFile::IomFile(struct iom_file *pointee1) 
     411              12 : : pointee(pointee1 ? pointee1->getRef() : 0){
     412              12 : }
     413              24 : IomFile::IomFile(const IomFile& src) 
     414              24 : : pointee(src.pointee ? src.pointee->getRef() : 0){
     415              24 : }
     416               0 : IomFile& IomFile::operator=(const IomFile& src){
     417               0 :   if(this!=&src){
     418               0 :     if(pointee && !pointee->freeRef()){
     419               0 :       delete pointee;
     420                 :     }
     421               0 :     pointee=src.pointee ? src.pointee->getRef() : 0;
     422                 :   }
     423               0 :   return *this;
     424                 : }
     425              87 : IomFile::~IomFile(){
     426              87 :   if(pointee && !pointee->freeRef()){
     427              12 :     delete pointee;
     428                 :   }
     429            2226 : }
     430                 : 

Generated by: LCOV version 1.7