No Description https://ascii.town
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

merge.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Copyright (C) 2017 Curtis McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define _XOPEN_SOURCE_EXTENDED
  17. #include <curses.h>
  18. #include <err.h>
  19. #include <fcntl.h>
  20. #include <locale.h>
  21. #include <stdio.h>
  22. #include <sysexits.h>
  23. #include <wchar.h>
  24. #include "torus.h"
  25. static void curse(void) {
  26. setlocale(LC_CTYPE, "");
  27. initscr();
  28. start_color();
  29. assume_default_colors(0, 0);
  30. if (COLORS >= 16) {
  31. for (short pair = 1; pair < 0x80; ++pair) {
  32. init_pair(pair, pair & 0x0F, (pair & 0xF0) >> 4);
  33. }
  34. } else {
  35. for (short pair = 1; pair < 0100; ++pair) {
  36. init_pair(pair, pair & 007, (pair & 070) >> 3);
  37. }
  38. }
  39. color_set(ColorWhite, NULL);
  40. mvhline(CellRows, 0, 0, CellCols);
  41. mvhline(CellRows * 2 + 1, 0, 0, CellCols);
  42. mvvline(0, CellCols, 0, CellRows * 2 + 1);
  43. mvaddch(CellRows, CellCols, ACS_RTEE);
  44. mvaddch(CellRows * 2 + 1, CellCols, ACS_LRCORNER);
  45. color_set(0, NULL);
  46. cbreak();
  47. noecho();
  48. keypad(stdscr, true);
  49. set_escdelay(100);
  50. }
  51. static attr_t colorAttr(uint8_t color) {
  52. if (COLORS >= 16) return A_NORMAL;
  53. return (color & ColorBright) ? A_BOLD : A_NORMAL;
  54. }
  55. static short colorPair(uint8_t color) {
  56. if (COLORS >= 16) return color;
  57. return (color & 0x70) >> 1 | (color & 0x07);
  58. }
  59. static void drawTile(int offsetY, const struct Tile *tile) {
  60. for (uint8_t cellY = 0; cellY < CellRows; ++cellY) {
  61. for (uint8_t cellX = 0; cellX < CellCols; ++cellX) {
  62. uint8_t color = tile->colors[cellY][cellX];
  63. uint8_t cell = tile->cells[cellY][cellX];
  64. cchar_t cch;
  65. wchar_t wch[] = { CP437[cell], L'\0' };
  66. setcchar(&cch, wch, colorAttr(color), colorPair(color), NULL);
  67. mvadd_wch(offsetY + cellY, cellX, &cch);
  68. }
  69. }
  70. }
  71. int main(int argc, char *argv[]) {
  72. if (argc != 4) return EX_USAGE;
  73. FILE *fileA = fopen(argv[1], "r");
  74. if (!fileA) err(EX_NOINPUT, "%s", argv[1]);
  75. FILE *fileB = fopen(argv[2], "r");
  76. if (!fileB) err(EX_NOINPUT, "%s", argv[2]);
  77. FILE *fileC = fopen(argv[3], "w");
  78. if (!fileC) err(EX_CANTCREAT, "%s", argv[3]);
  79. curse();
  80. struct Tile tileA, tileB;
  81. for (;;) {
  82. size_t countA = fread(&tileA, sizeof(tileA), 1, fileA);
  83. if (ferror(fileA)) err(EX_IOERR, "%s", argv[1]);
  84. size_t countB = fread(&tileB, sizeof(tileB), 1, fileB);
  85. if (ferror(fileB)) err(EX_IOERR, "%s", argv[2]);
  86. if (!countA && !countB) break;
  87. if (!countA || !countB) errx(EX_DATAERR, "different size inputs");
  88. const struct Tile *tileC = (tileA.meta.accessTime > tileB.meta.accessTime)
  89. ? &tileA
  90. : &tileB;
  91. if (tileA.meta.modifyTime != tileB.meta.modifyTime) {
  92. drawTile(0, &tileA);
  93. drawTile(CellRows + 1, &tileB);
  94. move(CellRows * 2 + 2, 0);
  95. refresh();
  96. int c;
  97. do { c = getch(); } while (c != 'a' && c != 'b');
  98. tileC = (c == 'a') ? &tileA : &tileB;
  99. }
  100. fwrite(tileC, sizeof(*tileC), 1, fileC);
  101. if (ferror(fileC)) err(EX_IOERR, "%s", argv[3]);
  102. }
  103. endwin();
  104. return EX_OK;
  105. }