#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "TString.h"
#include "TSystem.h"
#include "hdstutilities.h"
using namespace std;
ClassImp(HDstUtilities)
Bool_t HDstUtilities::readCmdLineArgsFromFile(const Char_t* filename,
Int_t* argc, Char_t*** argv)
{
FILE* file = NULL;
Int_t line_num = 0;
Char_t line[MAX_LINE_LENGTH] = { '\0' };
Char_t argument[MAX_LINE_LENGTH + 2] = { '\0' };
Char_t* line_end = NULL;
Char_t* option_begin = NULL;
Char_t* option_end = NULL;
Char_t* value_begin = NULL;
Char_t* value_end = NULL;
*argc = 1;
*argv = (Char_t**)malloc( sizeof(Char_t*) );
(*argv)[0] = strdup( filename );
if ((file = fopen( filename, "r" )) == NULL)
{
fprintf( stderr, "==> ERROR: Could not open input file %s!\n", filename);
return kFALSE;
}
while (fgets( line, MAX_LINE_LENGTH, file ) != NULL)
{
line_num++;
if ((line_end = strrchr( line, '\n' )) != NULL)
{
*line_end = '\0';
}
if (line[strspn( line, " \t" )] == '#' ||
line[strspn( line, " \t" )] == '\0' )
{
continue;
}
if ((line_end = strchr( line, '#' )) == NULL)
{
line_end = line + strlen( line );
}
*line_end = '\0';
option_begin = line + strspn( line, " \t-" );
option_end = option_begin + strcspn( option_begin, " =" );
value_begin = option_end + strspn( option_end, " =\"'" );
for (value_end = line_end - 1;
*value_end == ' ' || *value_end == '\t' ||
*value_end == '"' || *value_end == '\'';
value_end--);
value_end++;
if (option_begin >= line && option_begin < option_end &&
option_end < value_begin && value_begin < value_end &&
value_end <= line_end)
{
*option_end = '\0';
*value_end = '\0';
}
else
{
fprintf( stderr,
"==> ERROR: Wrong syntax in line %d of file %s!\n",
line_num, filename );
fclose( file );
return kFALSE;
}
*argument = '\0';
strcat( argument, "--" );
strcat( argument, option_begin );
strcat( argument, "=" );
strcat( argument, value_begin );
(*argc)++;
*argv = (Char_t**)realloc( *argv, *argc * sizeof(Char_t*) );
(*argv)[*argc - 1] = strdup( argument );
}
if (feof( file ))
{
fclose( file );
return kTRUE;
}
else
{
fclose( file );
fprintf( stderr, "==> ERROR: Could not read from file %s\n", filename );
return kFALSE;
}
}
void HDstUtilities::str2CmdLineArgs(const Char_t* input, Int_t* argc, Char_t*** argv)
{
const Char_t* source;
Char_t* dest;
Char_t* argument;
size_t length;
size_t size;
*argc = 1;
*argv = (Char_t**)malloc( sizeof(Char_t*) );
(*argv)[0] = strdup( "from-string" );
length = 0;
size = strlen( input );
argument = (Char_t*)calloc( size + 1, sizeof(char) );
source = input + strspn( input, " \t" );
dest = argument;
while (source < input + size)
{
if (*source == '\'')
{
length = strcspn( source + 1, "'" );
strncpy( dest, source + 1, length );
source += length + 2;
dest += length;
}
else if (*source == '"')
{
length = strcspn( source + 1, "\"" );
strncpy( dest, source + 1, length );
source += length + 2;
dest += length;
}
else if (*source == ' ' || *source == '\t')
{
*dest = '\0';
dest = argument;
source += strspn( source + 1, " \t" ) + 1;
(*argc)++;
*argv = (Char_t**)realloc( *argv, *argc * sizeof(Char_t*) );
(*argv)[*argc - 1] = strdup( argument );
}
else
{
*dest = *source;
source++;
dest++;
}
}
if (dest != argument)
{
*dest = '\0';
(*argc)++;
*argv = (Char_t**)realloc( *argv, *argc * sizeof(Char_t*) );
(*argv)[*argc - 1] = strdup( argument );
}
free( argument );
}
void HDstUtilities::freeCmdLineArgs(Int_t argc, Char_t** argv)
{
while (argc > 0)
{
free( argv[--argc] );
}
if (argv != NULL)
{
free( argv );
}
}
Char_t* HDstUtilities::extractArg(Int_t* argc, Char_t*** argv, const Char_t* arg)
{
Char_t* result = NULL;
Char_t** new_argv = NULL;
Int_t new_argc = 0;
size_t length = 0;
Int_t found = 0;
Int_t has_value = 0;
Int_t i = 0;
length = strlen( arg );
if (*(arg + length - 1) == ':')
{
has_value = 1;
length--;
}
for (i = 0; i < *argc; i++)
{
if (strncmp( (*argv)[i], arg, length ) == 0 && !found)
{
if (has_value && i + 1 < *argc)
{
result = strdup( (*argv)[i + 1] );
found = 1;
i++;
}
else
{
result = strdup( arg );
found = 1;
}
}
else
{
new_argc++;
new_argv = (Char_t**)realloc( new_argv, new_argc * sizeof(Char_t*) );
new_argv[new_argc - 1] = strdup( (*argv)[i] );
}
}
*argc = new_argc;
*argv = new_argv;
return result;
}
TString HDstUtilities::GetFileStem(const TString& dir, const TString& file)
{
TString stem = file;
Ssiz_t first_name_length = stem.Index( " ", 0 );
if (first_name_length != kNPOS)
stem.Resize( first_name_length );
stem = gSystem->BaseName( stem.Data() );
Ssiz_t pos = -1;
Ssiz_t ext_pos = kNPOS;
while (1)
{
if ((pos = stem.Index( ".", pos + 1 )) == kNPOS)
break;
ext_pos = pos;
}
if (ext_pos != kNPOS)
stem.Resize( ext_pos );
if (!dir.IsNull())
stem.Prepend( dir + "/" );
return stem;
}
Last change: Sat May 22 12:55:00 2010
Last generated: 2010-05-22 12:55
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.