Arusekk

conio.h for linux v1.0.4d

Jul 22nd, 2015
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | None | 0 0
  1. #ifndef __WIN32
  2. #include <bits/stdc++.h>
  3. #include <termios.h>
  4. #include <unistd.h>
  5. #include <thread>
  6. #define STD_OUTPUT_HANDLE stdout
  7. #define GetStdHandle(x) x
  8. #define kbhit _kbhit
  9. #define getch _getch
  10. struct termios termios_saved_attributes;
  11. void termios_reset_input_mode() {
  12.   tcsetattr(STDIN_FILENO, TCSANOW, &termios_saved_attributes);
  13. }
  14. void termios_set_input_mode() {
  15.   struct termios tattr;
  16.   if (!isatty(STDIN_FILENO)) {
  17.     fprintf(stderr, "Not a terminal.\n");
  18.     exit(EXIT_FAILURE);
  19.   }
  20.   tcgetattr(STDIN_FILENO, &termios_saved_attributes);
  21.   atexit(termios_reset_input_mode);
  22.   tcgetattr(STDIN_FILENO, &tattr);
  23.   tattr.c_lflag &= ~(ICANON|ECHO);
  24.   tattr.c_cc[VMIN] = 1;
  25.   tattr.c_cc[VTIME] = 0;
  26.   tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr);
  27. }
  28. void Sleep(int ms) {
  29.   fflush(stdout);
  30.   timespec tim, tim2;
  31.   tim.tv_sec = ms/1000;
  32.   tim.tv_nsec = 1000000L*(ms%1000);
  33.   int err;
  34.   assert((err=nanosleep(&tim, &tim2))>=0);
  35. }
  36. std::queue<char> _gotch;
  37. bool _life_pending=true;
  38. class _unimportant {
  39. public:
  40.   _unimportant() {
  41.     _life_pending=true;
  42.   }
  43.   ~_unimportant() {
  44.     _life_pending=false;
  45.   }
  46. } _not_really_important;
  47. void _real_init_hit() {
  48.   while (_life_pending) {
  49.     termios_set_input_mode();
  50.     _gotch.push(getchar());
  51.     termios_reset_input_mode();
  52.   }
  53. }
  54. void _init_hit() {
  55.   static bool isinited=false;
  56.   if (isinited) return;
  57.   isinited=true;
  58.   std::thread thr(_real_init_hit);
  59.   thr.detach();
  60. }
  61. bool _kbhit() {
  62.   _init_hit();
  63.   return !_gotch.empty();
  64. }
  65. char _getch() {
  66.   while (!kbhit()) Sleep(1);
  67.   char r=_gotch.front();
  68.   _gotch.pop();
  69.   return r;
  70. }
  71. struct COORD {
  72.   int X;
  73.   int Y;
  74. };
  75. void SetConsoleCursorPosition(FILE*f, COORD p) {
  76.   fprintf(f, "\x1b[%d;%dH", p.Y+1, p.X+1);
  77.   fflush(f);
  78. }
  79. const int COLOR_WIN_TO_LNX[]={0,4,2,6,1,5,3,7};
  80. const int COLOR_LNX_TO_WIN[]={0,4,2,6,1,5,3,7};
  81. void SetConsoleTextAttribute(FILE*f, short color) {
  82.   fflush(f);
  83.   int fg=color%16;
  84.   int bg=color/16;
  85.   int xdfg=3+(fg&8)/4*3;
  86.   int xdbg=4+(bg&8)/4*3;
  87.   bg=COLOR_WIN_TO_LNX[bg&7];
  88.   fg=COLOR_WIN_TO_LNX[fg&7];
  89.   if (color & 8192) return; // 0x2000 flag makes no change happen
  90.   fprintf(f, "\x1b[%d%d;%d%dm", xdbg, bg, xdfg, fg);
  91. }
  92. const char* CP852_2_UTF[]={"\xc3\x87",
  93. "\xc3\xbc",
  94. "\xc3\xa9",
  95. "\xc3\xa2",
  96. "\xc3\xa4",
  97. "\xc5\xaf",
  98. "\xc4\x87",
  99. "\xc3\xa7",
  100. "\xc5\x82",
  101. "\xc3\xab",
  102. "\xc5\x90",
  103. "\xc5\x91",
  104. "\xc3\xae",
  105. "\xc5\xb9",
  106. "\xc3\x84",
  107. "\xc4\x86",
  108. "\xc3\x89",
  109. "\xc4\xb9",
  110. "\xc4\xba",
  111. "\xc3\xb4",
  112. "\xc3\xb6",
  113. "\xc4\xbd",
  114. "\xc4\xbe",
  115. "\xc5\x9a",
  116. "\xc5\x9b",
  117. "\xc3\x96",
  118. "\xc3\x9c",
  119. "\xc5\xa4",
  120. "\xc5\xa5",
  121. "\xc5\x81",
  122. "\xc3\x97",
  123. "\xc4\x8d",
  124. "\xc3\xa1",
  125. "\xc3\xad",
  126. "\xc3\xb3",
  127. "\xc3\xba",
  128. "\xc4\x84",
  129. "\xc4\x85",
  130. "\xc5\xbd",
  131. "\xc5\xbe",
  132. "\xc4\x98",
  133. "\xc4\x99",
  134. "\xc2\xac",
  135. "\xc5\xba",
  136. "\xc4\x8c",
  137. "\xc5\x9f",
  138. "\xc2\xab",
  139. "\xc2\xbb",
  140. "\xe2\x96\x91",
  141. "\xe2\x96\x92",
  142. "\xe2\x96\x93",
  143. "\xe2\x94\x82",
  144. "\xe2\x94\xa4",
  145. "\xc3\x81",
  146. "\xc3\x82",
  147. "\xc4\x9a",
  148. "\xc5\x9e",
  149. "\xe2\x95\xa3",
  150. "\xe2\x95\x91",
  151. "\xe2\x95\x97",
  152. "\xe2\x95\x9d",
  153. "\xc5\xbb",
  154. "\xc5\xbc",
  155. "\xe2\x94\x90",
  156. "\xe2\x94\x94",
  157. "\xe2\x94\xb4",
  158. "\xe2\x94\xac",
  159. "\xe2\x94\x9c",
  160. "\xe2\x94\x80",
  161. "\xe2\x94\xbc",
  162. "\xc4\x82",
  163. "\xc4\x83",
  164. "\xe2\x95\x9a",
  165. "\xe2\x95\x94",
  166. "\xe2\x95\xa9",
  167. "\xe2\x95\xa6",
  168. "\xe2\x95\xa0",
  169. "\xe2\x95\x90",
  170. "\xe2\x95\xac",
  171. "\xc2\xa4",
  172. "\xc4\x91",
  173. "\xc4\x90",
  174. "\xc4\x8e",
  175. "\xc3\x8b",
  176. "\xc4\x8f",
  177. "\xc5\x87",
  178. "\xc3\x8d",
  179. "\xc3\x8e",
  180. "\xc4\x9b",
  181. "\xe2\x94\x98",
  182. "\xe2\x94\x8c",
  183. "\xe2\x96\x88",
  184. "\xe2\x96\x84",
  185. "\xc5\xa2",
  186. "\xc5\xae",
  187. "\xe2\x96\x80",
  188. "\xc3\x93",
  189. "\xc3\x9f",
  190. "\xc3\x94",
  191. "\xc5\x83",
  192. "\xc5\x84",
  193. "\xc5\x88",
  194. "\xc5\xa0",
  195. "\xc5\xa1",
  196. "\xc5\x94",
  197. "\xc3\x9a",
  198. "\xc5\x95",
  199. "\xc5\xb0",
  200. "\xc3\xbd",
  201. "\xc3\x9d",
  202. "\xc5\xa3",
  203. "\xc2\xb4",
  204. "\xc2\xad",
  205. "\xcb\x9d",
  206. "\xcb\x9b",
  207. "\xcb\x87",
  208. "\xcb\x98",
  209. "\xc2\xa7",
  210. "\xc3\xb7",
  211. "\xc2\xb8",
  212. "\xc2\xb0",
  213. "\xc2\xa8",
  214. "\xcb\x99",
  215. "\xc5\xb1",
  216. "\xc5\x98",
  217. "\xc5\x99",
  218. "\xe2\x96\xa0",
  219. "\xc2\xa0"};
  220. const char* spec219="\x1b[7m \x1b[27m";
  221. char spec178[14];
  222. const char* con2utf[]={
  223. "\0",
  224. "\xe2\x98\xba",
  225. "\xe2\x98\xbb",
  226. "\xe2\x99\xa5",
  227. "\xe2\x99\xa6",
  228. "\xe2\x99\xa3",
  229. "\xe2\x99\xa0",
  230. "\x07",
  231. "\x08",
  232. "\t",
  233. "\n",
  234. "\xe2\x99\x80",
  235. "\xe2\x99\x82",
  236. "\r",
  237. "\xe2\x99\xac",
  238. "\xe2\x98\xbc",
  239. "\xe2\x96\xb6",
  240. "\xe2\x97\x80",
  241. "\xe2\x86\x95",
  242. "\xe2\x80\xbc",
  243. "\xc2\xb6",
  244. "\xc2\xa7",
  245. "\xe2\x96\x82",
  246. "\xe2\x86\xa8",
  247. "\xe2\x86\x91",
  248. "\xe2\x86\x93",
  249. "\xe2\x86\x92",
  250. "\xe2\x86\x90",
  251. "\xe2\x8c\x99",
  252. "\xe2\x86\x94",
  253. "\xe2\x96\xb2",
  254. "\xe2\x96\xbc"};
  255. typedef unsigned char uchar;
  256. const char* cp2utf(char& x) {
  257.   uchar us=x;
  258.   if (us<32) return con2utf[us];
  259.   if (us<128) return &x;
  260.   if (us==219) return spec219;
  261.   if (!spec178[0])
  262.     sprintf(spec178,"\x1b[7m%s\x1b[27m",CP852_2_UTF[176-128]);
  263.   if (us==178) return spec178;
  264.   return CP852_2_UTF[us-128];
  265. }
  266. int printf(const char* fmt, ...) {
  267.   char s[1000];
  268.   va_list argz;
  269.   va_start(argz, fmt);
  270.   size_t len=vsprintf(s, fmt, argz);
  271.   va_end(argz);
  272.   size_t i;
  273.   char c[2]={'\0','\0'};
  274.   for (i=0; i<len; i++) {
  275.     c[0]=s[i];
  276.     fputs(cp2utf(c[0]), stdout);
  277.   }
  278.   fflush(stdout);
  279.   return 1;
  280. }
  281. #else // __WIN32
  282.  
  283. #include <conio.h>
  284. #include <windows.h>
  285.  
  286. #endif // __WIN32
Advertisement
Add Comment
Please, Sign In to add comment