This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
forked from sass/libsass
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsass.cpp
151 lines (128 loc) · 4.11 KB
/
sass.cpp
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
73
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <cstdlib>
#include <cstring>
#include <vector>
#include <sstream>
#ifndef SASS
#include "sass.h"
#endif
#include "context.hpp"
#include "error_handling.hpp"
extern "C" {
using namespace std;
struct Sass_Context* make_sass_context()
{ return (Sass_Context*) calloc(1, sizeof(Sass_Context)); }
void free_sass_context(struct Sass_Context* ctx)
{
if (ctx->output_string) free(ctx->output_string);
if (ctx->error_message) free(ctx->error_message);
free(ctx);
}
namespace Sass {
enum Sass_Source { FILE_SOURCE, STRING_SOURCE };
static void compile_sass(struct Sass_Context* c_ctx,
Sass::Sass_Source src_option)
{
using namespace Sass;
try {
Context cpp_ctx(
Context::Data().source_c_str (c_ctx->input_string)
.entry_point (c_ctx->input_path ?
c_ctx->input_path :
"")
.output_style ((Output_Style)
c_ctx->output_style)
.source_comments (c_ctx->source_comments)
.source_maps (c_ctx->source_maps)
.image_path (c_ctx->image_path ?
c_ctx->image_path :
"")
.include_paths_c_str (c_ctx->include_paths_string)
.include_paths_array (/*c_ctx->include_paths_array*/0)
.include_paths (vector<string>())
);
if (src_option == FILE_SOURCE) cpp_ctx.compile_file();
else cpp_ctx.compile_string();
c_ctx->error_message = 0;
c_ctx->error_status = 0;
}
catch (Error& e) {
stringstream msg_stream;
msg_stream << e.path << ":" << e.line << ": error: " << e.message << endl;
string msg(msg_stream.str());
char* msg_str = (char*) malloc(msg.size() + 1);
strcpy(msg_str, msg.c_str());
c_ctx->error_status = 1;
c_ctx->output_string = 0;
c_ctx->error_message = msg_str;
}
catch (bad_alloc& ba) {
stringstream msg_stream;
msg_stream << "Unable to allocate memory: " << ba.what() << endl;
string msg(msg_stream.str());
char* msg_str = (char*) malloc(msg.size() + 1);
strcpy(msg_str, msg.c_str());
c_ctx->error_status = 1;
c_ctx->output_string = 0;
c_ctx->error_message = msg_str;
}
}
}
void compile_sass_file(struct Sass_Context* c_ctx)
{ Sass::compile_sass(c_ctx, Sass::FILE_SOURCE); }
void compile_sass_string(struct Sass_Context* c_ctx)
{ Sass::compile_sass(c_ctx, Sass::STRING_SOURCE); }
union Sass_Value make_sass_boolean(int val)
{
union Sass_Value v;
v.boolean.tag = SASS_BOOLEAN;
v.boolean.value = val;
return v;
}
union Sass_Value make_sass_number(double val, const char* unit)
{
union Sass_Value v;
v.number.tag = SASS_NUMBER;
v.number.value = val;
v.number.unit = strdup(unit);
return v;
}
union Sass_Value make_sass_color(double r, double g, double b, double a)
{
union Sass_Value v;
v.color.tag = SASS_COLOR;
v.color.r = r;
v.color.g = g;
v.color.b = b;
v.color.a = a;
return v;
}
union Sass_Value make_sass_string(const char* val)
{
union Sass_Value v;
v.string.tag = SASS_STRING;
v.string.value = strdup(val);
return v;
}
union Sass_Value make_sass_list(size_t len, enum Sass_Separator sep)
{
union Sass_Value v;
v.list.tag = SASS_LIST;
v.list.length = len;
v.list.separator = sep;
v.list.values = (union Sass_Value*) malloc(sizeof(union Sass_Value)*len);
return v;
}
union Sass_Value make_sass_null()
{
union Sass_Value v;
v.null.tag = SASS_NULL;
return v;
}
union Sass_Value make_sass_error(const char* msg)
{
union Sass_Value v;
v.error.tag = SASS_ERROR;
v.error.message = strdup(msg);
return v;
}
}