drake  1.0.0
src/link.c
Go to the documentation of this file.
00001 /*
00002  Copyright 2015 Nicolas Melot
00003 
00004  This file is part of Drake.
00005 
00006  Drake is free software: you can redistribute it and/or modify
00007  it under the terms of the GNU 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  Drake 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 General Public License for more details.
00015 
00016  You should have received a copy of the GNU General Public License
00017  along with Drake. If not, see <http://www.gnu.org/licenses/>.
00018 
00019 */
00020 
00021 
00022 #include <stdlib.h>
00023 #include <string.h>
00024 
00025 #include <drake/link.h>
00026 #include <drake/platform.h>
00027 
00028 link_tp *
00029 pelib_alloc_struct(link_tp)()
00030 {
00031         return malloc(sizeof(link_tp));
00032 }
00033 
00034 link_tp*
00035 pelib_alloc(link_tp)(void* aux)
00036 {
00037         return pelib_alloc_struct(link_tp)();
00038 }
00039 
00040 int
00041 pelib_init(link_tp)(link_tp* link)
00042 {
00043         *link = NULL;
00044 
00045         return 1;
00046 }
00047 
00048 int
00049 pelib_free_struct(link_tp)(link_tp* link)
00050 {
00051         free(link);
00052 
00053         return 1;
00054 }
00055 
00056 int
00057 pelib_free(link_tp)(link_tp* link)
00058 {
00059         return pelib_free(link_tp)(link);
00060 }
00061 
00062 int
00063 pelib_copy(link_tp)(link_tp src, link_tp* dst)
00064 {
00065         if(dst != NULL)
00066         {
00067                 *dst = src;
00068                 return 1;
00069         }
00070         else
00071         {
00072                 return 0;
00073         }
00074 }
00075 
00076 int
00077 pelib_compare(link_tp)(link_tp l1, link_tp l2)
00078 {
00079         return 0;
00080 }
00081 
00082 FILE*
00083 pelib_printf(link_tp)(FILE* stream, link_tp link)
00084 {
00085         char *str;
00086         str = pelib_string(link_tp)(link);
00087         fprintf(stream, "%s\n", str);
00088 
00089         free(str);
00090 
00091         return stream;
00092 }
00093 
00094 FILE*
00095 pelib_printf_detail(link_tp)(FILE* stream, link_tp link, int level)
00096 {
00097         char *str;
00098         str = pelib_string_detail(link_tp)(link, level);
00099         fprintf(stream, "%s\n", str);
00100 
00101         free(str);
00102 
00103         return stream;
00104 }
00105 
00106 size_t
00107 pelib_fwrite(link_tp)(link_tp link, FILE* file)
00108 {
00109         fprintf(stderr, "[%s:%d] Not implemented\n", __FILE__, __LINE__);
00110         return 0;
00111 }
00112 
00113 size_t
00114 pelib_fread(link_tp)(link_tp* link, FILE* file)
00115 {
00116         fprintf(stderr, "[%s:%d] Not implemented\n", __FILE__, __LINE__);
00117         return 0;
00118 }
00119 
00120 //#define link_str_chk printf("[%s:%s:%d] Hello world!\n", __FILE__, __FUNCTION__, __LINE__);
00121 #define link_str_chk
00122 char*
00123 pelib_string(link_tp)(link_tp link)
00124 {
00125 link_str_chk
00126         return pelib_string_detail(link_tp)(link, 0);
00127 }
00128 
00129 char*
00130 pelib_string_detail(link_tp)(link_tp link, int level)
00131 {
00132         char *task, *str, *cfifo;
00133 
00134 link_str_chk
00135         if(link->cons != NULL)
00136         {
00137 link_str_chk
00138                 task = pelib_string_detail(task_tp)(link->cons, level == 0 ? level : level - 1);
00139 link_str_chk
00140         }
00141         else
00142         {
00143 link_str_chk
00144                 task = malloc(strlen(".") + 1);
00145 link_str_chk
00146                 sprintf(task, "%s", ".");
00147 link_str_chk
00148         }
00149 
00150 link_str_chk
00151         if(level == 0)
00152         {
00153 link_str_chk
00154                 str = malloc(strlen(task) + 3);
00155 link_str_chk
00156                 sprintf(str, "->%s", task);
00157 link_str_chk
00158         }
00159         else
00160         {
00161 link_str_chk
00162                 if(link->buffer != NULL)
00163                 {
00164 link_str_chk
00165 //                      cfifo = pelib_string_detail(cfifo_t(int))(*link->buffer, level - 1);
00166                         cfifo=malloc(sizeof(char) * sizeof(link_tp) * 2);
00167                         sprintf(cfifo, "%p", link->buffer);
00168 link_str_chk
00169                 }
00170                 else
00171                 {
00172 link_str_chk
00173                         cfifo = malloc(sizeof(char) * 2);
00174 link_str_chk
00175                         sprintf(cfifo, ".");
00176 link_str_chk
00177                 }
00178 
00179 link_str_chk
00180                 str = malloc(strlen(cfifo) + strlen(task) + 3 + sizeof(link_tp) * 2 + 2 + 2 + 1 + 1);
00181 link_str_chk
00182                 sprintf(str, "->{%p:%s->%s}", link, cfifo, task);
00183 link_str_chk
00184 
00185                 free(cfifo);
00186 link_str_chk
00187         }
00188 
00189 link_str_chk
00190         free(task);
00191 
00192 link_str_chk
00193         return str;
00194 }
00195 
00196 int
00197 pelib_destroy(link_tp)(link_tp link)
00198 {
00199         return 1;
00200 }
00201 
00202 #define PAIR_KEY_T string
00203 #define PAIR_VALUE_T link_tp
00204 #include <pelib/pair.c>
00205 
00206 #define ITERATOR_T pair_t(string, link_tp)
00207 #include <pelib/iterator.c>
00208 
00209 #define MAP_KEY_T string
00210 #define MAP_VALUE_T link_tp
00211 #include <pelib/map.c>
00212 
00213 #define ARRAY_T link_tp
00214 #include <pelib/array.c>