Login
f-delta.c at [ee03f7343f]
Login

File f-apps/f-delta.c artifact c338817d2d part of check-in ee03f7343f


/* -*- 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/
  
  *****************************************************************************
   This file houses an application for creating Fossil-format deltas
   from, and applying them to, files.
*/

#include "fossil-scm/fossil-cli.h" /* Fossil App mini-framework */
#include "fossil-scm/fossil-internal.h"
#include <string.h>

static void fcli_local_help(){
  puts("Usage:\n");
  printf("\t%s file1 file2\n", fcli.appName);
  printf("\t%s -a|--apply delta_file fileVersion1\n\n", fcli.appName);

  puts("The first form outputs a fossil-format "
       "delta of two files.");
  puts("The second form applies a delta (the first file) "
       "to the second file.");
  puts("\nAll output goes to stdout.\n");
}

int main(int argc, char * const * argv ){
  int rc = 0;
  char * f1 = NULL;
  char * f2 = NULL;
  char doApply = 0;
  fsl_error err = fsl_error_empty;
  fsl_buffer b1 = fsl_buffer_empty,
    b2 = fsl_buffer_empty,
    d12 = fsl_buffer_empty
    ;
  fcli.appHelp = fcli_local_help;
  rc = fcli_setup(argc, argv);
  if(FSL_RC_BREAK==rc) /* --help */return 0;
  else if(rc) goto end;

  doApply = fcli_flag2("a", "apply", NULL);
  if(fcli_has_unused_flags(0)) goto end;
  f1 = fcli_next_arg(1);
  f2 = fcli_next_arg(1);
  if(!f2){
    rc = fcli_err_set(FSL_RC_MISUSE,
                      "Invalid arguments. Try --help.");
    goto end;
  }

  rc = fsl_buffer_fill_from_filename(&b1, f1);
  if(rc){
    rc = fcli_err_set(rc, "Could not open file: %s\n", f1);
    goto end;
  }
  rc = fsl_buffer_fill_from_filename(&b2, f2);
  if(rc){
    rc = fcli_err_set(rc, "Could not open file: %s\n", f2);
    goto end;
  }
  if(doApply){
    rc = fsl_buffer_delta_apply2(&b2, &b1, &b2, &err);
    if(err.code){
      f_out("DELTA ERROR: #%d (%s): %b\n", err.code,
            fsl_rc_cstr(err.code), &err.msg);
    }
    if(rc) goto end;
    f_out("%b", &b2);
  }else{
    rc = fsl_buffer_delta_create(&b1, &b2, &d12);
    if(rc) goto end;
    assert(!rc);
    if(1){
      /* extra verification */
      rc = fsl_buffer_delta_apply(&b1, &d12, &b1);
      assert(0==fsl_buffer_compare(&b1, &b2));
      if(rc) goto end;
    }
    f_out("%b\n", &d12);
  }
  
  end:
  fsl_error_clear(&err);
  fsl_free(f1);
  fsl_free(f2);
  fsl_buffer_clear(&b1);
  fsl_buffer_clear(&b2);
  fsl_buffer_clear(&d12);
  return (fcli_err_report(0)||rc)
    ? EXIT_FAILURE : EXIT_SUCCESS;
}