-
Notifications
You must be signed in to change notification settings - Fork 4
/
ichimain.c
1791 lines (1584 loc) · 55.3 KB
/
ichimain.c
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
/*
* International Chemical Identifier (InChI)
* Version 1
* Software version 1.07
* 20/11/2023
*
* The InChI library and programs are free software developed under the
* auspices of the International Union of Pure and Applied Chemistry (IUPAC).
* Originally developed at NIST.
* Modifications and additions by IUPAC and the InChI Trust.
* Some portions of code were developed/changed by external contributors
* (either contractor or volunteer) which are listed in the file
* 'External-contributors' included in this distribution.
*
* IUPAC/InChI-Trust Licence No.1.0 for the
* International Chemical Identifier (InChI)
* Copyright (C) IUPAC and InChI Trust
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the IUPAC/InChI Trust InChI Licence No.1.0,
* or any later version.
*
* Please note that this library is distributed WITHOUT ANY WARRANTIES
* whatsoever, whether expressed or implied.
* See the IUPAC/InChI-Trust InChI Licence No.1.0 for more details.
*
* You should have received a copy of the IUPAC/InChI Trust InChI
* Licence No. 1.0 with this library; if not, please e-mail:
*
* info@inchi-trust.org
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>
#include <limits.h>
#include <float.h>
#ifndef COMPILE_ANSI_ONLY
#include <conio.h>
#ifndef TARGET_LIB_FOR_WINCHI
#include <windows.h>
#endif
#endif
#include "../../../INCHI_BASE/src/mode.h"
#if( BUILD_WITH_AMI == 1 && defined( _MSC_VER ) && MSC_AMI == 1 )
#include <malloc.h>
#include <io.h>
#endif
#ifdef _WIN32
#include <crtdbg.h>
#endif
#include "../../../INCHI_BASE/src/ichitime.h"
#include "../../../INCHI_BASE/src/incomdef.h"
#include "../../../INCHI_BASE/src/ichidrp.h"
#include "../../../INCHI_BASE/src/inpdef.h"
#include "../../../INCHI_BASE/src/ichi.h"
#include "../../../INCHI_BASE/src/strutil.h"
#include "../../../INCHI_BASE/src/util.h"
#include "../../../INCHI_BASE/src/ichierr.h"
#include "../../../INCHI_BASE/src/ichimain.h"
#include "../../../INCHI_BASE/src/ichicomp.h"
#include "../../../INCHI_BASE/src/ichi_io.h"
#ifdef TARGET_EXE_STANDALONE
#include "../../../INCHI_BASE/src/inchi_api.h"
#endif
#include "../../../INCHI_BASE/src/bcf_s.h"
/* Console-specific */
#if !defined(TARGET_API_LIB) && !defined(COMPILE_ANSI_ONLY)
/* Use Windows additional features */
/****************************************************************************/
int user_quit(struct tagINCHI_CLOCK* ic,
const char* msg,
unsigned long ulMaxTime)
{
#if defined(TARGET_LIB_FOR_WINCHI)
return 0;
#endif
#if ( !defined(TARGET_LIB_FOR_WINCHI) && defined(_WIN32) )
int quit, enter, ret;
printf("%s", msg); /* djb-rwth: format string added for security */
if (ulMaxTime)
{
inchiTime ulEndTime;
InchiTimeGet(&ulEndTime);
InchiTimeAddMsec(ic, &ulEndTime, ulMaxTime);
while (!_kbhit())
{
if (bInchiTimeIsOver(ic, &ulEndTime))
{
printf("\n");
return 0;
}
MySleep(100);
}
}
while (1)
{
quit = ('q' == (ret = _getch()) || 'Q' == ret || /*Esc*/ 27 == ret); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
enter = ('\r' == ret);
if (ret == 0xE0)
{
ret = _getch(); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
}
else
{
_putch(ret); /* echo */
}
if (quit || enter)
{
break;
}
printf("\r");
printf("%s", msg); /* djb-rwth: format string added for security */
}
_putch('\n');
return quit;
#else
return 0;
#endif /* #if ( defined(_WIN32) && !defined(TARGET_LIB_FOR_WINCHI) ) */
}
/****************************************************************************/
void eat_keyboard_input(void)
{
int ret_val; /* djb-rwth: adding return value */
#ifndef TARGET_LIB_FOR_WINCHI
while (_kbhit())
{
if (0xE0 == _getch())
{
ret_val = _getch(); /* djb-rwth: return value variable added */
}
}
#endif
}
#endif /* end of !COMPILE_ANSI_ONLY */
#ifndef TARGET_LIB_FOR_WINCHI
/* COVERS THE CODE FROM HERE TO THE END OF FILE */
/* Enable/disable internal tests */
/* Uncomment for INCHI_LIB testing only */
/*#define TEST_FPTRS*/
/* Windows-console-mode specific */
int bInterrupted = 0;
#if ( defined( _WIN32 ) && defined( _CONSOLE ) )
#ifndef COMPILE_ANSI_ONLY
/****************************************************************************/
BOOL WINAPI MyHandlerRoutine(DWORD dwCtrlType /* control signal type */)
{
if (dwCtrlType == CTRL_C_EVENT ||
dwCtrlType == CTRL_BREAK_EVENT ||
dwCtrlType == CTRL_CLOSE_EVENT ||
dwCtrlType == CTRL_LOGOFF_EVENT)
{
bInterrupted = 1;
return TRUE;
}
return FALSE;
}
/****************************************************************************/
int WasInterrupted(void)
{
#ifdef _DEBUG
if (bInterrupted)
{
int stop = 1; /* for debug only <BRKPT> */
}
#endif
return bInterrupted;
}
#if ( BUILD_WITH_AMI == 1 )
#define CTRL_STOP_EVENT 101
#endif
#endif /* ifndef COMPILE_ANSI_ONLY */
#endif /* if( defined( _WIN32 ) && defined( _CONSOLE ) ) */
/****************************************************************************/
int main(int argc, char* argv[])
{
/*************************/
#if ( BUILD_WITH_AMI == 1 )
/*************************/
/**** IF IN AMI MODE, main() STARTS HERE ****/
int i, ret = 0, ami = 0; /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
/* Check if multiple inputs expected */
for (i = 1; i < argc; i++)
{
if (argv[i][0] == INCHI_OPTION_PREFX)
{
if (!inchi_stricmp(argv[i] + 1, "AMI"))
{
ami = 1;
break;
}
}
}
if (ami)
{
ret = ProcessMultipleInputFiles(argc, argv); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
}
else
{
ret = ProcessSingleInputFile(argc, argv); /* djb-rwth: ignoring LLVM warning: variable used to store function return value */
}
return 0;
}
/****************************************************************************/
int ProcessMultipleInputFiles(int argc, char* argv[])
{
int i, ret = 0, nfn_ins = 0,
AMIOutStd = 0, AMILogStd = 0, AMIPrbNone = 0;
char* fn_out, * fn_log, * fn_prb;
char pNUL[] = "NUL";
char** fn_ins = NULL, ** targv = NULL;
int ret_val; /* djb-rwth: adding return value */
#if( BUILD_WITH_AMI == 1 && defined( _MSC_VER ) && MSC_AMI == 1 )
#ifdef _WIN64
typedef __int64 intptr_t; /* x64 */
#else
typedef long int intptr_t; /* VC 6.0 has no intptr_t and no x64 */
#endif
struct _finddata_t file_info;
intptr_t hFile = -1;
int retFile, lenPath;
char* pName, * pOutPath = NULL;
char pathname[_MAX_PATH];
char szBlank[] = "";
int numFiles = 0; /* counts processed files */
#else
int p;
#endif
fn_ins = (char**)inchi_calloc(argc, sizeof(char*));
if (!fn_ins)
{
fprintf(stderr, "Not enough memory.\n");
goto exit_ami;
}
/* Check for other options and collect inputs. */
for (i = 1; i < argc; i++)
{
if (argv[i][0] == INCHI_OPTION_PREFX)
{
if (!inchi_stricmp(argv[i] + 1, "STDIO"))
{
fprintf(stderr, "Options AMI and STDIO are not compatible.\n");
goto exit_ami;
}
else if (!inchi_stricmp(argv[i] + 1, "AMIOutStd"))
{
AMIOutStd = 1;
}
else if (!inchi_stricmp(argv[i] + 1, "AMILogStd"))
{
AMILogStd = 1;
}
else if (!inchi_stricmp(argv[i] + 1, "AMIPrbNone"))
{
AMIPrbNone = 1;
}
#if( BUILD_WITH_AMI == 1 && defined( _MSC_VER ) && MSC_AMI == 1 )
else if (!inchi_memicmp(argv[i] + 1, "OP:", 3))
{
pOutPath = argv[i] + 4; /* output path */
}
#endif
}
else
{
fn_ins[nfn_ins] = argv[i];
nfn_ins++;
}
}
if (!nfn_ins)
{
fprintf(stderr, "At least one input file is expected in AMI mode.\n");
goto exit_ami;
}
targv = (char**)inchi_calloc((long long)argc + 3, sizeof(char*)); /* djb-rwth: cast operator added */
if (!targv)
{
fprintf(stderr, "Not enough memory.\n");
goto exit_ami;
}
#if( BUILD_WITH_AMI == 1 && defined( _MSC_VER ) && MSC_AMI == 1 )
if ((pName = strrchr(fn_ins[0], INCHI_PATH_DELIM))) /* djb-rwth: addressing LLVM warning */
{
pName++;
lenPath = pName - fn_ins[0];
}
else
{
pName = fn_ins[0];
lenPath = 0;
}
for (hFile = _findfirst(fn_ins[0], &file_info), retFile = 0;
!retFile && -1 != hFile;
retFile = _findnext(hFile, &file_info), numFiles++)
#else
for (p = 0; p < nfn_ins; p++)
#endif
{
int targc;
#if( BUILD_WITH_AMI == 1 && defined( _MSC_VER ) && MSC_AMI == 1 )
const char* fn_in;
int inlen = lenPath + strlen(file_info.name);
if (!file_info.size || (file_info.attrib & _A_SUBDIR) || inlen >= _MAX_PATH)
{
continue;
}
memcpy(pathname, fn_ins[0], lenPath);
strcpy(pathname + lenPath, file_info.name);
fn_in = pathname;
if (0 == numFiles % 5000)
{
ret_val = _heapmin(); /* reduce heap fragmentation */ /* djb-rwth: return value variable added */
}
#else
const char* fn_in = fn_ins[p];
int inlen = strlen(fn_in);
#endif
fn_out = fn_log = fn_prb = NULL;
targv[0] = argv[0];
targv[1] = (char*)fn_in;
targc = 1;
if (AMIOutStd)
{
targv[++targc] = pNUL;
}
#if( BUILD_WITH_AMI == 1 && defined( _MSC_VER ) && MSC_AMI == 1 && ALLOW_EMPTY_PATHS == 1 )
else if (pOutPath)
{
targv[++targc] = szBlank; /* output name will be created in process_single_input(...) */
}
#endif
else
{
/* make output name as input name plus ext. */
fn_out = (char*)inchi_calloc((long long)inlen + 6, sizeof(char)); /* djb-rwth: cast operator added */
if (fn_out)
{
strcpy(fn_out, fn_in);
strcat(fn_out, ".txt");
}
targv[++targc] = fn_out;
}
if (AMILogStd)
{
targv[++targc] = pNUL;
}
#if( BUILD_WITH_AMI == 1 && defined( _MSC_VER ) && MSC_AMI == 1 && ALLOW_EMPTY_PATHS == 1 )
else if (pOutPath)
{
targv[++targc] = szBlank; /* output name will be created in process_single_input(...) */
}
#endif
else
{
/* Make log name as input name plus ext. */
fn_log = (char*)inchi_calloc((long long)inlen + 6, sizeof(char)); /* djb-rwth: cast operator added */
if (fn_log)
{
strcpy(fn_log, fn_in);
strcat(fn_log, ".log");
}
targv[++targc] = fn_log;
}
if (AMIPrbNone)
{
targv[++targc] = pNUL;
}
#if( BUILD_WITH_AMI == 1 && defined( _MSC_VER ) && MSC_AMI == 1 && ALLOW_EMPTY_PATHS == 1 )
else if (pOutPath)
{
targv[++targc] = szBlank; /* output name will be created in process_single_input(...) */
}
#endif
else
{
/* Make problem file name as input file name plus ext. */
fn_prb = (char*)inchi_calloc((long long)inlen + 6, sizeof(char)); /* djb-rwth: cast operator added */
if (fn_prb)
{
strcpy(fn_prb, fn_in);
strcat(fn_prb, ".prb");
}
targv[++targc] = fn_prb;
}
for (i = 1; i < argc; i++)
{
if (argv[i][0] == INCHI_OPTION_PREFX)
{
/* avoid strnicmp/strncasecmp */
if ((strlen(argv[i]) > 3) &&
(toupper(argv[i][1]) == 'A') && (toupper(argv[i][2]) == 'M') && (toupper(argv[i][3]) == 'I'))
{
continue;
}
targv[++targc] = argv[i];
}
}
targv[++targc] = NULL; /* djb-rwth: ui_rr */
ret = ProcessSingleInputFile(targc, targv); /* ProcessSingleInputFile() is a former main() */
if (fn_out)
{
inchi_free(fn_out);
}
if (fn_log)
{
inchi_free(fn_log);
}
if (fn_prb)
{
inchi_free(fn_prb);
}
#if ( defined( _WIN32 ) && defined( _CONSOLE ) && !defined( COMPILE_ANSI_ONLY ) )
if (ret == CTRL_STOP_EVENT)
{
goto exit_ami;
}
#endif
}
exit_ami:
#if( BUILD_WITH_AMI == 1 && defined( _MSC_VER ) && MSC_AMI == 1 )
if (hFile != -1)
{
_findclose(hFile);
}
#endif
if (targv)
{
inchi_free(targv);
}
if (fn_ins)
{
inchi_free(fn_ins);
}
return 0;
}
/****************************************************************************/
int ProcessSingleInputFile(int argc, char* argv[])
{
/**************************************/
#endif /* #if ( BUILD_WITH_AMI == 1 ) */
/**************************************/
/**** IF NOT IN AMI MODE, main() STARTS HERE ****/
int bReleaseVersion = bRELEASE_VERSION;
const int nStrLen = INCHI_SEGM_BUFLEN;
int nRet = 0;
int i;
long num_err, num_output, num_inp;
/* long rcPict[4] = {0,0,0,0}; */
unsigned long ulDisplTime = 0; /* infinite, milliseconds */
unsigned long ulTotalProcessingTime = 0;
CANON_GLOBALS CG;
INCHI_CLOCK ic;
char szTitle[MAX_SDF_HEADER + MAX_SDF_VALUE + 256];
char szSdfDataValue[MAX_SDF_VALUE + 1];
INPUT_PARMS inp_parms;
INPUT_PARMS* ip = &inp_parms;
STRUCT_DATA struct_data;
STRUCT_DATA* sd = &struct_data;
ORIG_ATOM_DATA OrigAtData; /* 0=> disconnected, 1=> original */
ORIG_ATOM_DATA* orig_inp_data = &OrigAtData;
ORIG_ATOM_DATA PrepAtData[2]; /* 0=> disconnected, 1=> original */
ORIG_ATOM_DATA* prep_inp_data = PrepAtData;
PINChI2* pINChI[INCHI_NUM];
PINChI_Aux2* pINChI_Aux[INCHI_NUM];
char* pLF, * pTAB;
INCHI_IOS_STRING temp_string_container;
INCHI_IOS_STRING* strbuf = &temp_string_container;
INCHI_IOSTREAM outputstr, logstr, prbstr, instr;
INCHI_IOSTREAM* pout = &outputstr, * plog = &logstr, * pprb = &prbstr, * inp_file = &instr;
#ifdef TARGET_EXE_STANDALONE
int inchi_ios_type = INCHI_IOS_TYPE_STRING;
#else
int inchi_ios_type = INCHI_IOS_TYPE_FILE;
#endif
#if ( READ_INCHI_STRING == 1 )
const int may_get_inchi_string_input = 1;
#else
const int may_get_inchi_string_input = 0;
#endif
int bInChI2Structure = 0;
int output_error_inchi = 0;
/* internal tests --- */
#ifndef TEST_FPTRS
STRUCT_FPTRS* pStructPtrs = NULL;
#else
STRUCT_FPTRS struct_fptrs, * pStructPtrs = &struct_fptrs; /* INCHI_LIB debug only */
#endif
#if ( defined(_WIN32) && defined(_MSC_VER) )
#if WINVER >= 0x0501 /* XP or newer */ /* 0x0600 Vista or newer */
DWORD tick_inchi_start, tick_inchi_stop;
tick_inchi_start = GetTickCount64(); /* djb-rwth: GetTickCount64() should be used */
#endif
#endif
#ifdef _WIN32
#if ( TRACE_MEMORY_LEAKS == 1 )
_CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
/* for execution outside the VC++ debugger uncomment one of the following two */
/*#define MY_REPORT_FILE _CRTDBG_FILE_STDERR */
/*#define MY_REPORT_FILE _CRTDBG_FILE_STDOUT */
#ifdef MY_REPORT_FILE
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, MY_REPORT_FILE);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, MY_REPORT_FILE);
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT, MY_REPORT_FILE);
#else
_CrtSetReportMode(_CRT_WARN | _CRT_ERROR, _CRTDBG_MODE_DEBUG);
#endif
/* turn on floating point exceptions */
{
/* Get the default control word. */
int cw = _controlfp(0, 0);
/* Set the exception masks OFF, turn exceptions on. */
/*cw &=~(EM_OVERFLOW|EM_UNDERFLOW|EM_INEXACT|EM_ZERODIVIDE|EM_DENORMAL);*/
cw &= ~(EM_OVERFLOW | EM_UNDERFLOW | EM_ZERODIVIDE | EM_DENORMAL);
/* Set the control word. */
_controlfp(cw, MCW_EM);
}
#endif
#endif
sd->bUserQuit = 0;
#if ( defined( _WIN32 ) && defined( _CONSOLE ) && !defined( COMPILE_ANSI_ONLY ) )
if (SetConsoleCtrlHandler(MyHandlerRoutine, 1))
{
ConsoleQuit = WasInterrupted;
}
#endif
num_inp = 0;
num_err = 0;
num_output = 0;
inchi_ios_init(inp_file, INCHI_IOS_TYPE_FILE, NULL);
inchi_ios_init(pout, inchi_ios_type, NULL);
inchi_ios_init(plog, inchi_ios_type, stdout);
inchi_ios_init(pprb, inchi_ios_type, NULL);
memset(strbuf, 0, sizeof(*strbuf)); /* djb-rwth: memset_s C11/Annex K variant?; dereferencing strbuf */
if (argc == 1 || (argc == 2 && (argv[1][0] == INCHI_OPTION_PREFX)) &&
(!strcmp(argv[1] + 1, "?") || !inchi_stricmp(argv[1] + 1, "help"))) /* djb-rwth: addressing LLVM warning */
{
HelpCommandLineParms(plog);
inchi_ios_flush(plog);
return 0;
}
/* original input structure */
memset(orig_inp_data, 0, sizeof(*orig_inp_data)); /* djb-rwth: memset_s C11/Annex K variant? */
memset(prep_inp_data, 0, 2 * sizeof(*prep_inp_data)); /* djb-rwth: memset_s C11/Annex K variant? */
memset(pINChI, 0, sizeof(pINChI)); /* djb-rwth: memset_s C11/Annex K variant? */
memset(pINChI_Aux, 0, sizeof(pINChI_Aux)); /* djb-rwth: memset_s C11/Annex K variant? */
memset(szSdfDataValue, 0, sizeof(szSdfDataValue)); /* djb-rwth: memset_s C11/Annex K variant? */
memset(&CG, 0, sizeof(CG)); /* djb-rwth: memset_s C11/Annex K variant? */
memset(&ic, 0, sizeof(ic)); /* djb-rwth: memset_s C11/Annex K variant? */
plog->f = stderr;
if (0 > ReadCommandLineParms(argc, (const char**)argv, ip,
szSdfDataValue, &ulDisplTime,
bReleaseVersion, plog))
{
goto exit_function;
}
if (!OpenFiles(&(inp_file->f), &(pout->f), &(plog->f), &(pprb->f), ip))
{
goto exit_function;
}
if (ip->bNoStructLabels)
{
ip->pSdfLabel = NULL;
ip->pSdfValue = NULL;
}
else if (ip->nInputType == INPUT_INCHI_PLAIN ||
ip->nInputType == INPUT_INCHI)
{
/* the input may contain both the header and the label of the structure */
if (!ip->pSdfLabel)
{
ip->pSdfLabel = ip->szSdfDataHeader;
}
if (!ip->pSdfValue)
{
ip->pSdfValue = szSdfDataValue;
}
}
set_line_separators(ip->bINChIOutputOptions, &pLF, &pTAB);
save_command_line(argc, argv, plog);
PrintInputParms(plog, ip);
inchi_ios_flush2(plog, stderr);
if (0 >= inchi_strbuf_init(strbuf, INCHI_STRBUF_INITIAL_SIZE, INCHI_STRBUF_SIZE_INCREMENT))
{
inchi_ios_eprint(plog, "Cannot allocate internal string buffer. Terminating\n");
inchi_ios_flush2(plog, stderr);
goto exit_function;
}
/* Process InChI string as input; output may be */
/* a) InChI string or b) structure */
/*#if ( READ_INCHI_STRING == 1 )*/
if (may_get_inchi_string_input && ip->nInputType == INPUT_INCHI)
{
bInChI2Structure = 0 != (ip->bReadInChIOptions & READ_INCHI_TO_STRUCTURE);
memset(sd, 0, sizeof(*sd)); /* djb-rwth: memset_s C11/Annex K variant? */
if (bInChI2Structure)
{
/* loop through file lines here */
INCHI_IOSTREAM tmpinpustream;
INCHI_IOS_STRING* pTmpIn = &tmpinpustream.s;
int crlf2lf = 0, preserve_lf = 1, read_result = 0;
inchi_ios_init(&tmpinpustream, INCHI_IOS_TYPE_STRING, NULL);
while (1)
{
char* p, * pi;
read_result = inchi_strbuf_getline(pTmpIn, inp_file->f, crlf2lf, preserve_lf);
if (read_result == -1)
#if (FIX_ONE_LINE_INCHI_INPUT_CONVERSION_ISSUE==1)
{
if (!pTmpIn->pStr)
{
break;
}
if (strlen(pTmpIn->pStr) < 8) /* 7 = strlen("InChI=1") */
{
break;
}
}
#else
break; /* EOF or read error */
#endif
p = pTmpIn->pStr;
if (!p)
{
continue;
}
pi = strstr(p, "InChI=1");
if (pi != p)
{
continue;
}
num_inp++;
ip->lMolfileNumber = num_inp;
ReadWriteInChI(&ic, &CG, &tmpinpustream, pout, plog, ip, sd, NULL, 0, 0, NULL, NULL, NULL, 0, NULL);
/*fprintf( stderr, "%ld", num_inp );*/
inchi_strbuf_reset(pTmpIn);
inchi_ios_flush2(plog, stderr);
}
fprintf(stderr, "\r");
inchi_strbuf_close(pTmpIn);
}
else
{
/* loop through file lines within ReadWriteInChI */
ReadWriteInChI(&ic, &CG, inp_file, pout, plog, ip, sd, NULL, 0, 0, NULL, NULL, NULL, 0, NULL);
num_inp = sd->fPtrStart;
num_err = sd->fPtrEnd;
}
inchi_ios_flush2(plog, stderr);
ulTotalProcessingTime = sd->ulStructTime;
/*num_inp = sd->fPtrStart;
num_err = sd->fPtrEnd;
*/
goto exit_function;
}
ulTotalProcessingTime = 0;
if (pStructPtrs)
{
memset(pStructPtrs, 0, sizeof(pStructPtrs[0])); /* djb-rwth: memset_s C11/Annex K variant? */
}
output_error_inchi = ip->bINChIOutputOptions2 & INCHI_OUT_INCHI_GEN_ERROR;
/*************************************************************/
/* Main cycle : read input structures and create their INChI */
/*************************************************************/
while (!sd->bUserQuit && !bInterrupted)
{
int do_renumbering = 0;
int next_action;
int have_err_in_GetOneStructure = 0;
int dup_fail = 0;
ORIG_ATOM_DATA SavedOrigAtData; /* 0=> disconnected, 1=> original */
ORIG_ATOM_DATA* saved_orig_inp_data = &SavedOrigAtData;
char ikey0[28];
ikey0[0] = '\0';
next_action = GetTheNextRecordOfInputFile(&ic, sd, ip, szTitle,
inp_file, plog, pout, pprb,
orig_inp_data, &num_inp, pStructPtrs,
&nRet, &have_err_in_GetOneStructure,
&num_err, output_error_inchi);
if (next_action == DO_EXIT_FUNCTION)
{
goto exit_function;
}
else if (next_action == DO_BREAK_MAIN_LOOP)
{
break;
}
else if (next_action == DO_CONTINUE_MAIN_LOOP)
{
if (nRet == _IS_ERROR) /* may also be _IS_SKIP ... */
{
if (output_error_inchi)
{
Output_RecordInfo(pout, num_inp, ip->bNoStructLabels, ip->pSdfLabel, ip->pSdfValue, ip->lSdfId,
pLF, pTAB);
emit_empty_inchi(ip, num_inp, pLF, pTAB, pout);
}
}
continue;
}
/* Create INChI for each connected component of the structure;
optionally display them;
output INChI for the whole structure */
#if (RENUMBER_ATOMS_AND_RECALC_V106 == 1 )
if (ip->bRenumber == 1)
{
do_renumbering = 1;
}
#endif
if (do_renumbering == 0)
{
/* Normal calculations */
next_action = CalcAndPrintINCHIAndINCHIKEY(&ic, &CG, sd, ip, szTitle,
pINChI, pINChI_Aux,
inp_file, plog, pout, pprb,
orig_inp_data, prep_inp_data, &num_inp, pStructPtrs,
&nRet, have_err_in_GetOneStructure,
&num_err, output_error_inchi,
strbuf, &ulTotalProcessingTime,
pLF, pTAB, ikey0,
0 /* not silent */);
FreeAllINChIArrays(pINChI, pINChI_Aux, sd->num_components);
FreeOrigAtData(orig_inp_data);
FreeOrigAtData(prep_inp_data);
FreeOrigAtData(prep_inp_data + 1);
}
else
{
/* Internal test mode: renumber atoms and recalculate repeatedly */
long int nrepeat = 1;
/* 2! = 2 3! = 6 4! = 24 5! = 120 6! = 520 7! = 5040 8! = 40320 */
if (orig_inp_data->num_inp_atoms == 1)
{
nrepeat = 1;
}
else if (orig_inp_data->num_inp_atoms == 2)
{
nrepeat = 2;
}
else if (orig_inp_data->num_inp_atoms == 3)
{
nrepeat = 6;
}
else
{
nrepeat = 1000; /* 100000;*/ /*16;*/
}
/* correct (decrease repeat number) for relatively large molecules */
if (orig_inp_data->num_inp_atoms > 128)
{
nrepeat = 100; /* 100000;*/ /*16;*/
}
if (orig_inp_data->num_inp_atoms > 256)
{
nrepeat = 50; /* 100000;*/ /*16;*/
}
if (orig_inp_data->num_inp_atoms > 512)
{
nrepeat = 25; /* 100000;*/ /*16;*/
}
if (orig_inp_data->num_inp_atoms > 1024)
{
nrepeat = 10; /* 100000;*/ /*16;*/
}
/*inchi_ios_eprint(plog, "Number of random atom renumberings up to: %-ld\n", nrepeat);
inchi_ios_flush2(plog, stderr);*/
#if 0
else if (orig_inp_data->num_inp_atoms == 4)
{
nrepeat = 24;
}
else
{
nrepeat = 10;
}
else if (orig_inp_data->num_inp_atoms == 4)
{
nrepeat = 24;
}
else if (orig_inp_data->num_inp_atoms == 5)
{
nrepeat = 120;
}
else if (orig_inp_data->num_inp_atoms == 6)
{
nrepeat = 520;
}
else if (orig_inp_data->num_inp_atoms == 7)
{
nrepeat = 1000;
}
else
{
nrepeat = 10000;
}
#endif
next_action = RepeatedlyRenumberAtomsAndRecalcINCHI(&ic, &CG, sd, ip, szTitle,
pINChI, pINChI_Aux,
inp_file, plog, pout, pprb,
orig_inp_data, prep_inp_data, &num_inp, pStructPtrs,
&nRet, have_err_in_GetOneStructure,
&num_err, output_error_inchi,
strbuf, &ulTotalProcessingTime,
pLF, pTAB, nrepeat);
} /* if (ip->bRenumber == 1) */
if (next_action == DO_EXIT_FUNCTION)
{
goto exit_function;
}
else if (next_action == DO_BREAK_MAIN_LOOP)
{
break;
}
else if (next_action == DO_CONTINUE_MAIN_LOOP)
{
continue;
}
} /* end of main cycle - while ( !sd->bUserQuit && !bInterrupted ) */
exit_function:
/* Avoid memory leaks in case of fatal error */
if (pStructPtrs && pStructPtrs->fptr)
{
inchi_free(pStructPtrs->fptr);
}
/* Free INChI memory */
FreeAllINChIArrays(pINChI, pINChI_Aux, sd->num_components);
/* Free structure data */
FreeOrigAtData(orig_inp_data);
FreeOrigAtData(prep_inp_data);
FreeOrigAtData(prep_inp_data + 1);
/* Close files */
inchi_ios_close(inp_file);
inchi_ios_close(pout);
inchi_ios_close(pprb);
{
int hours, minutes, seconds, mseconds;
SplitTime(ulTotalProcessingTime, &hours, &minutes, &seconds, &mseconds);
inchi_ios_eprint(plog, "Finished processing %ld structure%s: %ld error%s, processing time %d:%02d:%02d.%02d\n",
num_inp, num_inp == 1 ? "" : "s",
num_err, num_err == 1 ? "" : "s",
hours, minutes, seconds, mseconds / 10);
inchi_ios_flush2(plog, stderr);
}
#if ( defined(_WIN32) && defined(_MSC_VER) )
#if WINVER >= 0x0501 /* XP or newer */ /* 0x0600 Vista or newer */
tick_inchi_stop = GetTickCount64(); /* djb-rwth: GetTickCount64() should be used */
inchi_ios_eprint(plog, "\nElapsed walltime: %d msec.\n", tick_inchi_stop - tick_inchi_start);
inchi_ios_flush2(plog, stderr);
#endif
#endif
/* Final cleanup */
inchi_ios_close(plog);
inchi_strbuf_close(strbuf);
for (i = 0; i < MAX_NUM_PATHS; i++)
{
if (ip->path[i])
{
inchi_free((void*)ip->path[i]); /* cast deliberately discards 'const' qualifier */
ip->path[i] = NULL;
}
}
SetBitFree(&CG);
#if ( ( BUILD_WITH_AMI==1 ) && defined( _WIN32 ) && defined( _CONSOLE ) && !defined( COMPILE_ANSI_ONLY ) )
if (bInterrupted)
{
return CTRL_STOP_EVENT;
}
#endif