Skip to content

Instantly share code, notes, and snippets.

@zvakanaka
Created April 8, 2016 03:30
Show Gist options
  • Save zvakanaka/ba89dd7941cc2e3b039825b7655c5a92 to your computer and use it in GitHub Desktop.
Save zvakanaka/ba89dd7941cc2e3b039825b7655c5a92 to your computer and use it in GitHub Desktop.
//lamepeg lame terminal picture decoder
//made by Adam Quinton 2010
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdlib.h>
#include <unistd.h>
#define RELOAD 18
#define MAXROW 41
#define cerr cerr << "\E[31m"
#define DEF "\E[0m"
#define FRAMEAMOUNT 0//default number of times to refresh, 0 is infinite
using namespace std;
struct Col
{
int amount;//amount of command line params
int reload;//refresh wait
bool odd;//special color
char firstFile[256];
char secondFile[256];
char thirdFile[256];
char lastFile[256];
int fileLine;//dumps numbers to config
char code;//color number
int flip;//counter to keep track of current file
bool ranToRun;//checks if has been run at least once
int timesRan;//how many times has the program been ran
int frameAmount;//amount of frames to be played before ending
}col;//short for color
void msSleep(float ms);
void display();
void task();
int main(int argc, char **argv)
{
//B = black, r = red, g = green, y = yellow,
//b = blue, v, p = purple, c, i = cyan w = white
//specials: o = orange, t = tan
col.odd = false;//reset odd color flag
if (!col.ranToRun)
{
col.frameAmount = FRAMEAMOUNT;
col.timesRan = 0;
ifstream fin;
fin.open("frame");
if (!fin.fail())
{
fin >> col.frameAmount;
cerr << "Frame amount set to " << col.frameAmount << DEF << endl;
col.frameAmount--;
}
fin.open("/etc/lpg/refresh.conf");
if (fin.fail())
col.reload = RELOAD;//refresh pause(default = 16)
else
{
fin >> col.fileLine;
col.reload = col.fileLine;
cerr << "Reload set to " << col.reload << DEF << endl;
}
col.flip = 1;
if (argc == 1)//none
{
cout << "How many files?(1-4): ";
cin >> col.amount;
if (col.amount < 1 || col.amount > 4)
{
cout << col.amount << " is not between 1 and 4\n";
return 1;
}
else
{
if (col.amount > 0)
{
//experimental
cin >> col.firstFile;
if (col.amount == 1)
{
strcpy(col.secondFile, col.firstFile);
strcpy(col.thirdFile, col.firstFile);
strcpy(col.lastFile, col.firstFile);
}
}
if (col.amount > 1)
{
cout << "Enter filename 2: ";
cin >> col.secondFile;
if (col.amount == 2)
{
strcpy(col.thirdFile, col.firstFile);
strcpy(col.lastFile, col.secondFile);
}
}
if (col.amount > 2)
{
cout << "Enter filename 3: ";
cin >> col.thirdFile;
if (col.amount == 3)
strcpy(col.lastFile, col.secondFile);
}
if (col.amount > 3)
{
cout << "Enter filename 4: ";
cin >> col.lastFile;
}
}
}
else if (argc == 3)//2
{
strcpy(col.firstFile, argv[1]);
strcpy(col.secondFile, argv[2]);
strcpy(col.thirdFile, argv[1]);
strcpy(col.lastFile, argv[2]);
}
else if (argc == 4)//3
{
strcpy(col.firstFile, argv[1]);
strcpy(col.secondFile, argv[2]);
strcpy(col.thirdFile, argv[3]);
strcpy(col.lastFile, argv[2]);
}
else if (argc == 5)//4!
{
strcpy(col.firstFile, argv[1]);
strcpy(col.secondFile, argv[2]);
strcpy(col.thirdFile, argv[3]);
strcpy(col.lastFile, argv[4]);
}
else if (argc == 2)//1
{
strcpy(col.firstFile, argv[1]);
strcpy(col.secondFile, argv[1]);
strcpy(col.thirdFile, argv[1]);
strcpy(col.lastFile, argv[1]);
}
}
col.ranToRun = true;
task();
msSleep(col.reload);
main(argc, argv);
return 0;
}
//msSleep calls usleep in hundreths of a second
void msSleep(float ms)
{
usleep(ms * 10000);
}
//DISPLAY stays open until the file is done being read and displayed
void display()
{
ifstream fin;
if (col.flip == 1)
{
fin.open(col.firstFile);
if (fin.fail())
{
cout << "\E[91;1mERROR: \E[1;4;95m"
<< col.firstFile
<< "\E[0;91;1m does not exist\E[0m\n";
exit(1);
}
col.flip = 2;
}
else if (col.flip == 2)
{
fin.open(col.secondFile);
if (fin.fail())
{
cout << "\E[91;1mERROR: \E[1;4;95m"
<< col.secondFile
<< "\E[0;91;1m does not exist\E[0m\n";
exit(1);
}
col.flip = 3;
}
else if (col.flip == 3)
{
fin.open(col.thirdFile);
if (fin.fail())
{
cout << "\E[91;1mERROR: \E[1;4;95m"
<< col.thirdFile
<< "\E[0;91;1m does not exist\E[0m\n";
exit(1);
}
col.flip = 4;
}
else if (col.flip == 4)
{
fin.open(col.lastFile);
if (fin.fail())
{
cout << "\E[91;1mERROR: \E[1;4;95m"
<< col.lastFile
<< "\E[0;91;1m does not exist\E[0m\n";
exit(1);
}
col.flip = 1;
}
for(int c = 0, rowLength = 0; !fin.eof(); c++, rowLength++)
{
col.odd = false;
int color;
fin.get(col.code);
if (col.code == '\n')
{
//cerr << rowLength << DEF;
cout << endl;
rowLength = 0;
}
else if (rowLength < MAXROW)
{
switch (col.code)
{
case 'B':
color = 40;
break;
case 'r':
color = 41;
break;
case 'g':
color = 42;
break;
case 'y':
color = 43;
break;
case 't':
col.odd = true;
color = 94;
break;
case 'o':
col.odd = true;
color = 202;
break;
case 'i':
color = 44;
break;
case 'v':
case 'p':
color = 45;
break;
case 'b':
color = 46;
break;
case 'w':
case 'd':
color = 47;
break;
case '\n':
color = 0;
cout << endl;
break;
case ' ':
color = 0;
break;
default:
color = 0;
}
if (col.odd)
cout << "\E[48;5;" << color << "m ";
else
cout << "\E[1;" << color << "m ";
}
cout << "\E[0m";
}
fin.close();
}
void task()
{
system("clear");
cout << endl;
display();
col.timesRan++;
if (col.timesRan > col.frameAmount && col.frameAmount)
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment