Debian Codegraph @

gcc/testsuite/gm2/projects/pim/run/pass/random/Options.mod

raw
1 (* Copyright (C) 2015 Free Software Foundation, Inc. *)
2 (* This file is part of GNU Modula-2.
3
4 GNU Modula-2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 3, or (at your option) any later
7 version.
8
9 GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with gm2; see the file COPYING. If not, write to the Free Software
16 Foundation, 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA. *)
18
19 IMPLEMENTATION MODULE Options ; (*!m2pim+gm2*)
20
21 FROM DynamicStrings IMPORT String, InitString, KillString ;
22 FROM StringConvert IMPORT stoc ;
23 FROM StrIO IMPORT WriteString, WriteLn ;
24 FROM StdIO IMPORT Write ;
25 FROM ASCII IMPORT nul ;
26 FROM GetOpt IMPORT GetOpt ;
27 FROM libc IMPORT printf, exit ;
28 FROM Chance IMPORT GetSeed, SetSeed ;
29 FROM WriteMap IMPORT SetOutputFile ;
30
31 FROM BoxMap IMPORT MaxX, MaxY,
32 MinRoomLength, MaxRoomLength,
33 MinCorridorLength, MaxCorridorLength, TotalCorridorLength ;
34
35 IMPORT UnixArgs ;
36
37
38 CONST
39 programName = "rndpen" ;
40
41
42 (*
43 Max -
44 *)
45
46 PROCEDURE Max (a, b: CARDINAL) : CARDINAL ;
47 BEGIN
48 IF a > b
49 THEN
50 RETURN a
51 ELSE
52 RETURN b
53 END
54 END Max ;
55
56
57 (*
58 Min -
59 *)
60
61 PROCEDURE Min (a, b: CARDINAL) : CARDINAL ;
62 BEGIN
63 IF a < b
64 THEN
65 RETURN a
66 ELSE
67 RETURN b
68 END
69 END Min ;
70
71
72 (*
73 Init -
74 *)
75
76 PROCEDURE Init ;
77 BEGIN
78 userX := MaxX ;
79 userY := MaxY ;
80 userMinRoomLength := MinRoomLength ;
81 userMaxRoomLength := MaxRoomLength ;
82 userMinCorridorLength := MinCorridorLength ;
83 userMaxCorridorLength := MaxCorridorLength ;
84 userTotalCorridorLength := TotalCorridorLength
85 END Init ;
86
87
88 (*
89 CheckLimits -
90 *)
91
92 PROCEDURE CheckLimits ;
93 VAR
94 l: CARDINAL ;
95 BEGIN
96 l := Min (userX, userY) ;
97 userMaxRoomLength := Min (l, userMaxRoomLength) ;
98 userMinRoomLength := Min (userMinRoomLength, userMaxRoomLength) ;
99 userMaxCorridorLength := Min (l, userMaxCorridorLength) ;
100 userMinCorridorLength := Min (userMinCorridorLength, userMaxCorridorLength)
101 END CheckLimits ;
102
103
104 (*
105 help -
106 *)
107
108 PROCEDURE help (code: INTEGER) ;
109 BEGIN
110 printf ("Usage %s [-a minroomsize] [-b maxroomsize] [-c mincorridorlength] [-d maxcorridorlength] [-e totalcorridorlength] [-h] [-o outputfile] [-s seed] [-x maxx] [-y maxy]\n", programName) ;
111 printf (" -a minroomsize (default is %d)\n", MinRoomLength) ;
112 printf (" -b maxroomsize (default is %d)\n", MaxRoomLength) ;
113 printf (" -c mincorridorsize (default is %d)\n", MinCorridorLength) ;
114 printf (" -d maxcorridorsize (default is %d)\n", MaxCorridorLength) ;
115 printf (" -e totalcorridorlength (default is %d)\n", TotalCorridorLength) ;
116 printf (" -o outputfile (default is stdout)\n") ;
117 printf (" -s seed (default is %d)\n", GetSeed ()) ;
118 printf (" -x minx for whole map (default is %d)\n", MaxX) ;
119 printf (" -y maxy for whole map (default is %d)\n", MaxY) ;
120 exit (code)
121 END help ;
122
123
124 (*
125 HandleOptions -
126 *)
127
128 PROCEDURE HandleOptions ;
129 VAR
130 optind,
131 opterr,
132 optopt: INTEGER ;
133 arg,
134 s, l : String ;
135 ch : CHAR ;
136 BEGIN
137 l := InitString (':a:b:c:d:e:o:s:hx:y:') ;
138 s := NIL ;
139 arg := NIL ;
140 ch := GetOpt (UnixArgs.GetArgC (), UnixArgs.GetArgV (), l,
141 arg, optind, opterr, optopt) ;
142 WHILE ch # nul DO
143 CASE ch OF
144
145 'a': userMinRoomLength := stoc (arg) |
146 'b': userMaxRoomLength := stoc (arg) |
147 'c': userMinCorridorLength := stoc (arg) |
148 'd': userMaxCorridorLength := stoc (arg) |
149 'e': userTotalCorridorLength := stoc (arg) |
150 'h': help (0) |
151 'o': SetOutputFile(arg) |
152 's': SetSeed (stoc (arg)) |
153 'x': userX := stoc (arg) |
154 'y': userY := stoc (arg) |
155 '?': printf ("illegal option\n") ; help (1)
156
157 ELSE
158 WriteString ("unrecognised option '-") ; Write (ch) ; WriteString ('"') ; WriteLn ;
159 exit (1)
160 END ;
161 arg := KillString (arg) ;
162 ch := GetOpt (UnixArgs.GetArgC (), UnixArgs.GetArgV (), l,
163 arg, optind, opterr, optopt)
164 END ;
165 CheckLimits
166 END HandleOptions ;
167
168
169 BEGIN
170 Init
171 END Options.