old-graph: Move graph API into the common graph struct
[tig.git] / include / tig / util.h
blob77f66f834021be9d187e33fccafeff3fe60ccaad
1 /* Copyright (c) 2006-2014 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
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 General Public License for more details.
14 #ifndef TIG_UTIL_H
15 #define TIG_UTIL_H
17 #include "tig/tig.h"
18 #include "tig/types.h"
21 * Error handling.
24 #define STATUS_CODE_INFO(_) \
25 _(CUSTOM_MESSAGE, NULL), \
26 _(NO_OPTION_VALUE, "No option value"), \
27 _(OUT_OF_MEMORY, "Out of memory"), \
28 _(FILE_DOES_NOT_EXIST, "File does not exist"), \
29 _(UNMATCHED_QUOTATION, "Unmatched quotation"), \
31 enum status_code {
32 SUCCESS,
33 #define STATUS_CODE_ENUM(name, msg) ERROR_ ## name
34 STATUS_CODE_INFO(STATUS_CODE_ENUM)
37 const char *get_status_message(enum status_code code);
38 enum status_code error(const char *fmt, ...) PRINTF_LIKE(1, 2);
39 enum status_code success(const char *fmt, ...) PRINTF_LIKE(1, 2);
41 typedef void (*die_fn)(void);
42 extern die_fn die_callback;
43 void TIG_NORETURN die(const char *err, ...) PRINTF_LIKE(1, 2);
44 void warn(const char *msg, ...) PRINTF_LIKE(1, 2);
46 static inline int
47 count_digits(unsigned long i)
49 int digits;
51 if (!i)
52 return 1;
54 for (digits = 0; i; digits++)
55 i /= 10;
56 return digits;
59 static inline int
60 apply_step(double step, int value)
62 if (step >= 1)
63 return (int) step;
64 value *= step + 0.01;
65 return value ? value : 1;
69 * Git data formatters.
72 struct time {
73 time_t sec;
74 int tz;
77 struct ident {
78 const char *name;
79 const char *email;
82 extern const struct ident unknown_ident;
84 int timecmp(const struct time *t1, const struct time *t2);
85 int ident_compare(const struct ident *i1, const struct ident *i2);
87 const char *mkdate(const struct time *time, enum date date);
88 const char *mkfilesize(unsigned long size, enum file_size format);
89 const char *mkauthor(const struct ident *ident, int cols, enum author author);
90 const char *mkmode(mode_t mode);
91 const char *mkstatus(const char status, enum status_label label);
93 #define author_trim(cols) (cols == 0 || cols > 10)
96 * Allocation helper.
99 #define DEFINE_ALLOCATOR(name, type, chunk_size) \
100 static type * \
101 name(type **mem, size_t size, size_t increase) \
103 size_t num_chunks = (size + chunk_size - 1) / chunk_size; \
104 size_t num_chunks_new = (size + increase + chunk_size - 1) / chunk_size;\
105 type *tmp = *mem; \
107 if (mem == NULL || num_chunks != num_chunks_new) { \
108 size_t newsize = num_chunks_new * chunk_size * sizeof(type); \
110 tmp = realloc(tmp, newsize); \
111 if (tmp) { \
112 *mem = tmp; \
113 if (num_chunks_new > num_chunks) { \
114 size_t offset = num_chunks * chunk_size; \
115 size_t oldsize = offset * sizeof(type); \
117 memset(tmp + offset, 0, newsize - oldsize); \
122 return tmp; \
125 #endif
126 /* vim: set ts=8 sw=8 noexpandtab: */