2016년 6월 29일 수요일

indent printf

#define iprintf(fmt, ...) printf("%*s" fmt, 2*depth, "", ##__VA_ARGS__)
int depth;

void print_level()
{
        if (depth > 4)
                return;

        iprintf("level %d\n", depth);

        ++depth;
        print_level();
        --depth;

        iprintf("level %d\n", depth);
}

print_level();

level 0
  level 1
    level 2
      level 3
        level 4
        level 4
      level 3
    level 2
  level 1
level 0

hex dump

#include <stdio.h>
#include <inttypes.h>

inline char nibble(char n)
{
        return n + (n > 9 ? 'a' - 10 : '0');
}

static void dump(const void *data, unsigned len, const char *ind)
{
#define DUMPW  16
#define ZBASE (DUMPW * 3)

        if (!data) return;
        if (!ind) ind = "";

        char buf[DUMPW * 4 + 1];
        const int8_t *d = (const int8_t *)data;
        unsigned lines = (len + DUMPW - 1) / DUMPW;
        unsigned l, p, z;

        for (l = 0; l < lines; ++l) {
                for (p = z = 0; z < DUMPW; ++z) {
                        uint32_t c = *d++;
                        buf[ZBASE + z] = (0x20 <= c && c <= 0x7e) ? c : '.';
                        buf[p++] = nibble((c >> 4) & 0xF);
                        buf[p++] = nibble(c & 0xF);
                        buf[p++] = ' ';
                        if (--len == 0)
                                break;
                }
                //for (; p < ZBASE; ++p) buf[p] = ' ';
                memset(buf + p, ' ', ZBASE - p);
                buf[ZBASE + z + 1] = 0;
                printf("%s%s\n", ind, buf);
        }
}

2016년 6월 14일 화요일

tip: git push

~/.bashrc
--------------------------------------------------------------------------------
alias gerrit_push='git push origin HEAD:refs/for/`git branch | sed -n "/^\*/{s/..//;p}"`'
alias git_push='git push origin `git branch | sed -n "/^\*/{s/..//;p}"`'
--------------------------------------------------------------------------------