k-dot-p 0.1

oneoff_1d_argprocess.c++

Go to the documentation of this file.
00001 /*
00002  *oneoff_1d_argprocess: process args for oneoff_1d
00003 
00004     Copyright (C) 2010 Joseph Pingenot
00005 
00006     This program is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU Affero General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU Affero General Public License for more details.
00015 
00016     You should have received a copy of the GNU Affero General Public License
00017     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 */
00020 
00021 #include "oneoff_1d_argprocess.h++"
00022 
00023 #include <stdio.h>
00024 #include <glib.h>
00025 #include <gio/gio.h>
00026 #include <gio/gunixinputstream.h>
00027 #include <stdlib.h>
00028 #include "kdotp/makestruct_argprocess.h++"
00029 #include <libmodelxx/function_specification.h++>
00030 #include "config.h"
00031 
00032 using namespace kdotp::libmodelxx::postprocessing;
00033 
00034 extern "C" {
00035   void program_print_version(FILE* f);
00036   void libkdotp_print_version(FILE* f);
00037   void libmodelxx_print_version(FILE* f);
00038   void liblapackwrap_print_version(FILE* f);
00039 }
00040 
00041 void print_version_info(void) {
00042   program_print_version(stdout);
00043   libmodelxx_print_version(stdout);
00044   libkdotp_print_version(stdout);
00045   liblapackwrap_print_version(stdout);
00046   printf("autotools info:\n\tProject: %s\n\tVersion: %s\n", PACKAGE_NAME, VERSION);
00047 }
00048 
00049 struct template_parameter_range* new_template_parameter_range(char* name, double from, double to, double step) {
00050   struct template_parameter_range *tpr = (struct template_parameter_range*)malloc(sizeof(template_parameter_range));
00051   if(tpr == NULL) return NULL;
00052   tpr->symbol = name;
00053   tpr->from = from;
00054   tpr->to = to;
00055   tpr->stepsize = step;
00056   return tpr;
00057 }
00058 
00059 int process_args(int argc, char** argv, struct args* args) {
00060   int errored = 0;
00061   gchar **file_names = NULL;
00062   gboolean print_version = FALSE;
00063   gchar** parameters=NULL;
00064   args->matdb = (char*)"/home/solarion/dist/share/materials-databases/matdb.txt";
00065   GOptionEntry options[] = {
00066     { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &file_names,},
00067     { "version", 'v', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_NONE, &print_version, "Print version information about this program", NULL },
00068     { "matdb", 'm', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_STRING, &(args->matdb), "Use an alternate materials database file.", NULL },
00069     {"parameter", 'p', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_STRING_ARRAY, &parameters, "Provide a template parameter argument, in the form param,value (single value) or param,start,stop,step (sequence)\n"},
00070     {NULL}
00071   };
00072   GOptionContext *ctx;
00073   ctx = g_option_context_new("- sets up a structure for k-dot-p calculations");
00074   g_option_context_add_main_entries(ctx, options, "Options");
00075 
00076   g_option_context_parse(ctx, &argc, &argv, NULL);
00077 
00078   gchar* htext = g_option_context_get_help(ctx, FALSE, NULL);
00079   g_option_context_free(ctx);
00080 
00081   args->filestreams = NULL;
00082   GFileInputStream *instream;
00083   GError *gerr = NULL;
00084   if(file_names != NULL) {
00085     for(int i = 0; file_names[i] != NULL; i++) {
00086       struct file_info *fi = (struct file_info*)malloc(sizeof(struct file_info));
00087       if(fi == NULL) {
00088   fprintf(stderr, "ERROR: unable to allocate input file info struct!\n");
00089   return 500;
00090       }
00091       if((file_names[i][0] == '-') && (file_names[i][1] == '\0')) {
00092   fi->instream = (GInputStream*)g_unix_input_stream_new(0, FALSE);
00093   fi->filename = g_string_new("-");
00094       }else{
00095   GFile *f = g_file_new_for_commandline_arg(file_names[i]);
00096   gerr = NULL;
00097   instream = g_file_read(f, NULL, &gerr);
00098   if(gerr != NULL) {
00099     fprintf(stderr, "makestruct: ERROR reading in structure file (%s): %s", g_file_get_uri(f), gerr->message);
00100     return 100;
00101   }
00102   g_object_unref(f);
00103   fi->instream = (GInputStream*)instream;
00104   fi->filename = g_string_new(file_names[i]);
00105       }
00106       args->filestreams = g_list_append(args->filestreams, fi);
00107     }
00108   }
00109   if(args->filestreams == NULL) {
00110     fprintf(stderr, "ERROR: you MUST give at least one file to process!\n");
00111     errored=1;
00112   }
00113 
00114   char** tparam_entry;
00115   args->template_parameters = NULL;
00116   if(parameters != NULL) {
00117     for(unsigned i=0; parameters[i] != NULL; i++) {
00118       tparam_entry = g_strsplit(parameters[i], ",", -1);
00119       int len=g_strv_length(tparam_entry);
00120       struct template_parameter_range* tpr;
00121       if(len == 2) {
00122   /*parameter,value form*/
00123   tpr = new_template_parameter_range(tparam_entry[0], strtod(tparam_entry[1], NULL), strtod(tparam_entry[1], NULL), strtod(tparam_entry[1], NULL));
00124       }else if(len == 4) {
00125   tpr = new_template_parameter_range(tparam_entry[0], strtod(tparam_entry[1], NULL), strtod(tparam_entry[2], NULL), strtod(tparam_entry[3], NULL));
00126       }else{
00127   fprintf(stderr, "ERROR: invalid parameteter entry: incorrect number of parameter range arguments (got %d; expected 2 or 4; should be either parameter,value or parameter,start,stop,step)", len);
00128   return 15;
00129       }
00130       if(tpr == NULL) {
00131   fprintf(stderr, "ERROR: unable to allocate space for new template parameter range (parameter is %s)\n", tparam_entry[0]);
00132   return 16;
00133       }
00134       args->template_parameters = g_list_append(args->template_parameters, tpr);
00135     }
00136   }
00137 
00138   if(print_version){
00139     print_version_info();
00140     return 1;
00141   }
00142   if(errored){
00143     printf("%s", htext);
00144     printf("\n\n**For version and compilation information, run with --version***\n");
00145     return 3;
00146   }
00147   return 0;
00148 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines