rllib  1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
Functions
rlcutil.h File Reference
#include <stdarg.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "rldefine.h"
Include dependency graph for rlcutil.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int rlSetDebugPrintf (int state)
 
int rlDebugPrintf (const char *format,...)
 
int rlLastLinePrintf (const char *format,...)
 
int rlexec (const char *command)
 
const char * rlpass (const char *p)
 
char * rlstrncpy (char *dest, const char *source, int n)
 
int rlvsnprintf (char *text, int len, const char *format, va_list ap)
 
int rlsnprintf (char *text, int len, const char *format,...)
 
void rlSetSigtermHandler (void(*handler)(void *arg), void *arg)
 
const char * rlFindFile (const char *pattern, int *context)
 
const char * rlGetInifile (const char *name)
 
int rlSwapShort (int val)
 
int rlEib1 (int command)
 
int rlEib2 (int command)
 
int rlLon1 (int command)
 
int rlLon2 (int command)
 
int rlProfibus1 (int command)
 
int rlProfibus2 (int command)
 
int rlCan1 (int command)
 
int rlCan2 (int command)
 
int rlBrowser (const char *htmlfile)
 
int rlsystem (const char *command)
 
int rlSubmitPvserver (const char *env, const char *path, const char *pvs, const char *options=NULL)
 
int rlOption (const char *string, const char *option)
 
int rlIntOption (const char *string, const char *option, int _default)
 
float rlFloatOption (const char *string, const char *option, float _default)
 
const char * rlTextOption (const char *string, const char *option, const char *_default)
 
int rlCopyTextfile (const char *source, const char *destination)
 
int rlupper (char *str)
 
int rllower (char *str)
 
int rlStartsWith (const char *str, const char *startstr)
 
int rlStrMatch (const char *str, const char *wild)
 
int rlStat (const char *filepath, struct stat *buf)
 
int rlMkdir (const char *dir, int mode=0744)
 
int rlBitSet (int bitnumber, int *value)
 
int rlBitClear (int bitnumber, int *value)
 
int rlBitChange (int bitnumber, int *value)
 
int rlBitTest (int bitnumber, int *value)
 

Function Documentation

int rlBitChange ( int  bitnumber,
int *  value 
)
<pre>

XOR bit bitnumber in value Return value

Definition at line 788 of file rlcutil.cpp.

{
int bit = 1 << bitnumber;
*value = *value ^ bit;
return *value;
}
int rlBitClear ( int  bitnumber,
int *  value 
)
<pre>

Clear bit bitnumber in value Return value

Definition at line 780 of file rlcutil.cpp.

{
int bit = 1 << bitnumber;
bit = ~bit;
*value = *value & bit;
return *value;
}
int rlBitSet ( int  bitnumber,
int *  value 
)
<pre>

Set bit bitnumber in value Return value

Definition at line 773 of file rlcutil.cpp.

{
int bit = 1 << bitnumber;
*value = *value | bit;
return *value;
}
int rlBitTest ( int  bitnumber,
int *  value 
)
<pre>

Test bit bitnumber in value Return 0 | 1

Definition at line 795 of file rlcutil.cpp.

{
int bit = 1 << bitnumber;
if(*value & bit) return 1;
return 0;
}
int rlBrowser ( const char *  htmlfile)
<pre>

Call internet browser

Definition at line 577 of file rlcutil.cpp.

{
char buf[4096];
int ret = 0;
#ifdef RLUNIX
sprintf(buf,"konqueror %s &", htmlfile);
ret = system(buf);
#endif
#ifdef RLWIN32
if(get_iexplore(buf) < 0) return -1;
strcat(buf," ");
strcat(buf,htmlfile);
ret = mysystem(buf);
#endif
return ret;
}//------------------------------------------------------------------------
int rlCan1 ( int  command)

Definition at line 395 of file rlcutil.cpp.

{
char buf[80];
sprintf(buf,"buscommand -can1 %d",command);
return system(buf);
}
int rlCan2 ( int  command)

