rusle2_from_ssurgo.cc [tools/Rusle2SoilsXMLCreator/pg/rusle2_from_ssurgo_build_newbackup] Revision:   Date:
/* 
   rusle2_from_ssurgo - Process SSURGO v2.2 into database of soil string data files and xml.

   2011 Eric M. Hudish

   This program is free software. It was developed in cooperation between ZedX, Inc.
   and The National Resource Conservation Service (NRCS).

   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.
*/

#include "libintl.h"
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <getopt.h>
#include <iostream>
#include <fstream>
#include <string>
#include "system.h"
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
#include <cppdb/frontend.h>
#include <iostream>
#include <ctime>

#include "rusle2_from_ssurgo.h"

using namespace std;

#define EXIT_FAILURE 1

extern "C" {
  char *xstrdup (char *p);
}

static void usage (int status);

/* The name the program was run with, stripped of any leading path. */
char *program_name;

/* Option flags and variables */

unsigned short VERBOSE;
char *oname = "stdout";					/* --output */
FILE *ofile;
char *desired_directory = "soils\\";	/* --directory */
char *areasymbol;						/* --areasymbol */
char *pgservice = "ssurgo";				/* --pgservice */
int want_dry_run;						/* --dry-run */

static struct option const long_options[] =
{
  {"output", required_argument, 0, 'o'},
  {"verbose", no_argument, 0, 'v'},
  {"dry-run", no_argument, 0, 18},
  {"areasymbol", required_argument, 0, 'a'},
  {"directory", required_argument, 0, 'd'},
  {"pgservice", required_argument, 0, 'p'},
  {"help", no_argument, 0, 'h'},
  {"version", no_argument, 0, 'V'},
  {NULL, 0, NULL, 0}
};



static int decode_switches (int argc, char **argv);
static void test();

int
main (int argc, char **argv)
{
  /*
  * this initialize the library and check potential ABI mismatches
  * between the version it was compiled for and the actual shared
  * library used.
  */
  LIBXML_TEST_VERSION

  int i;

  cout << "Hello World" << endl;
  test();
  exit(-1);

  program_name = argv[0];

  i = decode_switches (argc, argv);

  /* do the work */
  if( areasymbol == NULL ){
	cerr << "You must enter a valid Area Symbol." << endl;
	 usage(0);
  }

  /* Ensure we have a directory */
  if( desired_directory == NULL ){
	cerr << "Directory cannot be NULL." << endl;
	usage(EXIT_FAILURE);
  }

  /* Ensure we have a pg_service */
  if( pgservice == NULL ){
	cerr << "PG_SERVICE cannot be NULL." << endl;
	usage(EXIT_FAILURE);
  }
	
  /* process our ssurgo!!! */
  int numProcessed = process_ssurgo2ss ( desired_directory, pgservice, areasymbol );

  exit (0);
}

/* Set all the option flags according to the switches specified.
   Return the index of the first non-option argument.  */

static int
decode_switches (int argc, char **argv)
{
  int c;

  ofile = stdout;

  while ((c = getopt_long (argc, argv, 
			   "v"	/* verbose */
			   "o:"	/* output */
			   "a:" /* areasymbol */
		  	   "d:" /* directory */
			   "p:" /* pgservice */
			   "h"	/* help */
			   "V",	/* version */
			   long_options, (int *) 0)) != EOF)
    {
      switch (c)
	{
	case 'o':		/* --output */
	  oname = xstrdup(optarg);
	  ofile = fopen(oname, "w");
	  if (!ofile)
	    {
	      fprintf(stderr, "cannot open %s for writing", optarg);
	      exit(1);
	    }
	  break;
	case 'v':
		VERBOSE++;
		break;
	case 18:		/* --dry-run */
	  want_dry_run = 1;
	  break;
	case 'a':		/* --areasymbol */
	  areasymbol = xstrdup(optarg);
	  break;
	case 'd':		/* --directory */
	  desired_directory = xstrdup(optarg);
	  break;
	case 'p':		/* --pgservice */
	  pgservice = xstrdup(optarg);
	  break;
	case 'V':
	  printf ("rusle2_from_ssurgo %s\n", VERSION);
	  exit (0);

	case 'h':
	  usage (0);

	default:
	  usage (EXIT_FAILURE);
	}
    }

  return optind;
}


static void
usage (int status)
{
  printf (_("%s - Process SSURGO v2.2 into database of soil string data files and xml.\n"), program_name);
  printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
  printf (_("Options:\n"
"  -o, --output NAME          send output to NAME instead of standard output\n"
"  --dry-run                  take no real actions\n"
"  --verbose                  print more information\n"
"  --directory NAME           use specified directory as prefix in soil string (default is soils\\)\n"
"  --areasymbol NAME          create soil string data for areasymbol NAME\n"
"  --pgservice NAME           use NAME as pgservice when connecting to database \n"
"  -h, --help                 display this help and exit\n"
"  -V, --version              output version information and exit\n"
"\n"
"  Output is:\n"
"  areaSYM|muSYM|muKEY|r2_path|r2_name\n\n"
));
  exit (status);
}

static void test()
{

        try {
                cppdb::session sql("odbc:DSN=csipmssql;UID=sa;PWD=csurams#1");
                
                cppdb::result res = sql << "SELECT areatypename, areasymbol, areaname FROM ssurgo.legend";

                while(res.next()) {
                        std::string areatypename;
                        std::string areasymbol;
                        std::string areaname;
                        res >> areatypename >> areasymbol >> areaname;
                        cout <<areatypename << ' '<<areasymbol <<' '<<areaname<< endl;
                }

                //res = sql << "SELECT n,f FROM test WHERE id=?" << 1 << cppdb::row;
                //if(!res.empty()) {
                //        int n = res.get<int>("n");
                //        double f=res.get<double>(1);
                //        std::cout << "The values are " << n <<" " << f << std::endl;
                //}
        }
        catch(std::exception const &e) {
                cerr << "ERROR: " << e.what() << endl;
        }
}