parrotsay.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <https://www.gnu.org/licenses/>.
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. char* parrot = "\
  17. \\\n\
  18. \\ ▄▄▄▄▄▄▄▄\n\
  19. \\ ▄ ▄ ▄▄ ▄\n\
  20. ▄ ▄  ▄\n\
  21.  ▄   ▄▄▄▄▄    \n\
  22.  ▄     ▄ ▄\n\
  23.  ▄      \n\
  24.    ▄    \n\
  25.    ▄ ▄   \n\
  26.  ▄ ▄ ▄ ▄▄ ▄\n\
  27.  ▄ ▄▄ ▄\n\
  28.   ▄ ▄▄▄▄ ▄▄ ▄\n\
  29.   ▄▄▄▄ ▄▄ \n\
  30. ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀\n\
  31. \n\
  32. \n";
  33. int charcnt(char* s, char c) {
  34. int n = 0;
  35. for(int i = 0; s[i] != 0; i++) {
  36. if(s[i] == c) n++;
  37. }
  38. return n;
  39. }
  40. char* tabstospaces(char* s, int n) {
  41. int tabscount = charcnt(s, '\t');
  42. size_t len = strlen(s);
  43. char *new_s = calloc((1 + len + tabscount * (n - 1)), sizeof(char));
  44. char *new_p = new_s;
  45. for(;*s != 0;s++) {
  46. if(*s == '\t') {
  47. for(int k = 0; k < n; k++) {
  48. *(new_p++) = ' ';
  49. }
  50. } else {
  51. *(new_p++) = *s;
  52. }
  53. }
  54. return new_s;
  55. }
  56. void printline(int length) {
  57. putchar('+');
  58. for(int i = 0; i < length; i++) {
  59. putchar('-');
  60. }
  61. putchar('+');
  62. putchar('\n');
  63. }
  64. int main(int argc, char** argv) {
  65. char** message_rows = malloc(sizeof(char*));
  66. size_t rows = 0;
  67. int buffer_size = 1;
  68. int longest_line_size = 0;
  69. if(argc > 1) {
  70. *message_rows = calloc(1, sizeof(char));
  71. for(int i = 1; i < argc; i++) {
  72. *message_rows = realloc(*message_rows, sizeof(char) * ( strlen(*message_rows)
  73. + strlen(*(argv + i))
  74. + (i == 1 ? 0 : 1)));
  75. if(i != 1) {
  76. *message_rows = strcat(*message_rows, " ");
  77. }
  78. *message_rows = strcat(*message_rows, *(argv + i));
  79. }
  80. rows = 1;
  81. longest_line_size = strlen(*message_rows);
  82. } else {
  83. size_t line_length = 0;
  84. ssize_t nread;
  85. char* line = NULL;
  86. while((nread = getline(&line, &line_length, stdin)) != -1) {
  87. if(rows + 1 >= buffer_size) {
  88. buffer_size *= 2;
  89. message_rows = realloc(message_rows, sizeof(char*) * buffer_size);
  90. }
  91. char *untabbed_line = tabstospaces(line, 4);
  92. size_t untabbed_line_length = strlen(untabbed_line);
  93. message_rows[rows] = calloc(1 + untabbed_line_length, sizeof(char));
  94. strcpy(message_rows[rows], untabbed_line);
  95. message_rows[rows][strcspn(untabbed_line, "\n")] = 0;
  96. rows++;
  97. if(untabbed_line_length > longest_line_size) {
  98. longest_line_size = untabbed_line_length;
  99. }
  100. }
  101. if (line != NULL) free(line);
  102. }
  103. printline(longest_line_size + 2);
  104. for (size_t i = 0; i < rows; i++) {
  105. printf("| %s%*s |\n", message_rows[i], longest_line_size - strlen(message_rows[i]), "");
  106. free(message_rows[i]);
  107. }
  108. printline(longest_line_size + 2);
  109. free(message_rows);
  110. printf("%s", parrot);
  111. return 0;
  112. }