Commit 92450f84 by Aaron Leung

Catching file I/O errors.

parent ddbc68b5
...@@ -54,17 +54,19 @@ namespace Sass { ...@@ -54,17 +54,19 @@ namespace Sass {
{ {
if (!source) { if (!source) {
std::FILE *f; std::FILE *f;
// TO DO: CHECK f AGAINST NULL/0
f = std::fopen(path.c_str(), "rb"); 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); int len = std::ftell(f);
if (len < 0) throw path;
std::rewind(f); 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]; source = new char[len + 1];
std::fread(source, sizeof(char), len, f); std::fread(source, sizeof(char), len, f);
if (std::ferror(f)) throw path;
source[len] = '\0'; source[len] = '\0';
end = source + len; end = source + len;
std::fclose(f); if (std::fclose(f)) throw path;
own_source = true; own_source = true;
} }
position = source; position = source;
...@@ -80,17 +82,19 @@ namespace Sass { ...@@ -80,17 +82,19 @@ namespace Sass {
lexed(Token::make()) lexed(Token::make())
{ {
std::FILE *f; std::FILE *f;
// TO DO: CHECK f AGAINST NULL/0
f = std::fopen(path.c_str(), "rb"); 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); int len = std::ftell(f);
if (len < 0) throw path;
std::rewind(f); 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]; source = new char[len + 1];
std::fread(source, sizeof(char), len, f); std::fread(source, sizeof(char), len, f);
if (std::ferror(f)) throw path;
source[len] = '\0'; source[len] = '\0';
end = source + len; end = source + len;
std::fclose(f); if (std::fclose(f)) throw path;
position = source; position = source;
context.source_refs.push_back(source); context.source_refs.push_back(source);
++context.ref_count; ++context.ref_count;
...@@ -116,6 +120,9 @@ namespace Sass { ...@@ -116,6 +120,9 @@ namespace Sass {
void Document::syntax_error(string message, size_t ln) void Document::syntax_error(string message, size_t ln)
{ throw Error(Error::syntax, ln ? ln : line_number, path, message); } { 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::string;
using std::stringstream; using std::stringstream;
using std::endl; using std::endl;
......
...@@ -154,6 +154,7 @@ namespace Sass { ...@@ -154,6 +154,7 @@ namespace Sass {
const char* look_for_attrib(const char* start = 0); const char* look_for_attrib(const char* start = 0);
void syntax_error(string message, size_t ln = 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); string emit_css(CSS_Style style);
......
...@@ -46,9 +46,14 @@ namespace Sass { ...@@ -46,9 +46,14 @@ namespace Sass {
const char* curr_path_start = path.c_str(); const char* curr_path_start = path.c_str();
const char* curr_path_end = folders(curr_path_start); const char* curr_path_end = folders(curr_path_start);
string current_path(curr_path_start, curr_path_end - curr_path_start); string current_path(curr_path_start, curr_path_end - curr_path_start);
Document importee(current_path + import_path, context); try {
importee.parse_scss(); Document importee(current_path + import_path, context);
return importee.root; importee.parse_scss();
return importee.root;
}
catch (string path) {
read_error("error reading file \"" + path + "\"");
}
} }
Node Document::parse_mixin_definition() Node Document::parse_mixin_definition()
......
...@@ -21,5 +21,5 @@ div[hux ~= "hello"] { ...@@ -21,5 +21,5 @@ div[hux ~= "hello"] {
foo: boo; foo: boo;
@include moogoo; @include moogoo;
dux: mux; 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