942,790 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 166
  • C++ RSS
Sep 7th, 2010
0

A Class to format string on console

Expand Post »
You can use the following class to align text such as paragraphs to be centered, right, or left on a console. Also it supports some more methods. I have written this so it will be helpful for some and also for me. I'm not that pro so have wasted a lot of time to test and design this small class. So hope some of you will get some value from it. ... Also please test and tell me if there are unexpected results. Please feel free to comment about the class. I would really appreciate it.

C++ Syntax (Toggle Plain Text)
  1. /**
  2.  * Email: sinaru_52@yahoo.com
  3.  */
  4. #ifndef CONSOLE_H
  5. #define CONSOLE_H
  6. #include <iostream>
  7.  
  8. using namespace std;
  9. /**
  10.  * This class is designed to format string outputs on the console. You can align
  11.  * strings to right side, left side, or center. Support newline character inside
  12.  * the string. Do not support backspace character.
  13.  * @param str
  14.  */
  15. class Console
  16. {
  17. public:
  18. /**
  19.   * The string will be right aligned and shown to on the standard console.
  20.   * This method does not modifies the passed string.
  21.   * @param str
  22.   */
  23. void printRight(string str)
  24. {
  25. cout << getFrontNewLines(str);
  26. cout << alignRight(str) << endl;
  27. }
  28. /**
  29.   * The string will be left aligned and shown to on the standard console.
  30.   * This method does not modifies the passed string.
  31.   * @param str
  32.   */
  33. void printLeft(string str)
  34. {
  35. cout << getFrontNewLines(str);
  36. cout << alignLeft(str) << endl;
  37. }
  38. /**
  39.   * The string will be Centered and shown to on the standard console.
  40.   * This method does not modifies the passed string.
  41.   * @param str
  42.   */
  43. void printCenter(string str)
  44. {
  45. cout << getFrontNewLines(str);
  46. cout << alignCenter(str) << endl;
  47. }
  48.  
  49. /**
  50.   * The passed string will be to be right aligned according to the console.
  51.   * This method modidifies the original string. You can either use this to
  52.   * assign to another string or just use the method to modify the original
  53.   * string itself.
  54.   * @param str
  55.   * @return formatted text.
  56.   */
  57. string alignRight(string& str)
  58. {
  59. int len = str.length();
  60.  
  61. int loc = getBreakLoc(str);
  62.  
  63. if (0 < loc)
  64. {
  65. string tem;
  66. if (str[loc] == '\n')
  67. tem = str.substr(0, loc);
  68. else
  69. tem = str.substr(0, loc + 1);
  70.  
  71. str = str.substr(loc + 1, len - 1);
  72. removeTrailingSpaces(tem);
  73. return getRightSpace(tem) + tem + '\n' + alignRight(str);
  74. }
  75.  
  76. else
  77. {
  78. string newLines = "";
  79.  
  80. int i = 0;
  81. while (str[i] == '\n')
  82. {
  83. newLines = newLines + '\n';
  84. i++;
  85. }
  86.  
  87. if (i != 0)
  88. str = str.substr(i, len-1);
  89.  
  90. removeTrailingSpaces(str);
  91. return newLines + getRightSpace(str) + str;
  92. }
  93. }
  94. /**
  95.   * The passed string will be to be left aligned according to the console.
  96.   * This method modidifies the original string. You can either use this to
  97.   * assign to another string or just use the method to modify the original
  98.   * string itself.
  99.   * @param str
  100.   * @return formatted text.
  101.   */
  102. string alignLeft(string & str)
  103. {
  104. int len = str.length();
  105.  
  106. int loc = getBreakLoc(str);
  107.  
  108. if (0 < loc)
  109. {
  110. string tem;
  111. if (str[loc] == '\n')
  112. tem = str.substr(0, loc);
  113. else
  114. tem = str.substr(0, loc + 1);
  115.  
  116. str = str.substr(loc + 1, len - 1);
  117. removeFrontSpaces(tem);
  118. return getLeftSpace() + tem + '\n' + alignLeft(str);
  119. }
  120. else
  121. {
  122. string newLines = "";
  123.  
  124. int i = 0;
  125. while (str[i] == '\n')
  126. {
  127. newLines = newLines + '\n';
  128. i++;
  129. }
  130.  
  131. if (i != 0)
  132. str = str.substr(i, len - 1);
  133.  
  134. removeFrontSpaces(str);
  135. return newLines + getLeftSpace() + str;
  136. }
  137. }
  138.  
  139. /**
  140.   * The passed string will be to be centered aligned according to the console.
  141.   * This method modidifies the original string. You can either use this to
  142.   * assign to another string or just use the method to modify the original
  143.   * string itself.
  144.   * @param str
  145.   * @return formatted text.
  146.   */
  147. string alignCenter(string &str)
  148. {
  149. int len = str.length();
  150.  
  151. int loc = getBreakLoc(str);
  152.  
  153. if (0 < loc)
  154. {
  155. string tem;
  156. if (str[loc] == ' ' || str[loc] == '\n')
  157. tem = str.substr(0, loc);
  158. else
  159. tem = str.substr(0, loc + 1);
  160.  
  161. str = str.substr(loc + 1, len - 1);
  162.  
  163. return getCenterSpace(tem) + tem + '\n' + alignCenter(str);
  164. }
  165. else
  166. {
  167. string newLines = "";
  168.  
  169. int i = 0;
  170. while (str[i] == '\n')
  171. {
  172. newLines = newLines + '\n';
  173. i++;
  174. }
  175.  
  176. if (i != 0)
  177. str = str.substr(i, len - 1);
  178.  
  179. return newLines + getCenterSpace(str) + str;
  180. }
  181. }
  182.  
  183. /**
  184.   * You can use this method to set the pargraph width
  185.   * @param width
  186.   */
  187. void setParaWidth(int width)
  188. {
  189. if (width < 0)
  190. {
  191. cout << "Error: paragraph width cannot be a negative value." << endl;
  192. return;
  193. }
  194.  
  195. if (width > conWidth)
  196. {
  197. cout << "Error: Paragraph width is more than console width(ie. " <<
  198. conWidth << "). width set to " << conWidth << "." << endl;
  199. return;
  200. }
  201.  
  202. paraWidth = width;
  203. }
  204.  
  205. /**
  206.   * This add a left gap to left aligned strings. You can only insert a gap
  207.   * if the paragraph width is less than console width.
  208.   * @param gap
  209.   */
  210. void setLeftGap(int gap)
  211. {
  212. if(conWidth - paraWidth == 0)
  213. {
  214. cout <<"Error: You don't have space to set a left gap because\n"
  215. "paragraph width is equal to console width. Reducing paragraph\n"
  216. "width can fix this problem."<<endl;
  217. return;
  218. }
  219.  
  220. if (gap < 0)
  221. {
  222. cout << "Error: Left gap cannot be a negative value." << endl;
  223. return;
  224. }
  225.  
  226. if (gap > conWidth - paraWidth)
  227. {
  228. cout << "Error: Left gap is too large. The maximum value you can set is "
  229. << conWidth - paraWidth <<"." << endl;
  230. return;
  231. }
  232.  
  233. leftGap = gap;
  234. }
  235.  
  236. /**
  237.   * This add a right gap to right aligned strings. You can only insert a gap
  238.   * if the paragraph width is less than console width.
  239.   * @param gap
  240.   */
  241. void setRightGap(int gap)
  242. {
  243. if(conWidth - paraWidth == 0)
  244. {
  245. cout <<"Error: You don't have space to set a right gap because\n"
  246. "paragraph width is equal to console width. Reducing paragraph\n"
  247. "width can fix this problem."<<endl;
  248. return;
  249. }
  250.  
  251. if (gap < 0)
  252. {
  253. cout << "Error: Right gap cannot be a negative value." << endl;
  254. return;
  255. }
  256.  
  257. if (gap > conWidth - paraWidth)
  258. {
  259. cout << "Error: Right gap is too large. The maximum value you can set is "
  260. << conWidth - paraWidth <<"." << endl;
  261. return;
  262. }
  263. rightGap = gap;
  264. }
  265.  
  266. void updateConWidth()
  267. {
  268. conWidth = getConWidth();
  269. }
  270.  
  271. Console()
  272. {
  273. conWidth = getConWidth();
  274. paraWidth = conWidth;
  275. leftGap = 0;
  276. rightGap = 0;
  277. }
  278.  
  279. private:
  280. void removeTrailingSpaces(string& str)
  281. {
  282. int len = str.length();
  283. int i;
  284.  
  285. for (i = len - 1; i >= 0; i--)
  286. if (str[i] != ' ')
  287. break;
  288.  
  289. str = str.substr(0, i + 1);
  290. }
  291.  
  292. void removeFrontSpaces(string& str)
  293. {
  294. int len = str.length();
  295. int i;
  296.  
  297. for (i = 0; i < len; i++)
  298. if (str[i] != ' ')
  299. break;
  300.  
  301. str = str.substr(i, len + 1);
  302. }
  303.  
  304. string getCenterSpace(const string& str)
  305. {
  306. string spaces = "";
  307. int noofspaces = (conWidth - str.length()) / 2;
  308.  
  309. for (int i = 0; i < noofspaces; i++)
  310. spaces = spaces + ' ';
  311.  
  312. return spaces;
  313. }
  314.  
  315. string getRightSpace(const string& str)
  316. {
  317. string spaces = "";
  318. int noofspaces = (conWidth - str.length()) - rightGap;
  319. int i;
  320. for (i = 0; i < noofspaces; i++)
  321. spaces = spaces + ' ';
  322.  
  323. return spaces;
  324. }
  325.  
  326. string getLeftSpace()
  327. {
  328. string spaces = "";
  329. for (int i = 0; i < leftGap; i++)
  330. spaces = spaces + ' ';
  331.  
  332. return spaces;
  333. }
  334.  
  335. int getConWidth()
  336. {
  337. //Update this function according to your OS and return the console
  338. // width
  339. return 80;
  340. }
  341.  
  342. string getFrontNewLines(string& str)
  343. {
  344. int len = str.length();
  345. string newLines="";
  346.  
  347. int i;
  348.  
  349. for (i = 0; i < len; i++)
  350. if (str[i] != '\n')
  351. break;
  352.  
  353. newLines = str.substr(0, i);
  354. str = str.substr(i, len);
  355.  
  356. return newLines;
  357. }
  358.  
  359. int getBreakLoc(string &str)
  360.  
  361. {
  362. int length = str.length();
  363.  
  364.  
  365. for (int i = 0; i < length && i < paraWidth; i++)
  366. if (str[i] == '\n')
  367. return i;
  368.  
  369. if (length > paraWidth)
  370. {
  371. int loc = paraWidth - 1;
  372.  
  373. if (str[loc] != '\n' &&
  374. (str[loc + 1] != ' ' && str[loc + 1] != '\n'))
  375. {
  376. for (; loc > 0; loc--)
  377. if (str[loc] == ' ' || str[loc] == '\n')
  378. return loc;
  379.  
  380. if (loc == 0)
  381. {
  382. loc = paraWidth - 1;
  383. str.insert(loc, "-");
  384. return loc;
  385. }
  386. }
  387. else
  388. return paraWidth - 1;
  389. }
  390. else
  391. return -1;
  392.  
  393. }
  394.  
  395. int leftGap;
  396. int rightGap;
  397. int conWidth;
  398. int paraWidth;
  399. string firstNewLines;
  400. };
  401. #endif /* CONSOLE_H */
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sinaru is offline Offline
18 posts
since Dec 2009
Sep 7th, 2010
0
Re: A Class to format string on console
A small mistake at line 198. It shouldn't say "width set to " << conWidth << "." << endl;". Now I saw it. It should be removed.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sinaru is offline Offline
18 posts
since Dec 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: overloading
Next Thread in C++ Forum Timeline: Filing In Winform c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC