Last active
April 22, 2019 09:53
-
-
Save talhaHavadar/b0c52110a9dd26135d479a515456dcf8 to your computer and use it in GitHub Desktop.
Custom Output Stream in C++.
This file contains hidden or 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
// | |
// ostream.cpp | |
// utility | |
// | |
// Created by Talha Havadar on 18.04.2019. | |
// Copyright © 2019 Talha Can Havadar. All rights reserved. | |
// | |
#include "ostream.hpp" | |
#include <iostream> | |
using namespace utility; | |
void utility::char_swap(char* ch1, char* ch2) | |
{ | |
char temp = *ch2; | |
*ch2 = *ch1; | |
*ch1 = temp; | |
} | |
void utility::reverse(char *str, int length) | |
{ | |
int start = 0; | |
int end = length - 1; | |
while (start < end) { | |
utility::char_swap(str + start, str + end); | |
start++; | |
end--; | |
} | |
} | |
OStream::OStream(): basefield(10), padding(PADDING_RESET_VALUE) { } | |
void OStream::setBasefield(int8_t base) | |
{ | |
this->basefield = base; | |
} | |
char* OStream::getBuffer() const | |
{ | |
return const_cast<char*>(this->buffer); | |
} | |
void OStream::convertInt32ToChar(int32_t value, char* const strVal, const int8_t base) const | |
{ | |
int i = 0; | |
bool isNegative = false; | |
if (value == 0) | |
{ | |
strVal[i++] = '0'; | |
strVal[i] = '\0'; | |
return; | |
} | |
if (value < 0 && base == 10) | |
{ | |
value = -value; | |
isNegative = true; | |
} | |
while (value != 0) | |
{ | |
int remainder = value % base; | |
// we need remainder > 9 check for base 16 cases. | |
strVal[i++] = (remainder > 9) ? (remainder - 10) + 'a' : remainder + '0'; | |
value = value/base; | |
} | |
if (base == 16) { | |
strVal[i++] = 'x'; | |
strVal[i++] = '0'; | |
} | |
else if (base == 2) | |
{ | |
strVal[i++] = 'b'; | |
strVal[i++] = '0'; | |
} | |
if (isNegative && base == 10) { | |
strVal[i++] = '-'; | |
} | |
strVal[i] = '\0'; | |
// reverse the string | |
utility::reverse(strVal, i); | |
} | |
void OStream::flushBuffer() | |
{ | |
memset(this->buffer, 0, this->BUFFER_SIZE); | |
} | |
OStream& OStream::operator<<(const int32_t val) | |
{ | |
// TODO: send val using uart. | |
char strVal[100] = {0}; | |
this->convertInt32ToChar(val, strVal, this->basefield); | |
strcat(this->getBuffer(), strVal); | |
return *this; | |
} | |
OStream& OStream::operator<<(const uint32_t val) | |
{ | |
char strVal[100] = {0}; | |
this->convertInt32ToChar(val, strVal, this->basefield); | |
strcat(this->getBuffer(), strVal); | |
return *this; | |
} | |
OStream& OStream::operator<<(const char *val) | |
{ | |
strcat(this->getBuffer(), val); | |
return *this; | |
} | |
OStream& OStream::operator<<(bool val) | |
{ | |
if (val) { | |
strcat(this->getBuffer(), "true"); | |
} else { | |
strcat(this->getBuffer(), "false"); | |
} | |
return *this; | |
} | |
OStream& OStream::operator<<(OStreamModifierFunc func) | |
{ | |
return func(*this); | |
} |
This file contains hidden or 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
// | |
// ostream.hpp | |
// utility | |
// | |
// Created by Talha Havadar on 18.04.2019. | |
// Copyright © 2019 Talha Can Havadar. All rights reserved. | |
// | |
#ifndef ostream_hpp | |
#define ostream_hpp | |
#include <iostream> | |
#include "string.h" | |
namespace utility { | |
class OStream; | |
typedef OStream& (*OStreamModifierFunc)(OStream&); | |
typedef OStream& (*OStreamPadModifierFunc)(OStream&, int); | |
void reverse(char str[], int length); | |
void char_swap(char* ch1, char* ch2); | |
class OStream | |
{ | |
private: | |
static const int BUFFER_SIZE = 1024; | |
static const int PADDING_RESET_VALUE = -1; | |
int8_t basefield; | |
int8_t padding; | |
char buffer[BUFFER_SIZE] = {0}; | |
void convertInt32ToChar(int32_t value, char* const strVal, const int8_t base) const; | |
void applyPadding(const int padding, char* str) const; | |
public: | |
OStream(); | |
void setBasefield(int8_t base); | |
void setPadding(int8_t padding); | |
void resetPadding(); | |
char* getBuffer() const; | |
void flushBuffer(); | |
OStream& operator<<(bool val); | |
OStream& operator<<(const int32_t val); | |
OStream& operator<<(const uint32_t val); | |
OStream& operator<<(const char* val); | |
OStream& operator<<(OStreamModifierFunc func); | |
OStream& operator<<(OStreamPadModifierFunc func); | |
}; | |
static OStream console = OStream(); | |
OStream& hex(OStream& out); | |
inline OStream& hex(OStream& out) | |
{ | |
out.setBasefield(16); | |
return out; | |
}; | |
OStream& dec(OStream& out); | |
inline OStream& dec(OStream& out) | |
{ | |
out.setBasefield(10); | |
return out; | |
}; | |
OStream& endl(OStream& out); | |
inline OStream& endl(OStream& out) | |
{ | |
strcat(out.getBuffer(), "\r\n"); | |
return out; | |
}; | |
OStream& flush(OStream& out); | |
inline OStream& flush(OStream& out) | |
{ | |
// TODO: use uart send function | |
std::cout << out.getBuffer(); | |
out.flushBuffer(); | |
return out; | |
}; | |
OStream& pad(OStream& out, int len); | |
inline OStream& pad(OStream& out, int len) | |
{ | |
out.setPadding(len); | |
return out; | |
} | |
} | |
#endif /* ostream_hpp */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment