|
gimme-alpha 0.1
|
00001 /* 00002 ** gimme_alpha_types.c 00003 ** 00004 ** Made by (Johnny Q. Hacker) 00005 ** Login <solarion@borkborkbork> 00006 ** 00007 00008 Copyright (C) 2008 Joseph Pingenot 00009 00010 This program is free software: you can redistribute it and/or modify 00011 it under the terms of the GNU Affero General Public License as published by 00012 the Free Software Foundation, either version 3 of the License, or 00013 (at your option) any later version. 00014 00015 This program is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU Affero General Public License for more details. 00019 00020 You should have received a copy of the GNU Affero General Public License 00021 along with this program. If not, see <http://www.gnu.org/licenses/>. 00022 00023 ** Started on Wed Oct 22 17:03:57 2008 Johnny Q. Hacker 00024 ** Last update Sun May 12 01:17:25 2002 Speed Blue 00025 */ 00026 00027 #include <unistd.h> 00028 #include <stdlib.h> 00029 #include <stdio.h> 00030 #include "gimme_alpha_types.h" 00031 00032 ___const char* format_string_for_type_scan(enum data_types t) { 00033 switch(t) { 00034 case INT_TYPE: return "%d"; 00035 case LONG_INT_TYPE: return "%ld"; 00036 case FLOAT_TYPE: return "%f"; 00037 case DOUBLE_TYPE: return "%le"; 00038 case LONG_DOUBLE_TYPE: return "%e"; 00039 case COMPLEX_TYPE: return NULL; 00040 case STRING_TYPE: 00041 default: 00042 return "%s"; 00043 } 00044 } 00045 00046 ___const char* format_string_for_type_print(enum data_types t) { 00047 switch(t) { 00048 case INT_TYPE: return "%d"; 00049 case LONG_INT_TYPE: return "%ld"; 00050 case FLOAT_TYPE: return "%f"; 00051 case DOUBLE_TYPE: return "%G"; 00052 case LONG_DOUBLE_TYPE: return "%lG"; 00053 case COMPLEX_TYPE: return NULL; 00054 case STRING_TYPE: 00055 default: 00056 return "%s"; 00057 } 00058 } 00059 00060 ___malloc void* string_to_data(char* s, enum data_types t) { 00061 if(t == STRING_TYPE) return s; 00062 if(t == COMPLEX_TYPE) return NULL; 00063 char *format = format_string_for_type_scan(t); 00064 if(unlikely(format == NULL)) return NULL; 00065 size_t size; 00066 switch(t) { 00067 case INT_TYPE: size = sizeof(int); break; 00068 case LONG_INT_TYPE: size = sizeof(long int); break; 00069 case FLOAT_TYPE: size = sizeof(float); break; 00070 case DOUBLE_TYPE: size = sizeof(double); break; 00071 case LONG_DOUBLE_TYPE: size = sizeof(long double); break; 00072 default: 00073 return NULL; 00074 } 00075 void* data; 00076 if(unlikely((data = malloc(size)) == NULL)) return NULL; 00077 int n; 00078 if(unlikely((n = sscanf(s, format, data)) != 1)) { 00079 free(data); 00080 return NULL; 00081 } 00082 return data; 00083 } 00084 00085 ___malloc void* string_to_data_x2(char* s1, char* s2, enum data_types t) { 00086 if(t == STRING_TYPE) { 00087 GString *s = g_string_new(s1); 00088 s = g_string_append(s, s2); 00089 char* ess = s->str; 00090 g_string_free(s, FALSE); 00091 return ess; 00092 } 00093 if(t == COMPLEX_TYPE) return NULL; 00094 char *format = format_string_for_type_scan(t); 00095 if(unlikely(format == NULL)) return NULL; 00096 size_t size; 00097 switch(t) { 00098 case INT_TYPE: size = sizeof(int); break; 00099 case LONG_INT_TYPE: size = sizeof(long int); break; 00100 case FLOAT_TYPE: size = sizeof(float); break; 00101 case DOUBLE_TYPE: size = sizeof(double); break; 00102 case LONG_DOUBLE_TYPE: size = sizeof(long double); break; 00103 default: 00104 return NULL; 00105 } 00106 char* data; 00107 if(unlikely((data = malloc(size*2)) == NULL)) return NULL; 00108 int n; 00109 if(unlikely((n = sscanf(s1, format, &data[0])) != 1)) { 00110 free(data); 00111 return NULL; 00112 } 00113 if(unlikely((n = sscanf(s2, format, &data[size])) != 1)) { 00114 free(data); 00115 return NULL; 00116 } 00117 return data; 00118 } 00119 00120 ___malloc void* string_to_data_xN(gchar** s, enum data_types t) { 00121 int N = g_strv_length(s); 00122 if(t == STRING_TYPE) { 00123 GString *gs = g_string_new(s[0]); 00124 for(int i=1; i<N; i++) { 00125 gs = g_string_append(gs, s[i]); 00126 } 00127 char* ess = gs->str; 00128 g_string_free(gs, FALSE); 00129 return ess; 00130 } 00131 if(t == COMPLEX_TYPE) return NULL; 00132 char *format = format_string_for_type_scan(t); 00133 if(unlikely(format == NULL)) return NULL; 00134 size_t size; 00135 switch(t) { 00136 case INT_TYPE: size = sizeof(int); break; 00137 case LONG_INT_TYPE: size = sizeof(long int); break; 00138 case FLOAT_TYPE: size = sizeof(float); break; 00139 case DOUBLE_TYPE: size = sizeof(double); break; 00140 case LONG_DOUBLE_TYPE: size = sizeof(long double); break; 00141 default: 00142 return NULL; 00143 } 00144 char* data; 00145 if(unlikely((data = malloc(size*N)) == NULL)) return NULL; 00146 int n; 00147 for(int i=0; i<N; i++) { 00148 if(unlikely((n = sscanf(s[i], format, &data[size*i])) != 1)) { 00149 free(data); 00150 return NULL; 00151 } 00152 } 00153 return data; 00154 } 00155 00156 ___malloc void* string_to_data_fortran(char* s, enum data_types t) { 00157 if(t == STRING_TYPE) return s; 00158 if(t == COMPLEX_TYPE) return NULL; 00159 char *format = format_string_for_type_scan(t); 00160 if(unlikely(format == NULL)) return NULL; 00161 size_t size; 00162 switch(t) { 00163 case INT_TYPE: size = sizeof(int); break; 00164 case LONG_INT_TYPE: size = sizeof(long int); break; 00165 case FLOAT_TYPE: size = sizeof(float); break; 00166 case DOUBLE_TYPE: size = sizeof(double); break; 00167 case LONG_DOUBLE_TYPE: size = sizeof(long double); break; 00168 default: 00169 return NULL; 00170 } 00171 void* data; 00172 if(unlikely((data = malloc(size)) == NULL)) return NULL; 00173 int n; 00174 if(unlikely((n = sscanf(s, format, data)) != 1)) { 00175 free(data); 00176 return NULL; 00177 } 00178 return data; 00179 } 00180 00181 00182 /*If it's a string, we'll make a copy*/ 00183 ___malloc void* string_to_data_generic(char* s, enum data_types t, enum data_type_string_format_type fmt) { 00184 switch(fmt) { 00185 case STRING_FORMATTED_LIKE_C: 00186 return string_to_data(s, t); 00187 case STRING_FORMATTED_LIKE_FORTRAN: 00188 return string_to_data_fortran(s, t); 00189 default: 00190 return NULL; 00191 } 00192 }