docs
[mirrors/Programs.git] / misc / gbook_offline / gb_console.php
1 <?php
2
3 //READLINE Function
4 function readline($prompt="") {
5 echo $prompt;
6
7 /*
8 $ifp = fopen("php://stdin", "r"); //PHP <= 4
9 $line = fgets($ifp, 999); //PHP <= 4
10 fclose($ifp); //PHP <= 4
11 */
12
13 $line = fgets(STDIN, 999); //PHP >= 5
14 return trim($line);
15 }
16 //READLINE End
17
18 //Hobit-01.txt
19
20 //Settings:
21 $prompt = "\n> ";
22 $random_out = "Random judged: ";
23 $nextf_out = "\nNext file: ";
24 exec("chcp 1250"); //Only Windows - set CP1250 - national alphabets - needed for Cestina ;)
25
26 //CODE
27 //Init:
28 $game = ""; //Hra natazena v pameti
29 $file = ""; //Posledni otevreny soubor
30 $nextf = ""; //Soubor nacteny z odkazu
31 $pos = 2; //Pozice ve hre
32 $histpos = "1";
33 $hist[$histpos] = "START";
34
35
36 echo(
37 "OGB Engine 0.6\n".
38 "Harvie 2oo7\n".
39 "http://gbook.wz.cz/\n".
40 "-----------------------\n".
41 "Type h[ENTER] for help.\n"
42 );
43
44 while(1) {
45
46 $in_line = readline($prompt);
47 $in_cmds = explode(" ", $in_line);
48 $in_cmds[0] = strtolower($in_cmds[0]);
49
50 //h - print help
51 if ($in_cmds[0] == "h") {
52 echo(
53 "HELP:\n".
54 "h - Show help\n".
55 "f - Show files\n".
56 "l file - Load file\n".
57 "l URL - Load file from HTTP or FTP\n".
58 "y - set CP 1250 ONLY WINDOWS!!! (for some national characters)\n".
59 "# - Go to line #\n".
60 "2 - Go to start\n".
61 "p - Print history\n".
62 "n - Load next file from link\n".
63 "r - Reload file\n".
64 "c - Close file\n".
65 "q - Quit\n"
66 );
67 }
68
69 //l - load game
70 if ($in_cmds[0] == "l") {
71 if ( file_exists( trim($in_cmds[1]) ) || strpos(" ".$in_cmds[1], ":") )
72 {
73 if ( strpos(" ".$in_cmds[1], ":") ) {
74 echo("DOWNLOADING...\n");
75 }
76 $game = file( trim($in_cmds[1]) );
77 $file = trim($in_cmds[1]);
78 $histpos++;
79 $hist[$histpos] = "\nLOAD: ".trim($in_cmds[1])."\n";
80 echo("LOADED: $in_cmds[1]\n");
81 print_r(explode("|", $game[0]));
82 echo("\n");
83 }
84 else
85 {
86 echo("CAN'T LOAD: $in_cmds[1]\n");
87 }
88 }
89
90 //r - reload
91 if ($in_cmds[0] == "r") {
92 $histpos++;
93 $hist[$histpos] = "RELOAD";
94 $game = file($file);
95 }
96
97 //p - history
98 if ($in_cmds[0] == "p") {
99 echo( implode(";", $hist) );
100 }
101
102 //c - close
103 if ($in_cmds[0] == "c") {
104 $histpos++;
105 $hist[$histpos] = "CLOSE\n";
106 $game = ("");
107 }
108
109 //# - show line
110 if ( is_numeric( trim($in_cmds[0]) ) ) {
111 $pos = round(trim($in_cmds[0]));
112 $posi = ($pos - 1);
113 if(isset($game[$posi])) {
114
115 //history
116 $histpos++;
117 $hist[$histpos] = $pos;
118
119 //nextf
120 if ( strpos( (" ".$game[$posi]), "%%" ) ) {
121 $nextf = explode("%%", $game[$posi]);
122 $nextf = explode("|", $nextf[1]);
123 $nextf = $nextf[0];
124 }
125
126 //rnd
127 $rnd = "";
128 if ( strpos( (" ".$game[$posi]), "{" ) ) {
129 $rnd = explode("{", $game[$posi]);
130 $rnd = explode("}", $rnd[1]);
131 $rnd = explode("\\\\", $rnd[0]);
132 $rnd = $rnd[rand( 0, (sizeof($rnd) - 1) )];
133 $rnd = ($random_out.$rnd);
134 }
135
136 //out
137 echo("$pos: ". $game[$posi]);
138
139 //rnd
140 echo ($rnd);
141
142 //nextf
143 if( is_file($nextf) ) {
144 echo($nextf_out.$nextf."\nType n[ENTER]2[ENTER] to go on.");
145 }
146 }
147 }
148
149 //f - files
150 if ( $in_cmds[0] == "f" ) {
151 echo("FILES:\n\n");
152 $dfp=opendir('.');
153 while (false!==($file = readdir($dfp))) {
154 if ($file != "." && $file != "..") {
155 echo "$file\n";
156 }
157 }
158 closedir($dfp);
159 }
160
161 //y - CP 1250 (needed for Czech language)
162 if ( $in_cmds[0] == "y" ) {
163 system("chcp 1250"); //Only Windows
164 }
165
166 //i - info
167 if ($in_cmds[0] == "i") {
168 print_r(explode("|", $game[0]));
169 echo("\n");
170 }
171
172 //n - nextfile
173 if ($in_cmds[0] == "n" && is_file($nextf) ) {
174 $game = file($nextf);
175 $file = $nextf;
176 $histpos++;
177 $hist[$histpos] = "\n
178 NEXTF: $nextf\n";
179 $nextf = "";
180 }
181
182 //q - quit
183 if ($in_cmds[0] == "q") {
184 die("Exited by user at $file - $pos.\n");
185 }
186
187 }
188
189 ?>
This page took 0.475005 seconds and 4 git commands to generate.