1 /******************************************************************************
2 *
3 * Copyright (C) 2004-2008, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexey Krivonogov ( gentee )
11 *
12 ******************************************************************************/
13
14 /*-----------------------------------------------------------------------------
15 * Id: console L "Console"
16 *
17 * Summary: Console library. Functions for working with the console.
18 *
19 * List: *,congetch,congetstr,conread,conrequest,conyesno
20 *
21 -----------------------------------------------------------------------------*/
22
23 /*-----------------------------------------------------------------------------
24 * Id: conread F
25 *
26 * Summary: Get a string entered by the user.
27 *
28 * Params: input - The variable of the str type for getting data.
29 *
30 * Return: #lng/retpar(input)
31 *
32 -----------------------------------------------------------------------------*/
33
34 func str conread( str input )
35 {
36 uint len
37
38 input.setlen( 0 )
39 input.reserve( 512 )
40 len = scan( input.ptr(), 512 )
41 input.setlen( len - ?( len >= 2 && input[ len - 1 ] == 0xA, 2, 0 ))
42 return input
43 }
44
45 /*-----------------------------------------------------------------------------
46 * Id: congetstr F
47 *
48 * Summary: Getting a string after text is displayed. Get the string entered by
49 the user with some text displayed before that.
50 *
51 * Params: output - Text for displaying.
52 input - The variable of the str type for getting data.
53 *
54 * Return: #lng/retpar(input)
55 *
56 -----------------------------------------------------------------------------*/
57
58 func str congetstr( str output, str input )
59 {
60 print( output )
61 return conread( input )
62 }
63
64 /*-----------------------------------------------------------------------------
65 * Id: conrequest F
66 *
67 * Summary: Displaying a multiple choice request on the console.
68 *
69 * Params: output - Request text.
70 answer - Enumerating possible answer letters. Answer variants are /
71 separated by '|'. For example, "Nn|Yy"
72 *
73 * Return: The function returns the number of the selected variant beginning
74 from 0.
75 *
76 -----------------------------------------------------------------------------*/
77
78 func uint conrequest( str output, str answer )
79 {
80 int ch
81 uint i ret
82
83 label again
84 print( output )
85 ch = getch()
86 ret = 0
87 fornum i = 0, *answer
88 {
89 if answer[i] == '|'
90 {
91 ret++
92 continue
93 }
94 if answer[i] == ch
95 {
96 str stemp
97
98 print( stemp.appendch(ch) += "\n" )
99 return ret
100 }
101 }
102 print("\n")
103 goto again
104
105 return ret
106 }
107
108 /*-----------------------------------------------------------------------------
109 * Id: conyesno F
110 *
111 * Summary: Displaying a question on the console.
112 *
113 * Params: output - Question text.
114 *
115 * Return: The function returns 1 if the answer is 'yes' and 0 otherwise.
116 *
117 -----------------------------------------------------------------------------*/
118
119 func uint conyesno( str output )
120 {
121 return conrequest( output, "Nn|Yy" )
122 }
123
124 /*-----------------------------------------------------------------------------
125 ** Id: congetch F
126 *
127 * Summary: Displaying text and waiting for a keystroke.
128 *
129 * Params: output - Message text.
130 *
131 * Return: The function returns the value of the pressed key.
132 *
133 -----------------------------------------------------------------------------*/
134
135 func uint congetch( str output )
136 {
137 print( output )
138 return getch()
139 }
140