Skip to content

Commit

Permalink
Replace copy_c_str/strdup with sass_strdup
Browse files Browse the repository at this point in the history
Plus a few other minor improvements
  • Loading branch information
mgreter committed Mar 8, 2015
1 parent 62b3226 commit 7e1b94b
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 70 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ SOURCES = \
constants.cpp \
context.cpp \
contextualize.cpp \
copy_c_str.cpp \
cssize.cpp \
error_handling.cpp \
eval.cpp \
Expand Down
1 change: 0 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ libsass_la_SOURCES = \
constants.cpp constants.hpp \
context.cpp context.hpp \
contextualize.cpp contextualize.hpp \
copy_c_str.cpp copy_c_str.hpp \
error_handling.cpp error_handling.hpp \
eval.cpp eval.hpp \
expand.cpp expand.hpp \
Expand Down
9 changes: 5 additions & 4 deletions context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#endif

#include "ast.hpp"
#include "util.hpp"
#include "sass.h"
#include "context.hpp"
#include "plugins.hpp"
Expand All @@ -19,7 +20,6 @@
#include "cssize.hpp"
#include "extend.hpp"
#include "remove_placeholders.hpp"
#include "copy_c_str.hpp"
#include "color_names.hpp"
#include "functions.hpp"
#include "backtrace.hpp"
Expand All @@ -37,6 +37,7 @@
namespace Sass {
using namespace Constants;
using namespace File;
using namespace Sass;
using std::cerr;
using std::endl;

Expand Down Expand Up @@ -282,7 +283,7 @@ namespace Sass {
if (source_map_file != "" && !omit_source_map_url) {
output += linefeed + format_source_mapping_url(source_map_file);
}
return copy_c_str(output.c_str());
return sass_strdup(output.c_str());
}

Block* Context::parse_file()
Expand Down Expand Up @@ -335,7 +336,7 @@ namespace Sass {
add_source(input_path, input_path, contents);
return parse_file();
}
add_source(input_path, input_path, copy_c_str(source_c_str));
add_source(input_path, input_path, sass_strdup(source_c_str));
return parse_file();
}

Expand Down Expand Up @@ -371,7 +372,7 @@ namespace Sass {
if (source_map_file == "") return 0;
char* result = 0;
string map = emitter.generate_source_map(*this);
result = copy_c_str(map.c_str());
result = sass_strdup(map.c_str());
return result;
}

Expand Down
13 changes: 0 additions & 13 deletions copy_c_str.cpp

This file was deleted.

10 changes: 0 additions & 10 deletions copy_c_str.hpp

This file was deleted.

