summaryrefslogtreecommitdiff
path: root/other/Kermit/src/unload.cpp
blob: 90630eaa5435aa7be7ac087a9c6093f6ac892449 (plain)
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
/*
 * unload.cpp:
 * written by palmers / teso
 */
#include <Kermit>
#include <string>
#include <iostream>
#include <fstream>
#include <stack>

#define PROGRAM		"unload"
#define VERSION		"0.0.1"
#define AUTHOR		"palmers / teso"

#define LINE_LENGTH	16384


void usage (char *s)
{
  cout << PROGRAM << VERSION << " by " << AUTHOR << endl;
  cout << "Usage: " << s << " [options] <file>" << endl;
  cout << "Options:" << endl;
  cout << "\t-l:\t restore in linear order [default: reversed order]" << endl;
  cout << endl;
  exit (0);
}


int main (int argc, char **argv)
{
  bool		linear = false;
  int		x = 0;
  ifstream	fs;
  char		line[LINE_LENGTH + 1];
  Patch		*tp = NULL;
  rwKernel	*rw = NULL;

  if (argc < 2)
    usage (argv[0]);

  for (x = 1; x < argc; x++)
    {
      if (argv[x][0] == '-')
	{
	  switch (argv[x][1])
	    {
	      case 'l':
		linear = true;
		break;
	      default:
		cout << "unknow option: " << argv[x] << endl;
		usage (argv[0]);
		break;
	    }
	}
    }

  fs.open (argv[argc - 1]);
  if (!fs.is_open ())
    {
      cerr << "failed to open \"" << argv[argc - 1] << "\"" << endl;
      abort ();
    }

  if (linear)
    {
      while (!fs.eof ())
	{
          fs.getline (line, LINE_LENGTH, '\n');
          tp = new Patch (string (line), rw);
          tp->remove ();
	  delete tp;
	}
    }
  else
    {
      stack<Patch *> pstack;
      while (!fs.eof ())
	{
          fs.getline (line, LINE_LENGTH, '\n');
          tp = new Patch (string (line), rw);
          pstack.push (tp);
	}

      while (!pstack.empty ())
	{
	  tp = pstack.top ();
	  tp->remove ();
	  delete tp;
	  pstack.pop ();
	}
    }

  return 0;
}