Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
  • e0545167ce4c42ac712e745d4e106e6045795ff7
  • master par défaut protégée
  • dev-deurstann-3
  • dev-deurstann-2
  • dev-kubat
  • dev-deurstann
  • dev-sting
7 résultats

index.html

Blame
  • common.c 3,89 Kio
    #define _POSIX_C_SOURCE 200809L
    
    #include <common/common.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/stat.h>
    
    void
    __not_implemented(const char *func, char *file, int line)
    {
        LOG_ERROR_SCT("GENERAL", "Function %s in file %s at line %d not implemented", func, file, line);
        abort();
    }
    
    inline void
    __unused(void *dummy, ...)
    {
        (void) dummy;
    }
    
    uint32_t
    be_uint32_t(const uint8_t bytes[], size_t n)
    {
        uint32_t res = 0;
        for (size_t i = 0; i < n; i++)
            res = (res << 8u) | bytes[i];
        return res;
    }
    
    uint64_t
    be_uint64_t(const uint8_t bytes[], size_t n)
    {
        uint64_t res = 0;
        for (size_t i = 0; i < n; i++)
            res = (res << 8u) | bytes[i];
        return res;
    }
    
    char *
    trim(char *str, size_t len, char c)
    {
        char *res = (char *) str;
        char *end;
    
        for (; len != 0 && *res == c; ++str, --len)
            continue;
    
        if (*res == 0)
            return res;
    
        end = res + len - sizeof(char);
        len = 0;
    
        for (; res < end && *end == c; --end, ++len)
            continue;
    
        if (len > 0)
            end[1] = 0;
    
        return res;
    }
    
    int
    is_utf8(const char *string)
    {
        if (!string)
            return 1;
    
        const unsigned char *bytes = (const unsigned char *)string;
        while (*bytes) {
            /* ASCII */
            if  (bytes[0] == 0x09 || bytes[0] == 0x0A ||
                 bytes[0] == 0x0D || (0x20 <= bytes[0] && bytes[0] <= 0x7E)) {
                bytes += 1;
                continue;
            }
    
            /* non-overlong 2-byte */
            if ((0xC2 <= bytes[0] && bytes[0] <= 0xDF) &&
                (0x80 <= bytes[1] && bytes[1] <= 0xBF)) {
                bytes += 2;
                continue;
            }
    
            if ( (bytes[0] == 0xE0 &&
                  (0xA0 <= bytes[1] && bytes[1] <= 0xBF)  &&
                  (0x80 <= bytes[2] && bytes[2] <= 0xBF)) ||    /* excluding overlongs */
                 (((0xE1 <= bytes[0] && bytes[0] <= 0xEC) ||
                   bytes[0] == 0xEE || bytes[0] == 0xEF)  &&
                  (0x80 <= bytes[1] && bytes[1] <= 0xBF)  &&
                  (0x80 <= bytes[2] && bytes[2] <= 0xBF)) ||    /* straight 3-byte */
                 (bytes[0] == 0xED &&
                  (0x80 <= bytes[1] && bytes[1] <= 0x9F)  &&
                  (0x80 <= bytes[2] && bytes[2] <= 0xBF))) {    /* excluding surrogates */
                bytes += 3;
                continue;
            }
    
            if ( (bytes[0] == 0xF0  &&                          /* planes 1-3 */
                  (0x90 <= bytes[1] && bytes[1] <= 0xBF)  &&
                  (0x80 <= bytes[2] && bytes[2] <= 0xBF)  &&
                  (0x80 <= bytes[3] && bytes[3] <= 0xBF)) ||
                 ((0xF1 <= bytes[0] && bytes[0] <= 0xF3)  &&
                  (0x80 <= bytes[1] && bytes[1] <= 0xBF)  &&
                  (0x80 <= bytes[2] && bytes[2] <= 0xBF)  &&
                  (0x80 <= bytes[3] && bytes[3] <= 0xBF)) ||    /* planes 4-15 */
                 (bytes[0] == 0xF4 &&
                  (0x80 <= bytes[1] && bytes[1] <= 0x8F)  &&
                  (0x80 <= bytes[2] && bytes[2] <= 0xBF)  &&
                  (0x80 <= bytes[3] && bytes[3] <= 0xBF))) {    /* plane 16 */
                bytes += 4;
                continue;
            }
    
            return 2;
        }
    
        return 0;
    }
    
    int
    get_stdin_line(const char *prompt, char *buf, size_t len)
    {
        if (prompt)
            fprintf(stdout, "%s", prompt);
        if (!fgets(buf, len, stdin))
            return 1;
        buf[len - 1] = 0u;
        return is_utf8(buf);
    }
    
    inline long
    get_mtime(const char *path)
    {
        struct stat statbuf;
        return (stat(path, &statbuf) == -1) ? 0 : statbuf.st_mtime;
    }
    
    void *
    safe_malloc(size_t size)
    {
        void *tmp;
        tmp = malloc(size);
        if (!tmp) {
            LOG_ERROR_SCT("GENERAL", "Out of memory, can't alloc '%ld' Bytes", size);
            exit(1);
        }
        return tmp;
    }
    
    int
    read_self_exe(char *path, size_t len)
    {
        return ! (readlink(SELF_EXECUTABLE_LINUX,   path, len - 1) > 0) ||
               (readlink(SELF_EXECUTABLE_FREEBSD, path, len - 1) > 0) ||
               (readlink(SELF_EXECUTABLE_SOLARIS, path, len - 1) > 0);
    }