Blame src/sass.cpp

Packit bfcc33
#include "sass.hpp"
Packit bfcc33
#include <cstdlib>
Packit bfcc33
#include <cstring>
Packit bfcc33
#include <vector>
Packit bfcc33
#include <sstream>
Packit bfcc33
Packit bfcc33
#include "sass.h"
Packit bfcc33
#include "file.hpp"
Packit bfcc33
#include "util.hpp"
Packit bfcc33
#include "sass_context.hpp"
Packit bfcc33
#include "sass_functions.hpp"
Packit bfcc33
Packit bfcc33
namespace Sass {
Packit bfcc33
Packit bfcc33
  // helper to convert string list to vector
Packit bfcc33
  std::vector<std::string> list2vec(struct string_list* cur)
Packit bfcc33
  {
Packit bfcc33
    std::vector<std::string> list;
Packit bfcc33
    while (cur) {
Packit bfcc33
      list.push_back(cur->string);
Packit bfcc33
      cur = cur->next;
Packit bfcc33
    }
Packit bfcc33
    return list;
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
}
Packit bfcc33
Packit bfcc33
extern "C" {
Packit bfcc33
  using namespace Sass;
Packit bfcc33
Packit bfcc33
  // Allocate libsass heap memory
Packit bfcc33
  // Don't forget string termination!
Packit bfcc33
  void* ADDCALL sass_alloc_memory(size_t size)
Packit bfcc33
  {
Packit bfcc33
    void* ptr = malloc(size);
Packit bfcc33
    if (ptr == NULL)
Packit bfcc33
      out_of_memory();
Packit bfcc33
    return ptr;
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
  char* ADDCALL sass_copy_c_string(const char* str)
Packit bfcc33
  {
Packit bfcc33
    size_t len = strlen(str) + 1;
Packit bfcc33
    char* cpy = (char*) sass_alloc_memory(len);
Packit bfcc33
    std::memcpy(cpy, str, len);
Packit bfcc33
    return cpy;
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
  // Deallocate libsass heap memory
Packit bfcc33
  void ADDCALL sass_free_memory(void* ptr)
Packit bfcc33
  {
Packit bfcc33
    if (ptr) free (ptr);
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
  // caller must free the returned memory
Packit bfcc33
  char* ADDCALL sass_string_quote (const char *str, const char quote_mark)
Packit bfcc33
  {
Packit bfcc33
    std::string quoted = quote(str, quote_mark);
Packit bfcc33
    return sass_copy_c_string(quoted.c_str());
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
  // caller must free the returned memory
Packit bfcc33
  char* ADDCALL sass_string_unquote (const char *str)
Packit bfcc33
  {
Packit bfcc33
    std::string unquoted = unquote(str);
Packit bfcc33
    return sass_copy_c_string(unquoted.c_str());
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
  char* ADDCALL sass_compiler_find_include (const char* file, struct Sass_Compiler* compiler)
Packit bfcc33
  {
Packit bfcc33
    // get the last import entry to get current base directory
Packit bfcc33
    Sass_Import_Entry import = sass_compiler_get_last_import(compiler);
Packit bfcc33
    const std::vector<std::string>& incs = compiler->cpp_ctx->include_paths;
Packit bfcc33
    // create the vector with paths to lookup
Packit bfcc33
    std::vector<std::string> paths(1 + incs.size());
Packit bfcc33
    paths.push_back(File::dir_name(import->abs_path));
Packit bfcc33
    paths.insert( paths.end(), incs.begin(), incs.end() );
Packit bfcc33
    // now resolve the file path relative to lookup paths
Packit bfcc33
    std::string resolved(File::find_include(file, paths));
Packit bfcc33
    return sass_copy_c_string(resolved.c_str());
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
  char* ADDCALL sass_compiler_find_file (const char* file, struct Sass_Compiler* compiler)
Packit bfcc33
  {
Packit bfcc33
    // get the last import entry to get current base directory
Packit bfcc33
    Sass_Import_Entry import = sass_compiler_get_last_import(compiler);
Packit bfcc33
    const std::vector<std::string>& incs = compiler->cpp_ctx->include_paths;
Packit bfcc33
    // create the vector with paths to lookup
Packit bfcc33
    std::vector<std::string> paths(1 + incs.size());
Packit bfcc33
    paths.push_back(File::dir_name(import->abs_path));
Packit bfcc33
    paths.insert( paths.end(), incs.begin(), incs.end() );
Packit bfcc33
    // now resolve the file path relative to lookup paths
Packit bfcc33
    std::string resolved(File::find_file(file, paths));
Packit bfcc33
    return sass_copy_c_string(resolved.c_str());
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
  // Make sure to free the returned value!
Packit bfcc33
  // Incs array has to be null terminated!
Packit bfcc33
  // this has the original resolve logic for sass include
Packit bfcc33
  char* ADDCALL sass_find_include (const char* file, struct Sass_Options* opt)
Packit bfcc33
  {
Packit bfcc33
    std::vector<std::string> vec(list2vec(opt->include_paths));
Packit bfcc33
    std::string resolved(File::find_include(file, vec));
Packit bfcc33
    return sass_copy_c_string(resolved.c_str());
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
  // Make sure to free the returned value!
Packit bfcc33
  // Incs array has to be null terminated!
Packit bfcc33
  char* ADDCALL sass_find_file (const char* file, struct Sass_Options* opt)
Packit bfcc33
  {
Packit bfcc33
    std::vector<std::string> vec(list2vec(opt->include_paths));
Packit bfcc33
    std::string resolved(File::find_file(file, vec));
Packit bfcc33
    return sass_copy_c_string(resolved.c_str());
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
  // Get compiled libsass version
Packit bfcc33
  const char* ADDCALL libsass_version(void)
Packit bfcc33
  {
Packit bfcc33
    return LIBSASS_VERSION;
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
  // Get compiled libsass version
Packit bfcc33
  const char* ADDCALL libsass_language_version(void)
Packit bfcc33
  {
Packit bfcc33
    return LIBSASS_LANGUAGE_VERSION;
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
}
Packit bfcc33
Packit bfcc33
namespace Sass {
Packit bfcc33
Packit bfcc33
  // helper to aid dreaded MSVC debug mode
Packit bfcc33
  char* sass_copy_string(std::string str)
Packit bfcc33
  {
Packit bfcc33
    // In MSVC the following can lead to segfault:
Packit bfcc33
    // sass_copy_c_string(stream.str().c_str());
Packit bfcc33
    // Reason is that the string returned by str() is disposed before
Packit bfcc33
    // sass_copy_c_string is invoked. The string is actually a stack
Packit bfcc33
    // object, so indeed nobody is holding on to it. So it seems
Packit bfcc33
    // perfectly fair to release it right away. So the const char*
Packit bfcc33
    // by c_str will point to invalid memory. I'm not sure if this is
Packit bfcc33
    // the behavior for all compiler, but I'm pretty sure we would
Packit bfcc33
    // have gotten more issues reported if that would be the case.
Packit bfcc33
    // Wrapping it in a functions seems the cleanest approach as the
Packit bfcc33
    // function must hold on to the stack variable until it's done.
Packit bfcc33
    return sass_copy_c_string(str.c_str());
Packit bfcc33
  }
Packit bfcc33
Packit bfcc33
}