main.cpp 2.24 KB
Newer Older
Nick Sharp's avatar
Nick Sharp committed
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
#include "CMU462.h"
#include "viewer.h"
#include "drawsvg.h"

#include <sys/stat.h>
#include <dirent.h>
#include <iostream>

using namespace std;
using namespace CMU462;

#define msg(s) cerr << "[DrawSVG] " << s << endl;

int loadFile( DrawSVG* drawsvg, const char* path ) {

  SVG* svg = new SVG();

  if( SVGParser::load( path, svg ) < 0) {
    delete svg;
    return -1;
  }
  
  drawsvg->newTab( svg );
  return 0;
}

int loadDirectory( DrawSVG* drawsvg, const char* path ) {

  DIR *dir = opendir (path);
  if(dir) {
    
    struct dirent *ent; size_t n = 0;
    
    // load files
    string pathname = path; 
    if (pathname[pathname.back()] != '/') pathname.push_back('/');
    while (((ent = readdir (dir)) != NULL) && (n < 9)) {

      string filename = ent->d_name;
      string filesufx = filename.substr(filename.find_last_of(".") + 1);
      if (filesufx == "svg" ) {
        cerr << "[DrawSVG] Loading " << filename << "... "; 
        if (loadFile(drawsvg, (pathname + filename).c_str()) < 0) {
          cerr << "Failed (Invalid SVG file)" << endl;
        } else {
          cerr << "Succeeded" << endl;
          n++;
        }
      }
    }

    closedir (dir);

    if (n) {
      msg("Successfully Loaded " << n << " files from " << path);
      return 0;
    }

    msg("No valid svg files found in " << path);
    return -1;
  } 

  msg("Could not open directory" << path);
  return -1;
}

int loadPath( DrawSVG* drawsvg, const char* path) {

  struct stat st;

  // file exist?
  if(stat(path, &st) < 0 ) {
Yuqiao Zeng's avatar
Yuqiao Zeng committed
73
    msg("File does not exist: " << path);
Nick Sharp's avatar
Nick Sharp committed
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
    return -1;
  }

  // load directory
  if( st.st_mode & S_IFDIR ) {
    return loadDirectory(drawsvg, path);
  } 

  // load file
  if( st.st_mode & S_IFREG ) {
    return loadFile(drawsvg, path);
  }

  msg("Invalid path: " << path);
  return -1;
}

int main( int argc, char** argv ) {

  // create viewer
  Viewer viewer = Viewer();

  // create drawsvg
  DrawSVG* drawsvg = new DrawSVG();

  // set drawsvg as renderer
  viewer.set_renderer(drawsvg);

  // load tests
  if( argc == 2 ) {
    if (loadPath(drawsvg, argv[1]) < 0) exit(0);
  } else {
    msg("Usage: drawsvg <path to test file or directory>"); exit(0);
  }

  // init viewer
  viewer.init();

  // start viewer
  viewer.start();

  return 0;
}