[vala] posix: Add parse and to_string methods and string.printf
- From: JÃrg Billeter <juergbi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala] posix: Add parse and to_string methods and string.printf
- Date: Sun, 13 May 2012 10:33:13 +0000 (UTC)
commit 5a79795c18f849a63f7d7a47e5229ae2902d0eae
Author: Aaron Andersen <aaron fosslib net>
Date: Sun May 13 12:28:24 2012 +0200
posix: Add parse and to_string methods and string.printf
Fixes bug 666436.
codegen/valaccodemethodcallmodule.vala | 77 +++++++++++++++++++
vapi/posix.vapi | 130 ++++++++++++++++++++++++++++++++
2 files changed, 207 insertions(+), 0 deletions(-)
---
diff --git a/codegen/valaccodemethodcallmodule.vala b/codegen/valaccodemethodcallmodule.vala
index fa3f727..296efc1 100644
--- a/codegen/valaccodemethodcallmodule.vala
+++ b/codegen/valaccodemethodcallmodule.vala
@@ -48,6 +48,8 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
// Enum.VALUE.to_string()
var en = (Enum) ma.inner.value_type.data_type;
ccall.call = new CCodeIdentifier (generate_enum_tostring_function (en));
+ } else if (context.profile == Profile.POSIX && ma.inner != null && ma.inner.value_type != null && ma.inner.value_type.data_type == string_type.data_type && ma.member_name == "printf") {
+ ccall.call = new CCodeIdentifier (generate_string_printf_function ());
}
} else if (itype is SignalType) {
var sig_type = (SignalType) itype;
@@ -822,5 +824,80 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
return to_string_func;
}
+
+ private string generate_string_printf_function () {
+ if (!add_wrapper ("string_printf")) {
+ // wrapper already defined
+ return "string_printf";
+ }
+
+ // declaration
+ var function = new CCodeFunction ("string_printf", "char*");
+ function.add_parameter (new CCodeParameter ("format", "const char*"));
+ function.add_parameter (new CCodeParameter.with_ellipsis ());
+ function.modifiers = CCodeModifiers.STATIC;
+
+ // definition
+ push_context (new EmitContext ());
+ push_function (function);
+
+ ccode.add_declaration ("int", new CCodeVariableDeclarator ("length"));
+ ccode.add_declaration ("va_list", new CCodeVariableDeclarator ("ap"));
+ ccode.add_declaration ("char*", new CCodeVariableDeclarator ("result"));
+
+ var va_start = new CCodeFunctionCall (new CCodeIdentifier ("va_start"));
+ va_start.add_argument (new CCodeIdentifier ("ap"));
+ va_start.add_argument (new CCodeIdentifier ("format"));
+
+ ccode.add_expression (va_start);
+
+ var vsnprintf = new CCodeFunctionCall (new CCodeIdentifier ("vsnprintf"));
+ vsnprintf.add_argument (new CCodeConstant ("NULL"));
+ vsnprintf.add_argument (new CCodeConstant ("0"));
+ vsnprintf.add_argument (new CCodeIdentifier ("format"));
+ vsnprintf.add_argument (new CCodeIdentifier ("ap"));
+
+ ccode.add_assignment (new CCodeIdentifier ("length"), new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, vsnprintf, new CCodeConstant ("1")));
+
+ var va_end = new CCodeFunctionCall (new CCodeIdentifier ("va_end"));
+ va_end.add_argument (new CCodeIdentifier ("ap"));
+
+ ccode.add_expression (va_end);
+
+ var malloc = new CCodeFunctionCall (new CCodeIdentifier ("malloc"));
+ malloc.add_argument (new CCodeIdentifier ("length"));
+
+ ccode.add_assignment (new CCodeIdentifier ("result"), malloc);
+
+ va_start = new CCodeFunctionCall (new CCodeIdentifier ("va_start"));
+ va_start.add_argument (new CCodeIdentifier ("ap"));
+ va_start.add_argument (new CCodeIdentifier ("format"));
+
+ ccode.add_expression (va_start);
+
+ vsnprintf = new CCodeFunctionCall (new CCodeIdentifier ("vsnprintf"));
+ vsnprintf.add_argument (new CCodeIdentifier ("result"));
+ vsnprintf.add_argument (new CCodeIdentifier ("length"));
+ vsnprintf.add_argument (new CCodeIdentifier ("format"));
+ vsnprintf.add_argument (new CCodeIdentifier ("ap"));
+
+ ccode.add_expression (vsnprintf);
+
+ va_end = new CCodeFunctionCall (new CCodeIdentifier ("va_end"));
+ va_end.add_argument (new CCodeIdentifier ("ap"));
+
+ ccode.add_expression (va_end);
+
+ ccode.add_return (new CCodeIdentifier ("result"));
+
+ // append to file
+ cfile.add_include ("stdarg.h");
+ cfile.add_function_declaration (function);
+ cfile.add_function (function);
+
+ pop_context ();
+
+ return "string_printf";
+ }
}
diff --git a/vapi/posix.vapi b/vapi/posix.vapi
index 9329ace..d6f684f 100644
--- a/vapi/posix.vapi
+++ b/vapi/posix.vapi
@@ -26,106 +26,220 @@
[CCode (cname = "bool", cheader_filename = "stdbool.h", default_value = "false")]
[BooleanType]
public struct bool {
+ public inline unowned string to_string () {
+ if (this) {
+ return "true";
+ } else {
+ return "false";
+ }
+ }
+
+ public static inline bool parse (string str) {
+ if (str == "true") {
+ return true;
+ } else {
+ return false;
+ }
+ }
}
[CCode (cname = "char", default_value = "\'\\0\'")]
[IntegerType (rank = 2, min = 0, max = 127)]
public struct char {
+ public inline string to_string () {
+ return "%c".printf (this);
+ }
}
[CCode (cname = "unsigned char", default_value = "\'\\0\'")]
[IntegerType (rank = 3, min = 0, max = 255)]
public struct uchar {
+ public inline string to_string () {
+ return "%hhu".printf (this);
+ }
}
[CCode (cname = "int", default_value = "0")]
[IntegerType (rank = 6)]
public struct int {
+ public inline string to_string () {
+ return "%d".printf (this);
+ }
+
+ [CCode (cname = "atoi", cheader_filename = "stdlib.h")]
+ public static int parse (string str);
}
[CCode (cname = "unsigned int", default_value = "0U")]
[IntegerType (rank = 7)]
public struct uint {
+ public inline string to_string () {
+ return "%u".printf (this);
+ }
}
[CCode (cname = "short", default_value = "0")]
[IntegerType (rank = 4, min = -32768, max = 32767)]
public struct short {
+ public inline string to_string () {
+ return "%hi".printf (this);
+ }
}
[CCode (cname = "unsigned short", default_value = "0U")]
[IntegerType (rank = 5, min = 0, max = 65535)]
public struct ushort {
+ public inline string to_string () {
+ return "%hu".printf (this);
+ }
}
[CCode (cname = "long", default_value = "0L")]
[IntegerType (rank = 8)]
public struct long {
+ public inline string to_string () {
+ return "%li".printf (this);
+ }
+
+ [CCode (cname = "atol", cheader_filename = "stdlib.h")]
+ public static long parse (string str);
}
[CCode (cname = "unsigned long", default_value = "0UL")]
[IntegerType (rank = 9)]
public struct ulong {
+ public inline string to_string () {
+ return "%lu".printf (this);
+ }
}
[CCode (cname = "size_t", cheader_filename = "sys/types.h", default_value = "0UL")]
[IntegerType (rank = 9)]
public struct size_t {
+ public inline string to_string () {
+ return "%zu".printf (this);
+ }
}
[CCode (cname = "ssize_t", cheader_filename = "sys/types.h", default_value = "0L")]
[IntegerType (rank = 8)]
public struct ssize_t {
+ public inline string to_string () {
+ return "%zi".printf (this);
+ }
}
[CCode (cname = "int8_t", cheader_filename = "stdint.h", default_value = "0")]
[IntegerType (rank = 1, min = -128, max = 127)]
public struct int8 {
+ [CCode (cname = "PRIi8", cheader_filename = "inttypes.h")]
+ public const string FORMAT;
+
+ public inline string to_string () {
+ return ("%" + FORMAT).printf (this);
+ }
}
[CCode (cname = "uint8_t", cheader_filename = "stdint.h", default_value = "0U")]
[IntegerType (rank = 3, min = 0, max = 255)]
public struct uint8 {
+ [CCode (cname = "PRIu8", cheader_filename = "inttypes.h")]
+ public const string FORMAT;
+
+ public inline string to_string () {
+ return ("%" + FORMAT).printf (this);
+ }
}
[CCode (cname = "int16_t", cheader_filename = "stdint.h", default_value = "0")]
[IntegerType (rank = 4, min = -32768, max = 32767)]
public struct int16 {
+ [CCode (cname = "PRIi16", cheader_filename = "inttypes.h")]
+ public const string FORMAT;
+
+ public inline string to_string () {
+ return ("%" + FORMAT).printf (this);
+ }
}
[CCode (cname = "uint16_t", cheader_filename = "stdint.h", default_value = "0U")]
[IntegerType (rank = 5, min = 0, max = 65535)]
public struct uint16 {
+ [CCode (cname = "PRIu16", cheader_filename = "inttypes.h")]
+ public const string FORMAT;
+
+ public inline string to_string () {
+ return ("%" + FORMAT).printf (this);
+ }
}
[CCode (cname = "int32_t", cheader_filename = "stdint.h", default_value = "0")]
[IntegerType (rank = 6)]
public struct int32 {
+ [CCode (cname = "PRIi32", cheader_filename = "inttypes.h")]
+ public const string FORMAT;
+
+ public inline string to_string () {
+ return ("%" + FORMAT).printf (this);
+ }
}
[CCode (cname = "uint32_t", cheader_filename = "stdint.h", default_value = "0U")]
[IntegerType (rank = 7)]
public struct uint32 {
+ [CCode (cname = "PRIu32", cheader_filename = "inttypes.h")]
+ public const string FORMAT;
+
+ public inline string to_string () {
+ return ("%" + FORMAT).printf (this);
+ }
}
[CCode (cname = "int64_t", cheader_filename = "stdint.h", default_value = "0LL")]
[IntegerType (rank = 10)]
public struct int64 {
+ [CCode (cname = "PRIi64", cheader_filename = "inttypes.h")]
+ public const string FORMAT;
+
+ public inline string to_string () {
+ return ("%" + FORMAT).printf (this);
+ }
+
+ [CCode (cname = "strtoll", cheader_filename = "stdlib.h")]
+ public static int64 parse (string str, out unowned string? end = null, int base = 10);
}
[CCode (cname = "uint64_t", cheader_filename = "stdint.h", default_value = "0ULL")]
[IntegerType (rank = 11)]
public struct uint64 {
+ [CCode (cname = "PRIu64", cheader_filename = "inttypes.h")]
+ public const string FORMAT;
+
+ public inline string to_string () {
+ return ("%" + FORMAT).printf (this);
+ }
+
+ [CCode (cname = "strtoull", cheader_filename = "stdlib.h")]
+ public static uint64 parse (string str, out unowned string? end = null, int base = 10);
}
[CCode (cname = "float", default_value = "0.0F")]
[FloatingType (rank = 1)]
public struct float {
+ public inline string to_string () {
+ return "%.8g".printf (this);
+ }
}
[CCode (cname = "double", default_value = "0.0")]
[FloatingType (rank = 2)]
public struct double {
+ public inline string to_string () {
+ return "%.17g".printf (this);
+ }
+
+ [CCode (cname = "strtod", cheader_filename = "stdlib.h")]
+ public static double parse (string str, out unowned string? end = null);
}
[CCode (cheader_filename = "time.h")]
@@ -139,14 +253,30 @@ public struct time_t {
[Immutable]
[CCode (cname = "char", const_cname = "const char", copy_function = "strdup", free_function = "free", cheader_filename = "stdlib.h,string.h")]
public class string {
+ [Deprecated (replacement = "int.parse")]
[CCode (cname="atoi")]
public int to_int();
+ [Deprecated (replacement = "long.parse")]
[CCode (cname="atol")]
public long to_long();
+ [Deprecated (replacement = "int64.parse")]
[CCode (cname="atoll")]
public int64 to_int64();
+ [Deprecated (replacement = "string.length")]
[CCode (cname="strlen")]
public int len();
+
+ [PrintfFormat]
+ public string printf (...);
+
+ public inline unowned string to_string () {
+ return this;
+ }
+
+ public int length {
+ [CCode (cname = "strlen")]
+ get;
+ }
}
[CCode (cname="printf", cheader_filename = "stdio.h")]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]