Dev Tool Tips/C/C++2013. 8. 16. 16:29

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


typedef struct temporary_number_s
{
    char buffer[32];
    char offset;
    char length;
} temporary_number_t;

char * formatNumber(long long num)
{
    static temporary_number_t source, destination;
    int    is_comma_position=0;
    int commas=0;
    int i=0;

    memset(&destination, 0, sizeof(temporary_number_t));

    /* -999 ~ 999 */
    if (num > -1000 && num < 1000 )
    {
        sprintf(destination.buffer, "%ld", num);
        return destination.buffer;
    }

    memset(&source, 0, sizeof(temporary_number_t));
    sprintf(source.buffer, "%ld", num);
    source.length = strlen(source.buffer); /* source.l is bigger than 3 */

    if(num<0)
        commas=(source.length-2)/3; /* exclude '-' character */
    else
        commas=(source.length-1)/3;

    destination.length = source.length + commas;

    destination.offset= destination.length-1;
    source.offset = source.length-1;

    is_comma_position=0;

    while (source.offset>-1)
    {
        destination.buffer[destination.offset] = source.buffer[source.offset];
        destination.offset--;
        source.offset--;

        is_comma_position++;

        if (is_comma_position == 3 && commas)
        {
            commas--;
            destination.buffer[destination.offset] = ',';
            destination.offset--;
            is_comma_position = 0;
        }
    }
    return destination.buffer;
}

int main(int argc, char **argv)
{
    printf("%s ... %s\n", argv[1], formatNumber(atol(argv[1])));
    return 0;
}

Posted by young.h.rhie
Dev Tool Tips/C/C++2012. 1. 3. 15:23
유용한 코드. 재사용을 위해 복사해둠.

#define BYTE_SWAP_16(n) ((((n) >> 8) & 0xffu) | (((n) & 0xffu) << 8))
#define BYTE_SWAP_32(x)  \
     ((((x) & 0xff000000u) >> 24) | (((x) & 0x00ff0000u) >>  8) |  \
      (((x) & 0x0000ff00u) <<  8) | (((x) & 0x000000ffu) << 24))
Posted by young.h.rhie
Dev Tool Tips/C/C++2011. 12. 1. 00:45
유용한 듯해서 다음에 필요하면 쓰려고 메모해둔다.

#define  IS_STARTS_WITH(haystack, needle)  (strncasecmp((char *)haystack, needle, sizeof(needle)-1)==0)

Posted by young.h.rhie
Dev Tool Tips/C/C++2011. 11. 13. 17:52

main() 함수보다 먼저 시작하고 메인 함수가 끝난 뒤에도 실행되는 함수

/* ---  test.c START  --- */
#include <stdio.h>

void hello(void) __attribute__ ((constructor)) ;
void goodbye(void) __attribute__ ((destructor)) ;

void hello(void)
{
        printf("Hello\n");
}

void goodbye(void)
{
        printf("BYE\n");
}

int main(int argc, char** argv)
{
        printf("==== main === \n");
        return 0;
}
/* ---  test.c END  --- */

$ cc test.c
$ ./a.out
Hello
==== main ===
BYE

Posted by young.h.rhie
Dev Tool Tips/C/C++2011. 5. 13. 16:57
version.h: ./MODEL_NAME
    rm -f $@
    @echo "creating $@ ..."; \
    version=`cat ./MODEL_NAME`; \
    today=`date +"%Y.%m.%d" `; \
    time=`date +"%H:%M:%S" `; \
    echo "#ifndef VERSION_H_INCLUDED" > $@; \
    echo "#define VERSION_H_INCLUDED"  >> $@; \
    echo "#define MY_VERSION \"$$version ($$today, $$time)\"" >> $@; \
    echo "#endif /* VERSION_H_INCLUDED */" >> $@

Posted by young.h.rhie
Dev Tool Tips/C/C++2010. 5. 6. 15:35

함수를 만들 때는 필요할 것 같았는데 막상 어떤 이유에서건 사용하지 않는 파라미터가 생기는 경우가 있다.
물론 함수를 호출하는 쪽에서나 정의한 곳, 선언한 곳 모두 찾아서 지우면 되긴 하지만
다음에도 쓸 가능성이 있고 해서 남겨둘 필요가 있을 때는 다음과 같이 매크로를 사용하면 편리하다.
이렇게 해두면 컴파일러에서 사용되지 않는 파라미터에 대한 경고가 나오는 것도 막을 수 있다.

