Commit 1f73d162 by Aaron Leung

Testing and debugging the context initialization functions.

parent ccc9f0a4
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "context.h"
#include "file.h"
sass_context *make_sass_context_from_file(char *path) {
static sass_context *sass_alloc_context() {
sass_context *ctx = malloc(sizeof(sass_context));
if (!ctx) {
printf("ERROR: could not allocate sass context object.\n");
printf("ERROR: could not allocate Sass context object.\n");
abort();
}
return ctx;
}
static char *sass_read_file(char *path) {
FILE *f;
f = fopen(path, "rb");
if (!f) {
printf("ERROR: could not open Sass source file %s", path);
abort();
}
fseek(f, 0L, SEEK_END);
int len = ftell(f);
rewind(f);
char *buf = (char *)malloc(len * sizeof(char) + 1);
fread(buf, sizeof(char), len, f);
buf[len] = '\0';
fclose(f);
return buf;
}
sass_context *sass_make_context_from_file(char *path) {
sass_context *ctx = sass_alloc_context();
ctx->path = path;
ctx->src = sass_read_file(path);
ctx->pos = src;
ctx->pos = ctx->src = sass_read_file(path);
ctx->line = 1;
return ctx;
}
sass_context *sass_make_context_from_string(char *src) {
size_t len = strlen(src);
sass_context *ctx = sass_alloc_context();
if (!(ctx->pos = ctx->src = (char *) malloc(len * sizeof(char) + 1) )) {
printf("ERROR: could not copy Sass source string.\n");
abort();
}
memcpy(ctx->src, src, len);
ctx->src[len] = '\0';
ctx->line = 1;
return ctx;
}
void *free_sass_context(sass_context *ctx) {
free(ctx->src);
free(ctx);
}
......@@ -4,8 +4,10 @@ typedef struct {
char *path; /* the full directory+filename of the source file */
char *src; /* the text of the entire source file */
char *pos; /* keeps track of the parser/scanner's current position */
size_t line; /* the number of the line currently being parsed/scanned */
jmp_buf env; /* the top of the exception-handling stack */
/* more to come */
} sass_context;
sass_context *make_sass_context_from_file(char *path);
\ No newline at end of file
sass_context *sass_make_context_from_file(char *path);
sass_context *sass_make_context_from_string(char *src);
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