ECOGEN 4.0
Evolutive, Compressible, Open, Genuine, Easy, N-phase
Loading...
Searching...
No Matches
IO.h
Go to the documentation of this file.
1//
2// ,---. ,--, .---. ,--, ,---. .-. .-.
3// | .-' .' .') / .-. ) .' .' | .-' | \| |
4// | `-. | |(_) | | |(_) | | __ | `-. | | |
5// | .-' \ \ | | | | \ \ ( _) | .-' | |\ |
6// | `--. \ `-. \ `-' / \ `-) ) | `--. | | |)|
7// /( __.' \____\ )---' )\____/ /( __.' /( (_)
8// (__) (_) (__) (__) (__)
9// Official webSite: https://code-mphi.github.io/ECOGEN/
10//
11// This file is part of ECOGEN.
12//
13// ECOGEN is the legal property of its developers, whose names
14// are listed in the copyright file included with this source
15// distribution.
16//
17// ECOGEN is free software: you can redistribute it and/or modify
18// it under the terms of the GNU General Public License as published
19// by the Free Software Foundation, either version 3 of the License,
20// or (at your option) any later version.
21//
22// ECOGEN is distributed in the hope that it will be useful,
23// but WITHOUT ANY WARRANTY; without even the implied warranty of
24// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25// GNU General Public License for more details.
26//
27// You should have received a copy of the GNU General Public License
28// along with ECOGEN (file LICENSE).
29// If not, see <http://www.gnu.org/licenses/>.
30
31#ifndef IO_H
32#define IO_H
33
34#include <fstream>
35#include <algorithm>
36#include <sstream>
37
38class IO
39{
40public:
41 IO();
42 virtual ~IO();
43
44 //Templates pour convertir donnees depuis ou vers string
45 //-----------------------------------------------------
46 template <typename T>
47 static std::string toString(const T& i)
48 {
49 std::ostringstream stream;
50 stream << i;
51 return stream.str();
52 }
53
54 template <typename T>
55 static T fromString(const std::string& str)
56 {
57 std::istringstream stream(str);
58 T tmp;
59 stream >> tmp;
60 return tmp;
61 }
62
63 //Templates Format Binaire pour le Legacy VTK
64 //-------------------------------------------
65
66 //Definition de template pour print au format binary
67 template <typename T>
68 static std::ostream& write(std::ostream& fluxSortie, T& value)
69 {
70 //Swap Little <-> Big endian eventuel
71 IO::endswap(&value);
72 return fluxSortie.write(reinterpret_cast<char*>(&value), sizeof(T));
73 }
74
75 // //Definition de template pour read au format binary
76 // template <typename T>
77 // static std::istream& read(std::istream &fluxEntree, T& value)
78 // {
79 // return fluxEntree.read(reinterpret_cast<char*>(&value), sizeof(T));
80 // //Swap Little <-> Big endian eventuel
81 // IO::endswap(&value);
82 // //Bug ici je pense, a voir...
83 // }
84
85 //Templates Format Binaire Base64 pour le XML VTK
86 //-----------------------------------------------
87
88 //Template to add any type of data to a string
89 template <typename T>
90 static void addToTheString(char* chaine, int& taille, T& value)
91 {
92 char* conversionChaine = reinterpret_cast<char*>(&value);
93 for (unsigned int octet = 0; octet < sizeof(value); octet++)
94 { chaine[taille++] = conversionChaine[octet]; }
95 }
96
97 //Definition de template pour print d un number au format binary base64
98 template <typename T>
99 static std::ostream& writeb64(std::ostream& fluxSortie, T& value)
100 {
101 //Swap Little <-> Big endian eventuel ne marche pas ??
102 //IO::endswap(&value);
103 //Encodage Base64
104 char* chaine = reinterpret_cast<char*>(&value);
105 int tailleChaine = sizeof(value);
106 return IO::writeb64Chaine(fluxSortie, chaine, tailleChaine);
107 }
108
109 // //ATTENTION !!!!!!!!!!!Read non Fonctionnelle !!!!!!!!!!!!!!
110 // //Definition de template pour read au format binary base64
111 // template <typename T>
112 // static std::istream& readb64(std::istream &fluxEntree, T& value)
113 // {
114 // return fluxEntree.read(reinterpret_cast<char*>(&value), sizeof(T));
115 // //Swap Little <-> Big endian eventuel
116 // IO::endswap(&value);
117 // };
118 // //ATTENTION !!!!!!!!!!!Read non Fonctionnelle !!!!!!!!!!!!!!
119
120 static std::ostream& writeb64Chaine(std::ostream& fluxSortie, char* chaineAEncoder, int& taille);
121
122 static void copyFile(std::string file, std::string srcFolder, std::string destFolder);
123
124private:
125
126 //Swap Little <-> Big Endian
127 template <typename T>
128 static void endswap(T* objp)
129 {
130 unsigned char* memp = reinterpret_cast<unsigned char*>(objp);
131 std::reverse(memp, memp + sizeof(T));
132 }
133
134};
135
136#endif // IO_H
137
138
139/*
140----------------Mode d emploi binary-----------------
141exemple avec un entier code sur 4 octets
142
143int value = 256 487 423
144Sa representation binary avec les bits de poids fort a gauche est :
1450000 1111 0100 1001 1010 1111 1111 1111
146En hexadecimal cela devient:
147 0 F 4 9 A F F F soit 0xF49AFFF
148Cet entier peut être stocke dans un tableau de 4 caracteres de 1 octet chacun par:
149char* tableau = reinterpret_cast<char* >(value)
150Chaque octet correspond alors a un caractere de la table ASCII (sur 8 bits)
151
152----------BASE64-------------
153
154Base64 : L'objectif est d'encoder chaque groupe de 24 bits successif par une chaîne de 4 caracteres simples.
155Pour passer en Base64 soit sur des multiples de 24 bits (4 x 6bits), on decoupe
156la representation binary en paquet de 6 bits. Dans notre cas, cela devient:
157000011 110100 100110 101111 111111 11
158le dernier etant incomplet on complete avec des 0 :
159000011 110100 100110 101111 111111 110000
160
161On associe ensuite a chaque code binary sur 6 bits un caractere simple parmis les 64 suivants:
162"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
163
164Ainsi, la transcription en Base64 donnera:
165 D 0 m v / w
166
167Tout ceci est stocke a nouveau dans des chaînes de caractere (8 bits par caractere),
168on complete alors par deux bits nuls devant chaque jeu de 6 bits :
16900000011 00110100 00100110 00101111 00111111 00110000
170on complete egalement pour obtenir un multiple de 4 caracteres avec le caractere '=':
171
172Ainsi la transcription final de notre entier en Base64 sera :
173 D0mv/w==
174
175qui aura la representation binary final (Pas sur pour le = si c'est sa representation ASCII ou pas)
17600000011 00110100 00100110 00101111 00111111 00110000 00111101 00111101
177
178------------------------------------------------------
179
180zone de tests inversion b64
181CwsLCwsLCws= //chaine binary 64 d'entiers sur 8 bits
182000010 110000 101100 001011 000010 110000 101100 001011 000010 110000 101100 //Traduction binary
18300001011 00001011 00001011 00001011 00001011 00001011 00001011 00001011 (00) //Regroupement par 8
18411 11 11 11 11 11 11 11 //Traduction entier sur 8 bits
185
186CQkJCQkJCQkJCQkJCQkJCQ==
187000010 010000 100100 001001 000010 010000 100100 001001 000010 010000 100100 001001 000010 010000 100100 001001 000010 010000 100100 001001 000010 010000
18800001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 0000
1899 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
190
191
192*/
Definition IO.h:39
static T fromString(const std::string &str)
Definition IO.h:55
static std::ostream & write(std::ostream &fluxSortie, T &value)
Definition IO.h:68
virtual ~IO()
Definition IO.cpp:40
IO()
Definition IO.cpp:36
static void copyFile(std::string file, std::string srcFolder, std::string destFolder)
Definition IO.cpp:91
static std::ostream & writeb64(std::ostream &fluxSortie, T &value)
Definition IO.h:99
static std::string toString(const T &i)
Definition IO.h:47
static void endswap(T *objp)
Definition IO.h:128
static void addToTheString(char *chaine, int &taille, T &value)
Definition IO.h:90
static std::ostream & writeb64Chaine(std::ostream &fluxSortie, char *chaineAEncoder, int &taille)
Definition IO.cpp:44