Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- class CharIter {
- public:
- virtual ~CharIter() {}
- virtual char next() = 0;
- };
- class SimpleChain: public CharIter {
- public:
- SimpleChain(const char *str)
- : m_str(str)
- {
- /* пусто */
- }
- virtual char next() {
- char result = *m_str;
- if (result) {
- ++ m_str;
- }
- return result;
- }
- private:
- const char *m_str;
- };
- #define YIELD_RETURN(point, value) \
- m_state = point; \
- return (value); \
- case point:
- class RLEDecode: public CharIter {
- public:
- RLEDecode(CharIter *src)
- : m_src(src)
- , m_state(Start)
- {
- /* пусто */
- }
- virtual char next();
- private:
- CharIter *m_src;
- enum State {
- Start, Normal, Repeat, End
- };
- State m_state;
- char m_current;
- int m_repeat;
- };
- char RLEDecode::next() {
- switch (m_state) {
- case Start:
- while (m_current = m_src->next(), m_current != '\0') {
- if (m_current == '!') {
- m_repeat = (unsigned char) m_src->next();
- m_current = m_src->next();
- while (m_repeat > 0) {
- YIELD_RETURN(Repeat, m_current);
- --m_repeat;
- }
- } else {
- YIELD_RETURN(Normal, m_current);
- }
- }
- while (true) {
- YIELD_RETURN(End, '\0');
- }
- }
- // компилятор ругается
- return '\0';
- }
- class Wrapper: public CharIter {
- public:
- Wrapper(CharIter *src, int width)
- : m_src(src)
- , m_width(width)
- , m_state(Start)
- {
- /* пусто */;
- }
- virtual char next();
- private:
- CharIter *m_src;
- int m_width;
- enum State {
- Start, InLine, NewLine, End
- };
- State m_state;
- char m_current;
- int m_col;
- };
- char Wrapper::next() {
- switch (m_state) {
- case Start:
- m_current = m_src->next();
- while (m_current != '\0') {
- for (
- m_col = 0;
- m_col < m_width && m_current != '\0';
- ++m_col, m_current = m_src->next()
- ) {
- YIELD_RETURN(InLine, m_current);
- }
- YIELD_RETURN(NewLine, '\n');
- }
- while (true) {
- YIELD_RETURN(End, '\0');
- }
- }
- // компилятор ругается
- return '\0';
- }
- int main() {
- const char *string =
- "12345678901234567890"
- "Hello, ten of A: !\12A, one bang: !\1!, 64 of x: !\100x.";
- SimpleChain source(string);
- RLEDecode rle(&source);
- Wrapper wrapper(&rle, 20);
- char current;
- while (current = wrapper.next(), current != '\0') {
- printf("%c", current);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement