Text and Text Tables
HEC-DSS can store text information in two forms: a text string and / or a text table (a table of strings). Text is often used for saving messages, comments or other information. For example, the entire user manual for DSSUTL was stored as text records in a DSS file, and the appropriate text displayed when a user request help on an item.
“text” is a regular null terminated char array, and a text table is a char array with multiple null terminated strings. There is no limit on the number of characters in a text or text table record (although one needs to be careful with memory) nor a limit on the number of characters per table cell. Table cells do not have to have the same number of characters. Along with the table, you may include column "labels".
Text tables are dimensioned to [number rows][number columns]. HEC-DSS takes a text table and stores it as individual null terminated strings. For example, a table of color characteristics might be:
Color | Wave Length | Temperature | Energy |
---|---|---|---|
Red | Long | Hot | Low |
Blue | Short | Cool | High |
Yellow | Med-long | Warm | Medium |
In the above example, the text table is stored as:
“Red\0long\0hot\0low\0Blue\0short\0cool\0high\0Yellow\0med-long\0warm\0medium”
and the labels are "Color\0wave length\0temperature\0energy\0".
A text table and text string are separate parts of a record and can be stored together (for example, you can have a description of the table with the table.)
A subset to the text table is a “text list”. A text list is simply a text table with only one column and shows up as a list of Strings.
zStructText is the struct used for storing and retrieving text in HEC-DSS. To store text in a HEC-DSS file using zStructText, use function ztextStore; or to store all text in a regular text file, use function ztextStoreFromFile. To retrieve text from HEC-DSS and pass in a zStructText, use function ztextRetrieve. To retrieve and store in a regular text file, use function ztextRetrieveToFile. You can also print text to standard out with function ztextStructPrint.
The primary text functions follow.
zstructTextNew
Creates a new text struct for storing and retrieving text data
zStructText* zstructTextNew(const char* pathname)
zstructTextStringNew
Creates a new text struct with a character string for storing text data. This struct is used for storing a single null terminated string.
zStructText* zstructTextStringNew(const char* pathname, char *text)
ztextStore
Store text from a zStructText struct. This may be a single string, a text list or a text table. See the definition of zStructText.
int ztextStore(long long *ifltab, zStructText *textStruct)
ztextStoreFromFile
Read the contents of a file and store as text in DSS with name "pathname". Text is stored as a text list, where each line is a row.
int ztextStoreFromFile(long long *ifltab, const char *pathname, const char *filename)
ztextRetrieve
Retrieve a text record into a zStructText struct.
int ztextRetrieve(long long *ifltab, zStructText *textStruct)
ztextRetrieveToFile
Retrieves a text record and then writes the contents to a regular text file.
int ztextRetrieveToFile(long long *ifltab, const char *pathname, const char *filename)
ztextStructPrint
Print the contents of a text struct to standard out (using printf).
void ztextStructPrint(zStructText *textStruct)
zStructText
typedef struct {
/* Required */
char *pathname;
// For a simple text string
char *textString;
int numberTextChars;
char *textTable;
// The total number of characters, including nulls
int numberTableChars;
// If a table, then the number of rows and columns
// If a list, then numberColumns = 1, numberRows = number items
// If a test string, then both are 1 or 0.
int numberRows;
int numberColumns;
/* Optional */
// If a text table, then numberColumns labels, each label null terminated
char *labels;
// The total number of characters in *labels
int numberLabelChars;
/* User header, used for further description of data/gage */
/* Not often used */
int *userHeader;
int userHeaderNumber;
// Informational only (on return only)
int dataType;
long long lastWrittenTime; // Seconds since 1970
long long fileLastWrittenTime;
char programName[17];
} zStructText;
Example: Text
#include <stdio.h>
#include <string.h>
#include "heclib.h"
// Storage and retrieval of a single line of text
int ExampleText1()
{
long long ifltab[250];
zStructText *textStruct1;
zStructText *textStruct2;
zStructText *textStruct3;
zStructText *textStruct4;
int status;
// Open the DSS file; Create if it doesn't exist
status = zopen(ifltab, "C:/temp/Example7.dss");
// If an error occured, messages will be printed to standard out
if (status != STATUS_OKAY) return status;
// Write a single string to DSS
textStruct1 = zstructTextStringNew("/Group/Location/Message 1/d/e/f/",
"This is text message 1 that is written to HEC-DSS");
status = ztextStore(ifltab, textStruct1);
zstructFree(textStruct1);
if (status != STATUS_OKAY) return status;
// Write a string to DSS, setting the string in the struct
textStruct2 = zstructTextNew("/Group/Location/Message 2/d/e/f/");
textStruct2->textString = mallocAndCopy("This is text message 2 that is written to HEC-DSS");
textStruct2->numberTextChars = (int)strlen(textStruct2→textString);
status = ztextStore(ifltab, textStruct2);
free(textStruct2→textString);
zstructFree(textStruct2);
if (status != STATUS_OKAY) return status;
// Read the data back in and print
textStruct3 = zstructTextNew("/Group/Location/Message 1/d/e/f/");
status = ztextRetrieve(ifltab, textStruct3);
if (status != STATUS_OKAY) return status;
// Print out to standard out
printf("Text for pathname %s\n", textStruct3→pathname);
ztextStructPrint(textStruct3);
zstructFree(textStruct3);
textStruct4 = zstructTextNew("/Group/Location/Message 2/d/e/f/");
status = ztextRetrieve(ifltab, textStruct4);
if (status != STATUS_OKAY) return status;
printf("\nText for pathname %s\n", textStruct4→pathname);
ztextStructPrint(textStruct4);
zstructFree(textStruct4);
zclose(ifltab);
return 0;
}
Text for pathname /Group/Location/Message 1/d/e/f/
This is text message 1 that is written to HEC-DSS
Text for pathname /Group/Location/Message 2/d/e/f/
This is text message 2 that is written to HEC-DSS
Example: Text Tables
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "heclib.h"
// Storage and retrieval of a text table. This table has 20 rows with 5 columns
int ExampleText2()
{
long long ifltab[250];
zStructText *textStruct1;
zStructText *textStruct2;
char string[] = { "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTSUVWXYZ" };
char textTable[5000];
char labels[200];
int status, row, col, k, n, ipos;
// Open the DSS file; Create if it doesn't exist
status = zopen(ifltab, "C:/temp/Example7.dss");
if (status != STATUS_OKAY) return status;
// Gen up data
textStruct1 = zstructTextNew("/Group/Location/Text Table/d/e/f/");
ipos = 0;
for (row = 0; row < 20; row++) {
for (col = 0; col < 5; col++) {
n = row / 25;
n = row - (n * 25) + 1;
for (k = (col + 2); k < (n + 8); k++) {
textTable[ipos++] = string[k];
}
textTable[ipos++] = '\0';
}
}
textStruct1->textTable = textTable;
textStruct1->numberTableChars = ipos;
textStruct1->numberColumns = 5;
textStruct1->numberRows = 20;
// Gen column headers
ipos = 0;
for (int col = 0; col < 5; col++) {
stringCopy(&labels[ipos], sizeof(labels), "Col ", _TRUNCATE);
ipos += 4;
itoa(col + 1, &labels[ipos], 10);
ipos += 2;
}
textStruct1->labels = labels;
textStruct1->numberLabelChars = ipos;
ztextStructPrint(textStruct1);
// Store text struct
status = ztextStore(ifltab, textStruct1);
zstructFree(textStruct1);
if (status != STATUS_OKAY) return status;
// Read the data back in and print
textStruct2 = zstructTextNew("/Group/Location/Text Table/d/e/f/");
status = ztextRetrieve(ifltab, textStruct2);
if (status != STATUS_OKAY) return status;
// Print out to standard out
ztextStructPrint(textStruct2);
zstructFree(textStruct2);
zclose(ifltab);
return 0;
}
Col 1 | Col 2 | Col 3 | Col 4 | Col 5 |
cdefghi | defghi | efghi | fghi | ghi |
cdefghij | defghij | efghij | fghij | ghij |
cdefghijk | defghijk | efghijk | fghijk | ghijk |
cdefghijkl | defghijkl | efghijkl | fghijkl | ghijkl |
cdefghijklm | defghijklm | efghijklm | fghijklm | ghijklm |
cdefghijklmn | defghijklmn | efghijklmn | fghijklmn | ghijklmn |
cdefghijklmno | defghijklmno | efghijklmno | fghijklmno | ghijklmno |
cdefghijklmnop | defghijklmnop | efghijklmnop | fghijklmnop | ghijklmnop |
cdefghijklmnopq | defghijklmnopq | efghijklmnopq | fghijklmnopq | ghijklmnopq |
cdefghijklmnopqr | defghijklmnopqr | efghijklmnopqr | fghijklmnopqr | ghijklmnopqr |
cdefghijklmnopqrs | defghijklmnopqrs | efghijklmnopqrs | fghijklmnopqrs | ghijklmnopqrs |
cdefghijklmnopqrst | defghijklmnopqrst | efghijklmnopqrst | fghijklmnopqrst | ghijklmnopqrst |
cdefghijklmnopqrstu | defghijklmnopqrstu | efghijklmnopqrstu | fghijklmnopqrstu | ghijklmnopqrstu |
cdefghijklmnopqrstuv | defghijklmnopqrstuv | efghijklmnopqrstuv | fghijklmnopqrstuv | ghijklmnopqrstuv |
cdefghijklmnopqrstuvw | defghijklmnopqrstuvw | efghijklmnopqrstuvw | fghijklmnopqrstuvw | ghijklmnopqrstuvw |
cdefghijklmnopqrstuvwx | defghijklmnopqrstuvwx | efghijklmnopqrstuvwx | fghijklmnopqrstuvwx | ghijklmnopqrstuvwx |
cdefghijklmnopqrstuvwxy | defghijklmnopqrstuvwxy | efghijklmnopqrstuvwxy | fghijklmnopqrstuvwxy | ghijklmnopqrstuvwxy |
cdefghijklmnopqrstuvwxyz | defghijklmnopqrstuvwxyz | efghijklmnopqrstuvwxyz | fghijklmnopqrstuvwxyz | ghijklmnopqrstuvwxyz |
cdefghijklmnopqrstuvwxyzA | defghijklmnopqrstuvwxyzA | efghijklmnopqrstuvwxyzA | fghijklmnopqrstuvwxyzA | ghijklmnopqrstuvwxyzA |
cdefghijklmnopqrstuvwxyzAB | defghijklmnopqrstuvwxyzAB | efghijklmnopqrstuvwxyzAB | fghijklmnopqrstuvwxyzAB | ghijklmnopqrstuvwxyzAB |
As a table from HEC-DSSVue: