home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / X / XParseGeom.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-21  |  4.2 KB  |  172 lines

  1. /* Copyright     Massachusetts Institute of Technology  1985, 1986, 1987 */
  2. /* $XConsortium: XParseGeom.c,v 11.18 91/02/21 17:23:05 rws Exp $ */
  3.  
  4. /*
  5. Permission to use, copy, modify, distribute, and sell this software and its
  6. documentation for any purpose is hereby granted without fee, provided that
  7. the above copyright notice appear in all copies and that both that
  8. copyright notice and this permission notice appear in supporting
  9. documentation, and that the name of M.I.T. not be used in advertising or
  10. publicity pertaining to distribution of the software without specific,
  11. written prior permission.  M.I.T. makes no representations about the
  12. suitability of this software for any purpose.  It is provided "as is"
  13. without express or implied warranty.
  14. */
  15.  
  16. #include "Xlibint.h"
  17. #include "Xutil.h"
  18.  
  19. #ifdef notdef
  20. /* 
  21.  *Returns pointer to first char ins search which is also in what, else NULL.
  22.  */
  23. static char *strscan (search, what)
  24. char *search, *what;
  25. {
  26.     int i, len = strlen (what);
  27.     char c;
  28.  
  29.     while ((c = *(search++)) != NULL)
  30.         for (i = 0; i < len; i++)
  31.             if (c == what [i])
  32.                 return (--search);
  33.     return (NULL);
  34. }
  35. #endif
  36.  
  37. /*
  38.  *    XParseGeometry parses strings of the form
  39.  *   "=<width>x<height>{+-}<xoffset>{+-}<yoffset>", where
  40.  *   width, height, xoffset, and yoffset are unsigned integers.
  41.  *   Example:  "=80x24+300-49"
  42.  *   The equal sign is optional.
  43.  *   It returns a bitmask that indicates which of the four values
  44.  *   were actually found in the string.  For each value found,
  45.  *   the corresponding argument is updated;  for each value
  46.  *   not found, the corresponding argument is left unchanged. 
  47.  */
  48.  
  49. static int
  50. ReadInteger(string, NextString)
  51. register char *string;
  52. char **NextString;
  53. {
  54.     register int Result = 0;
  55.     int Sign = 1;
  56.     
  57.     if (*string == '+')
  58.     string++;
  59.     else if (*string == '-')
  60.     {
  61.     string++;
  62.     Sign = -1;
  63.     }
  64.     for (; (*string >= '0') && (*string <= '9'); string++)
  65.     {
  66.     Result = (Result * 10) + (*string - '0');
  67.     }
  68.     *NextString = string;
  69.     if (Sign >= 0)
  70.     return (Result);
  71.     else
  72.     return (-Result);
  73. }
  74.  
  75. #if NeedFunctionPrototypes
  76. int XParseGeometry (
  77. _Xconst char *string,
  78. int *x,
  79. int *y,
  80. unsigned int *width,    /* RETURN */
  81. unsigned int *height)    /* RETURN */
  82. #else
  83. int XParseGeometry (string, x, y, width, height)
  84. char *string;
  85. int *x, *y;
  86. unsigned int *width, *height;    /* RETURN */
  87. #endif
  88. {
  89.     int mask = NoValue;
  90.     register char *strind;
  91.     unsigned int tempWidth, tempHeight;
  92.     int tempX, tempY;
  93.     char *nextCharacter;
  94.  
  95.     if ( (string == NULL) || (*string == '\0')) return(mask);
  96.     if (*string == '=')
  97.         string++;  /* ignore possible '=' at beg of geometry spec */
  98.  
  99.     strind = (char *)string;
  100.     if (*strind != '+' && *strind != '-' && *strind != 'x') {
  101.         tempWidth = ReadInteger(strind, &nextCharacter);
  102.         if (strind == nextCharacter) 
  103.             return (0);
  104.         strind = nextCharacter;
  105.         mask |= WidthValue;
  106.     }
  107.  
  108.     if (*strind == 'x' || *strind == 'X') {    
  109.         strind++;
  110.         tempHeight = ReadInteger(strind, &nextCharacter);
  111.         if (strind == nextCharacter)
  112.             return (0);
  113.         strind = nextCharacter;
  114.         mask |= HeightValue;
  115.     }
  116.  
  117.     if ((*strind == '+') || (*strind == '-')) {
  118.         if (*strind == '-') {
  119.               strind++;
  120.             tempX = -ReadInteger(strind, &nextCharacter);
  121.             if (strind == nextCharacter)
  122.                 return (0);
  123.             strind = nextCharacter;
  124.             mask |= XNegative;
  125.  
  126.         }
  127.         else
  128.         {    strind++;
  129.             tempX = ReadInteger(strind, &nextCharacter);
  130.             if (strind == nextCharacter)
  131.                 return(0);
  132.             strind = nextCharacter;
  133.         }
  134.         mask |= XValue;
  135.         if ((*strind == '+') || (*strind == '-')) {
  136.             if (*strind == '-') {
  137.                 strind++;
  138.                 tempY = -ReadInteger(strind, &nextCharacter);
  139.                 if (strind == nextCharacter)
  140.                         return(0);
  141.                 strind = nextCharacter;
  142.                 mask |= YNegative;
  143.  
  144.             }
  145.             else
  146.             {
  147.                 strind++;
  148.                 tempY = ReadInteger(strind, &nextCharacter);
  149.                 if (strind == nextCharacter)
  150.                         return(0);
  151.                 strind = nextCharacter;
  152.             }
  153.             mask |= YValue;
  154.         }
  155.     }
  156.     
  157.     /* If strind isn't at the end of the string the it's an invalid
  158.         geometry specification. */
  159.  
  160.     if (*strind != '\0') return (0);
  161.  
  162.     if (mask & XValue)
  163.         *x = tempX;
  164.      if (mask & YValue)
  165.         *y = tempY;
  166.     if (mask & WidthValue)
  167.             *width = tempWidth;
  168.     if (mask & HeightValue)
  169.             *height = tempHeight;
  170.     return (mask);
  171. }
  172.