#include #include #include #include "pics.h" using std::string; using std::vector; using std::max; #ifdef _MSC_VER //#include "../minmax.h" #else //using std::max; #endif string::size_type width(const vector& v) { string::size_type maxlen = 0; #ifdef _MSC_VER for(std::vector::size_type i = 0; i != v.size(); ++i) #else for(vector::size_type i = 0; i != v.size(); ++i) #endif maxlen = max(maxlen, v[i].size()); return maxlen; } vector frame(const vector& v) { vector ret; string::size_type maxlen = width(v); string border(maxlen + 4, '*'); // write the top border ret.push_back(border); // write each interior row, bordered by an asterisk and a space #ifdef _MSC_VER for (std::vector::size_type i = 0; i != v.size(); ++i) { #else for (vector::size_type i = 0; i != v.size(); ++i) { #endif ret.push_back("* " + v[i] + string(maxlen - v[i].size(), ' ') + " *"); } // write the bottom border ret.push_back(border); return ret; } vector vcat(const vector& top, const vector& bottom) { // copy the `top' picture vector ret = top; // copy entire `bottom' picture #ifdef _MSC_VER for (std::vector::const_iterator it = bottom.begin(); #else for (vector::const_iterator it = bottom.begin(); #endif it != bottom.end(); ++it) ret.push_back(*it); return ret; } vector hcat(const vector& left, const vector& right) { vector ret; // add 1 to leave a space between pictures string::size_type width1 = width(left) + 1; // indices to look at elements from `left' and `right' respectively #ifdef _MSC_VER std::vector::size_type i = 0, j = 0; #else vector::size_type i = 0, j = 0; #endif string s; // continue until we've seen all rows from both pictures while (i != left.size() || j != right.size()) { // construct new `string' to hold characters from both pictures //string s; // copy a row from the left-hand side, if there is one if (i != left.size()) s = left[i++]; // pad to full width s += string(width1 - s.size(), ' '); // copy a row from the right-hand side, if there is one if (j != right.size()) s += right[j++]; // add `s' to the picture we're creating ret.push_back(s); } return ret; }