/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>

#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#endif

#include <libmemcached/memcached.h>

static void test(memcached_st *cache)
{
    char *vkey = "v", *counterkey = "counter";
    int vval, vvallen, counterval, countervallen;
    char *p;
    memcached_return_t stat;
    /* basic put, get and remove */
    vval = 123;
    stat = memcached_set(cache, vkey, strlen(vkey), (char *)&vval, sizeof(vval), 0, 0);
    if (stat != MEMCACHED_SUCCESS)
    {
        printf("set failed: %s\n", memcached_error(cache));
        exit(1);
    }
    p = memcached_get(cache, vkey, strlen(vkey), &vvallen, NULL, &stat); 
    if (stat != MEMCACHED_SUCCESS)
    {
        printf("get failed: %s\n", memcached_error(cache));
        exit(1);
    }
    vval = *((int *)p);
    printf("%d\n", vval);
    stat = memcached_delete(cache, vkey, strlen(vkey), 0);
    if (stat != MEMCACHED_SUCCESS)
    {
        printf("delete failed: %s\n", memcached_error(cache));
        exit(1);
    }
    p = memcached_get(cache, vkey, strlen(vkey), &vvallen, NULL, &stat); 
    if (stat != MEMCACHED_SUCCESS && stat != MEMCACHED_NOTFOUND)
    {
        printf("get failed: %s\n", memcached_error(cache));
        exit(1);
    }
    if(stat == MEMCACHED_SUCCESS)
    {
        vval = *((int *)p);
        printf("%d\n", vval);
    }
    if(stat == MEMCACHED_NOTFOUND)
    {
        printf("<not found>\n");
    }
    vval = 123;
    stat = memcached_set(cache, vkey, strlen(vkey), (char *)&vval, sizeof(vval), 0, 0);
    if (stat != MEMCACHED_SUCCESS)
    {
        printf("set failed: %s\n", memcached_error(cache));
        exit(1);
    }
    p = memcached_get(cache, vkey, strlen(vkey), &vvallen, NULL, &stat); 
    if (stat != MEMCACHED_SUCCESS)
    {
        printf("get failed: %s\n", memcached_error(cache));
        exit(1);
    }
    vval = *((int *)p);
    printf("%d\n", vval);
    /* persistence */
    p = memcached_get(cache, counterkey, strlen(counterkey), &countervallen, NULL, &stat); 
    if (stat != MEMCACHED_SUCCESS && stat != MEMCACHED_NOTFOUND)
    {
        printf("get failed: %s\n", memcached_error(cache));
        exit(1);
    }
    if(stat == MEMCACHED_SUCCESS)
    {
        counterval = *((int *)p);
    }
    if(stat == MEMCACHED_NOTFOUND)
    {
        counterval = 0;
    }
    counterval++;
    printf("%d\n", counterval);
    stat = memcached_set(cache, counterkey, strlen(counterkey), (char *)&counterval, sizeof(counterval), 0, 0);
    if (stat != MEMCACHED_SUCCESS)
    {
        printf("set failed: %s\n", memcached_error(cache));
        exit(1);
    }
	/* list all not supported */
	/* listener not supported */
}

int main()
{
    const char *cfg = "--SERVER=localhost --BINARY-PROTOCOL";
    memcached_st *cache;
#ifdef WIN32
    WSADATA WSAData;
    WSAStartup(0x0101, &WSAData);
#endif
    cache = memcached(cfg, strlen(cfg));
    test(cache);
    memcached_free(cache);
#ifdef WIN32
    WSACleanup();
#endif
     return 0;
}
