|
Packit |
bfcc33 |
## Example operation.c
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
```C
|
|
Packit |
bfcc33 |
#include <stdio.h>
|
|
Packit |
bfcc33 |
#include <string.h>
|
|
Packit |
bfcc33 |
#include "sass/values.h"
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
int main( int argc, const char* argv[] )
|
|
Packit |
bfcc33 |
{
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// create two new sass values to be added
|
|
Packit |
bfcc33 |
union Sass_Value* string = sass_make_string("String");
|
|
Packit |
bfcc33 |
union Sass_Value* number = sass_make_number(42, "nits");
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// invoke the add operation which returns a new sass value
|
|
Packit |
bfcc33 |
union Sass_Value* total = sass_value_op(ADD, string, number);
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// no further use for the two operands
|
|
Packit |
bfcc33 |
sass_delete_value(string);
|
|
Packit |
bfcc33 |
sass_delete_value(number);
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// this works since libsass will always return a
|
|
Packit |
bfcc33 |
// string for add operations with a string as the
|
|
Packit |
bfcc33 |
// left hand side. But you should never rely on it!
|
|
Packit |
bfcc33 |
puts(sass_string_get_value(total));
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// invoke stringification (uncompressed with precision of 5)
|
|
Packit |
bfcc33 |
union Sass_Value* result = sass_value_stringify(total, false, 5);
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// no further use for the sum
|
|
Packit |
bfcc33 |
sass_delete_value(total);
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// print the result - you may want to make
|
|
Packit |
bfcc33 |
// sure result is indeed a string, altough
|
|
Packit |
bfcc33 |
// stringify guarantees to return a string
|
|
Packit |
bfcc33 |
// if (sass_value_is_string(result)) {}
|
|
Packit |
bfcc33 |
// really depends on your level of paranoia
|
|
Packit |
bfcc33 |
puts(sass_string_get_value(result));
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// finally free result
|
|
Packit |
bfcc33 |
sass_delete_value(result);
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
// exit status
|
|
Packit |
bfcc33 |
return 0;
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
}
|
|
Packit |
bfcc33 |
```
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
## Compile operation.c
|
|
Packit |
bfcc33 |
|
|
Packit |
bfcc33 |
```bash
|
|
Packit |
bfcc33 |
gcc -c operation.c -o operation.o
|
|
Packit |
bfcc33 |
gcc -o operation operation.o -lsass
|
|
Packit |
bfcc33 |
./operation # => String42nits
|
|
Packit |
bfcc33 |
```
|