Commit 92450f84 by Aaron Leung

Catching file I/O errors.

parent ddbc68b5
......@@ -54,17 +54,19 @@ namespace Sass {
{
if (!source) {
std::FILE *f;
// TO DO: CHECK f AGAINST NULL/0
f = std::fopen(path.c_str(), "rb");
std::fseek(f, 0, SEEK_END);
if (!f) throw path;
if (std::fseek(f, 0, SEEK_END)) throw path;
int len = std::ftell(f);
if (len < 0) throw path;
std::rewind(f);
// TO DO: WRAP THE new[] IN A TRY/CATCH BLOCK
// TO DO: CATCH THE POTENTIAL badalloc EXCEPTION
source = new char[len + 1];
std::fread(source, sizeof(char), len, f);
if (std::ferror(f)) throw path;
source[len] = '\0';
end = source + len;
std::fclose(f);
if (std::fclose(f)) throw path;
own_source = true;
}
position = source;
......@@ -80,17 +82,19 @@ namespace Sass {
lexed(Token::make())
{
std::FILE *f;
// TO DO: CHECK f AGAINST NULL/0
f = std::fopen(path.c_str(), "rb");
std::fseek(f, 0, SEEK_END);
if (!f) throw path;
if (std::fseek(f, 0, SEEK_END)) throw path;
int len = std::ftell(f);
if (len < 0) throw path;
std::rewind(f);
// TO DO: WRAP THE new[] IN A TRY/CATCH BLOCK
// TO DO: CATCH THE POTENTIAL badalloc EXCEPTION
source = new char[len + 1];
std::fread(source, sizeof(char), len, f);
if (std::ferror(f)) throw path;
source[len] = '\0';
end = source + len;
std::fclose(f);
if (std::fclose(f)) throw path;
position = source;
context.source_refs.push_back(source);
++context.ref_count;
......@@ -116,6 +120,9 @@ namespace Sass {
void Document::syntax_error(string message, size_t ln)
{ throw Error(Error::syntax, ln ? ln : line_number, path, message); }
void Document::read_error(string message, size_t ln)
{ throw Error(Error::read, ln ? ln : line_number, path, message); }
using std::string;
using std::stringstream;
using std::endl;
......
......@@ -154,6 +154,7 @@ namespace Sass {
const char* look_for_attrib(const char* start = 0);
void syntax_error(string message, size_t ln = 0);
void read_error(string message, size_t ln = 0);
string emit_css(CSS_Style style);
......
......@@ -46,9 +46,14 @@ namespace Sass {
const char* curr_path_start = path.c_str();
const char* curr_path_end = folders(curr_path_start);
string current_path(curr_path_start, curr_path_end - curr_path_start);
Document importee(current_path + import_path, context);
importee.parse_scss();
return importee.root;
try {
Document importee(current_path + import_path, context);
importee.parse_scss();
return importee.root;
}
catch (string path) {
read_error("error reading file \"" + path + "\"");
}
}
Node Document::parse_mixin_definition()
......
......@@ -21,5 +21,5 @@ div[hux ~= "hello"] {
foo: boo;
@include moogoo;
dux: mux;
color: foo($x: );
@import "foogoo";
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment