Guardtime Parameter and Task Handling SDK libparamset
PARAM SET Overview

The libparamset is a software development kit, written in plain C, for handling command-line parameters and program tasks. Parameters can be read from command line and a task can be extracted that matches with the given input. Process is covered with error detection and functions that generates helpful feedback messages to the user.

libparamset

The libparamset provides the following functionality:

Main objects

PARAM_SET contains user defined parameters that can be parsed from command line, read from configuration file or added from the code.

TASK_SET contains multiple task definitions (parameters that are mandatory, ignored or restricted for defined task) that can be analyzed against specified PARAM_SET to extract a signle consistent task that matches the input.

TASK is object returned by successful TASK_SET analyze that contains the matching task ID and PARAM_SET object.

Memory Management

The memory management obeys the following rules:

Workflow

Code examples

Example 1

A basic example that illustrates just how to parse the command-line options without any error handling and do the task. See Example 2 for more advanced use case.

#include <stdio.h>
int main(int argc, char** argv) {
PARAM_SET *set = NULL;
char buf[0xffff];
/* Create new param_set object. */
PARAM_SET_new("{i}{o}{dump}{debug}{r}{help|h}", &set);
/* Parse command-line. */
PARAM_SET_parseCMD(set, argc, argv, NULL, 0);
/* Document options. */
PARAM_SET_setHelpText(set, "i", "File input. To specify multiple input files, use -i multiple times.");
PARAM_SET_setHelpText(set, "o", "Output file.");
PARAM_SET_setHelpText(set, "r", "Reverse file.");
PARAM_SET_setHelpText(set, "help", "Show help message (You are reading it right now!).");
PARAM_SET_setHelpText(set, "dump", "Dump file content.");
PARAM_SET_setHelpText(set, "debug", "Print param_set object.");
/*
* See PARAM_SET_isSetByName, PARAM_SET_isOneOfSetByName, PARAM_SET_getStr,
* PARAM_SET_getValueCount and PARAM_SET_getObj to work with parameters.
*/
if (PARAM_SET_isSetByName(set, "h")) {
printf( "Usage:\n"
" util -i file [-r -o file][--dump]\n"
" util --debug\n\n");
printf( "Options:\n%s", PARAM_SET_helpToString(set, "i,o,r,h,dump,debug", 2, 10, 80, buf, sizeof(buf)));
} else if (PARAM_SET_isSetByName(set, "i,dump") && !PARAM_SET_isSetByName(set, "r,o,debug")) {
printf("Dump File.\n");
} else if (PARAM_SET_isSetByName(set, "i,r,o") && !PARAM_SET_isSetByName(set, "debug")) {
printf("Reverse File.\n");
} else if (PARAM_SET_isSetByName(set, "debug")) {
printf("Debug information:\n%s\n", PARAM_SET_toString(set, buf, sizeof(buf)));
} else {
printf("Unknown task. Use -h to get some help.\n");
}
}

Example 2

A simple example of a command-line tool that uses libparamset to specify parameter set and task set, parse the command line, handle errors and give some feedback to help get things working.

