pelib
2.0.0
|
00001 /* 00002 * integer.c 00003 * 00004 * Created on: 20 Feb 2012 00005 * Copyright 2012 Nicolas Melot 00006 * 00007 * This file is part of pelib. 00008 * 00009 * pelib is free software: you can redistribute it and/or modify 00010 * it under the terms of the GNU 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 * pelib 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 General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with pelib. If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 */ 00023 00024 #include <stdlib.h> 00025 #include <math.h> 00026 00027 #include <pelib/integer.h> 00028 00029 int 00030 pelib_copy(int)(int s1, int * s2) 00031 { 00032 *s2 = s1; 00033 00034 return 1; 00035 } 00036 00037 int 00038 pelib_init(int)(int *val) 00039 { 00040 *val = 0; 00041 00042 return 1; 00043 } 00044 00045 int 00046 pelib_destroy(int)(int val) 00047 { 00048 /* Do nothing */ 00049 return 1; 00050 } 00051 00052 int 00053 pelib_compare(int)(int a, int b) 00054 { 00055 return a - b; 00056 } 00057 00058 char* 00059 pelib_string(int)(int a) 00060 { 00061 size_t size; 00062 char* str; 00063 00064 size = (int)(a > 0 ? log10(a) : 1); 00065 str = malloc(sizeof(char) * (size + 2)); 00066 sprintf(str, "%i", a); 00067 00068 return str; 00069 } 00070 00071 char* 00072 pelib_string_detail(int)(int a, int level) 00073 { 00074 return pelib_string(int)(a); 00075 } 00076 00077 size_t 00078 pelib_fread(int)(int* buffer, FILE* stream) 00079 { 00080 size_t total; 00081 int num; 00082 int read, has_more; 00083 00084 read = 0; 00085 has_more = 1; 00086 num = 0; 00087 total = 0; 00088 00089 while (read != ' ') 00090 { 00091 if ((has_more = fread(&read, sizeof(char), 1, stream)) == 0 || read == ' ') 00092 { 00093 break; 00094 } 00095 00096 total += has_more; 00097 num = num * 10 + read - '0'; 00098 } 00099 00100 *buffer = num; 00101 return total; 00102 } 00103 00104 FILE* 00105 pelib_printf(int)(FILE* stream, int a) 00106 { 00107 char *str = pelib_string(int)(a); 00108 fprintf(stream, "%s\n", str); 00109 free(str); 00110 return stream; 00111 } 00112 00113 // Now include the generic array implementation 00114 #define ARRAY_T int 00115 #include "pelib/array.c" 00116 00117 // Now include the generic set implementation 00118 #define SET_T int 00119 #include "pelib/set.c" 00120 00121 // Now include the generic set implementation 00122 #define PAIR_KEY_T int 00123 #define PAIR_VALUE_T int 00124 #include "pelib/pair.c" 00125 00126 #define ITERATOR_T int 00127 #include "pelib/iterator.c" 00128 00129 #define ITERATOR_T pair_t(int, int) 00130 #include "pelib/iterator.c" 00131 00132 // Now include the generic set implementation 00133 #define MAP_KEY_T int 00134 #define MAP_VALUE_T int 00135 #include "pelib/map.c" 00136 00137 // Now include the generic set implementation 00138 #define SET_T pair_t(int, int) 00139 #include "pelib/set.c" 00140 00141 // Now include the generic fifo implementation 00142 #define CFIFO_T int 00143 #include "pelib/fifo.c" 00144 00145 // Now include the generic fifo implementation 00146 #define CFIFO_ARRAY_T int 00147 #include "pelib/fifo_array.c"