26 changes: 13 additions & 13 deletions sass_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <cstring>
#include <stdexcept>
#include "json.hpp"
#include "copy_c_str.hpp"
#include "util.hpp"
#include "context.hpp"
#include "sass_values.h"
#include "sass_context.h"
Expand Down Expand Up @@ -94,7 +94,7 @@ extern "C" {
// Used to create sourceMappingUrl
char* source_map_file;

// option for sourceRoot property
// Custom functions that can be called from sccs code
char* source_map_root;

// Custom functions that can be called from sccs code
Expand Down Expand Up @@ -176,7 +176,7 @@ extern "C" {
#define IMPLEMENT_SASS_OPTION_STRING_ACCESSOR(type, option) \
type ADDCALL sass_option_get_##option (struct Sass_Options* options) { return options->option; } \
void ADDCALL sass_option_set_##option (struct Sass_Options* options, type option) \
{ free(options->option); options->option = option ? copy_c_str(option) : 0; }
{ free(options->option); options->option = option ? sass_strdup(option) : 0; }

#define IMPLEMENT_SASS_CONTEXT_GETTER(type, option) \
type ADDCALL sass_context_get_##option (struct Sass_Context* ctx) { return ctx->option; }
Expand Down Expand Up @@ -232,10 +232,10 @@ extern "C" {
json_append_member(json_err, "message", json_mkstring(e.message.c_str()));
msg_stream << e.pstate.path << ":" << e.pstate.line+1 << ": " << e.message << endl;
c_ctx->error_json = json_stringify(json_err, " ");;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_text = strdup(e.message.c_str());
c_ctx->error_status = 1;
c_ctx->error_file = copy_c_str(e.pstate.path.c_str());
c_ctx->error_file = sass_strdup(e.pstate.path.c_str());
c_ctx->error_line = e.pstate.line+1;
c_ctx->error_column = e.pstate.column+1;
c_ctx->output_string = 0;
Expand All @@ -249,7 +249,7 @@ extern "C" {
json_append_member(json_err, "status", json_mknumber(2));
json_append_member(json_err, "message", json_mkstring(ba.what()));
c_ctx->error_json = json_stringify(json_err, " ");;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_text = strdup(ba.what());
c_ctx->error_status = 2;
c_ctx->output_string = 0;
Expand All @@ -263,7 +263,7 @@ extern "C" {
json_append_member(json_err, "status", json_mknumber(3));
json_append_member(json_err, "message", json_mkstring(e.what()));
c_ctx->error_json = json_stringify(json_err, " ");;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_text = strdup(e.what());
c_ctx->error_status = 3;
c_ctx->output_string = 0;
Expand All @@ -277,7 +277,7 @@ extern "C" {
json_append_member(json_err, "status", json_mknumber(4));
json_append_member(json_err, "message", json_mkstring(e.c_str()));
c_ctx->error_json = json_stringify(json_err, " ");;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_text = strdup(e.c_str());
c_ctx->error_status = 4;
c_ctx->output_string = 0;
Expand All @@ -291,7 +291,7 @@ extern "C" {
json_append_member(json_err, "status", json_mknumber(5));
json_append_member(json_err, "message", json_mkstring("unknown"));
c_ctx->error_json = json_stringify(json_err, " ");;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_text = strdup("unknown");
c_ctx->error_status = 5;
c_ctx->output_string = 0;
Expand Down Expand Up @@ -428,7 +428,7 @@ extern "C" {
}

// copy the included files on to the context (dont forget to free)
copy_strings(cpp_ctx->get_included_files(skip), &c_ctx->included_files, skip);
if (root) copy_strings(cpp_ctx->get_included_files(skip), &c_ctx->included_files, skip);

// return parsed block
return root;
Expand Down Expand Up @@ -658,7 +658,7 @@ extern "C" {
static void sass_clear_context (struct Sass_Context* ctx)
{
if (ctx == 0) return;
// release the allocated memory (mostly via copy_c_str)
// release the allocated memory (mostly via sass_strdup)
if (ctx->output_string) free(ctx->output_string);
if (ctx->source_map_string) free(ctx->source_map_string);
if (ctx->error_message) free(ctx->error_message);
Expand Down Expand Up @@ -757,7 +757,7 @@ extern "C" {

struct string_list* include_path = (struct string_list*) calloc(1, sizeof(struct string_list));
if (include_path == 0) return;
include_path->string = path ? copy_c_str(path) : 0;
include_path->string = path ? sass_strdup(path) : 0;
struct string_list* last = options->include_paths;
if (!options->include_paths) {
options->include_paths = include_path;
Expand All @@ -775,7 +775,7 @@ extern "C" {

struct string_list* plugin_path = (struct string_list*) calloc(1, sizeof(struct string_list));
if (plugin_path == 0) return;
plugin_path->string = path ? copy_c_str(path) : 0;
plugin_path->string = path ? sass_strdup(path) : 0;
struct string_list* last = options->plugin_paths;
if (!options->plugin_paths) {
options->plugin_paths = plugin_path;
Expand Down
7 changes: 4 additions & 3 deletions sass_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
#endif

#include <cstring>
#include "copy_c_str.hpp"
#include "util.hpp"
#include "context.hpp"
#include "sass_functions.h"

extern "C" {
using namespace std;
using namespace Sass;

// Struct to hold custom function callback
struct Sass_C_Function_Descriptor {
Expand Down Expand Up @@ -90,8 +91,8 @@ extern "C" {
{
Sass_Import* v = (Sass_Import*) calloc(1, sizeof(Sass_Import));
if (v == 0) return 0;
v->path = path ? Sass::copy_c_str(path) : 0;
v->base = base ? Sass::copy_c_str(base) : 0;
v->path = path ? sass_strdup(path) : 0;
v->base = base ? sass_strdup(base) : 0;
v->source = source;
v->srcmap = srcmap;
v->error = 0;
Expand Down
22 changes: 11 additions & 11 deletions sass_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <sstream>
#include <iostream>

#include "copy_c_str.hpp"
#include "util.hpp"
#include "context.hpp"
#include "inspect.hpp"
#include "error_handling.hpp"
Expand Down Expand Up @@ -147,31 +147,31 @@ extern "C" {
catch (Sass_Error& e) {
stringstream msg_stream;
msg_stream << e.pstate.path << ":" << e.pstate.line << ": " << e.message << endl;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_status = 1;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
}
catch(bad_alloc& ba) {
stringstream msg_stream;
msg_stream << "Unable to allocate memory: " << ba.what() << endl;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_status = 1;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
}
catch (std::exception& e) {
stringstream msg_stream;
msg_stream << "Error: " << e.what() << endl;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_status = 1;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
}
catch (string& e) {
stringstream msg_stream;
msg_stream << "Error: " << e << endl;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_status = 1;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
Expand All @@ -180,7 +180,7 @@ extern "C" {
// couldn't find the specified file in the include paths; report an error
stringstream msg_stream;
msg_stream << "Unknown error occurred" << endl;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_status = 1;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
Expand Down Expand Up @@ -237,31 +237,31 @@ extern "C" {
catch (Sass_Error& e) {
stringstream msg_stream;
msg_stream << e.path << ":" << e.pstate.line << ": " << e.message << endl;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_status = 1;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
}
catch(bad_alloc& ba) {
stringstream msg_stream;
msg_stream << "Unable to allocate memory: " << ba.what() << endl;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_status = 1;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
}
catch (std::exception& e) {
stringstream msg_stream;
msg_stream << "Error: " << e.what() << endl;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_status = 1;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
}
catch (string& e) {
stringstream msg_stream;
msg_stream << "Error: " << e << endl;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_status = 1;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
Expand All @@ -270,7 +270,7 @@ extern "C" {
// couldn't find the specified file in the include paths; report an error
stringstream msg_stream;
msg_stream << "Unknown error occurred" << endl;
c_ctx->error_message = copy_c_str(msg_stream.str().c_str());
c_ctx->error_message = sass_strdup(msg_stream.str().c_str());
c_ctx->error_status = 1;
c_ctx->output_string = 0;
c_ctx->source_map_string = 0;
Expand Down
11 changes: 6 additions & 5 deletions sass_values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

#include <cstdlib>
#include <cstring>
#include "copy_c_str.hpp"
#include "util.hpp"
#include "sass_values.h"

extern "C" {
using namespace std;
using namespace Sass;

struct Sass_Unknown {
enum Sass_Tag tag;
Expand Down Expand Up @@ -165,7 +166,7 @@ extern "C" {
if (v == 0) return 0;
v->number.tag = SASS_NUMBER;
v->number.value = val;
v->number.unit = unit ? Sass::copy_c_str(unit) : 0;
v->number.unit = unit ? sass_strdup(unit) : 0;
if (v->number.unit == 0) { free(v); return 0; }
return v;
}
Expand All @@ -187,7 +188,7 @@ extern "C" {
Sass_Value* v = (Sass_Value*) calloc(1, sizeof(Sass_Value));
if (v == 0) return 0;
v->string.tag = SASS_STRING;
v->string.value = val ? Sass::copy_c_str(val) : 0;
v->string.value = val ? sass_strdup(val) : 0;
if (v->string.value == 0) { free(v); return 0; }
return v;
}
Expand Down Expand Up @@ -228,7 +229,7 @@ extern "C" {
Sass_Value* v = (Sass_Value*) calloc(1, sizeof(Sass_Value));
if (v == 0) return 0;
v->error.tag = SASS_ERROR;
v->error.message = msg ? Sass::copy_c_str(msg) : 0;
v->error.message = msg ? sass_strdup(msg) : 0;
if (v->error.message == 0) { free(v); return 0; }
return v;
}
Expand All @@ -238,7 +239,7 @@ extern "C" {
Sass_Value* v = (Sass_Value*) calloc(1, sizeof(Sass_Value));
if (v == 0) return 0;
v->warning.tag = SASS_WARNING;
v->warning.message = msg ? Sass::copy_c_str(msg) : 0;
v->warning.message = msg ? sass_strdup(msg) : 0;
if (v->warning.message == 0) { free(v); return 0; }
return v;
}
Expand Down
Loading

0 comments on commit 7e1b94b

Please sign in to comment.