#include <stdio.h>
#ifdef _WIN32
#endif
enum {
VALUE_OK = 0,
VALUE_IS_NULL = 0x01,
VALUE_IS_EMPTY = 0x02,
VALUE_FILE_DOES_NOT_EXIST = 0x03,
};
int convertRepair_path(const char* arg, char* buf, unsigned len);
int isContentOk_path(const char* fname);
int isFormatOk_path(const char* fname);
const char *parameter_error_to_string(int err);
int main(int argc, char** argv, char **envp) {
PARAM_SET *set = NULL;
TASK_SET *task_set = NULL;
TASK *pTask = NULL;
char buf[1024];
char debug[0xffff];
/*
* Configure parameter set and its parameters.
* 1) Both o and i must not be NULL or empty string.
* 2) Both o and i converts '\' into '/'.
* 3) i must be a path to a file that must exist.
* 4) Next token after o and i is always bound with the flag.
* 5) All options with true or false do not bound anything.
*/
PARAM_SET_new("{i}{o}{dump}{debug}{r}{help|h}", &set);
PARAM_SET_addControl(set, "o", isFormatOk_path, NULL, convertRepair_path, NULL);
PARAM_SET_addControl(set, "i", isFormatOk_path, isContentOk_path, convertRepair_path, NULL);
/* Document options. */
PARAM_SET_setHelpText(set, "i", "File input. To specify multiple input files, use -i multiple times or use wildcards.");
PARAM_SET_setHelpText(set, "o", "Output file.");
PARAM_SET_setHelpText(set, "r", "Reverse file.");
PARAM_SET_setHelpText(set, "help", "Show help message (You are reading it right now!).");
PARAM_SET_setHelpText(set, "dump", "Dump file content.");
PARAM_SET_setHelpText(set, "debug", "Print param_setobject.");
/*
* To enable Windows file system wildcards, specify the wildcard expander
* function implementation and enable parsing option that enable wildcard
* processor.
*/
#ifdef _WIN32
#else
#endif
/*
* Describe different tasks:
* util -h
* util -i file_in --dump
* util -i file_in -o file_out -r
* util --debug [options, except -h]
*/
TASK_SET_new(&task_set);
TASK_SET_add(task_set, 0, "Help", "h", NULL, NULL, NULL);
TASK_SET_add(task_set, 1, "Only Dump file", "i,dump", NULL, "h,r,o,debug",NULL);
TASK_SET_add(task_set, 2, "Reverse file", "i,r,o", NULL, "h,debug", NULL);
TASK_SET_add(task_set, 3, "Debug", "debug", NULL, "h", NULL);
/* Parse command-line. */
PARAM_SET_parseCMD(set, argc, argv, NULL, 0);
/* Check for typos and unknown parameters. */
printf("%s\n", PARAM_SET_typosToString(set, NULL, buf, sizeof(buf)));
goto cleanup;
} else if (PARAM_SET_isUnknown(set)){
printf("%s\n", PARAM_SET_unknownsToString(set, "Error: ", buf, sizeof(buf)));
goto cleanup;
}
/* Check for invalid values. */
if (!PARAM_SET_isFormatOK(set)) {
printf("%s\n", PARAM_SET_invalidParametersToString(set, NULL, parameter_error_to_string, buf, sizeof(buf)));
goto cleanup;
}
/* As input parameter passed all checks try to extract a consistent task. */
TASK_SET_analyzeConsistency(task_set, set, 0.2);
TASK_SET_getConsistentTask(task_set, &pTask);
/* If task is not extracted try to give user some suggestions what to do to make things work. */
if (pTask == NULL) {
int ID;
if (TASK_SET_isOneFromSetTheTarget(task_set, 0.1, &ID)) {
printf("%s", TASK_SET_howToRepair_toString(task_set, set, ID, NULL, buf, sizeof(buf)));
} else {
printf("%s", TASK_SET_suggestions_toString(task_set, 3, buf, sizeof(buf)));
}
goto cleanup;
}
/*
* Task is extracted, check which one to run.
* See PARAM_SET_isSetByName, PARAM_SET_isOneOfSetByName, PARAM_SET_getStr,
* PARAM_SET_getValueCount and PARAM_SET_getObj to work with parameters.
*/
switch(TASK_getID(pTask)) {
case 0:
printf( "Usage:\n"
" util -i file [-r -o file][--dump]\n"
" util --debug\n\n");
printf( "Options:\n%s", PARAM_SET_helpToString(set, "i,o,r,h,dump,debug", 2, 10, 80, buf, sizeof(buf)));
break;
case 1:
printf("Dump File.\n");
break;
case 2:
printf("Reverse File.\n");
break;
case 3:
printf("Debug information:\n%s\n", PARAM_SET_toString(set, debug, sizeof(debug)));
break;
default:
printf("Error.\n");
break;
}
cleanup:
TASK_SET_free(task_set);
}
const char *parameter_error_to_string(int err) {
switch(err) {
case VALUE_OK: return "OK";
case VALUE_IS_NULL: return "Parameter must have value";
case VALUE_IS_EMPTY: return "Parameter must have content";
case VALUE_FILE_DOES_NOT_EXIST: return "File does not exist";
default: return "Unknown error";
}
}
int isFormatOk_path(const char* fname) {
if (fname == NULL) return VALUE_IS_NULL;
else if (*fname == '\0');
else return VALUE_OK;
}
int isContentOk_path(const char* fname) {
int result = 1;
FILE *f = NULL;
f = fopen(fname, "r");
result = f == NULL ? VALUE_FILE_DOES_NOT_EXIST : 0;
if (f != NULL) fclose(f);
return result;
}
int convertRepair_path(const char* arg, char* buf, unsigned len){
char *toBeReplaced = NULL;
if (arg == NULL || buf == NULL) return PST_INVALID_ARGUMENT;
strncpy(buf, arg, len - 1);
toBeReplaced = buf;
while ((toBeReplaced = strchr(toBeReplaced, '\\')) != NULL){
*toBeReplaced = '/';
toBeReplaced++;
}
return PST_OK;
}

Third party components

The SDK is using the following third party components: