Simple Dynamic Strings

SDS 라이브러리
홈페이지: https://github.com/antirez/sds

C용 스트링 처리 라이브러리. Concatenating, Formatting, Trimming strings and getting ranges, Fast number to string operations, String copying, Quoting strings, Tokenization, String joining.. 을 지원

아래는 concat의 예제로 메모리를 자동으로 동적 할당.

#include <sds.h>

void test(char **s)
{
	*s = sdscat(*s, "foo");
	*s = sdscat(*s, "bar");
	*s = sdscat(*s, "123");
}

void main(void)
{
	sds s;
	s = sdsempty();
	s = sdscat(s, "foo");
	s = sdscat(s, "bar");
	s = sdscat(s, "123");
	printf("%s\n", s);
	sdsfree(s);

	s = sdsempty();
	test(&s);
	printf("%s\n", s);
	sdsfree(s);
}

libz를 이용한 스트링 압축하기

easyzlib은 libz 라이브러리를 이용하여 간단하게 스트링을 압축할 수 있는 API를 제공하는 라이브러리이다.

easyzlib는 아래에서 다운로드 가능함.
http://www.firstobject.com/easy-zlib-c++-xml-compression.htm

easyzlib.zip

아래에 간단하게 테스트해본 예제.

#include "easyzlib.h"

void main(void)
{
	char *source = "hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.aaaaaaaaaaaaa";
	char *compressed;
	long int compressedSize;
	compressed = malloc(EZ_COMPRESSMAXDESTLENGTH(strlen(source)));

	ezcompress( compressed, &compressedSize, source, strlen(source) );

	printf("source %d bytes\n", strlen(source));
	printf("compressed %d bytes\n", compressedSize);
	
	char *uncompressed;
	long int uncompressedSize;
	uncompressed = malloc(999999);

	//ezuncompress( uncompressed, &uncompressedSize, 
	//		compressed, compressedSize);
	ezuncompress( uncompressed, &uncompressedSize, 
			compressed, compressedSize);
	*(uncompressed+uncompressedSize) = '\0';
	
	printf("uncompressed %d bytes\n", uncompressedSize);
	printf("compressed \n%s\n", compressed);
	printf("uncompressed \n%s", uncompressed);

	free(compressed);
	free(uncompressed);
}
$ ./test.exe
source 409 bytes
compressed 28 bytes
uncompressed 409 bytes
compressed
x쑰H苽▒W(▒/▒I麒e(;
uncompressed
hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.hello world.aaaaaaaaaaaaa