Blame src/sass.cpp

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