Repository: Konstantin8105/c4go
Pull request #461 (2019-06-15 19:49:00)

stdio: fscan test

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/461?src=pr&el=h1) Report
> Merging [#461](https://codecov.io/gh/Konstantin8105/c4go/pull/461?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/6c8a7d86f2f26cb2d16d449217553b540678c4e3?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/Konstantin8105/c4go/pull/461/graphs/tree.svg?width=650&token=esH2rHXlZ2&height=150&src=pr)](https://codecov.io/gh/Konstantin8105/c4go/pull/461?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #461      +/-   ##
==========================================
+ Coverage   84.77%   84.78%   +0.01%     
==========================================
  Files         192      192              
  Lines       11854    11859       +5     
==========================================
+ Hits        10049    10055       +6     
  Misses       1316     1316              
+ Partials      489      488       -1
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/461?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [noarch/stdio.go](https://codecov.io/gh/Konstantin8105/c4go/pull/461/diff?src=pr&el=tree#diff-bm9hcmNoL3N0ZGlvLmdv) | `81.45% <100%> (+0.64%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/461?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/461?src=pr&el=footer). Last update [6c8a7d8...ea3424a](https://codecov.io/gh/Konstantin8105/c4go/pull/461?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/noarch/stdio.go b/noarch/stdio.go
index b2473c9..8015364 100644
--- a/noarch/stdio.go
+++ b/noarch/stdio.go
@@ -1,11 +1,13 @@
 package noarch
 
 import (
+	"bytes"
 	"fmt"
 	"io"
 	"io/ioutil"
 	"os"
 	"reflect"
+	"strings"
 )
 
 // Programs generated by c4go will reference noarch.Stdin instead of os.Stdin
@@ -406,13 +408,16 @@ func Fprintf(f *File, format []byte, args ...interface{}) int32 {
 // string.
 func Fscanf(f *File, format []byte, args ...interface{}) int32 {
 	realArgs := prepareArgsForScanf(args)
+	goFormat := CStringToString(format)
+	iter := 0
+again:
 
-	// format is ignored
-	// See https://github.com/Konstantin8105/c4go/issues/607
-	_ = format
-
-	n, err := fmt.Fscan(f.OsFile, realArgs...)
+	n, err := fmt.Fscanf(f.OsFile, goFormat, realArgs...)
 	if err != nil {
+		if strings.Contains(err.Error(), "unexpected newline") && iter < 100 {
+			iter++
+			goto again
+		}
 		return -1
 	}
 
@@ -426,7 +431,7 @@ func finalizeArgsForScanf(realArgs []interface{}, args []interface{}) {
 	for i, arg := range realArgs {
 		if reflect.TypeOf(arg) == typeOfStringRef {
 			s := *arg.(*string)
-			copy(args[i].([]byte), []byte(s))
+			copy(args[i].([]byte), []byte(s+"\x00"))
 		} else {
 			GoPointerToCPointer(arg, args[i])
 		}
@@ -699,8 +704,9 @@ func Scanf(format []byte, args ...interface{}) int32 {
 }
 
 func Sscanf(str []byte, format []byte, args ...interface{}) int32 {
+	wws := bytes.Replace(str, []byte("\n"), []byte{' '}, -1) // without newline
 	realArgs := prepareArgsForScanf(args)
-	n, _ := fmt.Sscanf(CStringToString(str), CStringToString(format), realArgs...)
+	n, _ := fmt.Sscanf(CStringToString(wws), CStringToString(format), realArgs...)
 	finalizeArgsForScanf(realArgs, args)
 	return int32(n)
 }


Test patch:
diff --git a/tests/stdio.c b/tests/stdio.c
index 1bc30bd..0847b5e 100644
--- a/tests/stdio.c
+++ b/tests/stdio.c
@@ -196,7 +196,7 @@ void test_fscanf()
     pFile = fopen("./testdata/myfile2.txt", "w+");
     is_not_null(pFile);
 
-    fprintf(pFile, "%f \r\n %s %d %s", 3.1416, "PI", 42, "end");
+    fprintf(pFile, "%f \r\n\t\n %s %d %s", 3.1416, "PI", 42, "end");
     rewind(pFile);
     fscanf(pFile, "%f", &f);
     fscanf(pFile, "%s", str);
@@ -229,6 +229,20 @@ void test_fscanf()
 
     // remove temp file
     is_eq(remove("./testdata/myfile2.txt"), 0)
+
+    // test file fscan.txt
+	FILE * in = fopen("./tests/stdio_fscan.txt", "r");
+	char dummy[128];
+
+	for(int iter = 0;iter < 10;iter++)
+	{
+	     int e = fscanf(in,"%s", dummy);
+	     printf("fscan : iter[%d] : err = %d\n", iter, e);
+	     if (e < 0) {
+	  	 	break;
+	     }
+	     printf("fscan : iter[%d] : str = %s\n", iter, dummy);
+	}
 }
 
 void test_fgetc()
@@ -589,11 +603,13 @@ void test_getline()
 
 void test_sscanf()
 {
-    char sentence[] = "Rudolph is 12 years old";
+    char sentence[] = "Example\nRudolph is 12 years old";
+    char header[50];
     char temp[50];
     char str[20];
     int i;
-    sscanf(sentence, "%s %s %d", str, temp, &i);
+    sscanf(sentence, "%s %s %s %d", header, str, temp, &i);
+	printf("Header: %s\n", header);
     is_eq(i, 12);
     is_streq(str, "Rudolph");
 }
diff --git a/tests/stdio_fscan.txt b/tests/stdio_fscan.txt
new file mode 100644
index 0000000..0c4a407
--- /dev/null
+++ b/tests/stdio_fscan.txt
@@ -0,0 +1,7 @@
+
+#-----------#
+# Example 1 #
+#-----------#
+
+#========= | POINTS | =========# 
+9 # number of points