Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

122 wiersze
3.3 KiB

  1. #define new(type) (type*)malloc(sizeof(type))
  2. #define nullptr NULL
  3. #define concat_( a, b) a##b
  4. #define label(prefix, lnum) concat_(prefix,lnum)
  5. #define try \
  6. if (1) \
  7. goto label(body,__LINE__); \
  8. else \
  9. while (1) \
  10. if (1) { \
  11. if(error) return 0; \
  12. break; \
  13. } \
  14. else label(body,__LINE__):
  15. typedef enum { false, true } bool;
  16. int string_equal(char* a, char* b) {
  17. return !strcmp(a, b);
  18. }
  19. // asprintf implementation
  20. int _vscprintf_so(const char * format, va_list pargs) {
  21. int retval;
  22. va_list argcopy;
  23. va_copy(argcopy, pargs);
  24. retval = vsnprintf(nullptr, 0, format, argcopy);
  25. va_end(argcopy);
  26. return retval;
  27. }
  28. int vasprintf(char **strp, const char *fmt, va_list ap) {
  29. int len = _vscprintf_so(fmt, ap);
  30. if (len == -1) return -1;
  31. char *str = malloc((size_t) len + 1);
  32. if (!str) return -1;
  33. int r = vsnprintf(str, len + 1, fmt, ap); /* "secure" version of vsprintf */
  34. if (r == -1) return free(str), -1;
  35. *strp = str;
  36. return r;
  37. }
  38. int asprintf(char *strp[], const char *fmt, ...) {
  39. va_list ap;
  40. va_start(ap, fmt);
  41. int r = vasprintf(strp, fmt, ap);
  42. va_end(ap);
  43. return r;
  44. }
  45. // asprintf implementation end
  46. char* read_entire_file (char* filename) {
  47. char *fileContent = nullptr;
  48. FILE *fp = fopen(filename, "r");
  49. if (fp) {
  50. /* Go to the end of the file. */
  51. if (fseek(fp, 0L, SEEK_END) == 0) {
  52. /* Get the size of the file. */
  53. long bufsize = ftell(fp);
  54. if (bufsize == -1) {
  55. fputs("Empty file", stderr);
  56. goto closeFile;
  57. }
  58. /* Go back to the start of the file. */
  59. if (fseek(fp, 0L, SEEK_SET) != 0) {
  60. fputs("Error reading file", stderr);
  61. goto closeFile;
  62. }
  63. /* Allocate our buffer to that size. */
  64. fileContent = malloc(sizeof(char) * (bufsize) +1);
  65. /* Read the entire file into memory. */
  66. size_t newLen = fread(fileContent, sizeof(char), bufsize, fp);
  67. fileContent[bufsize-1] = '\0';
  68. if ( ferror( fp ) != 0 ) {
  69. fputs("Error reading file", stderr);
  70. }
  71. }
  72. closeFile:
  73. fclose(fp);
  74. }
  75. return fileContent;
  76. /* Don't forget to call free() later! */
  77. }
  78. char* read_line() {
  79. char* line = malloc(100), * linep = line;
  80. size_t lenmax = 100, len = lenmax;
  81. int c;
  82. if(line == NULL)
  83. return NULL;
  84. for(;;) {
  85. c = fgetc(stdin);
  86. if(c == EOF)
  87. break;
  88. if(--len == 0) {
  89. len = lenmax;
  90. char * linen = realloc(linep, lenmax *= 2);
  91. if(linen == NULL) {
  92. free(linep);
  93. return NULL;
  94. }
  95. line = linen + (line - linep);
  96. linep = linen;
  97. }
  98. if((*line++ = c) == '\n')
  99. break;
  100. }
  101. *line--; // we dont want the \n actually
  102. *line = '\0';
  103. return linep;
  104. }