File: parseGeometry.cpp

package info (click to toggle)
quickplot 0.8.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,544 kB
  • ctags: 1,019
  • sloc: cpp: 10,051; sh: 7,597; makefile: 176
file content (151 lines) | stat: -rw-r--r-- 3,120 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Copyright (c) 1998, 1999, 2003, 2004  Lance Arsenault, (GNU GPL (v2+))
 */
#include "config.h"

#include <stdio.h>
#include <stdlib.h>

// w and h will not be set if the width and height part of the
// geometry are not valid.  x and y will not be set if the x-position
// and y-position part of the geometry are not valid.
// The values for xSign and ySign may change.

// example:
//  -geometry  800x240+-30-200     w x h xSign x ySign y
//     ====> w=800 h=240 xSign=1 x=-30 ySign=-1 y=200


void parseGeometry(const char *geometry,
                   int &w, int &h,
                   int &x, int &y,
                   int &xSign, int & ySign)
{
  // If the geometry string is longer than 32 is won't work on any
  // display I've ever heard of.

  //  example: -geometry  800x240+30+200
  
  //  example: -geometry  800  x  240  +  300  +  200 \0
  //            geo=      WWW \0  HHH \0       Y  YYY \0
  //           xgeo=                   X  XXX \0
  
  // or even like: -geometry  800x240--30+-200
  
  char geo[32];
  char xgeo[32];
  snprintf(geo, 32, "%s", geometry);
  snprintf(xgeo, 32, "%s", geometry);
  
  char *W=NULL, *H=NULL, *X=NULL, *Y=NULL;
  
  char *s;
  
  // Look for W width and H height WxH
  s = geo;
  for(;*s != 'x' && *s != 'X' && *s != '\0'; s++)
    ;
  if(*s == 'x' || *s == 'X')
  {
    W = geo;
    *s = '\0'; // replace the 'x' || 'X' with '\0'.
    H = ++s;
    for(;*s != '-' && *s != '+' && *s != '\0'; s++)
      ;
    
    if(*s == '-' || *s == '+')
    {
      *s = '\0';
      s++;
    }
  }
  
  if(W && H && W[0] && H[0])
  {
    // resize the window based on W and H
    int w_ = atoi(W);
    int h_ = atoi(H);
    
    if(w_ >= 0 && w_ < 100000 && h_ >=0 && h_ < 100000)
    {
      w = w_;
      h = h_;
    }
  }

  // Look for Y position +Y or -Y
  if(W)
  {
    if(*s == '-' || *s == '+')
      s++;
    
    for(;*s != '-' && *s != '+' && *s != '\0'; s++)
      ;
    if(*s == '-' || *s == '+')
    {
      ySign = (*s == '+')? 1: -1;
      s++;
      Y = s;
    }
  }
  else // !W
  {
    s = geo;
    for(;*s != '-' && *s != '+' && *s != '\0'; s++)
      ;
    s++;
    if(*s == '-' || *s == '+')
      s++;
    
    for(;*s != '-' && *s != '+' && *s != '\0'; s++)
      ;
    if(*s == '-' || *s == '+')
    {
      ySign = (*s == '+')? 1: -1;
      s++;
      Y = s;
    }
  }
  
  
  // Look for X position +X or -X
  if(Y)
  {
    s = xgeo;
    for(;*s != '-' && *s != '+' && *s != '\0'; s++)
        ;      
    if(*s == '-' || *s == '+')
    {
      xSign = (*s == '+')? 1: -1;
      s++;
      X = s;
      s++;
      for(;*s != '-' && *s != '+' && *s != '\0'; s++)
        ;
      if(*s == '-' || *s == '+')
      {
        *s = '\0'; // replace the '+' || '-' with '\0'.
      }
      else
      {
        X = NULL;
        Y = NULL;
      }
    }
  }
  
  
  if(X && Y && X[0] && Y[0])
  {
    // Get x, y, xSign, and ySign and use in the
    // first call to on_expose_event().
    int x_ = atoi(X);
    int y_ = atoi(Y);
    
    if(x_ > -100000 && x_ < 100000 &&
       y_ > -100000 && y_ < 100000)
    {
      x = x_;
      y = y_;
    }
  }
}