1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
|
/**********************************************************************
obconversion.cpp - Declaration of OBFormat and OBConversion
Copyright (C) 2004 by Chris Morley
Some portions Copyright (C) 2005-2006 by Geoffrey Hutchison
This file is part of the Open Babel project.
For more information, see <http://openbabel.sourceforge.net/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
// Definition of OBConversion routines
#include <openbabel/babelconfig.h>
#ifdef _WIN32
#pragma warning (disable : 4786)
//using 'this' in base class initializer
#pragma warning (disable : 4355)
#ifdef GUI
#undef DATADIR
#include "stdafx.h" //(includes<windows.h>
#endif
#endif
#include <iosfwd>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
#include <locale>
#include <limits>
#include <typeinfo>
#include <stdlib.h>
#include <openbabel/obconversion.h>
#include <openbabel/locale.h>
#ifdef HAVE_LIBZ
#include "zipstream.h"
#endif
#if !HAVE_STRNCASECMP
extern "C" int strncasecmp(const char *s1, const char *s2, size_t n);
#endif
#ifndef BUFF_SIZE
#define BUFF_SIZE 32768
#endif
using namespace std;
//using namespace boost::iostreams;
namespace OpenBabel {
/** @class OBFormat obconversion.h <openbabel/obconversion.h>
Two sets of Read and Write functions are specified for each format
to handle two different requirements.
The "Convert" interface is for use in file format conversion applications. The
user interface, a console, a GUI, or another program is kept unaware of the
details of the chemistry and does not need to \#include mol.h. It is then
necessary to manipulate only pointers to OBBase in OBConversion and the user
interface, with all the construction and deletion of OBMol etc objects being
done in the Format classes or the OB core. The convention with "Covert"
interface functions is that chemical objects are made on the heap with new
in the ReadChemicalObject() functions and and deleted in WriteChemicalObject()
functions
The "API" interface is for programatic use of the OB routines in application
programs where mol.h is \#included. There is generally no creation or
destruction of objects in ReadMolecule() and WriteMolecule() and no restriction
on whether the pointers are to the heap or the stack.
**/
//***************************************************
/** @class OBConversion obconversion.h <openbabel/obconversion.h>
OBConversion maintains a list of the available formats,
provides information on them, and controls the conversion process.
A conversion is carried out by the calling routine, usually in a
user interface or an application program, making an instance of
OBConversion. It is loaded with the in and out formats, any options
and (usually) the default streams for input and output. Then either
the Convert() function is called, which allows a single input file
to be converted, or the extended functionality of FullConvert()
is used. This allows multiple input and output files, allowing:
- aggregation - the contents of many input files converted
and sent to one output file;
- splitting - the molecules from one input file sent to
separate output files;
- batch conversion - each input file converted to an output file.
These procedures constitute the "Convert" interface. OBConversion
and the user interface or application program do not need to be
aware of any other part of OpenBabel - mol.h is not \#included. This
allows any chemical object derived from OBBase to be converted;
the type of object is decided by the input format class.
However,currently, almost all the conversions are for molecules of
class OBMol.
///
OBConversion can also be used with an "API" interface
called from programs which manipulate chemical objects. Input/output is
done with the Read() and Write() functions which work with any
chemical object, but need to have its type specified. (The
ReadMolecule() and WriteMolecule() functions of the format classes
can also be used directly.)
Example code using OBConversion
<b>To read in a molecule, manipulate it and write it out.</b>
Set up an istream and an ostream, to and from files or elsewhere.
(cin and cout are used in the example). Specify the file formats.
@code
OBConversion conv(&cin,&cout);
if(conv.SetInAndOutFormats("SMI","MOL"))
{
OBMol mol;
if(conv.Read(&mol))
// ...manipulate molecule
conv->Write(&mol);
}
@endcode
A two stage construction is used to allow error handling
if the format ID is not recognized. This is necessary now that the
formats are dynamic and errors are not caught at compile time.
OBConversion::Read() is a templated function so that objects derived
from OBBase can also be handled, in addition to OBMol, if the format
routines are written appropriately.
<b>To make a molecule from a SMILES string.</b>
@code
std::string SmilesString;
OBMol mol;
stringstream ss(SmilesString)
OBConversion conv(&ss);
if(conv.SetInFormat("smi") && conv.Read(&mol))
// ...
@endcode
<b>To do a file conversion without manipulating the molecule.</b>
@code
#include <openbabel/obconversion.h> //mol.h is not needed
...set up an istream is and an ostream os
OBConversion conv(&is,&os);
if(conv.SetInAndOutFormats("SMI","MOL"))
{
conv.AddOption("h",OBConversion::GENOPTIONS); //Optional; (h adds expicit hydrogens)
conv.Convert();
}
@endcode
<b>To add automatic format conversion to an existing program.</b>
The existing program inputs from the file identified by the
const char* filename into the istream is. The file is assumed to have
a format ORIG, but other formats, identified by their file extensions,
can now be used.
@code
ifstream ifs(filename); //Original code
OBConversion conv;
OBFormat* inFormat = conv.FormatFromExt(filename);
OBFormat* outFormat = conv.GetFormat("ORIG");
istream* pIn = &ifs;
stringstream newstream;
if(inFormat && outFormat)
{
conv.SetInAndOutFormats(inFormat,outFormat);
conv.Convert(pIn,&newstream);
pIn=&newstream;
}
//else error; new features not available; fallback to original functionality
...Carry on with original code using pIn
@endcode
In certain Windows builds, a degree of independence from OpenBabel can be
achieved using DLLs. This code would be linked with obconv.lib.
At runtime the following DLLs would be in the executable directory:
obconv.dll, obdll.dll, one or more *.obf format files.
*/
int OBConversion::FormatFilesLoaded = 0;
// OBFormat* OBConversion::pDefaultFormat=NULL;
OBConversion::OBConversion(istream* is, ostream* os) :
pInFormat(NULL),pOutFormat(NULL), Index(0), StartNumber(1),
EndNumber(0), Count(-1), m_IsFirstInput(true), m_IsLast(true),
MoreFilesToCome(false), OneObjectOnly(false), CheckedForGzip(false),
NeedToFreeInStream(false), NeedToFreeOutStream(false),
pOb1(NULL), pAuxConv(NULL),pLineEndBuf(NULL),wInpos(0),wInlen(0)
{
pInStream=is;
pOutStream=os;
if (FormatFilesLoaded == 0)
FormatFilesLoaded = LoadFormatFiles();
//These options take a parameter
RegisterOptionParam("f", NULL, 1,GENOPTIONS);
RegisterOptionParam("l", NULL, 1,GENOPTIONS);
}
/////////////////////////////////////////////////
OBConversion::OBConversion(const OBConversion& o)
{
Index = o.Index;
Count = o.Count;
StartNumber = o.StartNumber;
EndNumber = o.EndNumber;
pInFormat = o.pInFormat;
pInStream = o.pInStream;
pOutFormat = o.pOutFormat;
pOutStream = o.pOutStream;
OptionsArray[0]= o.OptionsArray[0];
OptionsArray[1]= o.OptionsArray[1];
OptionsArray[2]= o.OptionsArray[2];
InFilename = o.InFilename;
rInpos = o.rInpos;
wInpos = o.wInpos;
rInlen = o.rInlen;
wInlen = o.wInlen;
m_IsLast = o.m_IsLast;
MoreFilesToCome= o.MoreFilesToCome;
OneObjectOnly = o.OneObjectOnly;
pOb1 = o.pOb1;
ReadyToInput = o.ReadyToInput;
m_IsFirstInput = o.m_IsFirstInput;
CheckedForGzip = o.CheckedForGzip;
NeedToFreeInStream = o.NeedToFreeInStream;
NeedToFreeOutStream = o.NeedToFreeOutStream;
pLineEndBuf = o.pLineEndBuf;
pAuxConv = NULL;
}
///////////////////////////////////////////////
OBConversion::~OBConversion()
{
if(pAuxConv!=this)
if(pAuxConv)
{
delete pAuxConv;
//pAuxConv has copies of pInStream, NeedToFreeInStream, pOutStream, NeedToFreeOutStream
//and may have already deleted the streams. So do not do it again.
NeedToFreeInStream = NeedToFreeOutStream = false;
}
// Free any remaining streams from convenience functions
if(pInStream && NeedToFreeInStream) {
delete pInStream;
pInStream=NULL;
NeedToFreeInStream = false;
}
if(pOutStream && NeedToFreeOutStream) {
delete pOutStream;
pOutStream=NULL;
NeedToFreeOutStream = false;
}
}
//////////////////////////////////////////////////////
/// Class information on formats is collected by making an instance of the class
/// derived from OBFormat(only one is usually required). RegisterFormat() is called
/// from its constructor.
///
/// If the compiled format is stored separately, like in a DLL or shared library,
/// the initialization code makes an instance of the imported OBFormat class.
int OBConversion::RegisterFormat(const char* ID, OBFormat* pFormat, const char* MIME)
{
return pFormat->RegisterFormat(ID, MIME);
}
//////////////////////////////////////////////////////
int OBConversion::LoadFormatFiles()
{
int count=0;
// if(FormatFilesLoaded) return 0;
// FormatFilesLoaded=true; //so will load files only once
#if defined(USING_DYNAMIC_LIBS)
//Depending on availablilty, look successively in
//FORMATFILE_DIR, executable directory,or current directory
string TargetDir;
#ifdef FORMATFILE_DIR
TargetDir="FORMATFILE_DIR";
#endif
DLHandler::getConvDirectory(TargetDir);
vector<string> files;
if(!DLHandler::findFiles(files,DLHandler::getFormatFilePattern(),TargetDir)) return 0;
vector<string>::iterator itr;
for(itr=files.begin();itr!=files.end();++itr)
{
if(DLHandler::openLib(*itr))
count++;
// Error handling is now handled by DLHandler itself
// else
// obErrorLog.ThrowError(__FUNCTION__, *itr + " did not load properly", obError);
}
#else
count = 1; //avoid calling this function several times
#endif //USING_DYNAMIC_LIBS
//Make instances for plugin classes defined in the data file
//This is hook for OBDefine, but does nothing if it is not loaded
//or if plugindefines.txt is not found.
OBPlugin* pdef = OBPlugin::GetPlugin("loaders","define");
if(pdef)
{
static vector<string> vec(3);
vec[1] = string("define");
vec[2] = string("plugindefines.txt");
pdef->MakeInstance(vec);
}
return count;
}
//////////////////////////////////////////////////////
/// Sets the formats from their ids, e g CML.
/// If inID is NULL, the input format is left unchanged. Similarly for outID
/// Returns true if both formats have been successfully set at sometime
bool OBConversion::SetInAndOutFormats(const char* inID, const char* outID)
{
return SetInFormat(inID) && SetOutFormat(outID);
}
//////////////////////////////////////////////////////
bool OBConversion::SetInAndOutFormats(OBFormat* pIn, OBFormat* pOut)
{
return SetInFormat(pIn) && SetOutFormat(pOut);
}
//////////////////////////////////////////////////////
bool OBConversion::SetInFormat(OBFormat* pIn)
{
if(pIn==NULL)
return true;
pInFormat=pIn;
return !(pInFormat->Flags() & NOTREADABLE);
}
//////////////////////////////////////////////////////
bool OBConversion::SetOutFormat(OBFormat* pOut)
{
pOutFormat=pOut;
return !(pOutFormat->Flags() & NOTWRITABLE);
}
//////////////////////////////////////////////////////
bool OBConversion::SetInFormat(const char* inID)
{
if(inID)
pInFormat = FindFormat(inID);
return pInFormat && !(pInFormat->Flags() & NOTREADABLE);
}
//////////////////////////////////////////////////////
bool OBConversion::SetOutFormat(const char* outID)
{
if(outID)
pOutFormat= FindFormat(outID);
return pOutFormat && !(pOutFormat->Flags() & NOTWRITABLE);
}
//////////////////////////////////////////////////////
int OBConversion::Convert(istream* is, ostream* os)
{
if (is) {
pInStream=is;
CheckedForGzip = false; // haven't checked this for gzip yet
}
if (os) pOutStream=os;
ostream* pOrigOutStream = pOutStream;
#ifdef HAVE_LIBZ
zlib_stream::zip_istream *zIn;
// only try to decode the gzip stream once
if (!CheckedForGzip) {
zIn = new zlib_stream::zip_istream(*pInStream);
if (zIn->is_gzip()) {
pInStream = zIn;
CheckedForGzip = true;
}
else
delete zIn;
}
#ifndef DISABLE_WRITE_COMPRESSION //Unsolved problem with compression under Windows
zlib_stream::zip_ostream zOut(*pOutStream);
if(IsOption("z",GENOPTIONS))
{
// make sure to output the header
zOut.make_gzip();
pOutStream = &zOut;
}
#endif
#endif
//The FilteringInputStreambuf delivers characters to the istream, pInStream,
//and receives characters this stream's original rdbuf.
//It filters them, converting CRLF and CR line endings to LF.
//seek and tellg requests to the stream are passed through to the original
//rdbuf. A FilteringInputStreambuf is installed only for appropriate formats
//- not for binary or XML formats - if not already present.
InstallStreamFilter();
int count = Convert();
pOutStream = pOrigOutStream;
return count;
}
////////////////////////////////////////////////////
/// Actions the "convert" interface.
/// Calls the OBFormat class's ReadMolecule() which
/// - makes a new chemical object of its chosen type (e.g. OBMol)
/// - reads an object from the input file
/// - subjects the chemical object to 'transformations' as specified by the Options
/// - calls AddChemObject to add it to a buffer. The previous object is first output
/// via the output Format's WriteMolecule(). During the output process calling
/// IsFirst() and GetIndex() (the number of objects including the current one already output.
/// allows more control, for instance writing \<cml\> and \</cml\> tags for multiple molecule outputs only.
///
/// AddChemObject does not save the object passed to it if it is NULL (as a result of a DoTransformation())
/// or if the number of the object is outside the range defined by
/// StartNumber and EndNumber.This means the start and end counts apply to all chemical objects
/// found whether or not they are output.
///
/// If ReadMolecule returns false the input conversion loop is exited.
///
int OBConversion::Convert()
{
if(pInStream==NULL || pOutStream==NULL)
{
obErrorLog.ThrowError(__FUNCTION__, "input or output stream not set", obError);
return 0;
}
if(!pInFormat) return 0;
Count=0;//number objects processed
if(!SetStartAndEnd())
return 0;
ReadyToInput=true;
m_IsLast=false;
pOb1=NULL;
wInlen=0;
if(pInFormat->Flags() & READONEONLY)
OneObjectOnly=true;
//Input loop
while(ReadyToInput && pInStream->good()) //Possible to omit? && pInStream->peek() != EOF
{
if(pInStream==&cin)
{
if(pInStream->peek()==-1) //Cntl Z Was \n but interfered with piping
break;
}
else
rInpos = pInStream->tellg();
bool ret=false;
try
{
ret = pInFormat->ReadChemObject(this);
SetFirstInput(false);
}
catch(...)
{
if(!IsOption("e", GENOPTIONS) && !OneObjectOnly)
{
obErrorLog.ThrowError(__FUNCTION__, "Convert failed with an exception" , obError);
return Index; // the number we've actually output so far
}
}
if(!ret)
{
//error or termination request: terminate unless
// -e option requested and successfully can skip past current object
if(!IsOption("e", GENOPTIONS) || pInFormat->SkipObjects(0,this)!=1)
break;
}
if(OneObjectOnly)
break;
// Objects supplied to AddChemObject() which may output them after a delay
//ReadyToInput may be made false in AddChemObject()
// by WriteMolecule() returning false or by Count==EndNumber
}
//Output last object
m_IsLast= !MoreFilesToCome;
//Output is deferred until the end with -C option
if(pOutFormat && (!IsOption("C",GENOPTIONS) || m_IsLast))
if(pOb1 && !pOutFormat->WriteChemObject(this))
Index--;
//Put AddChemObject() into non-queue mode
Count= -1;
EndNumber=StartNumber=0; pOb1=NULL;//leave tidy
MoreFilesToCome=false;
OneObjectOnly=false;
return Index; //The number actually output
}
//////////////////////////////////////////////////////
bool OBConversion::SetStartAndEnd()
{
unsigned int TempStartNumber=0;
const char* p = IsOption("f",GENOPTIONS);
if(p)
{
StartNumber=atoi(p);
if(StartNumber>1)
{
TempStartNumber=StartNumber;
//Try to skip objects now
int ret = pInFormat->SkipObjects(StartNumber-1,this);
if(ret==-1) //error
return false;
if(ret==1) //success:objects skipped
{
Count = StartNumber-1;
StartNumber=0;
}
}
}
p = IsOption("l",GENOPTIONS);
if(p)
{
EndNumber=atoi(p);
if(TempStartNumber && EndNumber<TempStartNumber)
EndNumber=TempStartNumber;
}
return true;
}
//////////////////////////////////////////////////////
/// Retrieves an object stored by AddChemObject() during output
OBBase* OBConversion::GetChemObject()
{
Index++;
return pOb1;
}
//////////////////////////////////////////////////////
/// Called by ReadMolecule() to deliver an object it has read from an input stream.
/// Used in two modes:
/// - When Count is negative it is left negative and the routine is just a store
/// for an OBBase object. The negative value returned tells the calling
/// routine that no more objects are required.
/// - When count is >=0, probably set by Convert(), it acts as a queue of 2:
/// writing the currently stored value before accepting the supplied one. This delay
/// allows output routines to respond differently when the written object is the last.
/// Count is incremented with each call, even if pOb=NULL.
/// Objects are not added to the queue if the count is outside the range
/// StartNumber to EndNumber. There is no upper limit if EndNumber is zero.
/// The return value is Count ((>0) or 0 if WriteChemObject returned false.
int OBConversion::AddChemObject(OBBase* pOb)
{
if(Count<0)
{
pOb1=pOb;
return Count; // <0
}
Count++;
if(Count>=(int)StartNumber)//keeps reading objects but does nothing with them
{
if(Count==(int)EndNumber)
ReadyToInput=false; //stops any more objects being read
rInlen = pInStream->tellg() - rInpos;
if(pOb)
{
if(pOb1 && pOutFormat) //see if there is an object ready to be output
{
//Output object
if (!pOutFormat->WriteChemObject(this))
{
//faultly write, so finish
--Index;
//ReadyToInput=false;
pOb1=NULL;
return 0;
}
//Stop after writing with single object output files
if(pOutFormat->Flags() & WRITEONEONLY)
{
// if there are more molecules to output, send a warning
stringstream errorMsg;
errorMsg << "WARNING: You are attempting to convert a file"
<< " with multiple molecule entries into a format"
<< " which can only store one molecule. The current"
<< " output will only contain the first molecule.\n\n";
errorMsg << "To convert this input into multiple separate"
<< " output files, with one molecule per file, try:\n"
<< "babel [input] [ouptut] -m\n\n";
errorMsg << "To pick one particular molecule"
<< " (e.g., molecule 4), try:\n"
<< "babel -f 4 -l 4 [input] [output]" << endl;
obErrorLog.ThrowError(__FUNCTION__, errorMsg.str(), obWarning);
ReadyToInput = false;
pOb1 = NULL;
return Count; // >0
}
}
pOb1=pOb;
wInpos = rInpos; //Save the position in the input file to be accessed when writing it
wInlen = rInlen;
}
}
return Count; // >0
}
//////////////////////////////////////////////////////
///Returns the number of objects which have been output or are currently being output.
///The outputindex is incremented when an object for output is fetched by GetChemObject().
///So the function will return 1 if called from WriteMolecule() during output of the first object.
int OBConversion::GetOutputIndex() const
{
//The number of objects actually written already from this instance of OBConversion
return Index;
}
void OBConversion::SetOutputIndex(int indx)
{
Index=indx;
}
//////////////////////////////////////////////////////
OBFormat* OBConversion::FindFormat(const char* ID)
{
return OBFormat::FindType(ID);
}
//////////////////////////////////////////////////
const char* OBConversion::GetTitle() const
{
return(InFilename.c_str());
}
void OBConversion::SetMoreFilesToCome()
{
MoreFilesToCome=true;
}
void OBConversion::SetOneObjectOnly(bool b)
{
OneObjectOnly=b;
m_IsLast=b;
}
/////////////////////////////////////////////////////////
OBFormat* OBConversion::FormatFromExt(const char* filename)
{
string file = filename;
string::size_type extPos = file.rfind('.');
if(extPos!=string::npos // period found
&& (file.substr(extPos + 1, file.size())).find("/")==string::npos) // and period is after the last "/"
{
// only do this if we actually can read .gz files
#ifdef HAVE_LIBZ
if (file.substr(extPos) == ".gz")
{
file.erase(extPos);
extPos = file.rfind('.');
if (extPos!=string::npos)
return FindFormat( (file.substr(extPos + 1, file.size())).c_str() );
}
else
#endif
return FindFormat( (file.substr(extPos + 1, file.size())).c_str() );
}
// Check the filename if no extension (e.g. VASP does not use extensions):
extPos = file.rfind('/');
if(extPos!=string::npos) {
return FindFormat( (file.substr(extPos + 1, file.size())).c_str() );
}
// If we are just passed the filename with no path, this should catch it:
return FindFormat( file.c_str() ); //if no format found
}
OBFormat* OBConversion::FormatFromMIME(const char* MIME)
{
return OBFormat::FormatFromMIME(MIME);
}
bool OBConversion::Read(OBBase* pOb, std::istream* pin)
{
if(pin) {
pInStream=pin;
CheckedForGzip = false; // haven't set this stream to gzip (yet)
}
if(!pInFormat || !pInStream) return false;
#ifdef HAVE_LIBZ
zlib_stream::zip_istream *zIn;
// only try to decode the gzip stream once
if (!CheckedForGzip) {
zIn = new zlib_stream::zip_istream(*pInStream);
if (zIn->is_gzip()) {
pInStream = zIn;
CheckedForGzip = true;
}
else
delete zIn;
}
#endif
InstallStreamFilter();
// Set the locale for number parsing to avoid locale issues: PR#1785463
obLocale.SetLocale();
// Also set the C++ stream locale
locale originalLocale = pInStream->getloc(); // save the original
locale cNumericLocale(originalLocale, "C", locale::numeric);
pInStream->imbue(cNumericLocale);
bool success = pInFormat->ReadMolecule(pOb, this);
// return the C locale to the original one
obLocale.RestoreLocale();
// Restore the original C++ locale as well
pInStream->imbue(originalLocale);
// If we failed to read, plus the stream is over, then check if this is a stream from ReadFile
if (!success && !pInStream->good() && NeedToFreeInStream) {
ifstream *inFstream = dynamic_cast<ifstream*>(pInStream);
if (inFstream != 0)
inFstream->close(); // We will free the stream later, but close the file now
}
return success;
}
void OBConversion::InstallStreamFilter()
{
//Do not install filtering input stream if a binary or XML format
//or if already installed in the current InStream (which may have changed).
//Deleting any old LErdbuf before contructing a new one ensures there is
//only one for each OBConversion object. It is deleted in the destructor.
if(pInFormat && !(pInFormat->Flags() & (READBINARY | READXML)) && pInStream->rdbuf()!=pLineEndBuf)
{
delete pLineEndBuf;
pLineEndBuf = new LErdbuf(pInStream->rdbuf());
pInStream->rdbuf(pLineEndBuf);
}
}
//////////////////////////////////////////////////
/// Writes the object pOb but does not delete it afterwards.
/// The output stream is lastingly changed if pos is not NULL
/// Returns true if successful.
bool OBConversion::Write(OBBase* pOb, ostream* pos)
{
if(pos) pOutStream=pos;
if(!pOutFormat || !pOutStream) return false;
ostream* pOrigOutStream = pOutStream;
#ifdef HAVE_LIBZ
#ifndef DISABLE_WRITE_COMPRESSION
zlib_stream::zip_ostream zOut(*pOutStream);
if(IsOption("z",GENOPTIONS))
{
// make sure to output the header
zOut.make_gzip();
pOutStream = &zOut;
}
#endif
#endif
SetOneObjectOnly(); //So that IsLast() returns true, which is important for XML formats
// Set the locale for number parsing to avoid locale issues: PR#1785463
obLocale.SetLocale();
// Also set the C++ stream locale
locale originalLocale = pOutStream->getloc(); // save the original
locale cNumericLocale(originalLocale, "C", locale::numeric);
pOutStream->imbue(cNumericLocale);
// The actual work is done here
bool success = pOutFormat->WriteMolecule(pOb,this);
pOutStream = pOrigOutStream;
// return the C locale to the original one
obLocale.RestoreLocale();
// Restore the C++ stream locale too
pOutStream->imbue(originalLocale);
return success;
}
//////////////////////////////////////////////////
/// Writes the object pOb but does not delete it afterwards.
/// The output stream not changed (since we cannot write to this string later)
/// Returns true if successful.
std::string OBConversion::WriteString(OBBase* pOb, bool trimWhitespace)
{
ostream *oldStream = pOutStream; // save old output
stringstream newStream;
string temp;
if(pOutFormat)
{
Write(pOb, &newStream);
}
pOutStream = oldStream;
temp = newStream.str();
if (trimWhitespace) // trim the trailing whitespace
{
string::size_type notwhite = temp.find_last_not_of(" \t\n\r");
temp.erase(notwhite+1);
}
return temp;
}
//////////////////////////////////////////////////
/// Writes the object pOb but does not delete it afterwards.
/// The output stream is lastingly changed to point to the file
/// Returns true if successful.
bool OBConversion::WriteFile(OBBase* pOb, string filePath)
{
if(!pOutFormat) return false;
// if we have an old stream, free this first before creating a new one
if (pOutStream && NeedToFreeOutStream) {
delete pOutStream;
}
ofstream *ofs = new ofstream;
NeedToFreeOutStream = true; // make sure we clean this up later
ios_base::openmode omode =
pOutFormat->Flags() & WRITEBINARY ? ios_base::out|ios_base::binary : ios_base::out;
ofs->open(filePath.c_str(),omode);
if(!ofs || !ofs->good())
{
obErrorLog.ThrowError(__FUNCTION__,"Cannot write to " + filePath, obError);
return false;
}
return Write(pOb, ofs);
}
void OBConversion::CloseOutFile()
{
if (pOutStream && NeedToFreeOutStream)
{
delete pOutStream;
NeedToFreeOutStream = false;
pOutStream = NULL;
}
}
////////////////////////////////////////////
bool OBConversion::ReadString(OBBase* pOb, std::string input)
{
// if we have an old stream, free this first before creating a new one
if (pInStream && NeedToFreeInStream) {
delete pInStream;
}
stringstream *pin = new stringstream(input);
NeedToFreeInStream = true; // make sure we clean this up later
return Read(pOb, pin);
}
////////////////////////////////////////////
bool OBConversion::ReadFile(OBBase* pOb, std::string filePath)
{
if(!pInFormat) return false;
// save the filename
InFilename = filePath;
// if we have an old stream, free this first before creating a new one
if (pInStream && NeedToFreeInStream) {
delete pInStream;
}
ifstream *ifs = new ifstream;
NeedToFreeInStream = true; // make sure we free this
ios_base::openmode imode =
pInFormat->Flags() & READBINARY ? ios_base::in|ios_base::binary : ios_base::in;
ifs->open(filePath.c_str(),imode);
if(!ifs || !ifs->good())
{
obErrorLog.ThrowError(__FUNCTION__,"Cannot read from " + filePath, obError);
return false;
}
return Read(pOb,ifs);
}
////////////////////////////////////////////
bool OBConversion::OpenInAndOutFiles(std::string infilepath, std::string outfilepath)
{
// if we have an old input stream, free this first before creating a new one
if (pInStream && NeedToFreeInStream)
delete pInStream;
// if we have an old output stream, free this first before creating a new one
if (pOutStream && NeedToFreeOutStream)
delete pOutStream;
ifstream *ifs = new ifstream;
NeedToFreeInStream = true; // make sure we free this
ifs->open(infilepath.c_str(),ios_base::in|ios_base::binary); //always open in binary mode
if(!ifs || !ifs->good())
{
obErrorLog.ThrowError(__FUNCTION__,"Cannot read from " + infilepath, obError);
return false;
}
pInStream = ifs;
InFilename = infilepath;
ofstream *ofs = new ofstream;
NeedToFreeOutStream = true; // make sure we clean this up later
ofs->open(outfilepath.c_str(),ios_base::out|ios_base::binary);//always open in binary mode
if(!ofs || !ofs->good())
{
obErrorLog.ThrowError(__FUNCTION__,"Cannot write to " + outfilepath, obError);
return false;
}
pOutStream = ofs;
return true;
}
////////////////////////////////////////////
const char* OBConversion::Description()
{
return
"Conversion options\n"
"-f <#> Start import at molecule # specified\n"
"-l <#> End import at molecule # specified\n"
"-e Continue with next object after error, if possible\n"
#ifdef HAVE_LIBZ
#ifndef DISABLE_WRITE_COMPRESSION //Unsolved problem with compression under Windows
"-z Compress the output with gzip\n"
#endif
#endif
"-k Attempt to translate keywords\n";
// -t All input files describe a single molecule
}
////////////////////////////////////////////
bool OBConversion::IsLast()
{
return m_IsLast;
}
////////////////////////////////////////////
bool OBConversion::IsFirstInput()
{
return m_IsFirstInput;
}
void OBConversion::SetFirstInput(bool b)
{
m_IsFirstInput = b;
/* //Also set or clear a general option
if(b)
AddOption("firstinput",GENOPTIONS);
else
RemoveOption("firstinput",GENOPTIONS);
*/
}
/////////////////////////////////////////////////
string OBConversion::BatchFileName(string& BaseName, string& InFile)
{
//Replaces * in BaseName by InFile without extension and path
string ofname(BaseName);
string::size_type pos = ofname.find('*');
if(pos != string::npos)
{
//Replace * by input filename
string::size_type posdot= InFile.rfind('.');
if(posdot == string::npos)
posdot = InFile.size();
else {
#ifdef HAVE_LIBZ
if (InFile.substr(posdot) == ".gz")
{
InFile.erase(posdot);
posdot = InFile.rfind('.');
if (posdot == string::npos)
posdot = InFile.size();
}
#endif
}
string::size_type posname= InFile.find_last_of("\\/");
ofname.replace(pos,1, InFile, posname+1, posdot-posname-1);
}
return ofname;
}
////////////////////////////////////////////////
string OBConversion::IncrementedFileName(string& BaseName, const int Count)
{
//Replaces * in BaseName by Count
string ofname(BaseName);
string::size_type pos = ofname.find('*');
if(pos!=string::npos)
{
char num[33];
snprintf(num, 33, "%d", Count);
ofname.replace(pos,1, num);
}
return ofname;
}
////////////////////////////////////////////////////
bool OBConversion::CheckForUnintendedBatch(const string& infile, const string& outfile)
{
//If infile == outfile issue error message and return false
//If name without the extensions are the same issue warning and return true;
//Otherwise return true
bool ret=true;
string inname1, inname2;
string::size_type pos;
pos = infile.rfind('.');
if(pos != string::npos)
inname1 = infile.substr(0,pos);
pos = outfile.rfind('.');
if(pos != string::npos)
inname2 = infile.substr(0,pos);
if(inname1==inname2)
obErrorLog.ThrowError(__FUNCTION__,
"This was a batch operation. For splitting, use non-empty base name for the output files", obWarning);
if(infile==outfile)
return false;
return true;
}
/**
Makes input and output streams, and carries out normal,
batch, aggregation, and splitting conversion.
Normal
Done if FileList contains a single file name and OutputFileName
does not contain a *.
Aggregation
Done if FileList has more than one file name and OutputFileName does
not contain * . All the chemical objects are converted and sent
to the single output file.
Splitting
Done if FileList contains a single file name and OutputFileName
contains a * . Each chemical object in the input file is converted
and sent to a separate file whose name is OutputFileName with the
* replaced by 1, 2, 3, etc. OutputFileName must have at least one
character other than the * before the extension.
For example, if OutputFileName is NEW*.smi then the output files are
NEW1.smi, NEW2.smi, etc.
Batch Conversion
Done if FileList has more than one file name and contains a * .
Each input file is converted to an output file whose name is
OutputFileName with the * replaced by the inputfile name without its
path and extension.
So if the input files were inpath/First.cml, inpath/Second.cml
and OutputFileName was NEW*.mol, the output files would be
NEWFirst.mol, NEWSecond.mol.
If FileList is empty, the input stream that has already been set
(usually in the constructor) is used. If OutputFileName is empty,
the output stream already set is used.
On exit, OutputFileList contains the names of the output files.
Returns the number of Chemical objects converted.
*/
int OBConversion::FullConvert(std::vector<std::string>& FileList, std::string& OutputFileName,
std::vector<std::string>& OutputFileList)
{
istream* pIs=NULL;
ostream* pOs=NULL;
ifstream is;
ofstream os;
stringstream ssOut;
bool HasMultipleOutputFiles=false;
int Count=0;
SetFirstInput();
bool CommonInFormat = pInFormat ? true:false; //whether set in calling routine
ios_base::openmode omode =
pOutFormat->Flags() & WRITEBINARY ? ios_base::out|ios_base::binary : ios_base::out;
obErrorLog.ClearLog();
try
{
ofstream ofs;
//OUTPUT
if(OutputFileName.empty())
pOs = NULL; //use existing stream
else
{
if(OutputFileName.find_first_of('*')!=string::npos) HasMultipleOutputFiles = true;
if(!HasMultipleOutputFiles)
{
//If the output file is the same as any of the input
//files, send the output to a temporary stringstream
vector<string>::iterator itr;
for(itr=FileList.begin();itr!=FileList.end();++itr)
{
if(*itr==OutputFileName)
{
pOs = &ssOut;
break;
}
}
if(itr==FileList.end())
{
os.open(OutputFileName.c_str(),omode);
if(!os)
{
obErrorLog.ThrowError(__FUNCTION__,"Cannot write to " + OutputFileName, obError);
return 0;
}
pOs=&os;
}
OutputFileList.push_back(OutputFileName);
}
}
if(IsOption("t",GENOPTIONS))
{
//Concatenate input file option (multiple files, single molecule)
if(HasMultipleOutputFiles)
{
obErrorLog.ThrowError(__FUNCTION__,
"Cannot have multiple output files and also concatenate input files (-t option)",obError);
return 0;
}
stringstream allinput;
vector<string>::iterator itr;
for(itr=FileList.begin();itr!=FileList.end();++itr)
{
ifstream ifs((*itr).c_str());
if(!ifs)
{
obErrorLog.ThrowError(__FUNCTION__,"Cannot open " + *itr, obError);
continue;
}
allinput << ifs.rdbuf(); //Copy all file contents
ifs.close();
}
Count = Convert(&allinput,pOs);
return Count;
}
//INPUT
if(FileList.empty())
{
pIs = NULL;
if(HasMultipleOutputFiles)
{
obErrorLog.ThrowError(__FUNCTION__,"Cannot use multiple output files without an input file", obError);
return 0;
}
}
else
{
if(FileList.size()>1 || OutputFileName.substr(0,2)=="*.")
{
//multiple input files
vector<string>::iterator itr, tempitr;
tempitr = FileList.end();
tempitr--;
for(itr=FileList.begin();itr!=FileList.end();++itr)
{
InFilename = *itr;
ifstream ifs;
if(!OpenAndSetFormat(CommonInFormat, &ifs))
continue;
if(HasMultipleOutputFiles)
{
//Batch conversion
string batchfile = BatchFileName(OutputFileName,*itr);
//With inputs like babel test.xxx -oyyy -m
//the user may have wanted to do a splitting operation
//Issue a message and abort if xxx==yyy which would overwrite input file
if(FileList.size()==1 && !CheckForUnintendedBatch(batchfile, InFilename))
return Count;
if(ofs.is_open()) ofs.close();
ofs.open(batchfile.c_str(), omode);
if(!ofs)
{
obErrorLog.ThrowError(__FUNCTION__,"Cannot open " + batchfile, obError);
return Count;
}
OutputFileList.push_back(batchfile);
SetOutputIndex(0); //reset for new file
Count += Convert(&ifs,&ofs);
}
else
{
//Aggregation
if(itr!=tempitr) SetMoreFilesToCome();
Count = Convert(&ifs,pOs);
}
}
if(!os.is_open() && !OutputFileName.empty() && !HasMultipleOutputFiles)
{
//Output was written to temporary string stream. Output it to the file
os.open(OutputFileName.c_str(),omode);
if(!os)
{
obErrorLog.ThrowError(__FUNCTION__,"Cannot write to " + OutputFileName, obError);
return Count;
}
os << ssOut.rdbuf();
}
return Count;
}
else
{
//Single input file
InFilename = FileList[0];
if(!OpenAndSetFormat(CommonInFormat, &is))
return 0;
pIs=&is;
if(HasMultipleOutputFiles)
{
//Splitting
//Output is put in a temporary stream and written to a file
//with an augmenting name only when it contains a valid object.
int Indx=1;
SetInStream(&is);
#ifdef HAVE_LIBZ
zlib_stream::zip_istream zIn(is);
#endif
for(;;)
{
stringstream ss;
SetOutStream(&ss);
SetOutputIndex(0); //reset for new file
SetOneObjectOnly();
#ifdef HAVE_LIBZ
if(Indx==1 && zIn.is_gzip()) {
SetInStream(&zIn);
CheckedForGzip = true; // we know this one is gzip'ed
}
#endif
int ThisFileCount = Convert();
if(ThisFileCount==0) break;
Count+=ThisFileCount;
if(ofs.is_open()) ofs.close();
string incrfile = IncrementedFileName(OutputFileName,Indx++);
ofs.open(incrfile.c_str(), omode);
if(!ofs)
{
obErrorLog.ThrowError(__FUNCTION__,"Cannot write to " + incrfile, obError);
return Count;
}
OutputFileList.push_back(incrfile);
#ifdef HAVE_LIBZ
#ifndef DISABLE_WRITE_COMPRESSION
if(IsOption("z",GENOPTIONS))
{
zlib_stream::zip_ostream zOut(ofs);
// make sure to output the header
zOut.make_gzip();
zOut << ss.rdbuf();
}
else
#endif
#endif
ofs << ss.rdbuf();
ofs.close();
ss.clear();
}
return Count;
}
}
}
//Single input and output files
Count = Convert(pIs,pOs);
if(!os.is_open() && !OutputFileName.empty())
{
//Output was written to temporary string stream. Output it to the file
os.open(OutputFileName.c_str(),omode);
if(!os)
{
obErrorLog.ThrowError(__FUNCTION__,"Cannot write to " + OutputFileName, obError);
return Count;
}
os << ssOut.rdbuf();
}
return Count;
}
catch(...)
{
obErrorLog.ThrowError(__FUNCTION__, "Conversion failed with an exception.",obError);
return Count;
}
return Count;
}
bool OBConversion::OpenAndSetFormat(bool SetFormat, ifstream* is)
{
//Opens file using InFilename and sets pInFormat if requested
if(!SetFormat)
{
pInFormat = FormatFromExt(InFilename.c_str());
if(pInFormat==NULL)
{
string::size_type pos = InFilename.rfind('.');
string ext;
if(pos!=string::npos)
ext = InFilename.substr(pos);
obErrorLog.ThrowError(__FUNCTION__, "Cannot read input format \""
+ ext + '\"' + " for file \"" + InFilename + "\"",obError);
return false;
}
}
ios_base::openmode imode;
#ifdef ALL_READS_BINARY //Makes unix files compatible with VC++6
imode = ios_base::in|ios_base::binary;
#else
imode = pInFormat->Flags() & READBINARY ? ios_base::in|ios_base::binary : ios_base::in;
#endif
is->open(InFilename.c_str(), imode);
if(!is->good())
{
obErrorLog.ThrowError(__FUNCTION__, "Cannot open " + InFilename, obError);
return false;
}
return true;
}
///////////////////////////////////////////////
void OBConversion::AddOption(const char* opt, Option_type opttyp, const char* txt)
{
//Also updates an option
if(txt==NULL)
OptionsArray[opttyp][opt]=string();
else
OptionsArray[opttyp][opt]=txt;
}
const char* OBConversion::IsOption(const char* opt, Option_type opttyp)
{
//Returns NULL if option not found or a pointer to the text if it is
map<string,string>::iterator pos;
pos = OptionsArray[opttyp].find(opt);
if(pos==OptionsArray[opttyp].end())
return NULL;
return pos->second.c_str();
}
bool OBConversion::RemoveOption(const char* opt, Option_type opttyp)
{
return OptionsArray[opttyp].erase(opt)!=0;//true if was there
}
void OBConversion::SetOptions(const char* options, Option_type opttyp)
{
while(*options)
{
string ch(1, *options++);
if(*options=='\"')
{
string txt = options+1;
string::size_type pos = txt.find('\"');
if(pos==string::npos)
return; //options is illformed
txt.erase(pos);
OptionsArray[opttyp][ch]= txt;
options += pos+2;
}
else
OptionsArray[opttyp][ch] = string();
}
}
OBConversion::OPAMapType& OBConversion::OptionParamArray(Option_type typ)
{
static OPAMapType* opa = new OPAMapType[3];
return opa[typ];
}
void OBConversion::RegisterOptionParam(string name, OBFormat* pFormat,
int numberParams, Option_type typ)
{
//Gives error message if the number of parameters conflicts with an existing registration
map<string,int>::iterator pos;
pos = OptionParamArray(typ).find(name);
if(pos!=OptionParamArray(typ).end())
{
if(pos->second!=numberParams)
{
string description("API");
if(pFormat)
description=pFormat->Description();
obErrorLog.ThrowError(__FUNCTION__,
"The number of parameters needed by option \"" + name + "\" in "
+ description.substr(0,description.find('\n'))
+ " differs from an earlier registration.", obError);
return;
}
}
OptionParamArray(typ)[name] = numberParams;
}
int OBConversion::GetOptionParams(string name, Option_type typ)
{
//returns the number of parameters registered for the option, or 0 if not found
map<string,int>::iterator pos;
pos = OptionParamArray(typ).find(name);
if(pos==OptionParamArray(typ).end())
return 0;
return pos->second;
}
/**
* Returns the list of supported input format
*/
std::vector<std::string> OBConversion::GetSupportedInputFormat()
{
vector<string> vlist;
OBPlugin::ListAsVector("formats", "in", vlist);
return vlist;
}
/**
* Returns the list of supported output format
*/
std::vector<std::string> OBConversion::GetSupportedOutputFormat()
{
vector<string> vlist;
OBPlugin::ListAsVector("formats", "out", vlist);
return vlist;
}
void OBConversion::ReportNumberConverted(int count, OBFormat* pFormat)
{
//Send info message to clog. This constructed from the TargetClassDescription
//of the specified class (or the output format if not specified).
//Get the last word on the first line of the description which should
//be "molecules", "reactions", etc and remove the s if only one object converted
if(!pFormat)
pFormat = pOutFormat;
string objectname(pFormat->TargetClassDescription());
string::size_type pos = objectname.find('\n');
if(pos==std::string::npos)
pos=objectname.size();
if(count==1) --pos;
objectname.erase(pos);
pos = objectname.rfind(' ');
if(pos==std::string::npos)
pos=0;
std::clog << count << objectname.substr(pos) << " converted" << endl;
}
void OBConversion::CopyOptions(OBConversion* pSourceConv, Option_type typ)
{
if(typ==ALL)
for(int i=0;i<3;++i)
OptionsArray[i]=pSourceConv->OptionsArray[i];
else
OptionsArray[typ]=pSourceConv->OptionsArray[typ];
}
//The following function and typedef are deprecated, and are present only
//for backward compatibility.
//Use OBConversion::GetSupportedInputFormat(), OBConversion::GetSupportedOutputFormat(),
//OBPlugin::List(), OBPlugin::OBPlugin::ListAsVector(),OBPlugin::OBPlugin::ListAsString(),
//or (in extremis) OBPlugin::PluginIterator instead.
typedef OBPlugin::PluginIterator Formatpos;
bool OBConversion::GetNextFormat(Formatpos& itr, const char*& str,OBFormat*& pFormat)
{
pFormat = NULL;
if(str==NULL)
itr = OBPlugin::Begin("formats");
else
itr++;
if(itr == OBPlugin::End("formats"))
{
str=NULL; pFormat=NULL;
return false;
}
static string s;
s =itr->first;
pFormat = static_cast<OBFormat*>(itr->second);
if(pFormat)
{
string description(pFormat->Description());
s += " -- ";
s += description.substr(0,description.find('\n'));
}
if(pFormat->Flags() & NOTWRITABLE) s+=" [Read-only]";
if(pFormat->Flags() & NOTREADABLE) s+=" [Write-only]";
str = s.c_str();
return true;
}
}//namespace OpenBabel
//! \file obconversion.cpp
//! \brief Implementation of OBFormat and OBConversion classes.
|