#define PARAM_UNSED(x) (void)x)

int test_function(int foo)
{
#if 0
  return foo+2;
#else
  PARAM_UNSED(foo);
  return 0;

#endif
}

참고로 Microsoft Visual Studio에서는 UNREFERENCED_PARAMETER라는 매크로로 다음과 같이 winnt.h에 정의되어 있다.

#define UNREFERENCED_PARAMETER(P)               (P)
#define DBG_UNREFERENCED_PARAMETER(P)        (P)
#define DBG_UNREFERENCED_LOCAL_VARIABLE(V)  (V)



Posted by young.h.rhie
Dev Tool Tips/C/C++2010. 4. 5. 15:30
printf 함수 포맷에서

1. %ld  ... long
2. %lu ... unsigned long, 이때 lu 대신 그냥 u라고만 해도 같은 효과임
Posted by young.h.rhie
Dev Tool Tips/C/C++2010. 4. 1. 10:44
최근 KLDP 사이트에서 누군가 올린 질문을 보고 곰곰 생각해본 문제다.

/* test.c */
  1     #include <stdio.h>
  2
  3     int main(int argc, char** argv)
  4     {
  5         int a[10]={0,1,2,3,4,5,6,7,8,9};
  6         printf("   a: %p\n", a);
  7         printf("  &a: %p\n", &a);
  8         printf(" a+1: %p\n", a+1);
  9         printf("&a+1: %p\n", &a+1);
 10        return 0;
 11     }  

$ cc test.c
$ ./a.out
       a: 0x7fff15795fc0
     &a: 0x7fff15795fc0
   a+1: 0x7fff15795fc4
&a+1: 0x7fff15795fe8

감이 오는가?
Posted by young.h.rhie
Dev Tool Tips/C/C++2009. 12. 1. 11:20
여러가지 방법을 생각할 수 있을 것이다.

방법 1. /proc/scsi/usb-storage 아래에 있는 파일의 수를 센다.

방법 2. 다음의 시스템 함수를 사용한다.
FILE *setmntent(const char *filename, const char *type);

struct mntent *getmntent_r(FILE *fp, struct mntent *mntbuf,
char *buf, int buflen);

Posted by young.h.rhie
Dev Tool Tips/C/C++2009. 11. 10. 18:22
정적라이브러리

$ gcc -c test.c
$ ar rc libtest.a test.o

공유라이브러리
$ gcc -fPIC -c test.c
$ gcc -shared -W1,-soname,libtest.so.1 -o libtest.so.1.0.1 test.o
$ cp libtest.so.1.0.1 /usr/local/lib
$ ln -s /usr/local/lib/libtest.so.1.0.1 /usr/local/lib/libtest.so

공유라이버리 사용
$ gcc -o mysoftware mysoftware.c -L/usr/local/lib -l



------ 아래는 공유라이브러리 사용 예 ------

/*-------------------shared_test.h-------------------*/

int lib_test(void);

 

/*-------------------shared_test.c-------------------*/

#include <stdio.h>

#include "shared_test.h"

 

int g_counter=0;

 

int lib_test(void)

{

       int ret=++g_counter;

       return ret;

}

 

/*-------------------test1.c-------------------*/

#include <stdio.h>

#include "shared_test.h"

 

int main(void)

{

       sleep(5);

       while(1)

       {

             printf("TEST1: %d\n", lib_test());

             sleep(5);

       }

       return 0;   

}

 

 

/*-------------------test2.c-------------------*/

#include <stdio.h>

#include "shared_test.h"

 

int main(void)

{

       sleep(5);

       while(1)

       {

             printf("TEST2: %d\n", lib_test());

             sleep(5);

       }

       return 0;   

}

 

------------- MAKEFILE -----------------

all: clean

       mkdir -p obj

       gcc -fPIC -c shared_test.c -o obj/shared_test.o

       gcc -shared -W1,-soname,libtest.so.1 -o obj/libtest.so obj/shared_test.o

       gcc -o obj/test1 test1.c -L./obj -ltest

       gcc -o obj/test2 test2.c -L./obj -ltest

 

clean:

       rm -rf obj/*

 


Posted by young.h.rhie