ECOGEN 6.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{
40 public:
41 IO();
42 virtual ~IO();
43
44 //Templates pour convertir donnees depuis ou vers string
45 //-----------------------------------------------------
46 template <typename T> static std::string toString(const T& i)
47 {
48 std::ostringstream stream;
49 stream << i;
50 return stream.str();
51 }
52
53 template <typename T> static T fromString(const std::string& str)
54 {
55 std::istringstream stream(str);
56 T tmp;
57 stream >> tmp;
58 return tmp;
59 }
60
61 //Templates Format Binaire pour le Legacy VTK
62 //-------------------------------------------
63
64 //Definition de template pour print au format binary
65 template <typename T> static std::ostream& write(std::ostream& fluxSortie, T& value)
66 {
67 //Swap Little <-> Big endian eventuel
68 IO::endswap(&value);
69 return fluxSortie.write(reinterpret_cast<char*>(&value), sizeof(T));
70 }
71
72 // //Definition de template pour read au format binary
73 // template <typename T>
74 // static std::istream& read(std::istream &fluxEntree, T& value)
75 // {
76 // return fluxEntree.read(reinterpret_cast<char*>(&value), sizeof(T));
77 // //Swap Little <-> Big endian eventuel
78 // IO::endswap(&value);
79 // //Bug ici je pense, a voir...
80 // }
81
82 //Templates Format Binaire Base64 pour le XML VTK
83 //-----------------------------------------------
84
85 //Template to add any type of data to a string
86 template <typename T> static void addToTheString(char* chaine, int& taille, T& value)
87 {
88 char* conversionChaine = reinterpret_cast<char*>(&value);
89 for (unsigned int octet = 0; octet < sizeof(value); octet++) {
90 chaine[taille++] = conversionChaine[octet];
91 }
92 }
93
94 //Definition de template pour print d un number au format binary base64
95 template <typename T> static std::ostream& writeb64(std::ostream& fluxSortie, T& value)
96 {
97 //Swap Little <-> Big endian eventuel ne marche pas ??
98 //IO::endswap(&value);
99 //Encodage Base64
100 char* chaine = reinterpret_cast<char*>(&value);
101 int tailleChaine = sizeof(value);
102 return IO::writeb64Chaine(fluxSortie, chaine, tailleChaine);
103 }
104
105 // //ATTENTION !!!!!!!!!!!Read non Fonctionnelle !!!!!!!!!!!!!!
106 // //Definition de template pour read au format binary base64
107 // template <typename T>
108 // static std::istream& readb64(std::istream &fluxEntree, T& value)
109 // {
110 // return fluxEntree.read(reinterpret_cast<char*>(&value), sizeof(T));
111 // //Swap Little <-> Big endian eventuel
112 // IO::endswap(&value);
113 // };
114 // //ATTENTION !!!!!!!!!!!Read non Fonctionnelle !!!!!!!!!!!!!!
115
116 static std::ostream& writeb64Chaine(std::ostream& fluxSortie, char* chaineAEncoder, int& taille);
117
118 static void copyFile(std::string file, std::string srcFolder, std::string destFolder);
119
120 private:
121 //Swap Little <-> Big Endian
122 template <typename T> static void endswap(T* objp)
123 {
124 unsigned char* memp = reinterpret_cast<unsigned char*>(objp);
125 std::reverse(memp, memp + sizeof(T));
126 }
127};
128
129#endif // IO_H
130
131/*
132----------------Mode d emploi binary-----------------
133exemple avec un entier code sur 4 octets
134
135int value = 256 487 423
136Sa representation binary avec les bits de poids fort a gauche est :
1370000 1111 0100 1001 1010 1111 1111 1111
138En hexadecimal cela devient:
139 0 F 4 9 A F F F soit 0xF49AFFF
140Cet entier peut être stocke dans un tableau de 4 caracteres de 1 octet chacun par:
141char* tableau = reinterpret_cast<char* >(value)
142Chaque octet correspond alors a un caractere de la table ASCII (sur 8 bits)
143
144----------BASE64-------------
145
146Base64 : L'objectif est d'encoder chaque groupe de 24 bits successif par une chaîne de 4 caracteres simples.
147Pour passer en Base64 soit sur des multiples de 24 bits (4 x 6bits), on decoupe
148la representation binary en paquet de 6 bits. Dans notre cas, cela devient:
149000011 110100 100110 101111 111111 11
150le dernier etant incomplet on complete avec des 0 :
151000011 110100 100110 101111 111111 110000
152
153On associe ensuite a chaque code binary sur 6 bits un caractere simple parmis les 64 suivants:
154"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
155
156Ainsi, la transcription en Base64 donnera:
157 D 0 m v / w
158
159Tout ceci est stocke a nouveau dans des chaînes de caractere (8 bits par caractere),
160on complete alors par deux bits nuls devant chaque jeu de 6 bits :
16100000011 00110100 00100110 00101111 00111111 00110000
162on complete egalement pour obtenir un multiple de 4 caracteres avec le caractere '=':
163
164Ainsi la transcription final de notre entier en Base64 sera :
165 D0mv/w==
166
167qui aura la representation binary final (Pas sur pour le = si c'est sa representation ASCII ou pas)
16800000011 00110100 00100110 00101111 00111111 00110000 00111101 00111101
169
170------------------------------------------------------
171
172zone de tests inversion b64
173CwsLCwsLCws= //chaine binary 64 d'entiers sur 8 bits
174000010 110000 101100 001011 000010 110000 101100 001011 000010 110000 101100 //Traduction binary
17500001011 00001011 00001011 00001011 00001011 00001011 00001011 00001011 (00) //Regroupement par 8
17611 11 11 11 11 11 11 11 //Traduction entier sur 8 bits
177
178CQkJCQkJCQkJCQkJCQkJCQ==
179000010 010000 100100 001001 000010 010000 100100 001001 000010 010000 100100 001001 000010 010000 100100 001001 000010 010000 100100 001001 000010 010000
18000001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 00001001 0000
1819 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
182
183
184*/
static T fromString(const std::string &str)
Definition IO.h:53
static std::ostream & write(std::ostream &fluxSortie, T &value)
Definition IO.h:65
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:85
static std::ostream & writeb64(std::ostream &fluxSortie, T &value)
Definition IO.h:95
static std::string toString(const T &i)
Definition IO.h:46
static void endswap(T *objp)
Definition IO.h:122
static void addToTheString(char *chaine, int &taille, T &value)
Definition IO.h:86
static std::ostream & writeb64Chaine(std::ostream &fluxSortie, char *chaineAEncoder, int &taille)
Definition IO.cpp:44