#include #include #include #include #include using std::cout; using std::cerr; using std::endl; const int TOTALWIDTH = 62; const int WIDTH = TOTALWIDTH - 32; const int MAX_CHARS = 1024; char signature[MAX_CHARS*2]; int end_pointer = -1; int curr_pointer = -1; int line = -1; void PrintOut(char ch) { cout << ch; } // print ch repeated times void PrintOut(int margin, char ch){ for( ; margin>0; margin--) { PrintOut((char)ch); } } void PrintBuf(char ch) { signature[end_pointer++] = ch; } // print ch repeated times void PrintBuf(int margin, char ch) { for( ; margin>0; margin--) { PrintBuf((char)ch); } } void PrintBufLine(int line = 1) { switch(line) { case 0: PrintOut(WIDTH,' '); cout << " *\n"; break; case 98: cout << " This space unintentionally *\n"; break; case 99: cout << " left blank. *\n"; break; default: if(curr_pointer >= end_pointer) { // cout << curr_pointer << " " << end_pointer; PrintOut(WIDTH,' '); cout << " *\n"; return; } while(signature[curr_pointer] != '\n') { PrintOut(signature[curr_pointer]); curr_pointer++; } PrintOut('\n'); curr_pointer++; } } void PrintNameLine(int line = -1) { switch(line) { case 0: cout << "* Jon Daley * "; break; case 1: // cout << "* jon@limedaley.com * "; // break; // case 2: cout << "* http://jon.limedaley.com/ * "; break; default: cout << "* * "; } } void PrintSig(void){ int curr_line = 0; switch(line) { case 0: PrintNameLine(0); // PrintBufLine(); PrintBufLine(98); PrintNameLine(1); // PrintBufLine(98); // PrintNameLine(2); PrintBufLine(99); break; case 1: PrintNameLine(0); PrintBufLine(); // PrintBufLine(0); PrintNameLine(1); PrintBufLine(0); // PrintBufLine(); // PrintNameLine(2); // PrintBufLine(0); break; case 2: PrintNameLine(0); PrintBufLine(); PrintNameLine(1); PrintBufLine(); break; case 3: PrintNameLine(0); PrintBufLine(); PrintNameLine(); PrintBufLine(); PrintNameLine(1); PrintBufLine(); break; case 4: case 5: PrintNameLine(); PrintBufLine(); PrintNameLine(0); PrintBufLine(); PrintNameLine(); PrintBufLine(); PrintNameLine(1); PrintBufLine(); PrintNameLine(); PrintBufLine(); break; default: for(int i=0; i= MAX_CHARS){ cerr << "Quote is too big. Increase MAX_CHARS if you want to be able to use this sig." << endl; return -1; } if(saved_chars[0] == 0) { save = true; ch = fgetc(stdin); // cout << "new char: " << (char)ch << endl; } else { save = false; ch = saved_chars[0]; for(i = 0; i < 3 && (saved_chars[i] != 0); i++) saved_chars[i] = saved_chars[i+1]; // cout << "old char: " << (char)ch << endl; } // We don't check for \n's if we are // reading off of the stack of saved chars // If there isn't a \n, then we just continue if(save && ch == '\n') { for(i = 0; i < 3; i++) { saved_chars[i] = ch; if(ch == '\n') { // cout << "got a \\n; "; ch = fgetc(stdin); // cout << "char: " << (char)ch << endl; } else{ break; } } saved_chars[i] = ch; // Got 3 \n's if(i == 3){ int real_chars_count = 0; char real_chars[5]; for(i = 0; i < 5; i++){ if(saved_chars[i] == '\n' || saved_chars[i] == 0) saved_chars[i] = 0; else{ real_chars[real_chars_count++] = saved_chars[i]; saved_chars[i] = 0; } } for(i = 0; i < real_chars_count; i++) saved_chars[i] = real_chars[i]; break; } else{ count--; continue; } } if(ch == -1) // Reached end of file break; quote[count] = (char)ch; // Save character in array } quote[count] = 0; if(rand() % 1000 < 1000/num_sig) { for(i = 0; quote[i] != 0 && i < MAX_CHARS; i++) saved_sig[i] = quote[i]; saved_count = count; saved_sig[i] = 0; } } // For each signature PrintOut(TOTALWIDTH,'*'); PrintOut('\n'); // Space out quote over multiple lines printed = 0; line = 0; curr_pointer = 0; end_pointer = 0; // Go until all characters have been printed while(printed < saved_count) { left_margin = 0; right_margin = 0; margin = 0; // Search for spaces and carriage returns // in order to space out quote by word // Stop at end of quote for(ptr = printed; (ptr < printed + WIDTH) && (ptr < saved_count); ptr++) { // Reached author line, treat special if(saved_sig[ptr] == '-' && saved_sig[ptr+1] == '-'){ align_right = true; } // Reached space, remember it, and keep looking if(saved_sig[ptr] == ' ') space = ptr; // We got to end of quote or end of line in quote, end sig line if(saved_sig[ptr] == '\n' || saved_sig[ptr] == 0) { space = ptr; align_right = false; break; } } if(ptr == saved_count) // If we reached end of quote, insert fake separator space = ptr; // Get length of line, and calculate need margin margin = (WIDTH - ((float)space -(float)printed)); // cerr << "margin: " << margin << " "; // If not on "author" line if(!align_right) { // Set margins equal left_margin = right_margin = (int)(margin/2); // If fractional value, set right margin one bigger if((int)(margin/2) != (float)(margin/2)) left_margin++; // cerr << left_margin << " " << right_margin << endl; } else { left_margin = (int)margin - right_margin; } // cerr << "Margin: " << margin << " R: " << right_margin << " L: " << left_margin << endl; PrintBuf(left_margin,' '); // Print the left margin for(ptr = printed; ptr < space; ptr++) { // Don't want to print an extra char if(saved_sig[ptr] != '\n' && saved_sig[ptr] != 0) PrintBuf((char)(saved_sig[ptr])); else PrintBuf(' '); // compensate for subtracting '\n' } PrintBuf(right_margin,' '); // Print right margin PrintBuf(' '); PrintBuf('*'); PrintBuf('\n'); // print final '*' printed = space+1; // set mark for where we have printed up to line++; } PrintSig(); PrintOut(TOTALWIDTH,'*'); // All done with quote, print final row of '*' PrintOut('\n'); return(0); }