Definition at line 403 of file rlcutil.cpp.

{
char buf[80];
sprintf(buf,"buscommand -can2 %d",command);
return system(buf);
}
int rlCopyTextfile ( const char *  source,
const char *  destination 
)
<pre>

Copy a Textfile (no binary file)

Definition at line 660 of file rlcutil.cpp.

{
FILE *fin, *fout;
if(source == NULL || destination == NULL) return -1;
fin = fopen(source,"r");
if(fin == NULL)
{
printf("rlCopyTextfile: could not read %s\n",source);
return -1;
}
fout = fopen(destination,"w");
if(fout == NULL)
{
fclose(fin);
printf("rlCopyTextfile: could not write %s\n",destination);
return -1;
}
char *line = new char[256*256];
while(fgets(line,sizeof(line)-1,fin) != NULL)
{
fprintf(fout,"%s",line);
}
delete [] line;
fclose(fin);
fclose(fout);
return 0;
}
int rlDebugPrintf ( const char *  format,
  ... 
)

Definition at line 60 of file rlcutil.cpp.

{
int ret;
char message[rl_PRINTF_LENGTH]; // should be big enough
if(rlDebugPrintfState == 0) return 0;
va_list ap;
va_start(ap,format);
ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
va_end(ap);
if(ret < 0) return ret;
return printf("%s",message);
}
int rlEib1 ( int  command)
<pre>

Send command to a bus system

Definition at line 347 of file rlcutil.cpp.

{
char buf[80];
sprintf(buf,"buscommand -eib1 %d",command);
return system(buf);
}
int rlEib2 ( int  command)

Definition at line 355 of file rlcutil.cpp.

{
char buf[80];
sprintf(buf,"buscommand -eib2 %d",command);
return system(buf);
}
int rlexec ( const char *  command)
  call execvp(arg[0],arg) on unix

Definition at line 93 of file rlcutil.cpp.

{
char *buf;
char *arg[512];
int i,iarg,ret;
buf = new char [strlen(command)+1];
strcpy(buf,command);
iarg = 0;
i = 0;
while(buf[i] != '\0')
{
if(buf[i] == '\"')
{
i++;
arg[iarg++] = &buf[i];
while(buf[i] != '\"' && buf[i] != '\0') i++;
if(buf[i] == '\0') break;
buf[i] = '\0';
}
else if(buf[i] != ' ' || i == 0)
{
arg[iarg++] = &buf[i];
while(buf[i] != ' ' && buf[i] != '\0') i++;
if(buf[i] == '\0') break;
buf[i] = '\0';
}
i++;
}
arg[iarg] = NULL;
ret = execvp(arg[0],arg);
delete [] buf;
return ret;
}
const char* rlFindFile ( const char *  pattern,
int *  context 
)
  context = 0 must be 0 on first call

Definition at line 241 of file rlcutil.cpp.

{
static char freturn[512];
#ifdef RLUNIX
static DIR *dirp;
static struct dirent *dp;
if(*context == 0) dirp = opendir(".");
*context = 1;
while((dp = readdir(dirp)) != NULL)
{
if(dp->d_name[0] == '.') ;
else if(strstr(dp->d_name,pattern) != NULL)
{
strcpy(freturn,dp->d_name);
return freturn;
}
else if(rlStrMatch(dp->d_name, pattern))
{
strcpy(freturn,dp->d_name);
return freturn;
}
}
closedir(dirp);
return NULL;
#endif
#ifdef __VMS
int i,ret;
static char file[512] = "";
static char wildcard[80];
struct dsc$descriptor_s dwildcard;
struct dsc$descriptor_s dfreturn;
strcpy(wildcard,pattern);
dwildcard.dsc$w_length = strlen(wildcard);
dwildcard.dsc$a_pointer = wildcard;
dwildcard.dsc$b_dtype = DSC$K_DTYPE_T;
dwildcard.dsc$b_class = DSC$K_CLASS_S;
dfreturn.dsc$w_length = sizeof(freturn);
dfreturn.dsc$a_pointer = &freturn[0];
dfreturn.dsc$b_dtype = DSC$K_DTYPE_T;
dfreturn.dsc$b_class = DSC$K_CLASS_S;
ret = LIB$FIND_FILE(&dwildcard,&dfreturn,context,0,0,0,0);
if (ret == RMS$_NMF) return NULL; // no more files found
else if(ret != RMS$_NORMAL) return NULL;
else if(strcmp(freturn,file) == 0) { file[0] = '\0'; return NULL; }
i=0;
while(freturn[i] > ' ')i++;
freturn[i] = '\0';
return freturn;
#endif
#ifdef RLWIN32
static WIN32_FIND_DATAA wfd;
static HANDLE hFindFile;
if(*context == 0) // find first
{
*context = 1;
hFindFile = FindFirstFileA(pattern,&wfd);
if(hFindFile == INVALID_HANDLE_VALUE) return NULL;
else strcpy(freturn,(const char *) &wfd.cFileName);
}
else // subsequent find
{
if(FindNextFileA(hFindFile,&wfd) == TRUE) strcpy(freturn,(const char *) &wfd.cFileName);
else { FindClose(hFindFile); return NULL; }
}
return freturn;
#endif
}
float rlFloatOption ( const char *  string,
const char *  option,
float  _default 
)
<pre>

Get option from string

Definition at line 619 of file rlcutil.cpp.

{
const char *cptr;
float ret;
cptr = strstr(string,option);
if(cptr == NULL) return def;
cptr = strstr(cptr,"=");
if(cptr == NULL) return def;
sscanf(cptr,"=%f",&ret);
return ret;
}
const char* rlGetInifile ( const char *  name)
<pre>

returns: ~/.name on Linux/Unix sys$login:name on OpenVMS USERPROFILE%\name on Windows

Definition at line 320 of file rlcutil.cpp.

{
static char buf[1024];
#ifdef RLUNIX
strcpy(buf,getenv("HOME"));
strcat(buf,"/.");
#endif
#ifdef __VMS
strcpy(buf,"sys$login:");
#endif
#ifdef RLWIN32
ExpandEnvironmentStringsA("%USERPROFILE%",buf,sizeof(buf)-1);
if(strcmp(buf,"%USERPROFILE%") == 0) strcpy(buf,"C:");
strcat(buf,"\\");;
#endif
strcat(buf,name);
return buf;
}
int rlIntOption ( const char *  string,
const char *  option,
int  _default 
)
<pre>

Get option from string

Definition at line 604 of file rlcutil.cpp.

{
const char *cptr;
int ret;
cptr = strstr(string,option);
if(cptr == NULL) return def;
cptr = strstr(cptr,"=");
if(cptr == NULL) return def;
sscanf(cptr,"=%d",&ret);
return ret;
}
int rlLastLinePrintf ( const char *  format,
  ... 
)
  like printf in the last line of a terminal

Definition at line 74 of file rlcutil.cpp.

{
int ret,i;
char message[rl_PRINTF_LENGTH]; // should be big enough
va_list ap;
va_start(ap,format);
ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
va_end(ap);
if(ret < 0) return ret;
message[79] = '\0';
printf("%c[80D",27); // move 80 columns left
printf("%s",message);
for(i=strlen(message); i<80; i++) printf(" ");
return 80;
}
int rlLon1 ( int  command)

Definition at line 363 of file rlcutil.cpp.

{
char buf[80];
sprintf(buf,"buscommand -lon1 %d",command);
return system(buf);
}
int rlLon2 ( int  command)

Definition at line 371 of file rlcutil.cpp.

{
char buf[80];
sprintf(buf,"buscommand -lon2 %d",command);
return system(buf);
}
int rllower ( char *  str)
<pre>

convert str to lower case

Definition at line 702 of file rlcutil.cpp.

{
if(str == NULL) return -1;
while(*str != '\0')
{
*str = tolower(*str);
str++;
}
return 0;
}
int rlMkdir ( const char *  dir,
int  mode = 0744 
)
<pre>

same as mkdir

Definition at line 762 of file rlcutil.cpp.

{
#ifdef RLWIN32
int ret = CreateDirectoryA(dir,NULL);
if(ret) return 0;
return -1;
#else
return mkdir(dir, mode);
#endif
}
int rlOption ( const char *  string,
const char *  option 
)
<pre>

Get option from string return = 0 # not found return = 1 # found

Definition at line 595 of file rlcutil.cpp.

{
const char *cptr;
cptr = strstr(string,option);
if(cptr == NULL) return 0;
return 1;
}
const char* rlpass ( const char *  p)
  encode plain text password p

Definition at line 131 of file rlcutil.cpp.

{
static char ret[4*16+8];
char buf[80];
int i,val;
ret[0] = '\0';
for(i=0; p[i] != '\0' && i<=16; i++)
{
val = p[i] * 16;
sprintf(buf,"%04X",val);
strcat(ret,buf);
}
return ret;
}
int rlProfibus1 ( int  command)

Definition at line 379 of file rlcutil.cpp.

{
char buf[80];
sprintf(buf,"buscommand -profibus1 %d",command);
return system(buf);
}
int rlProfibus2 ( int  command)

Definition at line 387 of file rlcutil.cpp.

{
char buf[80];
sprintf(buf,"buscommand -profibus2 %d",command);
return system(buf);
}
int rlSetDebugPrintf ( int  state)
Some C functions.
  like printf for debugging 

Definition at line 53 of file rlcutil.cpp.

{
if(state == 0) rlDebugPrintfState = 0;
return 0;
}
void rlSetSigtermHandler ( void(*)(void *arg)  handler,
void *  arg 
)
  set signal handler for signal SIGTERM

Definition at line 233 of file rlcutil.cpp.

{
rlsigtermarg = arg;
signal(SIGTERM,rlSigtermHandler);
}
int rlsnprintf ( char *  text,
int  len,
const char *  format,
  ... 
)
  like snprintf but portable

Definition at line 197 of file rlcutil.cpp.

{
int ret;
va_list ap;
va_start(ap,format);
#ifdef RLWIN32
ret = _vsnprintf(text, len, format, ap);
#endif
#ifdef __VMS
static char vms_is_deprecated[rl_PRINTF_LENGTH];
ret = vsprintf(vms_is_deprecated, format, ap);
rlstrncpy(text,vms_is_deprecated,len);
#endif
#ifdef RLUNIX
ret = vsnprintf(text, len, format, ap);
#endif
va_end(ap);
return ret;
}
int rlStartsWith ( const char *  str,
const char *  startstr 
)
<pre>

test if str starts with startstr

Definition at line 713 of file rlcutil.cpp.

{
int ret;
if(str == NULL || startstr == NULL) return 0;
ret = strncmp(str,startstr,strlen(startstr));
if(ret == 0) return 1;
return 0;
}
int rlStat ( const char *  filepath,
struct stat *  buf 
)
<pre>

same as stat

Definition at line 757 of file rlcutil.cpp.

{
return stat(filepath, buf);
}
int rlStrMatch ( const char *  str,
const char *  wild 
)
<pre>

test if str matches with wild

Definition at line 722 of file rlcutil.cpp.

{
if(strstr(str,wild) != NULL) return 1;
int i,w;
w = 0;
for(i=0; str[i] != '\0'; i++)
{
if(wild[w] == '*')
{
while(wild[w] == '*') w++;
if(wild[w] == '\0') return 1;
try_next:
if(wild[w] == '\0') return 0;
while(str[i] != wild[w] && str[i] != '\0') i++;
if(str[i] == '\0') return 0;
while(str[i] != '\0')
{
if(str[i] != wild[w]) goto try_next;
i++;
w++;
if(str[i] == '\0' && wild[w] == '\0') return 1;
}
if(wild[w] != '\0') return 0;
return 1;
}
else
{
if(str[i] != wild[w]) return 0;
}
w++;
}
return 1;
}
char* rlstrncpy ( char *  dest,
const char *  source,
int  n 
)
  strncpy + terminate with '\0'

