너무 괴로워서 다른 문서를 살펴보다가 va_list, va_arg 같은걸 보고, 일은 하기 싫고 코딩을 안하면 불안하고 해서 만들어 봤음... (생산성 0%...?)
GeSHi © 2004, Nigel McNie
-
#include <stdio.h>
-
#include <string.h>
-
#include <stdarg.h>
-
#include <stdlib.h>
-
-
int csharpStyleFormat(const char *format, ...)
-
{
-
int extractIdx = -1;
-
int argArray[256];
-
char buf[1024] = {0,};
-
int offset = 0;
-
-
va_list ap;
-
va_start(ap, format);
-
-
const char *cpst = format;
-
const char *st = format -1;
-
const char *end = NULL;
-
-
while (st = strchr(st+1, '{'), st) {
-
char tmpBuf[256] = {0,};
-
-
end = strchr(st+1, '}');
-
memcpy(tmpBuf, st+1, end-(st+1));
-
int index = atoi(tmpBuf);
-
-
while (index > extractIdx) {
-
argArray[++extractIdx] = va_arg(ap, int);
-
}
-
-
int length = (int)(st - cpst);
-
memcpy(buf+offset, cpst, length);
-
offset += length;
-
offset += sprintf(buf+offset, "%d", argArray[index]);
-
-
cpst = end + 1;
-
}
-
-
int length = strlen(cpst);
-
memcpy(buf+offset, cpst, length);
-
-
va_end(ap);
-
-
printf(buf);
-
return 0;
-
}
-
-
-
int main()
-
{
-
csharpStyleFormat("{2} = {0} + {1}\n", 10, 20, 10+20);
-
return 0;
-
}
Parsed in 0.047 seconds
코드는 이렇다.
csharpStyleFormat("{2} = {0} + {1}\n", 10, 20, 10+20);
라고 코드에 쓰면
30 = 10 + 20
이라고 출력됨.
변수타입은 오로지 int만 되는데 {2:d} 이런식으로 변수형을 지정하게 하면 다른 변수들도 가능할듯.
하지만 귀찮..
코드는 에러처리, 최적화는 생각도 하지 않았음...
아 가슴이 답답해...




![[http]](http://lagoons.net/wiki/imgs/http.png)
