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}"`'
--------------------------------------------------------------------------------

2016년 5월 31일 화요일

vim: grep 결과 컬러링

set grepprg=grep\ --color=always\ --exclude=tags\ --exclude=.git\ -n\ $*\ /dev/null
" --color=always: grep 결과에 색을 입혀준다.
" --exclude=GLOB: 매치된는 이름의 파일은 검색하지 않음
" :grep abc -wrn .  처럼 사용



참고
http://stackoverflow.com/questions/12764999/colorizing-the-output-of-make-grep-etc-in-vim

2016년 5월 15일 일요일

script: 파일 생성일로 파일명 변경

사진의 경우, 촬영 정보를 사용하여 파일 이름을 바꿔주는 툴이 많다.
동영상은 그런 툴이 없는듯 하다.

파일 생성일로 파일명을 바꿔준다. 
--------------------------------------------------------------------------------
#! /bin/bash

verbose=true
try_count=10

ext_list=(
        #extention, description
        "jpg,JPEG image data"
        "png,PNG image data"
        "mov,ISO Media, Apple QuickTime movie"
        # Add more
)

get_name()
{
        stat "$1" |
        sed -n '/Birth:/{s/^\s*Birth:\s*//;s/\(.\{22\}\).*/\1/;s/:/-/g;s/[ .]/_/g;p}'
}

find_next_name()
{
        for i in `seq 1 $try_count`; do
                [ -f "$1_$i.$2" ] || {
                        echo "${1}_$i"
                        return
                }
        done
}

find_ext()
{
        local EXT="${1##*.}"
        [ -z "$EXT" ] && {
                for i in "${ext_list[@]}"; do
                        desc="${i#*,}"
                        file $1 | grep -qw "$desc" && {
                                EXT="${i%%,*}"; break
                        }
                done
        }
        echo "$EXT"
}

ls -1 |
while read f; do
        [ -f "$f" ] || continue

        name=$(get_name "$f")
        ext=$(find_ext "$f")

        [ "$f" = "$name.$ext" ] && {
                [ $verbose = true ] && echo "'$f' is already changed"
                continue
        }

        [ -f "$name.$ext" ] && name=$(find_next_name "$name" "$ext")

        if [ -z "$name" ]; then
                echo "$f: too many files with same name. Use larger try_count"
        else
                [ $verbose = true ] && echo "mv '$f' '$name.$ext'"
                mv "$f" "$name.$ext"
        fi
done
--------------------------------------------------------------------------------

2016년 5월 12일 목요일

vim: 명령을 여러 창에 한 번에 적용하기

모든 버퍼에 적용
:bufdo %s/aaa/bbb/g | w

모든 윈도우에 적용
:windo set scb

--------------------------------------------------------------------------------
주의: bufdo의  예와 같이 내용을 수정/저장 하려는 경우
       현재 버퍼에 수정사항이 있으면 실행이 안되므로
       저장 후 실행해야 한다.
--------------------------------------------------------------------------------

2016년 4월 20일 수요일

script: oneline

이름: oneline

코드:
--------------------------------------------------------------------------------
#! /bin/bash

SEP=" "

help()
{
        echo "Joins multiple lines to one line"
        echo "usage: ${0##*/} [-d DELIMETER]"
        echo
        exit
}

while getopts "d:" opt; do
        case $opt in
                d) SEP="$OPTARG";;
                *) help;;
        esac
done

sed -n "H;\${x;s/^\n*//;s/\n*$//;s/\n/$SEP/g;p}"
--------------------------------------------------------------------------------