Last active
August 29, 2015 14:17
-
-
Save Huholoman/c41b32d4734423453828 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __PROGTEST__ | |
#include <cstdio> | |
#include <cstdlib> | |
#include <cstring> | |
#include <iostream> | |
#include <fstream> | |
#include <stdint.h> | |
using namespace std; | |
#endif /* __PROGTEST__ */ | |
typedef signed long long int mint; | |
class UtoF { | |
public: | |
UtoF( const char * inFile, const char * outFile ); | |
bool doit(); | |
private: | |
const char *inFile, *outFile; | |
ifstream is; | |
ofstream os; | |
bool convert(mint &from, int &bytes); | |
void setBytes(int &bytes, char &byte); | |
}; | |
UtoF::UtoF(const char * inFile, const char * outFile): | |
inFile(inFile), | |
outFile(outFile) | |
{ | |
is.open(inFile, ios::in | ios::binary); | |
if(!is.is_open()) { | |
cerr << "tady" << endl; | |
throw "couldnt open"; | |
} | |
} | |
bool UtoF::doit() | |
{ | |
char c, tmp; | |
mint buffer; | |
int currentByte = 0; | |
int bytes = 0; | |
while (is.get(c)) | |
{ | |
for (int i = 7; i >= 0; i--) | |
buffer |= ((c >> i) & 1) << i; | |
if(currentByte == 0) { | |
for (int i = 7; i >= 0; i--) | |
tmp |= ((buffer >> i) & 1) << i; | |
setBytes(bytes, tmp); | |
} | |
if(currentByte == bytes) { | |
if(bytes == 0) { | |
continue; | |
} | |
convert(buffer, bytes); | |
continue; | |
} | |
currentByte++; | |
} | |
return true; | |
} | |
void UtoF::setBytes(int &bytes, char &byte) | |
{ | |
for (int i = 7; i >= 0; i--) { | |
// if bit is 0 | |
if (((bytes >> i) & 1) == 0) { | |
if(i == 7) { | |
// if this is first iteration | |
// we set number of bits at 1 | |
bytes = 1; | |
} | |
if(bytes == 0) { | |
throw "wrong number of bytes"; | |
} | |
// in all cases 0 means end of byte cfg | |
break; | |
} | |
if(i == 7) { | |
// if bit is 1 and this is first iteration | |
// we set bytes as 0 and continue.. | |
bytes == 0; | |
continue; | |
} | |
// as long as there will be 1 in a row, we will bet increment | |
bytes++; | |
} | |
if(bytes == 1) { | |
byte |= 0 << 1; | |
return; | |
} | |
// cleare config bits | |
for(int i = 0; i < bytes+1; i++) { | |
byte &= ~(1 << i); | |
} | |
} | |
bool UtoF::convert(mint &from, int &bytes) | |
{ | |
for(int currentByte = 2; currentByte < bytes+1; currentByte++) { | |
for (int i = 7; i >= 0; i--) | |
cout << ((from >> i+(8*currentByte)) & 1); | |
} | |
} | |
bool Utf8ToFibonacci ( const char * inFile, const char * outFile ) | |
{ | |
ifstream is; | |
UtoF converter(inFile, outFile); | |
return converter.doit(); | |
} | |
bool FibonacciToUtf8 ( const char * inFile, const char * outFile ) | |
{ | |
// todo | |
} | |
#ifndef __PROGTEST__ | |
int main ( int argc, char * argv [] ) | |
{ | |
char uf[] = "./fib/toFib/test_0.utf8"; | |
char ff[] = "./fib/toFib/test_0.fib"; | |
Utf8ToFibonacci(uf,ff); | |
return 0; | |
} | |
#endif /* __PROGTEST__ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment