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