Definition at line 149 of file rlcutil.cpp.

{
int i;
for(i=0; i<n; i++)
{
if(source[i] == '\0') break;
dest[i] = source[i];
}
dest[i] = '\0';
return dest;
}
int rlSubmitPvserver ( const char *  env,
const char *  path,
const char *  pvs,
const char *  options = NULL 
)
<pre>

Submit a pvserver Example: rlSubmitPvserver("HOME","/temp/murx","pvs","-exit_on_bind_error -exit_after_last_client_terminates");

Definition at line 531 of file rlcutil.cpp.

{
if(env == NULL || path == NULL || pvs == NULL) return -1;
rlString command,cd;
const char *cptr;
#ifdef __VMS
cptr = env;
cd = cptr;
cd += path;
command = "spawn/nowait ";
command += cd;
command += pvs;
command += " ";
if(options != NULL) command += options;
command += " -cd=";
command += cd;
#else
cptr = getenv(env);
if(cptr == NULL) { printf("rlSubmitPvserver:ERROR env=%s is not set\n", env); return -2; }
cd += cptr;
cd += path;
command = "\"";
command += cd;
#ifdef RLWIN32
command += "\\";
#else
command += "/";
#endif
command += pvs;
command += "\" ";
if(options != NULL) command += options;
command += " \"-cd=";
command += cd;
command += "\"";
#endif
#ifdef RLUNIX
command += " &";
#endif
//printf("command=%s\n", command.text());
return rlsystem(command.text());
//example: rlSubmitPvserver("HOME","/temp/murx","pvs","-exit_on_bind_error -exit_after_last_client_terminates");
}
int rlSwapShort ( int  val)
<pre>

swaps bytes

Definition at line 341 of file rlcutil.cpp.

{
return (val & 0x0ff)*256 + val/256;
}
int rlsystem ( const char *  command)
<pre>

Call system(command)

Definition at line 522 of file rlcutil.cpp.

{
#ifdef RLWIN32
return mysystem(command);
#else
return system(command);
#endif
}
const char* rlTextOption ( const char *  string,
const char *  option,
const char *  _default 
)
<pre>

Get option from string

Definition at line 634 of file rlcutil.cpp.

{
int i = 0;
const char *cptr;
char quote;
static char ret[rl_PRINTF_LENGTH];
cptr = strstr(string,option);
if(cptr == NULL) return def;
cptr = strstr(cptr,"=");
if(cptr == NULL) return def;
cptr++;
quote = *cptr;
cptr++;
while(i < ((int) sizeof(ret)-1))
{
if(cptr[i] == quote || cptr[i] == '\0') break;
ret[i] = cptr[i];
i++;
}
ret[i] = '\0';
return ret;
}
int rlupper ( char *  str)
<pre>

convert str to upper case

Definition at line 691 of file rlcutil.cpp.

{
if(str == NULL) return -1;
while(*str != '\0')
{
*str = toupper(*str);
str++;
}
return 0;
}
int rlvsnprintf ( char *  text,
int  len,
const char *  format,
va_list  ap 
)
  like vsnprintf but portable

Definition at line 163 of file rlcutil.cpp.

{
int ret;
#ifdef RLWIN32
if(format == NULL || format[0] == '\0')
{
text[0] = '\0';
return 1;
}
ret = _vsnprintf(text, len, format, ap);
#endif
#ifdef __VMS
static char vms_is_deprecated[rl_PRINTF_LENGTH];
if(format == NULL || format[0] == '\0')
{
text[0] = '\0';
return 1;
}
ret = vsprintf(vms_is_deprecated, format, ap);
rlstrncpy(text,vms_is_deprecated,len);
#endif
#ifdef RLUNIX
if(format == NULL || format[0] == '\0')
{
text[0] = '\0';
return 1;
}
ret = vsnprintf(text, len, format, ap);
#endif
return ret;
}