chiark / gitweb /
Add gettext support
[elogind.git] / src / shared / util.h
index bb2c101569f40adf8e001cbb2ed756ad772c496d..45cb09443d62edd896c2c106e4ba6280f9b2ecf9 100644 (file)
 #include <sys/inotify.h>
 
 #if SIZEOF_PID_T == 4
-#  define PID_FMT "%" PRIu32
+#  define PID_PRI PRIi32
 #elif SIZEOF_PID_T == 2
-#  define PID_FMT "%" PRIu16
+#  define PID_PRI PRIi16
 #else
 #  error Unknown pid_t size
 #endif
+#define PID_FMT "%" PID_PRI
 
 #if SIZEOF_UID_T == 4
 #  define UID_FMT "%" PRIu32
@@ -69,7 +70,7 @@
 #endif
 
 #if SIZEOF_TIME_T == 8
-#  define PRI_TIME PRIu64
+#  define PRI_TIME PRIi64
 #elif SIZEOF_TIME_T == 4
 #  define PRI_TIME PRIu32
 #else
@@ -347,26 +348,29 @@ static inline uint32_t random_u32(void) {
 }
 
 /* For basic lookup tables with strictly enumerated entries */
-#define __DEFINE_STRING_TABLE_LOOKUP(name,type,scope)                   \
+#define _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope)          \
         scope const char *name##_to_string(type i) {                    \
                 if (i < 0 || i >= (type) ELEMENTSOF(name##_table))      \
                         return NULL;                                    \
                 return name##_table[i];                                 \
-        }                                                               \
-        scope type name##_from_string(const char *s) {                  \
-                type i;                                                 \
-                if (!s)                                                 \
-                        return (type) -1;                               \
-                for (i = 0; i < (type)ELEMENTSOF(name##_table); i++)    \
-                        if (name##_table[i] &&                          \
-                            streq(name##_table[i], s))                  \
-                                return i;                               \
-                return (type) -1;                                       \
-        }                                                               \
+        }
+
+ssize_t string_table_lookup(const char * const *table, size_t len, const char *key);
+
+#define _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)                                \
+        scope inline type name##_from_string(const char *s) {                                   \
+                return (type)string_table_lookup(name##_table, ELEMENTSOF(name##_table), s);    \
+        }
+
+#define _DEFINE_STRING_TABLE_LOOKUP(name,type,scope)                    \
+        _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,scope)          \
+        _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,scope)        \
         struct __useless_struct_to_allow_trailing_semicolon__
 
-#define DEFINE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,)
-#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) __DEFINE_STRING_TABLE_LOOKUP(name,type,static)
+#define DEFINE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,)
+#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP(name,type) _DEFINE_STRING_TABLE_LOOKUP(name,type,static)
+#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_TO_STRING(name,type,static)
+#define DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(name,type) _DEFINE_STRING_TABLE_LOOKUP_FROM_STRING(name,type,static)
 
 /* For string conversions where numbers are also acceptable */
 #define DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(name,type,max)         \
@@ -380,7 +384,7 @@ static inline uint32_t random_u32(void) {
                         if (!s)                                         \
                                 return log_oom();                       \
                 } else {                                                \
-                        r = asprintf(&s, "%u", i);                      \
+                        r = asprintf(&s, "%i", i);                      \
                         if (r < 0)                                      \
                                 return log_oom();                       \
                 }                                                       \
@@ -472,6 +476,8 @@ cpu_set_t* cpu_set_malloc(unsigned *ncpus);
 int status_vprintf(const char *status, bool ellipse, bool ephemeral, const char *format, va_list ap) _printf_(4,0);
 int status_printf(const char *status, bool ellipse, bool ephemeral, const char *format, ...) _printf_(4,5);
 
+#define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf))
+
 int fd_columns(int fd);
 unsigned columns(void);
 int fd_lines(int fd);
@@ -731,6 +737,8 @@ void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
                  int (*compar) (const void *, const void *, void *),
                  void *arg);
 
+#define _(String) gettext (String)
+void init_gettext(void);
 bool is_locale_utf8(void);
 
 typedef enum DrawSpecialChar {
@@ -849,7 +857,7 @@ static inline unsigned u32ctz(uint32_t n) {
 #endif
 }
 
-static inline int log2i(int x) {
+static inline unsigned log2i(int x) {
         assert(x > 0);
 
         return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
@@ -910,19 +918,19 @@ int unlink_noerrno(const char *path);
                 (void*)memset(_new_, 0, _size_);                        \
         })
 
-#define strappenda(a, ...)                                       \
-        ({                                                       \
-                int _len = strlen(a);                            \
-                unsigned _i;                                     \
-                char *_d_, *_p_;                                 \
-                const char *_appendees_[] = { __VA_ARGS__ };     \
-                for (_i = 0; _i < ELEMENTSOF(_appendees_); _i++) \
-                        _len += strlen(_appendees_[_i]);         \
-                _d_ = alloca(_len + 1);                          \
-                _p_ = stpcpy(_d_, a);                            \
-                for (_i = 0; _i < ELEMENTSOF(_appendees_); _i++) \
-                        _p_ = stpcpy(_p_, _appendees_[_i]);      \
-                _d_;                                             \
+#define strjoina(a, ...)                                                \
+        ({                                                              \
+                const char *_appendees_[] = { a, __VA_ARGS__ };         \
+                char *_d_, *_p_;                                        \
+                int _len_ = 0;                                          \
+                unsigned _i_;                                           \
+                for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
+                        _len_ += strlen(_appendees_[_i_]);              \
+                _p_ = _d_ = alloca(_len_ + 1);                          \
+                for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
+                        _p_ = stpcpy(_p_, _appendees_[_i_]);            \
+                *_p_ = 0;                                               \
+                _d_;                                                    \
         })
 
 #define procfs_file_alloca(pid, field)                                  \