summaryrefslogtreecommitdiffstats
path: root/labb6/src/huffmanutil.cpp
blob: 918a7c3e72b154422503e595977a97167f708924 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * TDDD86 Huffman Encoding
 * This file defines various utility functions used by the main client program.
 * See huffmanutil.h for documentation of each function.
 *
 * Please do not modify this provided file. Your turned-in files should work
 * with an unmodified version of all provided code files.
 */

#include "huffmanutil.h"
#include "bitstream.h"
#include "strlib.h"
#include "simpio.h"

string bitsToBytes(string text) {
    istringbitstream input(text);
    ostringstream out;
    while (true) {
        int bit = input.readBit();
        if (input.fail()) {
            break;
        }
        out.put(bit == 1 ? '1' : '0');
    }
    return out.str();
}

string bytesToBits(string text) {
    ostringbitstream out;
    for (int i = 0; i < (int) text.length(); i++) {
        out.writeBit(text[i] == '1' ? 1 : 0);
    }
    return out.str();
}

#if defined(_WIN32) || defined(_WIN64)
    #include <windows.h>
#else
    // assume POSIX
    #include <sys/stat.h>
#endif

bool confirmOverwrite(string filename) {
#if defined(_WIN32) || defined(_WIN64)
    bool fileExists = (GetFileAttributesA(filename.c_str()) != INVALID_FILE_ATTRIBUTES);
#else
    // assume POSIX
    struct stat fileInfo;
    bool fileExists = (stat(filename.c_str(), &fileInfo) == 0);
#endif
    if (!fileExists) {
        return true;
    } else {
        return yesOrNo(filename + " already exists.  Overwrite? (y/n) ");
    }
}

int fileSize(string filename) {
    ifstream input;
    input.open(filename.c_str(), ifstream::binary);
    input.seekg(0, ifstream::end);
    return (int) input.tellg();
}

void printBits(string text) {
    istringbitstream input(text);
    int i = 0;
    while (true) {
        i++;
        int bit = input.readBit();
        if (input.fail()) break;
        cout << bit;
        if (i > 0 && i % 8 == 0) {
            cout << " ";
        }
        if (i > 0 && i % 64 == 0) {
            cout << endl;
        }
    }
    cout << endl;
}

string promptForExistingFileName(string prompt) {
    while (true) {
        string filename = getLine(prompt);
#if defined(_WIN32) || defined(_WIN64)
        bool fileExists = (GetFileAttributesA(filename.c_str()) != INVALID_FILE_ATTRIBUTES);
#else
        // assume POSIX
        struct stat fileInfo;
        bool fileExists =  (stat(filename.c_str(), &fileInfo) == 0);
#endif
        if (fileExists) {
            return filename;
        } else {
            cout << "That file does not exist; please try again." << endl;
        }
    }
    return "";
}

string readEntireFileText(string filename) {
    ifstream input;
    input.open(filename.c_str());
    return readEntireFileText(input);
}

string readEntireFileText(istream& input) {
    ostringstream out;
    while (true) {
        int ch = input.get();
        if (input.fail()) {
            break;
        }
        out << (char) ch;
    }
    return out.str();
}

string stringReplace(string s, char oldChar, char newChar) {
    for (int i = (int) s.length() - 1; i >= 0; i--) {
        if (s[i] == oldChar) {
            s[i] = newChar;
        }
    }
    return s;
}

string stringReplace(string s, char oldChar, string newStr) {
    for (int i = (int) s.length() - 1; i >= 0; i--) {
        if (s[i] == oldChar) {
            s.erase(i, 1);
            if (newStr.length() > 0) {
                s.insert(i, newStr);
            }
        }
    }
    return s;
}

string stringReplace(string s, string oldStr, string newStr) {
    int l2 = oldStr.length();
    for (int i = (int) (s.length() - l2); i >= 0; i--) {
        if (s.substr(i, l2) == oldStr) {
            s.replace(i, l2, newStr);
        }
    }
    return s;
}

/*
 * Returns a printable string for the given character.  See bitstream.h.
 */
string toPrintableChar(int ch) {
    if (ch == '\n') {
        return "'\\n'";
    } else if (ch == '\t') {
        return "'\\t'";
    } else if (ch == '\r') {
        return "'\\r'";
    } else if (ch == '\f') {
        return "'\\f'";
    } else if (ch == '\b') {
        return "'\\b'";
    } else if (ch == '\0') {
        return "'\\0'";
    } else if (ch == ' ') {
        return "' '";
    } else if (ch == (int) PSEUDO_EOF) {
        return "EOF";
    } else if (ch == (int) NOT_A_CHAR) {
        return "NONE";
    } else if (!isgraph(ch)) {
        return "???";
    } else {
        return string("'") + (char) ch + string("'");
    }
}

bool yesOrNo(string prompt) {
    while (true) {
        string answer = trim(toLowerCase(getLine(prompt)));
        if (startsWith(answer, 'y')) {
            return true;
        } else if (startsWith(answer, 'n')) {
            return false;
        } else {
            cout << "Please type a word that begins with 'y' or 'n'." << endl;
        }
    }
}

string getRoot(string filename) {
   int dot = -1;
   int len = filename.length();
   for (int i = 0; i < len; i++) {
      char ch = filename[i];
      if (ch == '.') dot = i;
      if (ch == '/' || ch == '\\') dot = -1;
   }
   if (dot == -1) {
      return filename;
   } else {
      return filename.substr(0, dot);
   }
}