|
funkalicious 0.1
|
00001 /* 00002 ** get_vhartree_slice_args.c 00003 ** 00004 00005 00006 Copyright (C) 2010 Joseph Pingenot 00007 00008 This program is free software: you can redistribute it and/or modify 00009 it under the terms of the GNU Affero General Public License as published by 00010 the Free Software Foundation, either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU Affero General Public License for more details. 00017 00018 You should have received a copy of the GNU Affero General Public License 00019 along with this program. If not, see <http://www.gnu.org/licenses/>. 00020 00021 ** Made by (Johnny Q. Hacker) 00022 ** Login <solarion@borkborkbork> 00023 ** 00024 ** Started on Wed Feb 24 17:04:51 2010 Johnny Q. Hacker 00025 ** Last update Sun May 12 01:17:25 2002 Speed Blue 00026 */ 00027 00028 #include <errno.h> 00029 #include <stdlib.h> 00030 #include <stdio.h> 00031 #include <string.h> 00032 #include <gio/gio.h> 00033 #include <glib-2.0/glib.h> 00034 #include "config.h" 00035 #include "get_vhartree_slice_args.h" 00036 00037 extern void program_print_version(FILE* f); 00038 extern void libdotcode_print_version(FILE* f); 00039 00040 #define GET_VHARTREE_SLICE_ARGS_GERROR_DOMAIN 31900 00041 00042 struct args args = { 00043 .struct_list = NULL, 00044 .xslices = NULL, 00045 .yslices = NULL, 00046 .zslices = NULL, 00047 }; 00048 00049 double* new_double_from_string(const char* s, GError **err) { 00050 double* d = (double*)malloc(sizeof(double)); 00051 if(d == NULL) { 00052 *err = g_error_new(GET_VHARTREE_SLICE_ARGS_GERROR_DOMAIN, 1, "getChainPotential_args.c::new_double_from_string: Error occurred converting string (%s) to double: out of memory/malloc failed", s); 00053 return NULL; 00054 } 00055 char *end; 00056 errno = 0; 00057 *d = strtod(s, &end); 00058 if(((end == s) && (*d == 0)) || (errno != 0)) { 00059 *err = g_error_new(GET_VHARTREE_SLICE_ARGS_GERROR_DOMAIN, 2, "getChainPotential_args.c::new_double_from_string: Error occurred converting string (%s) to double: %s", s, strerror(errno)); 00060 free(d); 00061 return NULL; 00062 } 00063 return d; 00064 } 00065 00066 int* new_int_from_string(const char* s, GError **err) { 00067 int* d = (int*)malloc(sizeof(int)); 00068 if(d == NULL) { 00069 *err = g_error_new(GET_VHARTREE_SLICE_ARGS_GERROR_DOMAIN, 1, "getChainPotential_args.c::new_int_from_string: Error occurred converting string (%s) to int: out of memory/malloc failed", s); 00070 return NULL; 00071 } 00072 char *end; 00073 errno = 0; 00074 *d = strtod(s, &end); 00075 if(((end == s) && (*d == 0)) || (errno != 0)) { 00076 *err = g_error_new(GET_VHARTREE_SLICE_ARGS_GERROR_DOMAIN, 2, "getChainPotential_args.c::new_int_from_string: Error occurred converting string (%s) to int: %s", s, strerror(errno)); 00077 free(d); 00078 return NULL; 00079 } 00080 return d; 00081 } 00082 00083 00084 struct args* process_args(int argc, char **argv, GError **err) { 00085 int errored = 0; 00086 gchar **file_names = NULL; 00087 gboolean print_version = FALSE; 00088 gchar** xslices = NULL; 00089 gchar** yslices = NULL; 00090 gchar** zslices = NULL; 00091 GOptionEntry options[] = { 00092 { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &file_names,}, 00093 { "version", 'v', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_NONE, &print_version, "Print version information about this program", NULL }, 00094 { "xslice", 'X', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_STRING_ARRAY, &(xslices), "x coordinate (in grid sites) along which to output a 2D slice of the total density and the resulting potential", NULL}, 00095 { "yslice", 'Y', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_STRING_ARRAY, &(yslices), "y coordinate (in grid sites) along which to output a 2D slice of the total density and the resulting potential", NULL}, 00096 { "zslice", 'Z', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_STRING_ARRAY, &(zslices), "z coordinate (in grid sites) along which to output a 2D slice of the total density and the resulting potential", NULL}, 00097 {NULL} 00098 }; 00099 GOptionContext *ctx; 00100 ctx = g_option_context_new("- create a dotcode potential for the specified grid of nearest neighbors"); 00101 g_option_context_add_main_entries(ctx, options, "Options"); 00102 00103 g_option_context_parse(ctx, &argc, &argv, NULL); 00104 00105 args.xslices = NULL; 00106 int *d; 00107 GError *e = NULL; 00108 if(xslices != NULL) { 00109 for(int i=0; xslices[i] != NULL; i++) { 00110 if((d=new_int_from_string(xslices[i], &e)) != NULL) { 00111 args.xslices = g_list_append(args.xslices, d); 00112 }else{ 00113 *err = e; 00114 return NULL; 00115 } 00116 } 00117 } 00118 args.yslices = NULL; 00119 e = NULL; 00120 if(yslices != NULL) { 00121 for(int i=0; yslices[i] != NULL; i++) { 00122 if((d=new_int_from_string(yslices[i], &e)) != NULL) { 00123 args.yslices = g_list_append(args.yslices, d); 00124 }else{ 00125 *err = e; 00126 return NULL; 00127 } 00128 } 00129 } 00130 args.zslices = NULL; 00131 e = NULL; 00132 if(zslices != NULL) { 00133 for(int i=0; zslices[i] != NULL; i++) { 00134 if((d=new_int_from_string(zslices[i], &e)) != NULL) { 00135 args.zslices = g_list_append(args.zslices, d); 00136 }else{ 00137 *err = e; 00138 return NULL; 00139 } 00140 } 00141 } 00142 00143 gchar* htext = g_option_context_get_help(ctx, FALSE, NULL); 00144 g_option_context_free(ctx); 00145 00146 args.struct_list = NULL; 00147 if(file_names != NULL) { 00148 for(int i = 0; file_names[i] != NULL; i++) { 00149 args.struct_list = g_list_append(args.struct_list, g_file_new_for_commandline_arg(file_names[i])); 00150 } 00151 } 00152 if(args.struct_list == NULL) { 00153 fprintf(stderr, "ERROR: you MUST give at least one grid to process!\n"); 00154 errored=1; 00155 } 00156 00157 if(errored){ 00158 printf("%s", htext); 00159 printf("\n\n**For version and compilation information, run with --version***"); 00160 *err = g_error_new(GET_VHARTREE_SLICE_ARGS_GERROR_DOMAIN, 7, "\n"); 00161 return NULL; 00162 } 00163 if(print_version){ 00164 program_print_version(stdout); 00165 libdotcode_print_version(stdout); 00166 printf("autotools info:\n\tProject: %s\n\tVersion: %s\n", PACKAGE_NAME, VERSION); 00167 printf("Platform information:\n\tSize of int=%d double=%d\n\tEndianness: %s\n", SIZEOF_INT, SIZEOF_DOUBLE, 00168 #ifdef WORDS_BIGENDIAN 00169 "big" 00170 #else 00171 "little" 00172 #endif 00173 ); 00174 *err = g_error_new(GET_VHARTREE_SLICE_ARGS_GERROR_DOMAIN, 7, "\n"); 00175 return NULL; 00176 } 00177 if(errored){ 00178 return NULL; 00179 } 00180 return &args; 00181 }