Repository: Konstantin8105/c4go
Pull request #285 (2019-02-26 10:20:43)

CSTD


Patch:
diff --git a/README.md b/README.md
index ced67fb..c3944bd 100644
--- a/README.md
+++ b/README.md
@@ -125,7 +125,7 @@ func main() {
              float.h	          	    undefined
             iso646.h	          	    undefined
             limits.h	          	    undefined
-            locale.h	       0/3	           0%
+            locale.h	       3/3	         100%
               math.h	     37/58	        63.8%
             setjmp.h	       0/3	           0%
             signal.h	       0/3	           0%
@@ -134,7 +134,7 @@ func main() {
              stdio.h	     35/46	        76.1%
             stdlib.h	     33/47	        70.2%
             string.h	     16/24	        66.7%
-              time.h	      7/15	        46.7%
+              time.h	     13/15	        86.7%
              wchar.h	      3/68	        4.41%
             wctype.h	      0/22	           0%
 ```
diff --git a/noarch/locale.go b/noarch/locale.go
new file mode 100644
index 0000000..9fb3f9f
--- /dev/null
+++ b/noarch/locale.go
@@ -0,0 +1,17 @@
+package noarch
+
+type Lconv struct {
+	Currency_symbol []byte
+	IntCurrSymbol   []byte
+}
+
+func Setlocale(category int, locale []byte) []byte {
+	return []byte("dd")
+}
+
+func Localeconv() []Lconv {
+	var l Lconv
+	l.Currency_symbol = []byte("fake data")
+	l.IntCurrSymbol = []byte("fake data")
+	return append([]Lconv{}, l)
+}
diff --git a/noarch/time.go b/noarch/time.go
index c8238f1..49bf849 100644
--- a/noarch/time.go
+++ b/noarch/time.go
@@ -127,3 +127,13 @@ func Asctime(tm []Tm) []byte {
 		tm[0].TmMin, tm[0].TmSec,
 		1900+tm[0].TmYear))
 }
+
+type ClockT int64
+
+func Clock() ClockT {
+	return ClockT(time.Now().Unix())
+}
+
+func Difftime(end, start TimeT) float64 {
+	return float64(end - start)
+}
diff --git a/program/definition_function.go b/program/definition_function.go
index b461f88..2f31a50 100644
--- a/program/definition_function.go
+++ b/program/definition_function.go
@@ -310,6 +310,12 @@ var builtInFunctionDefinitions = map[string][]string{
 		"struct tm * gmtime(const time_t *) -> noarch.Gmtime",
 		"time_t mktime(struct tm *) -> noarch.Mktime",
 		"char * asctime(struct tm *) -> noarch.Asctime",
+		"clock_t clock(void) -> noarch.Clock",
+		"double difftime(time_t , time_t ) -> noarch.Difftime",
+	},
+	"locale.h": {
+		"struct lconv * localeconv(void) -> noarch.Localeconv",
+		"char * setlocale(int , const char * ) -> noarch.Setlocale",
 	},
 	"termios.h": {
 		// termios.h
diff --git a/program/definition_struct.go b/program/definition_struct.go
index a47007b..db1ec27 100644
--- a/program/definition_struct.go
+++ b/program/definition_struct.go
@@ -55,6 +55,24 @@ func (p *Program) initializationStructs() {
 		},
 	}
 
+	// locale.h
+	p.Structs["struct lconv"] = &Struct{
+		Name: "struct lconv",
+		Type: StructType,
+		Fields: map[string]interface{}{
+			"currency_symbol": "char *",
+			"int_curr_symbol": "char *",
+		},
+	}
+	p.Structs["c4go_struct lconv"] = &Struct{
+		Name: "github.com/Konstantin8105/c4go/noarch.Lconv",
+		Type: StructType,
+		Fields: map[string]interface{}{
+			"currency_symbol": "Currency_symbol",
+			"int_curr_symbol": "IntCurrSymbol",
+		},
+	}
+
 	// time.h
 	p.Structs["struct tm"] = &Struct{
 		Name: "struct tm",
diff --git a/program/definition_type.go b/program/definition_type.go
index 7ea9347..4a94ed7 100644
--- a/program/definition_type.go
+++ b/program/definition_type.go
@@ -79,4 +79,7 @@ var DefinitionType = map[string]string{
 	// sys/resource.h
 	"__rusage_who":   "int",
 	"__rusage_who_t": "int",
+
+	// time.h
+	"clock_t": "github.com/Konstantin8105/c4go/noarch.ClockT",
 }


Test patch:
diff --git a/cstd_test.go b/cstd_test.go
index 6523a34..866fc4f 100644
--- a/cstd_test.go
+++ b/cstd_test.go
@@ -85,7 +85,7 @@ var cstd = map[string][]string{
 	"iso646.h": {},
 	"limits.h": {},
 	"locale.h": {
-		"struct lconv",
+		"lconv", // "struct lconv",
 		"setlocale",
 		"localeconv",
 	},
@@ -263,7 +263,7 @@ var cstd = map[string][]string{
 		"clock_t",
 		"size_t",
 		"time_t",
-		"struct tm",
+		"tm", // "struct tm",
 	},
 	"wchar.h": {
 
@@ -328,7 +328,7 @@ var cstd = map[string][]string{
 		"wcsftime",
 		"mbstate_t",
 		"size_t",
-		"struct tm",
+		"tm", // "struct tm",
 		"wchar_t",
 		"wint_t",
 		"NULL",
diff --git a/tests/locale.c b/tests/locale.c
new file mode 100644
index 0000000..e6b6ee6
--- /dev/null
+++ b/tests/locale.c
@@ -0,0 +1,20 @@
+#include "tests.h"
+#include <locale.h>
+
+
+int main()
+{
+	plan(2);
+
+	setlocale (LC_MONETARY,"");
+	struct lconv * lc;
+	lc=localeconv();
+	// Local Currency Symbol
+	is_true(strlen(lc->currency_symbol) > 0);
+
+	// International Currency Symbol
+	is_true(strlen(lc->int_curr_symbol) > 0);
+
+	done_testing();
+}
+
diff --git a/tests/time.c b/tests/time.c
index 2a10480..cfb9e84 100644
--- a/tests/time.c
+++ b/tests/time.c
@@ -109,10 +109,45 @@ void test_gettime()
 	(void) r;
 }
 
+void test_clock()
+{
+	clock_t c = clock();
+	unsigned long l = c;
+	is_true(l > 0);
+}
+
+void test_difftime()
+{
+	time_t now;
+	struct tm newyear;
+	double seconds;
+	
+	time(&now);
+
+	double d = now;
+	d /= 1000;
+	long l = d;
+	l *= 1000;
+	now = ((time_t)(l));
+	
+	newyear = *localtime(&now);
+	
+	newyear.tm_hour = 0; newyear.tm_min = 0; newyear.tm_sec = 0;
+	newyear.tm_mon = 0;  newyear.tm_mday = 1;
+	
+	seconds = difftime(now,mktime(&newyear));
+	
+	printf ("%.f seconds since new year in the current timezone.\n", seconds);
+}
+
+void test_CLOCKS_PER_SEC()
+{
+	printf("%d\n",CLOCKS_PER_SEC);
+}
 
 int main()
 {
-    plan(23);
+    plan(24);
 
     // sorting in according to :
     // http://www.cplusplus.com/reference/ctime/
@@ -121,6 +156,9 @@ int main()
     START_TEST(gmtime);
     START_TEST(mktime);
     START_TEST(time);
+	START_TEST(clock);
+	START_TEST(difftime);
+	START_TEST(CLOCKS_PER_SEC);
 
     // sys/time.h
     START_TEST(timeval);