1 //
2 // CDDL HEADER START
3 //
4 // The contents of this file are subject to the terms of the Common
5 // Development and Distribution License Version 1.0 (the "License").
6 //
7 // You can obtain a copy of the license at
8 // http://www.opensource.org/licenses/CDDL-1.0. See the License for the
9 // specific language governing permissions and limitations under the License.
10 //
11 // When distributing Covered Code, include this CDDL HEADER in each file and
12 // include the License file in a prominent location with the name
13 // LICENSE.CDDL.
14 // If applicable, add the following below this CDDL HEADER, with the fields
15 // enclosed by brackets "[]" replaced with your own identifying information:
16 //
17 // Portions Copyright (c) [yyyy] [name of copyright owner].
18 // All rights reserved.
19 //
20 // CDDL HEADER END
21 //
22
23 //
24 // Copyright (c) 2016--2020, Regents of the University of Minnesota.
25 // All rights reserved.
26 //
27 // Contributors:
28 // Ryan S. Elliott
29 //
30
31 //
32 // Release: This file is part of the kim-api-2.2.1 package.
33 //
34
35
36 #include <climits>
37 #include <cstddef>
38 #include <iostream>
39 #include <string>
40
41 #ifndef KIM_LOG_VERBOSITY_HPP_
42 #include "KIM_LogVerbosity.hpp"
43 #endif
44 extern "C" {
45 #ifndef KIM_LOG_VERBOSITY_H_
46 #include "KIM_LogVerbosity.h"
47 #endif
48 } // extern "C"
49
50
51 #ifndef KIM_COLLECTION_HPP_
52 #include "KIM_Collection.hpp"
53 #endif
54 extern "C" {
55 #ifndef KIM_COLLECTION_H_
56 #include "KIM_Collection.h"
57 #endif
58 } // extern "C"
59
60 #ifndef KIM_COLLECTION_ITEM_TYPE_HPP_
61 #include "KIM_CollectionItemType.hpp"
62 #endif
63 extern "C" {
64 #ifndef KIM_COLLECTION_ITEM_TYPE_H_
65 #include "KIM_CollectionItemType.h"
66 #endif
67 } // extern "C"
68
69 #ifndef KIM_COLLECTIONS_HPP_
70 #include "KIM_Collections.hpp"
71 #endif
72 extern "C" {
73 #ifndef KIM_COLLECTIONS_H_
74 #include "KIM_Collections.h"
75 #endif
76 } // extern "C"
77
78 struct KIM_Collections
79 {
80 void * p;
81 };
82
83 #define CONVERT_POINTER \
84 KIM::Collections * pCollections \
85 = reinterpret_cast<KIM::Collections *>(collections->p)
86
87 namespace
88 {
makeItemTypeCpp(KIM_CollectionItemType const itemType)89 KIM::CollectionItemType makeItemTypeCpp(KIM_CollectionItemType const itemType)
90 {
91 return KIM::CollectionItemType(itemType.collectionItemTypeID);
92 }
93
makeCollectionCpp(KIM_Collection const collection)94 KIM::Collection makeCollectionCpp(KIM_Collection const collection)
95 {
96 return KIM::Collection(collection.collectionID);
97 }
98
makeLogVerbosityCpp(KIM_LogVerbosity logVerbosity)99 KIM::LogVerbosity makeLogVerbosityCpp(KIM_LogVerbosity logVerbosity)
100 {
101 return KIM::LogVerbosity(logVerbosity.logVerbosityID);
102 }
103 } // namespace
104
105
KIM_Collections_Create(KIM_Collections ** const collections)106 int KIM_Collections_Create(KIM_Collections ** const collections)
107 {
108 KIM::Collections * pCollections;
109 int error = KIM::Collections::Create(&pCollections);
110 if (error)
111 {
112 *collections = NULL;
113 return true;
114 }
115 else
116 {
117 (*collections) = new KIM_Collections;
118 (*collections)->p = (void *) pCollections;
119 return false;
120 }
121 }
122
KIM_Collections_Destroy(KIM_Collections ** const collections)123 void KIM_Collections_Destroy(KIM_Collections ** const collections)
124 {
125 if (*collections != NULL)
126 {
127 KIM::Collections * pCollections
128 = reinterpret_cast<KIM::Collections *>((*collections)->p);
129
130 KIM::Collections::Destroy(&pCollections);
131 }
132 delete (*collections);
133 *collections = NULL;
134 }
135
KIM_Collections_GetItemType(KIM_Collections * const collections,char const * const itemName,KIM_CollectionItemType * const itemType)136 int KIM_Collections_GetItemType(KIM_Collections * const collections,
137 char const * const itemName,
138 KIM_CollectionItemType * const itemType)
139 {
140 CONVERT_POINTER;
141
142 return pCollections->GetItemType(
143 itemName, reinterpret_cast<KIM::CollectionItemType *>(itemType));
144 }
145
KIM_Collections_GetItemLibraryFileNameAndCollection(KIM_Collections * const collections,KIM_CollectionItemType const itemType,char const * const itemName,char const ** const fileName,KIM_Collection * const collection)146 int KIM_Collections_GetItemLibraryFileNameAndCollection(
147 KIM_Collections * const collections,
148 KIM_CollectionItemType const itemType,
149 char const * const itemName,
150 char const ** const fileName,
151 KIM_Collection * const collection)
152 {
153 CONVERT_POINTER;
154
155 std::string const * pStrFileName;
156 int error = pCollections->GetItemLibraryFileNameAndCollection(
157 makeItemTypeCpp(itemType),
158 itemName,
159 &pStrFileName,
160 reinterpret_cast<KIM::Collection *>(collection));
161 if (error)
162 return true;
163 else
164 {
165 *fileName = pStrFileName->c_str();
166 return false;
167 }
168 }
169
KIM_Collections_CacheListOfItemMetadataFiles(KIM_Collections * const collections,KIM_CollectionItemType const itemType,char const * const itemName,int * const extent)170 int KIM_Collections_CacheListOfItemMetadataFiles(
171 KIM_Collections * const collections,
172 KIM_CollectionItemType const itemType,
173 char const * const itemName,
174 int * const extent)
175 {
176 CONVERT_POINTER;
177
178 return pCollections->CacheListOfItemMetadataFiles(
179 makeItemTypeCpp(itemType), itemName, extent);
180 }
181
KIM_Collections_GetItemMetadataFile(KIM_Collections * const collections,int const index,char const ** const fileName,unsigned int * const fileLength,unsigned char const ** const fileRawData,int * const availableAsString,char const ** const fileString)182 int KIM_Collections_GetItemMetadataFile(
183 KIM_Collections * const collections,
184 int const index,
185 char const ** const fileName,
186 unsigned int * const fileLength,
187 unsigned char const ** const fileRawData,
188 int * const availableAsString,
189 char const ** const fileString)
190 {
191 CONVERT_POINTER;
192
193 std::string const * pStrFileName;
194 std::string const ** ppStrFileName;
195 if (fileName == NULL)
196 ppStrFileName = NULL;
197 else
198 ppStrFileName = &pStrFileName;
199
200 std::string const * pStrFileString;
201 std::string const ** ppStrFileString;
202 if (fileString == NULL)
203 ppStrFileString = NULL;
204 else
205 ppStrFileString = &pStrFileString;
206
207 int error = pCollections->GetItemMetadataFile(index,
208 ppStrFileName,
209 fileLength,
210 fileRawData,
211 availableAsString,
212 ppStrFileString);
213 if (error)
214 return true;
215 else
216 {
217 if (fileName != NULL) *fileName = pStrFileName->c_str();
218 if (fileString != NULL)
219 {
220 if (pStrFileString == NULL)
221 *fileString = NULL;
222 else
223 *fileString = pStrFileString->c_str();
224 }
225 return false;
226 }
227 }
228
229 extern "C" {
KIM_Collections_GetItemMetadataFile_fortran(KIM_Collections * const collections,int const index,char const ** const fileName,long * const fileLength,unsigned char const ** const fileRawData,int * const availableAsString,char const ** const fileString)230 int KIM_Collections_GetItemMetadataFile_fortran(
231 KIM_Collections * const collections,
232 int const index,
233 char const ** const fileName,
234 long * const fileLength,
235 unsigned char const ** const fileRawData,
236 int * const availableAsString,
237 char const ** const fileString)
238 {
239 unsigned int uFileLength;
240 int error = KIM_Collections_GetItemMetadataFile(collections,
241 index,
242 fileName,
243 &uFileLength,
244 fileRawData,
245 availableAsString,
246 fileString);
247 if (error)
248 return true;
249 else if ((sizeof(int) == sizeof(long)) && (uFileLength > INT_MAX))
250 {
251 std::cerr << "* Error : long overflow for fileLength. " << __FILE__ << ":"
252 << __LINE__ << std::endl;
253 return true;
254 }
255 else
256 {
257 *fileLength = static_cast<long>(uFileLength);
258 return false;
259 }
260 }
261 } // extern "C"
262
KIM_Collections_CacheListOfItemNamesByType(KIM_Collections * const collections,KIM_CollectionItemType const itemType,int * const extent)263 int KIM_Collections_CacheListOfItemNamesByType(
264 KIM_Collections * const collections,
265 KIM_CollectionItemType const itemType,
266 int * const extent)
267 {
268 CONVERT_POINTER;
269
270 return pCollections->CacheListOfItemNamesByType(makeItemTypeCpp(itemType),
271 extent);
272 }
273
KIM_Collections_GetItemNameByType(KIM_Collections * const collections,int const index,char const ** const itemName)274 int KIM_Collections_GetItemNameByType(KIM_Collections * const collections,
275 int const index,
276 char const ** const itemName)
277 {
278 CONVERT_POINTER;
279
280 std::string const * pStrItemName;
281 int error = pCollections->GetItemNameByType(index, &pStrItemName);
282 if (error)
283 return true;
284 else
285 {
286 *itemName = pStrItemName->c_str();
287 return false;
288 }
289 }
290
KIM_Collections_CacheListOfItemNamesByCollectionAndType(KIM_Collections * const collections,KIM_Collection const collection,KIM_CollectionItemType const itemType,int * const extent)291 int KIM_Collections_CacheListOfItemNamesByCollectionAndType(
292 KIM_Collections * const collections,
293 KIM_Collection const collection,
294 KIM_CollectionItemType const itemType,
295 int * const extent)
296 {
297 CONVERT_POINTER;
298
299 return pCollections->CacheListOfItemNamesByCollectionAndType(
300 makeCollectionCpp(collection), makeItemTypeCpp(itemType), extent);
301 }
302
KIM_Collections_GetItemNameByCollectionAndType(KIM_Collections * const collections,int const index,char const ** const itemName)303 int KIM_Collections_GetItemNameByCollectionAndType(
304 KIM_Collections * const collections,
305 int const index,
306 char const ** const itemName)
307 {
308 CONVERT_POINTER;
309
310 std::string const * pStrItemName;
311 int error
312 = pCollections->GetItemNameByCollectionAndType(index, &pStrItemName);
313 if (error)
314 return true;
315 else
316 {
317 *itemName = pStrItemName->c_str();
318 return false;
319 }
320 }
321
KIM_Collections_GetItemLibraryFileNameByCollectionAndType(KIM_Collections * const collections,KIM_Collection const collection,KIM_CollectionItemType const itemType,char const * const itemName,char const ** const fileName)322 int KIM_Collections_GetItemLibraryFileNameByCollectionAndType(
323 KIM_Collections * const collections,
324 KIM_Collection const collection,
325 KIM_CollectionItemType const itemType,
326 char const * const itemName,
327 char const ** const fileName)
328 {
329 CONVERT_POINTER;
330
331 std::string const * pStrFileName;
332 int error = pCollections->GetItemLibraryFileNameByCollectionAndType(
333 makeCollectionCpp(collection),
334 makeItemTypeCpp(itemType),
335 itemName,
336 &pStrFileName);
337 if (error)
338 return true;
339 else
340 {
341 *fileName = pStrFileName->c_str();
342 return false;
343 }
344 }
345
KIM_Collections_CacheListOfItemMetadataFilesByCollectionAndType(KIM_Collections * const collections,KIM_Collection const collection,KIM_CollectionItemType const itemType,char const * const itemName,int * const extent)346 int KIM_Collections_CacheListOfItemMetadataFilesByCollectionAndType(
347 KIM_Collections * const collections,
348 KIM_Collection const collection,
349 KIM_CollectionItemType const itemType,
350 char const * const itemName,
351 int * const extent)
352 {
353 CONVERT_POINTER;
354
355 return pCollections->CacheListOfItemMetadataFilesByCollectionAndType(
356 makeCollectionCpp(collection),
357 makeItemTypeCpp(itemType),
358 itemName,
359 extent);
360 }
361
KIM_Collections_GetItemMetadataFileByCollectionAndType(KIM_Collections * const collections,int const index,char const ** const fileName,unsigned int * const fileLength,unsigned char const ** const fileRawData,int * const availableAsString,char const ** const fileString)362 int KIM_Collections_GetItemMetadataFileByCollectionAndType(
363 KIM_Collections * const collections,
364 int const index,
365 char const ** const fileName,
366 unsigned int * const fileLength,
367 unsigned char const ** const fileRawData,
368 int * const availableAsString,
369 char const ** const fileString)
370 {
371 CONVERT_POINTER;
372
373 std::string const * pStrFileName;
374 std::string const ** ppStrFileName;
375 if (fileName == NULL)
376 ppStrFileName = NULL;
377 else
378 ppStrFileName = &pStrFileName;
379
380 std::string const * pStrFileString;
381 std::string const ** ppStrFileString;
382 if (fileString == NULL)
383 ppStrFileString = NULL;
384 else
385 ppStrFileString = &pStrFileString;
386
387 int error
388 = pCollections->GetItemMetadataFileByCollectionAndType(index,
389 ppStrFileName,
390 fileLength,
391 fileRawData,
392 availableAsString,
393 ppStrFileString);
394 if (error)
395 return true;
396 else
397 {
398 if (fileName != NULL) *fileName = pStrFileName->c_str();
399 if (fileString != NULL)
400 {
401 if (pStrFileString == NULL)
402 *fileString = NULL;
403 else
404 *fileString = pStrFileString->c_str();
405 }
406 return false;
407 }
408 }
409
410 extern "C" {
KIM_Collections_GetItemMetadataFileByCollectionAndType_fortran(KIM_Collections * const collections,int const index,char const ** const fileName,long * const fileLength,unsigned char const ** const fileRawData,int * const availableAsString,char const ** const fileString)411 int KIM_Collections_GetItemMetadataFileByCollectionAndType_fortran(
412 KIM_Collections * const collections,
413 int const index,
414 char const ** const fileName,
415 long * const fileLength,
416 unsigned char const ** const fileRawData,
417 int * const availableAsString,
418 char const ** const fileString)
419 {
420 unsigned int uFileLength;
421 int error = KIM_Collections_GetItemMetadataFileByCollectionAndType(
422 collections,
423 index,
424 fileName,
425 &uFileLength,
426 fileRawData,
427 availableAsString,
428 fileString);
429 if (error)
430 return true;
431 else if ((sizeof(int) == sizeof(long)) && (uFileLength > INT_MAX))
432 {
433 std::cerr << "* Error : long overflow for fileLength. " << __FILE__ << ":"
434 << __LINE__ << std::endl;
435 return true;
436 }
437 else
438 {
439 *fileLength = static_cast<long>(uFileLength);
440 return false;
441 }
442 }
443 } // extern "C"
444
KIM_Collections_GetProjectNameAndSemVer(KIM_Collections * const collections,char const ** const projectName,char const ** const semVer)445 void KIM_Collections_GetProjectNameAndSemVer(
446 KIM_Collections * const collections,
447 char const ** const projectName,
448 char const ** const semVer)
449 {
450 CONVERT_POINTER;
451
452 std::string const * pStrProjectName;
453 std::string const ** ppStrProjectName;
454 if (projectName == NULL)
455 ppStrProjectName = NULL;
456 else
457 ppStrProjectName = &pStrProjectName;
458
459 std::string const * pStrSemVer;
460 std::string const ** ppStrSemVer;
461 if (semVer == NULL)
462 ppStrSemVer = NULL;
463 else
464 ppStrSemVer = &pStrSemVer;
465
466 pCollections->GetProjectNameAndSemVer(ppStrProjectName, ppStrSemVer);
467 if (projectName != NULL) *projectName = pStrProjectName->c_str();
468 if (semVer != NULL) *semVer = pStrSemVer->c_str();
469 }
470
KIM_Collections_GetEnvironmentVariableName(KIM_Collections * const collections,KIM_CollectionItemType const itemType,char const ** const name)471 int KIM_Collections_GetEnvironmentVariableName(
472 KIM_Collections * const collections,
473 KIM_CollectionItemType const itemType,
474 char const ** const name)
475 {
476 CONVERT_POINTER;
477
478 std::string const * pStrName;
479 int error = pCollections->GetEnvironmentVariableName(
480 makeItemTypeCpp(itemType), &pStrName);
481 if (error)
482 return true;
483 else
484 {
485 *name = pStrName->c_str();
486 return false;
487 }
488 }
489
KIM_Collections_GetConfigurationFileEnvironmentVariable(KIM_Collections * const collections,char const ** const name,char const ** const value)490 void KIM_Collections_GetConfigurationFileEnvironmentVariable(
491 KIM_Collections * const collections,
492 char const ** const name,
493 char const ** const value)
494 {
495 CONVERT_POINTER;
496
497 std::string const * pStrName;
498 std::string const ** ppStrName;
499 if (name == NULL)
500 ppStrName = NULL;
501 else
502 ppStrName = &pStrName;
503
504 std::string const * pStrValue;
505 std::string const ** ppStrValue;
506 if (value == NULL)
507 ppStrValue = NULL;
508 else
509 ppStrValue = &pStrValue;
510
511 pCollections->GetConfigurationFileEnvironmentVariable(ppStrName, ppStrValue);
512
513 if (name != NULL) *name = pStrName->c_str();
514 if (value != NULL) *value = pStrValue->c_str();
515 }
516
KIM_Collections_GetConfigurationFileName(KIM_Collections * const collections,char const ** const fileName)517 void KIM_Collections_GetConfigurationFileName(
518 KIM_Collections * const collections, char const ** const fileName)
519 {
520 CONVERT_POINTER;
521
522 std::string const * pStrFileName;
523 pCollections->GetConfigurationFileName(&pStrFileName);
524 *fileName = pStrFileName->c_str();
525 }
526
KIM_Collections_CacheListOfDirectoryNames(KIM_Collections * const collections,KIM_Collection const collection,KIM_CollectionItemType const itemType,int * const extent)527 int KIM_Collections_CacheListOfDirectoryNames(
528 KIM_Collections * const collections,
529 KIM_Collection const collection,
530 KIM_CollectionItemType const itemType,
531 int * const extent)
532 {
533 CONVERT_POINTER;
534
535 return pCollections->CacheListOfDirectoryNames(
536 makeCollectionCpp(collection), makeItemTypeCpp(itemType), extent);
537 }
538
KIM_Collections_GetDirectoryName(KIM_Collections * const collections,int const index,char const ** const directoryName)539 int KIM_Collections_GetDirectoryName(KIM_Collections * const collections,
540 int const index,
541 char const ** const directoryName)
542 {
543 CONVERT_POINTER;
544
545 std::string const * pStrDirectoryName;
546 int error = pCollections->GetDirectoryName(index, &pStrDirectoryName);
547 if (error)
548 return true;
549 else
550 {
551 *directoryName = pStrDirectoryName->c_str();
552 return true;
553 }
554 }
555
KIM_Collections_SetLogID(KIM_Collections * const collections,char const * const logID)556 void KIM_Collections_SetLogID(KIM_Collections * const collections,
557 char const * const logID)
558 {
559 CONVERT_POINTER;
560
561 pCollections->SetLogID(logID);
562 }
563
KIM_Collections_PushLogVerbosity(KIM_Collections * const collections,KIM_LogVerbosity const logVerbosity)564 void KIM_Collections_PushLogVerbosity(KIM_Collections * const collections,
565 KIM_LogVerbosity const logVerbosity)
566 {
567 CONVERT_POINTER;
568
569 pCollections->PushLogVerbosity(makeLogVerbosityCpp(logVerbosity));
570 }
571
KIM_Collections_PopLogVerbosity(KIM_Collections * const collections)572 void KIM_Collections_PopLogVerbosity(KIM_Collections * const collections)
573 {
574 CONVERT_POINTER;
575
576 pCollections->PopLogVerbosity();
577 }
578