Login
Artifact [340c089035]
Login

Artifact 340c08903533393a9fdade43e3ee6c8e1cea8ac0:


/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 
/* vim: set ts=2 et sw=2 tw=80: */
/*
** Copyright (c) 2013 D. Richard Hipp
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
**
** This program is distributed in the hope that it will be useful,
** but without any warranty; without even the implied warranty of
** merchantability or fitness for a particular purpose.
**
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*******************************************************************************
**
*/

/* Force assert() to always work... */
#if defined(NDEBUG)
#undef NDEBUG
#define DEBUG 1
#endif

#include <assert.h>
#include <stdlib.h> /* atexit() */
#include <string.h> /* strlen() */
#include "fossil/fossil2.h"

#define MARKER(pfexp)                                               \
  do{ printf("MARKER: %s:%d:%s():\t",__FILE__,__LINE__,__func__);   \
    printf pfexp;                                                   \
  } while(0)

#define VERBOSE(pfexp)                          \
  if(App.verbose) do { \
    printf("VERBOSE: %s:%d:%s():\t",__FILE__,__LINE__,__func__);   \
    printf pfexp;                                                   \
  } while(0)

static struct _App {
  fsl_ctx * f;
  char const * name;
  char const * dbFile;
  char verbose;
} App = {
NULL/*f*/,
NULL/*name*/,
NULL/*dbFile*/,
0/*verbose*/
};


static int test_buffer_0(){
  fsl_buffer buf = fsl_buffer_empty;
  int rc;
  fsl_ctx * f = App.f;
  fsl_size_t sz, szOrig;
  char const * infile = 0
    ? __FILE__
    : "fsl_buffer.c";
  assert(f);
  rc = fsl_buffer_fill_from_filename(&buf, infile);
  assert(!rc);
  sz = szOrig = buf.used;
  rc = fsl_buffer_compress( &buf, &buf );
  assert(!rc);
  assert(buf.used < sz);
  MARKER(("Compressed [%s]. Size: %"FSL_SIZE_T_PFMT
          " => %"FSL_SIZE_T_PFMT"\n", infile, szOrig, buf.used));
  sz = buf.used;
  rc = fsl_buffer_uncompress(&buf, &buf);
  assert(!rc);
  MARKER(("Uncompressed [%s]. Size: %"FSL_SIZE_T_PFMT
          " => %"FSL_SIZE_T_PFMT"\n", infile, sz, buf.used));
  assert(szOrig == buf.used);
  rc = fsl_buffer_reserve(&buf, 0);
  assert(!rc);
  return rc;  
}

static int test0(){
  fsl_ctx * f = NULL;
  int rc;
#if 0
  fsl_init_param init = fsl_init_param_empty;
  init.output = fsl_outputer_FILE;
  init.output.state.state = stdout;
  init.allocator = fsl_allocator_stdalloc;
  rc = fsl_init( &f, &init );
#elif 0
  fsl_init_param init = fsl_init_param_default;
  rc = fsl_init( &f, &init );
#else
  rc = fsl_init( &f, NULL );
#endif
  assert(!rc);
  App.f = f;
  VERBOSE(("Initialized fsl @%p\n", (void const *)f));

  if(App.dbFile){
    VERBOSE(("Trying to open db file [%s]...\n", App.dbFile));
    rc = fsl_repo_open_db( f, App.dbFile );
    if(rc) return rc;
    VERBOSE(("Opened db file [%s]\n",
             fsl_buffer_cstr(&f->dbRepo.filename)));
  }
  return 0;
}

static void my_atexit(){
  if(App.f){
    int rc;
    VERBOSE(("Finalizing fsl @%p\n", (void const *)App.f));
    rc = fsl_finalize( App.f );
    assert(!rc);
    App.f = 0;
  }
}

static int process_argv( int argc, char * const * argv ){
  int i;
  for( i = 1; i < argc; ++i ){
    char const * arg = argv[i];
#define ARG(F) if(0==strcmp(F,arg))
    ARG("-r") {
      App.dbFile = argv[++i];
      continue;
    }
    ARG("-v") {
      App.verbose = 1;
      continue;
    }
    else if('-' == *arg){
      MARKER(("Unknown flag: %s\n", arg));
      return FSL_RC_MISUSE;
    }
#undef ARG
  }
  return 0;
}

int main(int argc, char * const * argv ){
  int rc;
  rc = process_argv( argc, argv );
  if(rc) goto end;
  atexit( my_atexit );
  rc = test0();
  if(!rc) rc = test_buffer_0();

  end:
  MARKER(("Done! rc=%d (%s)\n", rc, fsl_rc_cstr(rc)));
  if(rc && App.f){
    char const * msg = NULL;
    int errRc = fsl_err_get( App.f, &msg, NULL );
    if(msg){
      MARKER(("Fossil says: error code #%d (%s): %s\n",
              errRc, fsl_rc_cstr(errRc), msg));
    }
  }
  return rc;
}

#undef MARKER
#undef VERBOSE