func compile(infile, outfile string, typ C.int, comments C.int) error { ctx := C.sass_new_file_context() defer C.sass_free_file_context(ctx) ctx.options.output_style = typ ctx.options.source_comments = comments ctx.input_path = C.CString(infile) ret := C.sass_compile_file(ctx) if ret != 0 || ctx.error_status != 0 { return errors.New(C.GoString(ctx.error_message)) } out, err := os.Create(outfile) if err != nil { return err } out.Write([]byte(C.GoString(ctx.output_string))) out.Close() return nil }
func CompileFile(goCtx *FileContext) { // set up the underlying C context struct cCtx := C.sass_new_file_context() cCtx.input_path = C.CString(goCtx.InputPath) defer C.free(unsafe.Pointer(cCtx.input_path)) cCtx.options.output_style = C.int(goCtx.Options.OutputStyle) if goCtx.Options.SourceComments { cCtx.options.source_comments = C.int(1) } else { cCtx.options.source_comments = C.int(0) } cCtx.options.include_paths = C.CString(strings.Join(goCtx.Options.IncludePaths, ":")) defer C.free(unsafe.Pointer(cCtx.options.include_paths)) cCtx.options.image_path = C.CString(goCtx.Options.ImagePath) defer C.free(unsafe.Pointer(cCtx.options.image_path)) // call the underlying C compile function to populate the C context C.sass_compile_file(cCtx) // extract values from the C context to populate the Go context object goCtx.OutputString = C.GoString(cCtx.output_string) goCtx.ErrorStatus = int(cCtx.error_status) goCtx.ErrorMessage = C.GoString(cCtx.error_message) // don't forget to free the C context! C.sass_free_file_context(cCtx) }