/*
    Copyright © 2010 Harald Sitter <apachelogger@ubuntu.com>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of
    the License or (at your option) version 3 or any later version
    accepted by the membership of KDE e.V. (or its successor approved
    by the membership of KDE e.V.), which shall act as a proxy
    defined in Section 14 of version 3 of the license.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif

// Steve1: 01101000 01101001 00100000 01110100 01101000 01100101 01110010 01100101

int isBinaryOctet(char *string)
{
    int length = strlen(string);
    if (length > 7) {
        length = 7;
    } else if (length < 7) {
        return FALSE;
    }
    int i = 0;
    for (; i < length; ++i) {
        if (string[i] == '1' || string[i] == '0') {
            continue;
        }
        return FALSE;
    }
    return TRUE;
}

int main(int argc, const char* argv[])
{
    char *string = (char*)malloc(sizeof(char));
    char caracter = 0;
    int length = 0;
    while ((caracter = fgetc(stdin)) != EOF) {
        string = (char*)realloc(string, sizeof(char) * (length + 2));
        string[length++] = caracter;
        string[length] = '\0';

        if (caracter == '\n') {

            int i = 0;
            while (i < length) {
                // Just print stuff that is not part of a binary sequence
                if ((string[i] != '1' && string[i] != '0') || !isBinaryOctet(&string[i])) {
                    putchar(string[i]);
                    ++i;
                    continue;
                }

                // Decode if we have a binary sequence at hand
                int c = 0;
                int n = 7;
                for (; n >= 0; --n) {
                    int tmp = 0;
                    if (string[i] == '1') tmp = 1;
                    tmp <<= n;
                    c |= tmp;
                    ++i;
                }

                // Skip additional whitespaces - if any
                while (i < length && string[i] == ' ') ++i;
                putchar((char)c);
            }

            putchar('\n');

            free(string);
            string = (char*)malloc(sizeof(char));
            length = 0;
        }
    }
}

