rllib  1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
rlcutil.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlcutil.cpp - description
3  -------------------
4  begin : Wed Dec 11 2002
5  copyright : (C) 2002 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include "rlcutil.h"
17 #include "rlstring.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdarg.h>
22 #include <signal.h>
23 
24 #ifdef RLUNIX
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <dirent.h>
30 #endif
31 
32 #ifdef __VMS
33 #include <descrip.h>
34 #include <rmsdef.h>
35 #include <ssdef.h>
36 #include <iodef.h>
37 #include <unixio.h>
38 #include <file.h>
39 #include <lib$routines.h>
40 #endif
41 
42 #ifdef RLWIN32
43 #include <windows.h>
44 #include <winreg.h>
45 #include <fcntl.h>
46 #include <io.h>
47 #include <sys\types.h>
48 #include <sys\stat.h>
49 #endif
50 
52 
53 int rlSetDebugPrintf(int state)
54 {
55  if(state == 0) rlDebugPrintfState = 0;
56  else rlDebugPrintfState = 1;
57  return 0;
58 }
59 
60 int rlDebugPrintf(const char *format, ...)
61 {
62  int ret;
63  char message[rl_PRINTF_LENGTH]; // should be big enough
64 
65  if(rlDebugPrintfState == 0) return 0;
66  va_list ap;
67  va_start(ap,format);
68  ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
69  va_end(ap);
70  if(ret < 0) return ret;
71  return printf("%s",message);
72 }
73 
74 int rlLastLinePrintf(const char *format, ...)
75 {
76  int ret,i;
77  char message[rl_PRINTF_LENGTH]; // should be big enough
78 
79  va_list ap;
80  va_start(ap,format);
81  ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
82  va_end(ap);
83  if(ret < 0) return ret;
84  message[79] = '\0';
85  printf("%c[80D",27); // move 80 columns left
86  printf("%s",message);
87  for(i=strlen(message); i<80; i++) printf(" ");
88  return 80;
89 }
90 
91 #ifdef RLUNIX
92 //------------------------------------------------------------------------
93 int rlexec(const char *command)
94 {
95  char *buf;
96  char *arg[512];
97  int i,iarg,ret;
98 
99  buf = new char [strlen(command)+1];
100  strcpy(buf,command);
101  iarg = 0;
102  i = 0;
103  while(buf[i] != '\0')
104  {
105  if(buf[i] == '\"')
106  {
107  i++;
108  arg[iarg++] = &buf[i];
109  while(buf[i] != '\"' && buf[i] != '\0') i++;
110  if(buf[i] == '\0') break;
111  buf[i] = '\0';
112  }
113  else if(buf[i] != ' ' || i == 0)
114  {
115  arg[iarg++] = &buf[i];
116  while(buf[i] != ' ' && buf[i] != '\0') i++;
117  if(buf[i] == '\0') break;
118  buf[i] = '\0';
119  }
120  i++;
121  }
122  arg[iarg] = NULL;
123 
124  ret = execvp(arg[0],arg);
125  delete [] buf;
126  return ret;
127 }
128 #endif
129 
130 //------------------------------------------------------------------------
131 const char *rlpass(const char *p)
132 {
133  static char ret[4*16+8];
134  char buf[80];
135  int i,val;
136 
137  ret[0] = '\0';
138  for(i=0; p[i] != '\0' && i<=16; i++)
139  {
140  val = p[i] * 16;
141  sprintf(buf,"%04X",val);
142  strcat(ret,buf);
143  }
144 
145  return ret;
146 }
147 
148 //------------------------------------------------------------------------
149 char *rlstrncpy(char *dest, const char *source, int n)
150 {
151  int i;
152 
153  for(i=0; i<n; i++)
154  {
155  if(source[i] == '\0') break;
156  dest[i] = source[i];
157  }
158  dest[i] = '\0';
159  return dest;
160 }
161 
162 //------------------------------------------------------------------------
163 int rlvsnprintf(char *text, int len, const char *format, va_list ap)
164 {
165  int ret;
166 
167 #ifdef RLWIN32
168  if(format == NULL || format[0] == '\0')
169  {
170  text[0] = '\0';
171  return 1;
172  }
173  ret = _vsnprintf(text, len, format, ap);
174 #endif
175 #ifdef __VMS
176  static char vms_is_deprecated[rl_PRINTF_LENGTH];
177  if(format == NULL || format[0] == '\0')
178  {
179  text[0] = '\0';
180  return 1;
181  }
182  ret = vsprintf(vms_is_deprecated, format, ap);
183  rlstrncpy(text,vms_is_deprecated,len);
184 #endif
185 #ifdef RLUNIX
186  if(format == NULL || format[0] == '\0')
187  {
188  text[0] = '\0';
189  return 1;
190  }
191  ret = vsnprintf(text, len, format, ap);
192 #endif
193  return ret;
194 }
195 
196 //------------------------------------------------------------------------
197 int rlsnprintf(char *text, int len, const char *format, ...)
198 {
199  int ret;
200 
201  va_list ap;
202  va_start(ap,format);
203 #ifdef RLWIN32
204  ret = _vsnprintf(text, len, format, ap);
205 #endif
206 #ifdef __VMS
207  static char vms_is_deprecated[rl_PRINTF_LENGTH];
208  ret = vsprintf(vms_is_deprecated, format, ap);
209  rlstrncpy(text,vms_is_deprecated,len);
210 #endif
211 #ifdef RLUNIX
212  ret = vsnprintf(text, len, format, ap);
213 #endif
214  va_end(ap);
215  return ret;
216 }
217 
218 //------------------------------------------------------------------------
219 static void *rlsigtermarg = NULL;
220 static void (*rlUserSigtermHandler)(void *arg) = NULL;
221 
222 static void rlSigtermHandler(int sig)
223 {
224  printf("entering rlSigtermHandler sig=%d\n",sig);
225  if(sig == SIGTERM && rlUserSigtermHandler != NULL) rlUserSigtermHandler(rlsigtermarg);
226 #ifdef RLUNIX
227  fflush(stdout);
228  fsync(fileno(stdout));
229 #endif
230  exit(0);
231 }
232 
233 void rlSetSigtermHandler(void (*handler)(void *arg), void *arg)
234 {
235  rlsigtermarg = arg;
236  rlUserSigtermHandler = handler;
237  signal(SIGTERM,rlSigtermHandler);
238 }
239 
240 //------------------------------------------------------------------------
241 const char *rlFindFile(const char *pattern, int *context)
242 {
243  static char freturn[512];
244 
245 #ifdef RLUNIX
246  static DIR *dirp;
247  static struct dirent *dp;
248 
249  if(*context == 0) dirp = opendir(".");
250  *context = 1;
251 
252  while((dp = readdir(dirp)) != NULL)
253  {
254  if(dp->d_name[0] == '.') ;
255  else if(strstr(dp->d_name,pattern) != NULL)
256  {
257  strcpy(freturn,dp->d_name);
258  return freturn;
259  }
260  else if(rlStrMatch(dp->d_name, pattern))
261  {
262  strcpy(freturn,dp->d_name);
263  return freturn;
264  }
265  }
266  closedir(dirp);
267  return NULL;
268 #endif
269 
270 #ifdef __VMS
271  int i,ret;
272  static char file[512] = "";
273  static char wildcard[80];
274  struct dsc$descriptor_s dwildcard;
275  struct dsc$descriptor_s dfreturn;
276 
277  strcpy(wildcard,pattern);
278 
279  dwildcard.dsc$w_length = strlen(wildcard);
280  dwildcard.dsc$a_pointer = wildcard;
281  dwildcard.dsc$b_dtype = DSC$K_DTYPE_T;
282  dwildcard.dsc$b_class = DSC$K_CLASS_S;
283 
284  dfreturn.dsc$w_length = sizeof(freturn);
285  dfreturn.dsc$a_pointer = &freturn[0];
286  dfreturn.dsc$b_dtype = DSC$K_DTYPE_T;
287  dfreturn.dsc$b_class = DSC$K_CLASS_S;
288 
289  ret = LIB$FIND_FILE(&dwildcard,&dfreturn,context,0,0,0,0);
290  if (ret == RMS$_NMF) return NULL; // no more files found
291  else if(ret != RMS$_NORMAL) return NULL;
292  else if(strcmp(freturn,file) == 0) { file[0] = '\0'; return NULL; }
293  i=0;
294  while(freturn[i] > ' ')i++;
295  freturn[i] = '\0';
296  return freturn;
297 #endif
298 
299 #ifdef RLWIN32
300  static WIN32_FIND_DATAA wfd;
301  static HANDLE hFindFile;
302 
303  if(*context == 0) // find first
304  {
305  *context = 1;
306  hFindFile = FindFirstFileA(pattern,&wfd);
307  if(hFindFile == INVALID_HANDLE_VALUE) return NULL;
308  else strcpy(freturn,(const char *) &wfd.cFileName);
309  }
310  else // subsequent find
311  {
312  if(FindNextFileA(hFindFile,&wfd) == TRUE) strcpy(freturn,(const char *) &wfd.cFileName);
313  else { FindClose(hFindFile); return NULL; }
314  }
315  return freturn;
316 #endif
317 }
318 
319 //------------------------------------------------------------------------
320 const char *rlGetInifile(const char *name)
321 {
322  static char buf[1024];
323 
324 #ifdef RLUNIX
325  strcpy(buf,getenv("HOME"));
326  strcat(buf,"/.");
327 #endif
328 #ifdef __VMS
329  strcpy(buf,"sys$login:");
330 #endif
331 #ifdef RLWIN32
332  ExpandEnvironmentStringsA("%USERPROFILE%",buf,sizeof(buf)-1);
333  if(strcmp(buf,"%USERPROFILE%") == 0) strcpy(buf,"C:");
334  strcat(buf,"\\");;
335 #endif
336 
337  strcat(buf,name);
338  return buf;
339 }
340 
341 int rlSwapShort(int val)
342 {
343  return (val & 0x0ff)*256 + val/256;
344 }
345 
346 //------------------------------------------------------------------------
347 int rlEib1(int command)
348 {
349  char buf[80];
350 
351  sprintf(buf,"buscommand -eib1 %d",command);
352  return system(buf);
353 }
354 
355 int rlEib2(int command)
356 {
357  char buf[80];
358 
359  sprintf(buf,"buscommand -eib2 %d",command);
360  return system(buf);
361 }
362 
363 int rlLon1(int command)
364 {
365  char buf[80];
366 
367  sprintf(buf,"buscommand -lon1 %d",command);
368  return system(buf);
369 }
370 
371 int rlLon2(int command)
372 {
373  char buf[80];
374 
375  sprintf(buf,"buscommand -lon2 %d",command);
376  return system(buf);
377 }
378 
379 int rlProfibus1(int command)
380 {
381  char buf[80];
382 
383  sprintf(buf,"buscommand -profibus1 %d",command);
384  return system(buf);
385 }
386 
387 int rlProfibus2(int command)
388 {
389  char buf[80];
390 
391  sprintf(buf,"buscommand -profibus2 %d",command);
392  return system(buf);
393 }
394 
395 int rlCan1(int command)
396 {
397  char buf[80];
398 
399  sprintf(buf,"buscommand -can1 %d",command);
400  return system(buf);
401 }
402 
403 int rlCan2(int command)
404 {
405  char buf[80];
406 
407  sprintf(buf,"buscommand -can2 %d",command);
408  return system(buf);
409 }
410 
411 #ifdef RLWIN32
412 static int get_iexplore(char *buf)
413 {
414  HKEY applications,iexplore,shell,open,command;
415  long ret;
416  unsigned long size,type;
417  char *cptr;
418 
419  buf[0] = '\0';
420 
421  ret = RegOpenKeyA(
422  HKEY_CLASSES_ROOT, // handle to open key
423  "Applications", // address of name of subkey to open
424  &applications // address of handle to open key
425  );
426  if(ret != ERROR_SUCCESS)
427  {
428  return -1;
429  }
430 
431  ret = RegOpenKeyA(
432  applications, // handle to open key
433  "iexplore.exe", // address of name of subkey to open
434  &iexplore // address of handle to open key
435  );
436  if(ret != ERROR_SUCCESS)
437  {
438  RegCloseKey(applications);
439  return -1;
440  }
441 
442  ret = RegOpenKeyA(
443  iexplore, // handle to open key
444  "shell", // address of name of subkey to open
445  &shell // address of handle to open key
446  );
447  if(ret != ERROR_SUCCESS)
448  {
449  RegCloseKey(applications);
450  RegCloseKey(iexplore);
451  return -1;
452  }
453 
454  ret = RegOpenKeyA(
455  shell, // handle to open key
456  "open", // address of name of subkey to open
457  &open // address of handle to open key
458  );
459  if(ret != ERROR_SUCCESS)
460  {
461  RegCloseKey(applications);
462  RegCloseKey(iexplore);
463  RegCloseKey(shell);
464  return -1;
465  }
466 
467  ret = RegOpenKeyA(
468  open, // handle to open key
469  "command", // address of name of subkey to open
470  &command // address of handle to open key
471  );
472  if(ret != ERROR_SUCCESS)
473  {
474  RegCloseKey(applications);
475  RegCloseKey(iexplore);
476  RegCloseKey(shell);
477  RegCloseKey(open);
478  return -1;
479  }
480 
481  ret = RegQueryValueExA(
482  command, // handle to key to query
483  "", // address of name of value to query
484  NULL, // reserved
485  &type, // address of buffer for value type
486  (unsigned char *) buf, // address of data buffer
487  &size // address of data buffer size
488  );
489 
490  RegCloseKey(applications);
491  RegCloseKey(iexplore);
492  RegCloseKey(shell);
493  RegCloseKey(open);
494  RegCloseKey(command);
495 
496  if(ret != ERROR_SUCCESS) return -1;
497 
498  cptr = strstr(buf," %1");
499  if(cptr != NULL) *cptr = '\0';
500 
501  return 0;
502 }
503 
504 static int mysystem(const char *command)
505 {
506  int ret;
507  STARTUPINFOA si = { sizeof(si)};
508  PROCESS_INFORMATION pi;
509  char cmd[4096];
510 
511  //strcpy(cmd,command);
512  ExpandEnvironmentStringsA(command,cmd,sizeof(cmd)-1);
513  ret = (int) CreateProcessA( NULL, cmd
514  , NULL, NULL
515  , FALSE, CREATE_NO_WINDOW
516  , NULL, NULL
517  , &si, &pi);
518  return ret;
519 }
520 #endif
521 
522 int rlsystem(const char *command)
523 {
524 #ifdef RLWIN32
525  return mysystem(command);
526 #else
527  return system(command);
528 #endif
529 }
530 
531 int rlSubmitPvserver(const char *env, const char *path, const char *pvs, const char *options)
532 {
533  if(env == NULL || path == NULL || pvs == NULL) return -1;
534  rlString command,cd;
535  const char *cptr;
536 
537 #ifdef __VMS
538  cptr = env;
539  cd = cptr;
540  cd += path;
541  command = "spawn/nowait ";
542  command += cd;
543  command += pvs;
544  command += " ";
545  if(options != NULL) command += options;
546  command += " -cd=";
547  command += cd;
548 #else
549  cptr = getenv(env);
550  if(cptr == NULL) { printf("rlSubmitPvserver:ERROR env=%s is not set\n", env); return -2; }
551  cd += cptr;
552  cd += path;
553  command = "\"";
554  command += cd;
555 #ifdef RLWIN32
556  command += "\\";
557 #else
558  command += "/";
559 #endif
560  command += pvs;
561  command += "\" ";
562  if(options != NULL) command += options;
563  command += " \"-cd=";
564  command += cd;
565  command += "\"";
566 #endif
567 
568 #ifdef RLUNIX
569  command += " &";
570 #endif
571 
572  //printf("command=%s\n", command.text());
573  return rlsystem(command.text());
574  //example: rlSubmitPvserver("HOME","/temp/murx","pvs","-exit_on_bind_error -exit_after_last_client_terminates");
575 }
576 
577 int rlBrowser(const char *htmlfile)
578 {
579  char buf[4096];
580  int ret = 0;
581 
582 #ifdef RLUNIX
583  sprintf(buf,"konqueror %s &", htmlfile);
584  ret = system(buf);
585 #endif
586 #ifdef RLWIN32
587  if(get_iexplore(buf) < 0) return -1;
588  strcat(buf," ");
589  strcat(buf,htmlfile);
590  ret = mysystem(buf);
591 #endif
592  return ret;
593 }//------------------------------------------------------------------------
594 
595 int rlOption(const char *string, const char *option)
596 {
597  const char *cptr;
598 
599  cptr = strstr(string,option);
600  if(cptr == NULL) return 0;
601  return 1;
602 }
603 
604 int rlIntOption(const char *string, const char *option, int def)
605 {
606  const char *cptr;
607  int ret;
608 
609  cptr = strstr(string,option);
610  if(cptr == NULL) return def;
611 
612  cptr = strstr(cptr,"=");
613  if(cptr == NULL) return def;
614 
615  sscanf(cptr,"=%d",&ret);
616  return ret;
617 }
618 
619 float rlFloatOption(const char *string, const char *option, float def)
620 {
621  const char *cptr;
622  float ret;
623 
624  cptr = strstr(string,option);
625  if(cptr == NULL) return def;
626 
627  cptr = strstr(cptr,"=");
628  if(cptr == NULL) return def;
629 
630  sscanf(cptr,"=%f",&ret);
631  return ret;
632 }
633 
634 const char *rlTextOption(const char *string, const char *option, const char *def)
635 {
636  int i = 0;
637  const char *cptr;
638  char quote;
639  static char ret[rl_PRINTF_LENGTH];
640 
641  cptr = strstr(string,option);
642  if(cptr == NULL) return def;
643 
644  cptr = strstr(cptr,"=");
645  if(cptr == NULL) return def;
646 
647  cptr++;
648  quote = *cptr;
649  cptr++;
650  while(i < ((int) sizeof(ret)-1))
651  {
652  if(cptr[i] == quote || cptr[i] == '\0') break;
653  ret[i] = cptr[i];
654  i++;
655  }
656  ret[i] = '\0';
657  return ret;
658 }
659 
660 int rlCopyTextfile(const char *source, const char *destination)
661 {
662  FILE *fin, *fout;
663 
664  if(source == NULL || destination == NULL) return -1;
665  fin = fopen(source,"r");
666  if(fin == NULL)
667  {
668  printf("rlCopyTextfile: could not read %s\n",source);
669  return -1;
670  }
671  fout = fopen(destination,"w");
672  if(fout == NULL)
673  {
674  fclose(fin);
675  printf("rlCopyTextfile: could not write %s\n",destination);
676  return -1;
677  }
678 
679  char *line = new char[256*256];
680  while(fgets(line,sizeof(line)-1,fin) != NULL)
681  {
682  fprintf(fout,"%s",line);
683  }
684  delete [] line;
685 
686  fclose(fin);
687  fclose(fout);
688  return 0;
689 }
690 
691 int rlupper(char *str)
692 {
693  if(str == NULL) return -1;
694  while(*str != '\0')
695  {
696  *str = toupper(*str);
697  str++;
698  }
699  return 0;
700 }
701 
702 int rllower(char *str)
703 {
704  if(str == NULL) return -1;
705  while(*str != '\0')
706  {
707  *str = tolower(*str);
708  str++;
709  }
710  return 0;
711 }
712 
713 int rlStartsWith(const char *str, const char *startstr)
714 {
715  int ret;
716  if(str == NULL || startstr == NULL) return 0;
717  ret = strncmp(str,startstr,strlen(startstr));
718  if(ret == 0) return 1;
719  return 0;
720 }
721 
722 int rlStrMatch(const char *str, const char *wild)
723 {
724  if(strstr(str,wild) != NULL) return 1;
725  int i,w;
726 
727  w = 0;
728  for(i=0; str[i] != '\0'; i++)
729  {
730  if(wild[w] == '*')
731  {
732  while(wild[w] == '*') w++;
733  if(wild[w] == '\0') return 1;
734 try_next:
735  if(wild[w] == '\0') return 0;
736  while(str[i] != wild[w] && str[i] != '\0') i++;
737  if(str[i] == '\0') return 0;
738  while(str[i] != '\0')
739  {
740  if(str[i] != wild[w]) goto try_next;
741  i++;
742  w++;
743  if(str[i] == '\0' && wild[w] == '\0') return 1;
744  }
745  if(wild[w] != '\0') return 0;
746  return 1;
747  }
748  else
749  {
750  if(str[i] != wild[w]) return 0;
751  }
752  w++;
753  }
754  return 1;
755 }
756 
757 int rlStat(const char *filepath, struct stat *buf)
758 {
759  return stat(filepath, buf);
760 }
761 
762 int rlMkdir(const char *dir, int mode)
763 {
764 #ifdef RLWIN32
765  int ret = CreateDirectoryA(dir,NULL);
766  if(ret) return 0;
767  return -1;
768 #else
769  return mkdir(dir, mode);
770 #endif
771 }
772 
773 int rlBitSet(int bitnumber, int *value)
774 {
775  int bit = 1 << bitnumber;
776  *value = *value | bit;
777  return *value;
778 }
779 
780 int rlBitClear(int bitnumber, int *value)
781 {
782  int bit = 1 << bitnumber;
783  bit = ~bit;
784  *value = *value & bit;
785  return *value;
786 }
787 
788 int rlBitChange(int bitnumber, int *value)
789 {
790  int bit = 1 << bitnumber;
791  *value = *value ^ bit;
792  return *value;
793 }
794 
795 int rlBitTest(int bitnumber, int *value)
796 {
797  int bit = 1 << bitnumber;
798  if(*value & bit) return 1;
799  return 0;
800 }
801