Skip to content

Instantly share code, notes, and snippets.

/* cpu-info.vala
*
* requirements:
* * lscpu (if not exists, query() always throws an error)
*
* deps:
* * glib-2.0
* * gobject-2.0
*
* Written in 2018 by kosmolot (kosmolot17@yandex.com)
@kosmolot
kosmolot / rand_tz.sh
Created January 21, 2018 07:51
Get a random timezone
#!usr/bin/env bash
rand_tz() {
list=`find /usr/share/zoneinfo/ -regex "[a-zA-Z0-9/-]+" -type f`
randf=`shuf -n1 -e $list`
prefix=/usr/share/zoneinfo/
echo ${randf#$prefix}
}
@kosmolot
kosmolot / wildcards.vala
Created January 1, 2018 11:07
Wildcards in vala
public class Container<T> : Object {
public T value { get; set; }
public Container (T value) {
_value = value;
}
}
void main () {
Container<void*> obj;
@kosmolot
kosmolot / pointer-to-array.vala
Last active May 7, 2021 23:04
A pointer to an array without copying (Vala)
unowned int[] pointer_to_array (int* p, uint length) {
return ((int[]) p)[0:length];
}
void main () {
int[] array = {111, 222, 333};
int* p = array;
unowned int[] array2 = pointer_to_array(p, array.length);
array[0] = 777;
@kosmolot
kosmolot / serialization.vala
Last active March 14, 2021 11:29
Vala partial serialization (using json-glib)
/*
* An example of serialization.
* This example serializes and deserializes only 'x' property.
*
* vala: 0.36.3
* json-glib: 1.3.2
*/
public class Point : GLib.Object, Json.Serializable {
public int x { get; set; }
@kosmolot
kosmolot / cubic_bezier.vala
Created July 19, 2017 15:42
cubic_bezier method (Vala)
public float cubic_bezier (float t, float a, float b, float c, float d) {
float u = 1 - t;
float tt = t * t;
float uu = u * u;
float uuu = uu * u;
float ttt = tt * t;
return (uuu * a) + (3 * uu * t * b) + (3 * u * tt * c) + (ttt * d);
}
@kosmolot
kosmolot / Half.vala
Created July 8, 2017 10:39
Half-precision floating-point. See https://stackoverflow.com/a/22283596
/**
* half-precision floating-point
*/
public struct Half {
public static int16 float_to_half (float single) {
int16 flt16;
int32 flt32 = 0;
Memory.copy(&flt32, &single, sizeof(float));
flt16 = (int16) (((flt32 & 0x7fffffff) >> 13) - (0x38000000 >> 13));
flt16 |= ((flt32 & 0x80000000) >> 16);
@kosmolot
kosmolot / array_concat.vala
Created July 8, 2017 10:36
Array concatenation (Vala)
/**
* This function doesn't use generic,
* because of 'sizeof(T)' problem.
*/
public void[] array_concat (void[] a, void[] b) {
void[] c = new void[a.length + b.length];
Memory.copy(c, a, a.length);
Memory.copy(&c[a.length], b, b.length);
return c;
}
@kosmolot
kosmolot / read_all_bytes_from_stream.vala
Created July 8, 2017 10:32
Reads all bytes from an InputStream (Vala)
public uint8[] read_all_bytes_from_stream (InputStream stream, ulong buf_size) throws Error {
uint8[] data = {};
uint8[] buf = new uint8[buf_size];
ssize_t bytes;
ulong total = 0;
while (true) {
bytes = stream.read(buf);
if (bytes <= 0) break;
ulong size = (ulong) (total + bytes);
@kosmolot
kosmolot / gcrypt-hash-example.vala
Last active July 19, 2017 15:44
A Vala GCrypt.Hash example
using GCrypt;
using Posix;
void main () {
Hash hash;
GCrypt.Error err = Hash.open(out hash, Hash.Algorithm.SHA256, Hash.Flag.SECURE);
if (err != 0) {
print("Error: %s\n", err.to_string());
Process.exit(EXIT_FAILURE);
}