rllib  1
Functions
rlcutil.h File Reference
#include <stdarg.h>
#include <ctype.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)

Function Documentation

int rlBrowser ( const char *  htmlfile)
Call internet browser

Definition at line 572 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 390 of file rlcutil.cpp.

{
  char buf[80];

  sprintf(buf,"buscommand -can1 %d",command);
  return system(buf);
}
int rlCan2 ( int  command)

Definition at line 398 of file rlcutil.cpp.

{
  char buf[80];

  sprintf(buf,"buscommand -can2 %d",command);
  return system(buf);
}
int rlCopyTextfile ( const char *  source,
const char *  destination 
)
Copy a Textfile (no binary file)

Definition at line 655 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)
Send command to a bus system

Definition at line 342 of file rlcutil.cpp.

{
  char buf[80];

  sprintf(buf,"buscommand -eib1 %d",command);
  return system(buf);
}
int rlEib2 ( int  command)

Definition at line 350 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;
    }
  }
  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_DATA wfd;
  static HANDLE hFindFile;

  if(*context == 0) // find first
  {
    *context = 1;
    hFindFile = FindFirstFile(pattern,&wfd);
    if(hFindFile == INVALID_HANDLE_VALUE)    return NULL;
    else                                     strcpy(freturn,(const char *) &wfd.cFileName);
  }
  else // subsequent find
  {
    if(FindNextFile(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 
)
Get option from string

Definition at line 614 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)
returns:
~/.name               on Linux/Unix
sys$login:name        on OpenVMS
USERPROFILE%\name    on Windows

Definition at line 315 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
  ExpandEnvironmentStrings("%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 
)
Get option from string

Definition at line 599 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 358 of file rlcutil.cpp.

{
  char buf[80];

  sprintf(buf,"buscommand -lon1 %d",command);
  return system(buf);
}
int rlLon2 ( int  command)

Definition at line 366 of file rlcutil.cpp.

{
  char buf[80];

  sprintf(buf,"buscommand -lon2 %d",command);
  return system(buf);
}
int rllower ( char *  str)
convert str to lower case

Definition at line 697 of file rlcutil.cpp.

{
  if(str == NULL) return -1;
  while(*str != '\0')
  {
    *str = tolower(*str);
    str++;
  }
  return 0;
}
int rlOption ( const char *  string,
const char *  option 
)
Get option from string
return = 0 # not found
return = 1 # found

Definition at line 590 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 374 of file rlcutil.cpp.

{
  char buf[80];

  sprintf(buf,"buscommand -profibus1 %d",command);
  return system(buf);
}
int rlProfibus2 ( int  command)

Definition at line 382 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;
  else           rlDebugPrintfState = 1;
  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;
  rlUserSigtermHandler = handler;
  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 
)
test if str starts with startstr

Definition at line 708 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;
}
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 
)
Submit a pvserver
Example:
rlSubmitPvserver("HOME","/temp/murx","pvs","-exit_on_bind_error -exit_after_last_client_terminates");

Definition at line 526 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)
swaps bytes

Definition at line 336 of file rlcutil.cpp.

{
  return (val & 0x0ff)*256 + val/256;
}
int rlsystem ( const char *  command)
Call system(command)

Definition at line 517 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 
)
Get option from string

Definition at line 629 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)
convert str to upper case

Definition at line 686 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;
}