-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisis.cpp
More file actions
350 lines (308 loc) · 7.93 KB
/
isis.cpp
File metadata and controls
350 lines (308 loc) · 7.93 KB
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
/*
* International Chemical Identifier (InChI)
* Version 1
* Software version 1.07
* April 30, 2024
*
* 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 "stdafx.h"
#include "isis.h"
#include <io.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////// ISIS Clipboard ///////////////////////
static UINT ctFormat;
static UINT skFormat;
/////////////////////////////////////////////////////////////////////////////
BOOL __stdcall InitMDLClipBoard()
{
ctFormat = RegisterClipboardFormat("MDLCT");
skFormat = RegisterClipboardFormat("MDLSK");
return (ctFormat != 0) || (skFormat != 0);
}
/////////////////////////////////////////////////////////////////////////////
static BOOL IsChemSiteClipboardAvailable()
{
BOOL bRet = FALSE;
HWND hwnd;
if( IsClipboardFormatAvailable(CF_TEXT) && (hwnd = GetClipboardOwner()) )
{
TCHAR OwnerName[_MAX_PATH];
int ret = GetClassName( hwnd, // handle to window
OwnerName, // class name
_MAX_PATH // size of class name buffer
);
bRet = (ret < _MAX_PATH) && (lstrcmp( OwnerName, _T("ChemSite")) == 0 );
}
return bRet;
}
/////////////////////////////////////////////////////////////////////////////
BOOL __stdcall IsMDLClipboardAvailable()
{
return ( ctFormat && IsClipboardFormatAvailable(ctFormat) ) ||
( skFormat && IsClipboardFormatAvailable(skFormat) && IsClipboardFormatAvailable(CF_TEXT) ) ||
IsChemSiteClipboardAvailable();
}
/////////////////////////////////////////////////////////////////////////////
static int SaveMDLCT( LPCTSTR Filename, const char* p, int nSize )
{
int i, len, nLine, the_rest, nAtoms, nBonds;
const char *q;
char szVal[4];
static char szEnd[] = "M END";
static char szRXN_Flag[] = "$RXN";
static char szRGF_Flag[] = "$MDL";
FILE *f;
f = _tfopen( Filename, _T("wt") );
if( f == NULL )
{
return 1;
}
#define FLAG_LEN 4
for ( i = nLine = 0; i < nSize && i+(len=(int)(unsigned char)p[i]) < nSize; i += len+1, nLine ++ )
{
// check if it is a MOLfile
if ( nLine == 0 && len >= FLAG_LEN )
{
for ( q = &p[i+1], the_rest=len; NULL != (q = (const char *) memchr( q, '$', the_rest ) ) && FLAG_LEN <= (the_rest = len-(q-&p[i+1]) ); q++ )
{
if ( 0 == memcmp( q, szRXN_Flag, FLAG_LEN ) || 0 == memcmp( q, szRGF_Flag, FLAG_LEN ) )
{
fclose(f);
return 2; // not a MOLfile
}
}
}
if ( len )
{
fwrite( &p[i+1], 1, len, f );
}
fwrite( "\n", 1, 1, f );
// extract number of atoms and bonds
if ( 3 == nLine && len > 3 )
{
strncpy( szVal, &p[i+1], 3 );
szVal[3] = '\0';
nAtoms = (int)strtol(szVal, NULL, 10);
strncpy( szVal, &p[i+1]+3, 3 );
szVal[3] = '\0';
nBonds = (int)strtol(&p[i+1]+3, NULL, 10);
}
// check if it is the last line
if ( nLine >= 4 + nAtoms + nBonds && len >= sizeof( szEnd ) - 1 && 0 == memcmp(&p[i+1], szEnd, len ) )
{
break;
}
}
fclose(f);
return (nLine <= 4) ? 3 : 0; // MOLfile should have at least 5 lines
}
/////////////////////////////////////////////////////////////////////////////
int __stdcall GetMDLClipboardData( HWND hwnd, LPCTSTR Filename )
{
DWORD dwSize;
LPVOID p;
int ret = -1;
HANDLE hCTBuffer = 0;
UINT nFormat = 0;
if(IsClipboardFormatAvailable(ctFormat))
{
nFormat = ctFormat;
}
else if( (IsClipboardFormatAvailable(skFormat) && IsClipboardFormatAvailable(CF_TEXT)) || IsChemSiteClipboardAvailable() )
{
nFormat = CF_TEXT;
}
if( nFormat == 0 )
{
return ret;
}
if (OpenClipboard(hwnd))
{
hCTBuffer = GetClipboardData(nFormat);
CloseClipboard();
}
if( (GlobalFlags(hCTBuffer) & GMEM_DISCARDED) != GMEM_DISCARDED )
{
dwSize = GlobalSize(hCTBuffer);
if( dwSize )
{
p = GlobalLock(hCTBuffer);
if ( p )
{
if( nFormat == ctFormat )
{
ret = SaveMDLCT( Filename, (const char *)p, dwSize );
}
else
{
FILE * f = _tfopen( Filename, _T("wb") );
if( f != NULL )
{
fputs( (char *)p, f );
fclose( f ) ;
ret = 0;
}
}
}
GlobalUnlock(hCTBuffer);
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
static HANDLE ReadMDLCT( LPCTSTR Filename )
{
DWORD dwSize;
char line[256];
char * p, * end;
char * buf = NULL;
int len;
FILE *f = NULL;
HANDLE hCTBuffer = 0;
f = _tfopen( Filename, _T("rt") );
if( f == NULL )
{
goto _cleanup;
}
dwSize = _filelength(_fileno(f));
if( dwSize == 0 )
{
goto _cleanup;
}
buf = (char *)calloc( dwSize + 1, 1 );
if( buf == NULL )
{
goto _cleanup;
}
p = buf;
while( fgets(line,sizeof(line),f) )
{
if( (end = strchr(line,'\n')) == NULL )
{
goto _cleanup;
}
*end = 0;
len = strlen(line);
*p++ = (char)len;
if( len )
{
strcpy(p,line);
p+=len;
}
}
dwSize = p - buf +1;
hCTBuffer = GlobalAlloc( GMEM_MOVEABLE | GMEM_DDESHARE, dwSize );
memmove( GlobalLock( hCTBuffer ), buf, dwSize );
GlobalUnlock( hCTBuffer );
_cleanup:
if( f )
{
fclose(f);
}
if( buf )
{
free(buf);
}
return hCTBuffer;
}
/////////////////////////////////////////////////////////////////////////////
static HANDLE ReadCFTXT( LPCTSTR Filename )
{
DWORD dwSize;
FILE *f = NULL;
HANDLE hCFTBuffer = 0;
char * buf = NULL;
f = _tfopen( Filename, _T("rb") );
if( f == NULL )
{
goto _cleanup;
}
dwSize = _filelength(_fileno(f));
if( dwSize == 0 )
{
goto _cleanup;
}
buf = (char *)calloc( dwSize + 1, 1 );
if( buf == NULL )
{
goto _cleanup;
}
fread(buf, dwSize, 1, f);
dwSize++;
hCFTBuffer = GlobalAlloc( GMEM_MOVEABLE | GMEM_DDESHARE, dwSize );
memmove( GlobalLock( hCFTBuffer ), buf, dwSize );
GlobalUnlock( hCFTBuffer );
_cleanup:
if( f )
{
fclose(f);
}
if( buf )
{
free(buf);
}
return hCFTBuffer;
}
/////////////////////////////////////////////////////////////////////////////
int __stdcall SetMDLCTClipboardData( HWND hwnd, LPCTSTR Filename )
{
HANDLE hCTBuffer = NULL, hTextBuffer = NULL;
int ret = -1;
if( ctFormat )
{
hCTBuffer = ReadMDLCT(Filename);
}
hTextBuffer = ReadCFTXT(Filename);
if( hCTBuffer || hTextBuffer )
{
if (OpenClipboard(hwnd))
{
EmptyClipboard();
SetClipboardData(ctFormat, hCTBuffer);
SetClipboardData(CF_TEXT, hTextBuffer);
CloseClipboard();
ret = 0;
}
else
{
if( hCTBuffer )
{
GlobalFree(hCTBuffer);
}
if( hTextBuffer )
{
GlobalFree(hTextBuffer);
}
}
}
return ret;
}