home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / database / ooed / useful.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-24  |  2.0 KB  |  121 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. #include "useful.hpp"
  6.  
  7. // Useful Functions
  8.  
  9. // Character string based functions
  10.  
  11. void rtrim(char *string)
  12. {
  13.  register int i;
  14.  if(!string) return;
  15.  i = strlen(string);
  16.  i--;
  17.  while(string[i] == ' ')
  18.    {
  19.     string[i] = NULL;
  20.     i--;
  21.    }
  22. }
  23.  
  24. void ltrim(char *string)
  25. {
  26.  char *tmpptr;
  27.  if(!string) return;
  28.  tmpptr = string;
  29.  while(*tmpptr == ' ') tmpptr++;
  30.  strcpy(string, tmpptr);
  31. }
  32.  
  33. void alltrim(char *string)
  34. {
  35.  rtrim(string);
  36.  ltrim(string);
  37. }
  38.  
  39. char *center(char *string)
  40. {
  41.  register int i;
  42.  int length, spaces, newlen;
  43.  char *tmpptr;
  44.  length = strlen(string);
  45.  if (length < 1 || length > 80)
  46.     {
  47.      return NULL;
  48.     }
  49.  spaces = 40 - (length / 2);
  50.  newlen = spaces + length;
  51.  tmpptr = new char[newlen + 1];
  52.  for(i=0; i < newlen + 1; ++i) tmpptr[i] = NULL;
  53.  for(i=0; i < spaces; ++i) strcat(tmpptr," ");
  54.  strcat(tmpptr, string);
  55.  return tmpptr;
  56. }
  57.  
  58. char *center(char *string, int width)
  59. {
  60.  register int i;
  61.  int length, spaces, newlen;
  62.  char *tmpptr;
  63.  length = strlen(string);
  64.  if (length < 1 || length > width)
  65.     {
  66.      return NULL;
  67.     }
  68.  spaces = (width / 2) - (length / 2);
  69.  newlen = spaces + length;
  70.  tmpptr = new char[newlen + 1];
  71.  for(i=0; i < newlen + 1; ++i) tmpptr[i] = NULL;
  72.  for(i=0; i < spaces; ++i) strcat(tmpptr," ");
  73.  strcat(tmpptr, string);
  74.  return tmpptr;
  75. }
  76.  
  77. // Other functions
  78.  
  79. void pause()
  80. {
  81.  while(!kbhit());
  82.  getche();
  83.  if(kbhit()) getche();
  84. }
  85.  
  86. int verify(char *yeschars, char *nochars)
  87. {
  88.  int x,y;
  89.  char ch, *tmpch;
  90.  x = wherex();
  91.  y = wherey();
  92.  while(1)
  93.    {
  94.     gotoxy(x,y);
  95.     ch = getchar();
  96.     tmpch = yeschars;
  97.     while(*tmpch)
  98.       {
  99.        if (*tmpch == ch) return 1;
  100.        ++tmpch;
  101.       }
  102.     tmpch = nochars;
  103.     while(*tmpch)
  104.       {
  105.        if (*tmpch == ch) return 0;
  106.        ++tmpch;
  107.       }
  108.    }
  109. }
  110.  
  111. int is_in(char ch,
  112.           char *str)
  113. {
  114.  while (*str)
  115.    {
  116.     if (*str == ch) return 1;
  117.     str++;
  118.    }
  119.  return 0;
  120. }
  121.