Print Table of Contents
This example demonstrates how to open an SDRC Universal file and display its table of contents using the Library Manager interface. We’ll walk through the process step by step, showing you how to initialize the library manager, open a file, and print a complete table of contents.
Overview
The example performs the following main tasks:
- Parse command line arguments, and validate license
- Open the file using Library Manager
- Check for errors and handle them appropriately
- Print the complete table of contents
- Clean up resources
Let’s examine each step in detail.
Step 1: Initialize and Setup
First, we include necessary headers, declare variables, and handle command line arguments:
#include "sam/base/base.h"
#include "sam/vdm/vdm.h"
#include "sam/base/license.h"
#include "sam/hoops_license.h"
int main(int argc, char** argv)
{
char inputFile[256];
/* check input arguments */
if (argc < 2) {
fprintf(stderr, "Usage: %s inputfile [appendfile]\n", argv[0]);
fprintf(stderr, " inputfile is blank, 'bumper.unv' is assumed\n");
strcpy(inputFile, "bumper.unv");
}
else {
strcpy(inputFile, argv[1]);
}
vsy_LicenseValidate(HOOPS_LICENSE);
The program provides helpful information about all the datasets in the “bumper.unv” file which is provided along with
the SDK. The license is validated using the vsy_LicenseValidate()
function.
Step 2: Open File with Library Manager
Next, we create a Library Manager and open the input file:
/* Open file */
vdm_LMan* libraryManager = vdm_LManBegin();
vdm_LManOpenFile(libraryManager, inputFile, nullptr);
The LMan (Library Manager) provides a high-level interface for file operations, automatically detecting the file format and initializing the appropriate library based on the file format. A nullptr is provided instead of options since we are not interested in any particular options or conventions while reading this file.
Step 3: Error Checking
After opening the file, we check for any errors and handle them appropriately:
/* check for error */
Vint ierr = vdm_LManError(libraryManager);
if (ierr) {
fprintf(stderr, "Error: opening file %s\n", inputFile);
vdm_LManCloseFile(libraryManager);
vdm_LManEnd(libraryManager);
exit(1);
}
Proper error checking is essential when working with file operations. The Library Manager provides a simple error
checking mechanism through vdm_LManError()
.
Step 4: Print Table of Contents
Now we use the Library Manager’s built-in table of contents function:
vdm_LManSetParami(libraryManager, LMAN_VERBOSE, SYS_ON);
vdm_LManTOC(libraryManager, "*");
The vdm_LManSetParami()
function is used to configure the Library Manager’s behavior. In this case, we set the
LMAN_VERBOSE
parameter to SYS_ON
, which enables detailed output formatting for the table of contents. This
provides the dataset attribute information.
Then, vdm_LManTOC()
function provides a complete table of contents for the file. The “*” parameter is a wildcard
that indicates to get information about all datasets. You can also use specific patterns like “D.N*” to get only
displacement datasets for each iteration/timestep.
Step 5: Cleanup
Finally, we close the file and free all allocated resources:
vdm_LManCloseFile(libraryManager);
vdm_LManEnd(libraryManager);
return 0;
The Library Manager interface requires only two cleanup calls: vdm_LManCloseFile()
to close the file and
vdm_LManEnd()
to free the Library Manager object.
Expected Output
When you run this example with a typical Universal file, you’ll see a comprehensive table of contents output similar to:
Library Table of Contents
Path: bumper.unv
Type: SDRC/IDEAS Universal File
IDst LRec NRow NCol Type NAtt Dataset
0 200 1 200 1 2 PARAMETER.INT.T
Model = Parameters
Description = Model Integer Parameters
1 200 1 200 3 2 PARAMETER.HOL.T
Model = Parameters
Description = Model Hollerith Parameters
2 1956 3 652 2 4 X.N
Model = Mesh
Description = Node Coordinates
Contents = Position
DataType = Vector
3 652 1 652 1 2 NID.N
Model = Mesh
Description = Node User Identifier
Complete Source Code
The complete source code for this example can be found at: src/sam/vdm/exam/exam1.cpp
You can also view the full source here:
1#include "sam/base/base.h"
2#include "sam/vdm/vdm.h"
3#include "sam/base/license.h"
4#include "sam/hoops_license.h"
5
6/*----------------------------------------------------------------------
7 Print Table of Contents
8----------------------------------------------------------------------*/
9int
10main(int argc, char** argv)
11{
12 char inputFile[256];
13 /* check input arguments */
14 if (argc < 2) {
15 fprintf(stderr, "Usage: %s inputfile [appendfile]\n", argv[0]);
16 fprintf(stderr, " inputfile is blank, 'bumper.unv' is assumed\n");
17 strcpy(inputFile, "bumper.unv");
18 }
19 else {
20 strcpy(inputFile, argv[1]);
21 }
22
23 vsy_LicenseValidate(HOOPS_LICENSE);
24
25 /* Open file */
26 vdm_LMan* libraryManager = vdm_LManBegin();
27 vdm_LManOpenFile(libraryManager, inputFile, nullptr);
28
29 /* check for error */
30 Vint ierr = vdm_LManError(libraryManager);
31 if (ierr) {
32 fprintf(stderr, "Error: opening file %s\n", inputFile);
33 vdm_LManCloseFile(libraryManager);
34 vdm_LManEnd(libraryManager);
35 exit(1);
36 }
37 /* Set verbose mode */
38 vdm_LManSetParami(libraryManager, LMAN_VERBOSE, SYS_ON);
39 vdm_LManTOC(libraryManager, "*");
40
41 vdm_LManCloseFile(libraryManager);
42 vdm_LManEnd(libraryManager);
43 return 0;
44}