File: tests-common.cc

package info (click to toggle)
openscad 2021.01-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 35,608 kB
  • sloc: cpp: 53,187; sh: 4,384; ansic: 4,382; python: 1,813; yacc: 853; javascript: 762; lex: 417; lisp: 163; xml: 127; makefile: 114
file content (42 lines) | stat: -rw-r--r-- 1,145 bytes parent folder | download | duplicates (5)
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
#include "tests-common.h"
#include "openscad.h"
#include "FileModule.h"
#include "handle_dep.h"

#include <boost/filesystem.hpp>

#include <sstream>
#include <fstream>

namespace fs=boost::filesystem;

/*!
	fakepath is used to force the parser to believe that the file is
	read from this location, in order to ensure that filepaths are
	evaluated relative to this path (for testing purposes).
*/
FileModule *parsefile(const char *filename, const char *fakepath)
{
	FileModule *root_module = NULL;

	handle_dep(filename);
	std::ifstream ifs(filename);
	if (!ifs.is_open()) {
		fprintf(stderr, "Can't open input file `%s'!\n", filename);
	}
	else {
		std::string text((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
		text += "\n" + commandline_commands;
		std::string pathname;
		if (fakepath) pathname = fakepath;
		else pathname = fs::path(filename).parent_path().generic_string();
		if(!parse(root_module, text.c_str(), pathname, pathname, false)) {
			delete root_module;             // parse failed
			root_module = NULL;
		}
		if (root_module) {
			root_module->handleDependencies();
		}
	}
	return root_module;
}