A Simple X Image Viewer
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.
 
 
 
 
 
 

116 line
2.5 KiB

  1. /* sxiv: util.c
  2. * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "options.h"
  21. #include "util.h"
  22. #define FNAME_LEN 512
  23. void cleanup();
  24. void* s_malloc(size_t size) {
  25. void *ptr;
  26. if (!(ptr = malloc(size)))
  27. die("could not allocate memory");
  28. return ptr;
  29. }
  30. void* s_realloc(void *ptr, size_t size) {
  31. if (!(ptr = realloc(ptr, size)))
  32. die("could not allocate memory");
  33. return ptr;
  34. }
  35. void warn(const char* fmt, ...) {
  36. va_list args;
  37. if (!fmt || options->quiet)
  38. return;
  39. va_start(args, fmt);
  40. fprintf(stderr, "sxiv: warning: ");
  41. vfprintf(stderr, fmt, args);
  42. fprintf(stderr, "\n");
  43. va_end(args);
  44. }
  45. void die(const char* fmt, ...) {
  46. va_list args;
  47. if (!fmt)
  48. return;
  49. va_start(args, fmt);
  50. fprintf(stderr, "sxiv: error: ");
  51. vfprintf(stderr, fmt, args);
  52. fprintf(stderr, "\n");
  53. va_end(args);
  54. cleanup();
  55. exit(1);
  56. }
  57. void size_readable(float *size, const char **unit) {
  58. const char *units[] = { "", "K", "M", "G" };
  59. int i;
  60. for (i = 0; i < LEN(units) && *size > 1024; ++i)
  61. *size /= 1024;
  62. *unit = units[MIN(i, LEN(units) - 1)];
  63. }
  64. char* readline(FILE *stream) {
  65. size_t len;
  66. char *buf, *s, *end;
  67. if (!stream || feof(stream) || ferror(stream))
  68. return NULL;
  69. len = FNAME_LEN;
  70. s = buf = (char*) s_malloc(len * sizeof(char));
  71. do {
  72. *s = '\0';
  73. fgets(s, len - (s - buf), stream);
  74. if ((end = strchr(s, '\n'))) {
  75. *end = '\0';
  76. } else if (strlen(s) + 1 == len - (s - buf)) {
  77. buf = (char*) s_realloc(buf, 2 * len * sizeof(char));
  78. s = buf + len - 1;
  79. len *= 2;
  80. } else {
  81. s += strlen(s);
  82. }
  83. } while (!end && !feof(stream) && !ferror(stream));
  84. if (ferror(stream)) {
  85. s = NULL;
  86. } else {
  87. s = (char*) s_malloc((strlen(buf) + 1) * sizeof(char));
  88. strcpy(s, buf);
  89. }
  90. free(buf);
  91. return s;
  92. }