Guest User

Untitled

a guest
Mar 8th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.18 KB | None | 0 0
  1. /*//////////////////////////////////////////////////////////////////////////
  2.  
  3. Formatted Console I/O header file.
  4.  
  5. Copyright (c) 2008, 2009, 2010, 2011 by Michael E. Leverington
  6.  
  7. Code is free for use in whole or in part and may be modified for other
  8. use as long as the above copyright statement is included.
  9.  
  10. Code Written: 08/18/2008
  11. Most Recent Update: 05/30/2010 - 5:30 p.m.
  12. Date Due for Review: 06/07/2010
  13.  
  14. /////////////////////////////////////////////////////////////////////////*/
  15. /*
  16. INSTRUCTIONS:
  17.  
  18. 1) This source file contains the functions you may use for input and
  19. output of assignments when you need to use a formatted console window
  20. I/O interface. You only need to include this file as a header file in your
  21. program. As long as you don't change this file, you can keep using it to
  22. develop other programs. It should be noted that header files usually only
  23. contain function headers, and do not normally contain function
  24. implementations. However this file is set up to make it easy for
  25. you to get started learning C++ programming.
  26.  
  27. 2) You MUST place the startCurses function at the beginning of your program
  28. (after your data initialization), and you must MUST place the endCurses
  29. function at the end of your program, just before the return 0 operation.
  30. Failure to do this will lead to significant program running problems.
  31.  
  32. 3) For any of the "promptFor..." functions, just place some prompting
  33. text into the function and call it. The user will be prompted for
  34. the appropriate value, and the user's response will be assigned
  35. to the variable you use. These functions are different from the
  36. standard functions because you must tell the prompt where on the screen
  37. (in x and y positions) to be displayed.
  38.  
  39. Example: userAge = promptForIntAt( 5, 5, "Enter your age: " );
  40.  
  41. Result Displayed (at location x = 5 & y - 5:
  42. Enter your age: {user answers here}
  43.  
  44. 4) For any of the "print...At" functions, you need to provide the following:
  45. - the x and y location to print at
  46. - value to be output
  47. - the justification; the value will be printed as follows:
  48. - to the left starting at the x, y location if "RIGHT" justified
  49. - to the right starting at the x, y location if "LEFT" justified
  50. - centered on the x, y location if "CENTER" justified
  51. - in the case of floating point values, the precision
  52.  
  53. Example 1: printStringAt( 5, 5, "This is a string", "CENTER" );
  54. Result Displayed: pipe is located at 5, 5 => |
  55. string is printed here as shown => This is a string
  56.  
  57. Explanation: "This is a string" is displayed centered
  58. on the x, y location
  59.  
  60. Example 2: printDoubleAt( 2, 30, 25.45678, 2, "RIGHT" );
  61. Result Displayed: pipe is located at 2, 30 => |
  62. 25.46
  63.  
  64. Explanation: The value 25.45678 is displayed right justified ending
  65. with the location 2, 30 and with 2 digits after the
  66. decimal point (called precision for purposes of
  67. this function)
  68.  
  69. 5) You are provided simple information for all of the functions in a
  70. standardized format. You will be using this format for your own
  71. functions in the near future.
  72.  
  73. END OF INSTRUCTIONS
  74. */
  75.  
  76. #ifndef CURSES_IO_H
  77. #define CURSES_IO_H
  78.  
  79. #include <string>
  80. #include <curses.h>
  81.  
  82. using namespace std;
  83. ///////////////////////////////////////////////////////////////////////////
  84. // Global Constants
  85. ///////////////////////////////////////////////////////////////////////////
  86.  
  87. // screen management
  88. const int SCRN_MIN_X = 0;
  89. const int SCRN_MIN_Y = 0;
  90. const int SCRN_MAX_X = 79;
  91. const int SCRN_MAX_Y = 24;
  92.  
  93. // keyboard management
  94. const int ENTER_KEY = 13;
  95. const int BACKSPACE_KEY = 8;
  96. const int KP_ENTER_KEY = 459;
  97. const int KP_MINUS = 464;
  98. const int KP_PLUS = 465;
  99. const int KP_SLASH = 458;
  100. const int KP_SPLAT = 463;
  101. const int KB_RIGHT_ARROW = 261;
  102. const int KB_LEFT_ARROW = 260;
  103. const int KB_DOWN_ARROW = 258;
  104. const int KB_UP_ARROW = 259;
  105. const int KB_PAGE_UP = 339;
  106. const int KB_PAGE_DN = 338;
  107. const int KB_ESCAPE = 27;
  108.  
  109. // string management
  110. const int MAX_INPUT_LENGTH = 60;
  111. const int MIN_STRING_SIZE = 15;
  112.  
  113. // operational constants
  114. const bool SET_BRIGHT = true;
  115. const int FIXED_WAIT = -1;
  116. const int DEFAULT_WAIT = 1;
  117. const int NO_RESPONSE = -1;
  118.  
  119.  
  120.  
  121. ///////////////////////////////////////////////////////////////////////////
  122. // Function Headers
  123. ///////////////////////////////////////////////////////////////////////////
  124.  
  125. /*
  126. name: clearScreen
  127. process: prints spaces across the given area using the presently
  128. specified color scheme
  129. input: upper left x, y, and lower right x, y locations
  130. output/to screen: specified area of screen is cleared
  131. dependencies: none
  132. */
  133. void clearScreen( int uprLeftX, int uprLeftY, int lwrRightX, int lwrRightY );
  134.  
  135. /*
  136. name: promptForCharAt
  137. process: prompts user for character at the specified location, then returns it
  138. input: x, y location, text to prompt user (string)
  139. output/returned: one character (char) is returned to calling function
  140. dependencies: uses waitForInput function
  141. */
  142. char promptForCharAt( int xPos, int yPos, const string &promptString );
  143.  
  144. /*
  145. name: promptForIntAt
  146. process: prompts user for integer at the specified location, then returns it
  147. input: x, y location, text to prompt user (string)
  148. output/returned: one integer (int) is returned to calling function
  149. dependencies: uses getInputString function
  150. */
  151. int promptForIntAt( int xPos, int yPos, const string &promptString );
  152.  
  153. /*
  154. name: promptForDoubleAt
  155. process: prompts user for floating point value at the specified location,
  156. then returns it
  157. input: x, y location, text to prompt user (string)
  158. output/returned: floating point value (double) is returned to calling function
  159. dependencies: uses getInputString function
  160. */
  161. double promptForDoubleAt( int xPos, int yPos, const string &promptString );
  162.  
  163. /*
  164. name: promptForStringAt
  165. process: prompts user for string at the specified location, then returns it
  166. input: x, y location, text to prompt user (string)
  167. output/returned: user entered text (string) is returned to calling function
  168. dependencies: uses getInputString function
  169. */
  170.  
  171. string promptForStringAt( int xPos, int yPos, const string &promptString );
  172.  
  173. /*
  174. name: promptForStringAt
  175. process: prompts user for string at the specified location, then returns it
  176. input: x, y location, text to prompt user (string)
  177. output/passed: user entered text (string) is returned to calling function
  178. dependencies: uses getInputString function
  179. */
  180. void promptForStringAt( int xPos, int yPos,
  181. const string &promptString, char resultString[] );
  182.  
  183. /*
  184. name: getLineAt
  185. process: acquires string from screen, returns it
  186. input: x, y location, length of string to capture (ints)
  187. output/returned: string extracted from screen
  188. dependencies: uses curses function
  189. */
  190. string getLineAt( int xPos, int yPos, int length );
  191.  
  192. /*
  193. name: getCharAt
  194. process: acquires character from screen, returns it
  195. input: x, y location of character to get
  196. output/returned: character extracted from screen
  197. dependencies: uses curses function
  198. */
  199. char getCharAt( int xPos, int yPos );
  200.  
  201. /*
  202. name: setColor
  203. process: sets foreground & background colors for future operations
  204. for initialization, sets a table of all color combinations
  205. input (for initialization): negative numbers, only accepted once
  206. input: color constants representing foreground and background
  207. Colors are: COLOR_BLACK, COLOR_WHITE, COLOR_RED, COLOR_MAGENTA,
  208. COLOR_YELLOW, COLOR_GREEN, COLOR_BLUE, COLOR_CYAN
  209. output/returned: no value is returned to the calling function
  210. dependencies: none
  211. */
  212. void setColor( short foreGround, short backGround, bool bright );
  213.  
  214. /*
  215. name: printCharAt
  216. process: places character at specified location
  217. input: x & y locations
  218. input: character value (char)
  219. output/to screen: places character at specified location
  220. output/returned: no value is returned to the calling function
  221. dependencies: none
  222. */
  223. void printCharAt( int xPos, int yPos, char charVal );
  224.  
  225. /*
  226. process: calculates position for justification, then place
  227. integer at specified location
  228. name: printIntAt
  229. input: x & y locations
  230. input: integer value (int)
  231. input: justification - "CENTER", "LEFT", "RIGHT"
  232. output/to screen: places integer at specified location,
  233. centered, left, or right justified with respect
  234. to given location
  235. output/returned: no value is returned to the calling function
  236. dependencies: uses printStringAt function
  237. */
  238. void printIntAt( int xPos, int yPos, int intVal, const string &justify );
  239.  
  240. /*
  241. name: printDoubleAt
  242. process: calculates position for justification, then place
  243. double value at specified location
  244. input: x & y locations
  245. input: double value (double)
  246. input: precision (int), for number of digits right of the decimal
  247. input: justification - "CENTER", "LEFT", "RIGHT"
  248. output/to screen: places double value at specified location,
  249. centered, left, or right justified with respect
  250. to given location, with specified precision
  251. output/returned: no value is returned to the calling function
  252. dependencies: uses printStringAt function
  253. */
  254.  
  255. void printDoubleAt( int xPos, int yPos, double doubleVal,
  256. int precision, const string &justify );
  257. /*
  258. name: printStringAt
  259. process: calculates position for justification, then place
  260. string at specified location
  261. input: x & y locations
  262. input: string value (string)
  263. input: justification - "CENTER", "LEFT", "RIGHT"
  264. output/to screen: places string at specified location,
  265. centered, left, or right justified with respect
  266. to given location
  267. output/returned: no value is returned to the calling function
  268. dependencies: uses intToString and printStringVertical function
  269. */
  270. void printStringAt( int xPos, int yPos, const string &outString,
  271. const string &justify );
  272.  
  273. /*
  274. name: printStringVertical
  275. process: calculates position for justification, then place
  276. string at specified location
  277. input: x & y locations
  278. input: string value (string)
  279. input: justification - "UP", "DOWN" {guaranteed input}
  280. output/to screen: places string at specified location,
  281. and prints vertically up or down as specified
  282. output/returned: no value is returned to the calling function
  283. dependencies: none
  284. */
  285. void printStringVertical( int xStart, int yStart, const string &text,
  286. const string &orient );
  287.  
  288. /*
  289. name: moveToXY
  290. process: moves cursor to x, y location on screen,
  291. if they are legal positions
  292. input: x and y locations (int)
  293. output/returned: none
  294. dependencies: none
  295. */
  296. void moveToXY( int xPos, int yPos );
  297.  
  298. /*
  299. name: waitForInput
  300. process: waits for user input as specified above; if time runs out withou
  301. user input, function returns constant ERR
  302. input: integer value to indicate one of three things:
  303. - timedWait < 0: wait for user response: any negative number
  304. - timedWait = 0: no wait, get input immediately
  305. - timedWait > 0: waits for "timedWait" tenths of a second,
  306. then acquires input
  307. output/returned: integer value from input, or -1 if no input
  308. dependencies: none
  309. */
  310. int waitForInput( int timedWait );
  311.  
  312. /*
  313. name: startCurses
  314. process: initializes all appropriate components of curses, including
  315. color operations and color table for use by setColor
  316. input: none
  317. output/returned: Boolean true/false to report success at initialization
  318. dependencies: none
  319. */
  320. bool startCurses();
  321.  
  322. /*
  323. name: endCurses
  324. process: shuts down curses operations
  325. input: none
  326. output/returned: none
  327. dependencies: none
  328. */
  329. void endCurses();
  330.  
  331. /*
  332. name: getInputString (supporting function)
  333. process: allows only specified characters to be input, allows backspace
  334. returns when ENTER key is pressed
  335. input: x & y locations
  336. input: string of characters allowed for input
  337. output (to screen): shows input process by user
  338. output (returned): string value is returned to calling function
  339. dependencies: uses waitForInput and charInString
  340. */
  341. string getInputString( int xPos, int yPos, const string &allowedChars );
  342.  
  343. /*
  344. name: charInString (supporting function)
  345. process: searches through given string and tests test character
  346. to see if it is in string; returns true if found, false if not
  347. input: a test character (char)
  348. input: string of characters to be tested
  349. output/returned: returns true if test character is found to be
  350. in given string
  351. dependencies: none
  352. */
  353. bool charInString( char testChar, const string &testString );
  354.  
  355. /*
  356. name: intToString (supporting function)
  357. process: recursively adds individual digits to string (backwards)
  358. input: value - integer (int) value to be converted to string
  359. output: string form of integer value is returned to calling function
  360. dependencies: none
  361. */
  362. string intToString( int value );
  363.  
  364. ///////////////////////////////////////////////////////////////////////////
  365. // Function Implementations
  366. ///////////////////////////////////////////////////////////////////////////
  367. //
  368. void clearScreen( int uprLeftX, int uprLeftY, int lwrRightX, int lwrRightY )
  369. {
  370. // initialize function
  371. const char SPACE = ' ';
  372. int yCtr, xCtr;
  373.  
  374. // loop across rows of the screen
  375. for( yCtr = uprLeftY; yCtr <= lwrRightY; yCtr++ )
  376. {
  377. // loop across the columns of the screen
  378. for( xCtr = uprLeftX; xCtr <= lwrRightX; xCtr++ )
  379. {
  380. // output a space
  381. mvaddch( yCtr, xCtr, SPACE );
  382. }
  383. }
  384. }
  385.  
  386. char promptForCharAt( int xPos, int yPos, const string &promptString )
  387. {
  388. // initialize function
  389. const char SPACE = ' ';
  390. int response, checkForBS = 0;
  391.  
  392. // print prompt string
  393. mvaddstr( yPos, xPos, promptString.c_str() );
  394.  
  395. // move cursor to correct location
  396. xPos += promptString.length();
  397. move( yPos, xPos );
  398.  
  399. do
  400. {
  401. // get character response
  402. response = waitForInput( FIXED_WAIT );
  403.  
  404. // print character response
  405. mvaddch( yPos, xPos, response );
  406.  
  407. // Wait for ENTER key, check for BACKSPACE key
  408. if( ( response != ENTER_KEY ) && ( response != KP_ENTER_KEY ) )
  409. {
  410. checkForBS = waitForInput( FIXED_WAIT );
  411.  
  412. if( checkForBS == BACKSPACE_KEY )
  413. {
  414. mvaddch( yPos, xPos, SPACE );
  415. move( yPos, xPos );
  416. checkForBS = 0; response = ENTER_KEY;
  417. }
  418. }
  419. }
  420. while( ( response == ENTER_KEY ) || ( response == KP_ENTER_KEY ) );
  421.  
  422. // return character response
  423. return ( char( response ) );
  424. }
  425.  
  426. int promptForIntAt( int xPos, int yPos, const string &promptString )
  427. {
  428. // initialize function
  429. string response;
  430. int index, numStringLength, answer;
  431. bool negFlag = false;
  432.  
  433. // print prompt string
  434. mvaddstr( yPos, xPos, promptString.c_str() );
  435.  
  436. // move cursor to response point
  437. xPos += promptString.length();
  438. move( yPos, xPos );
  439.  
  440. // get response from user in string form
  441. response = getInputString( xPos, yPos, "+-0123456789" );
  442.  
  443. // convert string to integer
  444. numStringLength = response.length();
  445. index = 0; answer = 0;
  446.  
  447. // check for negative sign, if anything entered
  448. if( ( numStringLength > 0 ) && ( response.at( index ) == '-' ) )
  449. {
  450. negFlag = true;
  451. index++;
  452. }
  453.  
  454. // skip front end zeroes
  455. while( ( index < numStringLength )
  456. && ( ( response.at( index ) == '0' )
  457. || ( response.at( index ) == '+' ) ) )
  458. {
  459. index ++;
  460. }
  461.  
  462. // verify some number is still there
  463. if( index >= numStringLength )
  464. {
  465. return 0;
  466. }
  467.  
  468. // load number
  469. while( index < numStringLength )
  470. {
  471. answer *= 10;
  472.  
  473. answer += int( response.at( index ) - '0' );
  474.  
  475. index++;
  476. }
  477.  
  478. if( negFlag )
  479. {
  480. answer *= -1;
  481. }
  482.  
  483. // return response
  484. return answer;
  485. }
  486.  
  487. double promptForDoubleAt( int xPos, int yPos, const string &promptString )
  488. {
  489. // initialize function
  490. string response;
  491. int index, numStringLength;
  492. double answer, fractionDigit, multiplier = 1.00;
  493. bool negFlag = false;
  494.  
  495. // print prompt string
  496. mvaddstr( yPos, xPos, promptString.c_str() );
  497.  
  498. // move cursor to response point
  499. xPos += promptString.length();
  500. move( yPos, xPos );
  501.  
  502. // get response from user in string form
  503. response = getInputString( xPos, yPos, ".+-0123456789" );
  504.  
  505. // convert string to double
  506. numStringLength = response.length();
  507. index = 0; answer = 0;
  508.  
  509. // check for negative sign, if anything entered
  510. if( ( numStringLength > 0 ) && ( response.at( index ) == '-' ) )
  511. {
  512. negFlag = true;
  513. index++;
  514. }
  515.  
  516. // skip front end zeroes
  517. while( ( index < numStringLength ) && ( response.at( index ) == '0' ) )
  518. {
  519. index ++;
  520. }
  521.  
  522. // verify some number is still there
  523. if( index >= numStringLength )
  524. {
  525. return 0.0;
  526. }
  527.  
  528. // load major number
  529. while( ( index < numStringLength ) && ( response.at( index ) != '.' ) )
  530. {
  531. answer *= 10.00;
  532.  
  533. answer += double( response.at( index ) - '0' );
  534.  
  535. index++;
  536. }
  537.  
  538. // skip decimal point if found
  539. index++;
  540. multiplier = 0.100;
  541.  
  542. // add fractional value; protect from second decimal point
  543. while( ( index < numStringLength ) && ( response.at( index ) != '.' ) )
  544. {
  545. fractionDigit = double( response.at( index ) - '0' );
  546. fractionDigit *= multiplier;
  547.  
  548. answer += fractionDigit;
  549.  
  550. multiplier /= 10.00; index++;
  551. }
  552.  
  553. if( negFlag )
  554. {
  555. answer *= -1.00;
  556. }
  557.  
  558. // return response
  559. return answer;
  560. }
  561.  
  562. string promptForStringAt( int xPos, int yPos, const string &promptString )
  563. {
  564. // initialize function
  565. string response;
  566.  
  567. // print prompt string
  568. mvaddstr( yPos, xPos, promptString.c_str() );
  569.  
  570. // move cursor to correct location
  571. xPos += promptString.length();
  572. move( yPos, xPos );
  573.  
  574. // takes in all characters except single and double quotes
  575. response = getInputString( xPos, yPos,
  576. "~!@#$%^&*()_+1234567890-=qwertyuiop[]QWERTYUIOP{} |asdfghjkl;ASDFGHJKL:zxcvbnm,./ZXCVBNM<>?" );
  577.  
  578. // return response
  579. return response;
  580. }
  581.  
  582. void promptForStringAt( int xPos, int yPos,
  583. const string &promptString, char resultString[] )
  584. {
  585. // initialize function
  586. string response;
  587.  
  588. // print prompt string
  589. mvaddstr( yPos, xPos, promptString.c_str() );
  590.  
  591. // move cursor to correct location
  592. xPos += promptString.length();
  593. move( yPos, xPos );
  594.  
  595. // takes in all characters except single and double quotes
  596. response = getInputString( xPos, yPos,
  597. "~!@#$%^&*()_+1234567890-=qwertyuiop[]QWERTYUIOP{} |asdfghjkl;ASDFGHJKL:zxcvbnm,./ZXCVBNM<>?" );
  598.  
  599. // set response
  600. strcpy( resultString, response.c_str() );
  601. }
  602.  
  603. string getLineAt( int xPos, int yPos, int length )
  604. {
  605. string newString = "";
  606. chtype charVal;
  607. char newChar;
  608.  
  609. while( length > 0 )
  610. {
  611. charVal = mvinch( yPos, xPos );
  612.  
  613. newChar = ( charVal & A_CHARTEXT );
  614.  
  615. newString += newChar;
  616.  
  617. xPos++; length--;
  618. }
  619.  
  620. return newString;
  621. }
  622.  
  623. char getCharAt( int xPos, int yPos )
  624. {
  625. return ( mvinch( yPos, xPos ) & A_CHARTEXT );
  626. }
  627.  
  628. void setColor( short foreGround, short backGround, bool bright )
  629. {
  630. // initialize function
  631. static short colorArr[ 8 ] [ 8 ];
  632. static bool initialized = false;
  633. short bgInit, fgInit, code = 0;
  634.  
  635. // if initialization is called
  636. if( ( foreGround< 0 ) || ( backGround < 0 ) )
  637. {
  638. // protect from re-initialization
  639. if( initialized )
  640. {
  641. attron( COLOR_PAIR( 0 ) );
  642. }
  643.  
  644. // initialize all combinations of fg/bg colors
  645. for( fgInit = 0; fgInit < 8; fgInit++ )
  646. {
  647. for( bgInit = 0; bgInit < 8; bgInit++ )
  648. {
  649. if( fgInit == bgInit )
  650. {
  651. // same fg and bg colors defaults to W on B
  652. colorArr[ fgInit ] [ bgInit ] = 0;
  653. }
  654. else
  655. {
  656. // store code and create a color pair
  657. code++;
  658. colorArr[ fgInit ] [ bgInit ] = code;
  659. init_pair( code, fgInit, bgInit );
  660. }
  661. }
  662. }
  663.  
  664. // trigger one-time initialization flag
  665. initialized = true;
  666.  
  667. // set codes to black on white
  668. foreGround = backGround = 0;
  669. }
  670.  
  671. // set the color to the specified color code
  672. if( bright )
  673. {
  674. attron( COLOR_PAIR( colorArr[ foreGround ] [ backGround ] ) | A_BOLD );
  675. }
  676. else
  677. {
  678. attron( COLOR_PAIR( colorArr[ foreGround ] [ backGround ] ) );
  679. }
  680. }
  681.  
  682. void printCharAt( int xPos, int yPos, char charVal )
  683. {
  684. // output the character
  685. mvaddch( yPos, xPos, charVal );
  686.  
  687. // update screen
  688. refresh();
  689. }
  690.  
  691. void printIntAt( int xPos, int yPos, int intVal, const string &justify )
  692. {
  693. // initialize function
  694. char tempString[ MIN_STRING_SIZE ];
  695.  
  696. // create string form of number
  697. sprintf( tempString, "%i", intVal );
  698.  
  699. // output in string form
  700. printStringAt( xPos, yPos, tempString, justify );
  701. }
  702.  
  703. void printDoubleAt( int xPos, int yPos, double doubleVal, int precision, const string &justify )
  704. {
  705. // initialize function
  706. char tempString[ MIN_STRING_SIZE ];
  707. string doubleString = "%0.";
  708.  
  709. // set precision in string terms
  710. doubleString += intToString( precision );
  711. doubleString += 'f';
  712.  
  713. // create string form of decimal number, using precision string
  714. sprintf( tempString, doubleString.c_str(), doubleVal );
  715.  
  716. // output in string form
  717. printStringAt( xPos, yPos, tempString, justify );
  718. }
  719.  
  720. void printStringAt( int xPos, int yPos, const string &outString, const string &justify )
  721. {
  722. // initialize function
  723. int stringLength = outString.length() - 1;
  724.  
  725. // check for right justification
  726. if( justify == "RIGHT" )
  727. {
  728. xPos -= stringLength;
  729. }
  730.  
  731. // check for center justification
  732. else if( justify == "CENTER" )
  733. {
  734. xPos -= ( stringLength / 2 );
  735. }
  736.  
  737. // check for vertical justification
  738. if( ( justify == "UP" ) || ( justify == "DOWN" ) )
  739. {
  740. printStringVertical( xPos, yPos, outString, justify );
  741. }
  742. else // otherwise horizontal justification
  743. {
  744. // protect from printing off screen
  745. if( xPos < SCRN_MIN_X )
  746. {
  747. xPos = SCRN_MIN_X;
  748. }
  749.  
  750. // if no horizontal decisions were made,
  751. // then use "LEFT" justify default
  752. mvaddstr( yPos, xPos, outString.c_str() );
  753. }
  754.  
  755. refresh();
  756. }
  757.  
  758. void printStringVertical( int xStart, int yStart, const string &text, const string &orient )
  759. {
  760. int yLoc = yStart, adder = 1;
  761. unsigned index = 0;
  762.  
  763. if( orient == "UP" )
  764. {
  765. adder = -1;
  766. }
  767.  
  768. while( index < text.length() )
  769. {
  770. if( ( yLoc >= SCRN_MIN_Y ) && ( yLoc <= SCRN_MAX_Y ) )
  771. {
  772. printCharAt( xStart, yLoc, text.at( index ) );
  773. }
  774.  
  775. yLoc += adder;
  776. index++;
  777. }
  778. }
  779.  
  780. void moveToXY( int xPos, int yPos )
  781. {
  782. bool legalXLoc = ( xPos >= SCRN_MIN_X && xPos <= SCRN_MAX_X );
  783. bool legalYLoc = ( yPos >= SCRN_MIN_Y && yPos <= SCRN_MAX_Y );
  784.  
  785. if( legalXLoc && legalYLoc )
  786. {
  787. move( yPos, xPos );
  788.  
  789. refresh();
  790. }
  791. }
  792.  
  793. int waitForInput( int timedWait )
  794. {
  795. // initialize function
  796. int response;
  797.  
  798. // initialize keyboard wait
  799. halfdelay( DEFAULT_WAIT );
  800.  
  801. // sets time constant to wait time
  802. if( timedWait > 0 )
  803. {
  804. halfdelay( timedWait );
  805.  
  806. response = getch();
  807.  
  808. halfdelay( DEFAULT_WAIT );
  809. }
  810.  
  811. // provides (almost) immediate response
  812. else if( timedWait == 0 )
  813. {
  814. response = getch();
  815. }
  816.  
  817. // waits for user to respond
  818. else
  819. {
  820. do
  821. {
  822. response = getch();
  823. }
  824. while( response == NO_RESPONSE );
  825. }
  826.  
  827. // covers KeyPad input
  828. switch( response )
  829. {
  830. case KP_PLUS:
  831. return '+';
  832. break;
  833.  
  834. case KP_MINUS:
  835. return '-';
  836. break;
  837.  
  838. case KP_SLASH:
  839. return '/';
  840. break;
  841.  
  842. case KP_SPLAT:
  843. return '*';
  844. break;
  845. }
  846.  
  847. return response;
  848. }
  849.  
  850. bool startCurses()
  851. {
  852. // Initialize the curses library
  853. initscr();
  854. //
  855. // Enable keyboard mapping
  856. keypad(stdscr, TRUE);
  857. //
  858. // Inhibit converting a newline into a carriage return
  859. // and a newline on output
  860. nonl();
  861. //
  862. // Accept input characters without the [ENTER] key
  863. cbreak();
  864. //
  865. // Inhibit input echoing
  866. // (i.e., key pressed will not be shown on the screen)
  867. noecho();
  868. //
  869. // Forces wait until available character
  870. // or time delay (of parameter - 0.1 second),
  871. // whichever comes first; replaced by function
  872. // halfdelay( STANDARD_WAIT );
  873. halfdelay( DEFAULT_WAIT );
  874.  
  875. // test for color problems with console
  876. if( ( !has_colors() ) || ( start_color() == ERR ) )
  877. {
  878. return false;
  879. }
  880.  
  881. // initialize the color set
  882. setColor( -1, -1, false );
  883.  
  884. // if everything worked, return success (true)
  885. return true;
  886. }
  887.  
  888. void endCurses()
  889. {
  890. // Shuts down curses interface
  891. endwin();
  892. }
  893.  
  894. string getInputString( int xPos, int yPos, const string &allowedChars )
  895. {
  896. // initialize function
  897. const char SPACE = ' ';
  898. const char NULL_CHARACTER = '\0';
  899. int response, index = 0;
  900. char inputString[ MAX_INPUT_LENGTH ];
  901.  
  902. // initialize string in case nothing is entered
  903. inputString[ 0 ] = '\0';
  904.  
  905. // repeatedly capture & process input characters
  906. // stop when ENTER key is pressed
  907. do
  908. {
  909. // place the cursor
  910. move( yPos, xPos + index );
  911.  
  912. // get the input as an integer
  913. response = waitForInput( FIXED_WAIT );
  914.  
  915. // accept key pad input if used
  916. switch( response )
  917. {
  918. case 465:
  919. response = '+';
  920. break;
  921.  
  922. case 464:
  923. response = '-';
  924. break;
  925.  
  926. case 463:
  927. response = '*';
  928. break;
  929.  
  930. case 458:
  931. response = '/';
  932. break;
  933. }
  934.  
  935. // if it is in the allowed list of characters,
  936. // accept and print it
  937. if( charInString( response, allowedChars.c_str() ) )
  938. {
  939. inputString[ index ] = response;
  940. inputString[ index + 1 ] = NULL_CHARACTER;
  941.  
  942. mvaddch( yPos, xPos + index, response );
  943.  
  944. index++;
  945. }
  946.  
  947. // if it is the backspace key, act on it
  948. else if( response == BACKSPACE_KEY )
  949. {
  950. if( index >= 0 )
  951. {
  952. if( index > 0 )
  953. {
  954. index--;
  955. }
  956.  
  957. inputString[ index ] = NULL_CHARACTER;
  958.  
  959. mvaddch( yPos, xPos + index, SPACE );
  960. }
  961. }
  962. }
  963. while( ( response != ENTER_KEY ) && ( response != KP_ENTER_KEY ) );
  964.  
  965. // return the generated string
  966. return string( inputString );
  967. }
  968.  
  969. bool charInString( char testChar, const string &testString )
  970. {
  971. // initialize function
  972. int stringLength = testString.length();
  973. int index = 0;
  974.  
  975. // search through string to find test character
  976. while( index < stringLength )
  977. {
  978. // if character is in string, return true
  979. if( testChar == testString.at( index ) )
  980. {
  981. return true;
  982. }
  983.  
  984. index++;
  985. }
  986.  
  987. // if characater not in string, return false
  988. return false;
  989. }
  990.  
  991. string intToString( int value )
  992. {
  993. // if there are digits left, reduce the value by a factor of 10
  994. // and pick off the next digit
  995. // repeat until all digits are used
  996. if( value > 0 )
  997. {
  998. return intToString( value / 10 )
  999. + char( ( value % 10 ) + '0' );
  1000. }
  1001.  
  1002. return "";
  1003. }
  1004.  
  1005. // End Curses I/O header file
  1006. #endif
Add Comment
Please, Sign In to add comment