Skip to content

Commit

Permalink
CStringToString
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotchance committed Jul 23, 2017
1 parent ee4b69a commit 19497b2
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions darwin/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func AssertRtn(
fmt.Fprintf(
os.Stderr,
"Assertion failed: (%s), function %s, file %s, line %d.\n",
noarch.NullTerminatedByteSlice(expression),
noarch.NullTerminatedByteSlice(functionName),
noarch.NullTerminatedByteSlice(filePath),
noarch.CStringToString(expression),
noarch.CStringToString(functionName),
noarch.CStringToString(filePath),
lineNumber,
)
os.Exit(134)
Expand Down
6 changes: 3 additions & 3 deletions linux/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func AssertFail(
fmt.Fprintf(
os.Stderr,
"a.out: %s:%d: %s: Assertion `%s' failed.\n",
noarch.NullTerminatedByteSlice(filePath),
noarch.CStringToString(filePath),
lineNumber,
noarch.NullTerminatedByteSlice(functionName),
noarch.NullTerminatedByteSlice(expression),
noarch.CStringToString(functionName),
noarch.CStringToString(expression),
)
os.Exit(134)

Expand Down
24 changes: 12 additions & 12 deletions noarch/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ func Fopen(filePath, mode []byte) *File {
var file *os.File
var err error

sFilePath := NullTerminatedByteSlice(filePath)
sFilePath := CStringToString(filePath)

// TODO: Only some modes are supported by fopen()
// https://github.com/elliotchance/c2go/issues/89
switch NullTerminatedByteSlice(mode) {
switch CStringToString(mode) {
case "r":
file, err = os.Open(sFilePath)
case "r+":
Expand Down Expand Up @@ -107,7 +107,7 @@ func Fclose(f *File) int {
//
// Proper file access shall be available.
func Remove(filePath []byte) int {
if os.Remove(NullTerminatedByteSlice(filePath)) != nil {
if os.Remove(CStringToString(filePath)) != nil {
return -1
}

Expand All @@ -130,8 +130,8 @@ func Remove(filePath []byte) int {
//
// Proper file access shall be available.
func Rename(oldName, newName []byte) int {
from := NullTerminatedByteSlice(oldName)
to := NullTerminatedByteSlice(newName)
from := CStringToString(oldName)
to := CStringToString(newName)

if os.Rename(from, to) != nil {
return -1
Expand Down Expand Up @@ -359,13 +359,13 @@ func Fprintf(f *File, format []byte, args ...interface{}) int {
typeOfByteSlice := reflect.TypeOf([]byte(nil))
for _, arg := range args {
if reflect.TypeOf(arg) == typeOfByteSlice {
realArgs = append(realArgs, NullTerminatedByteSlice(arg.([]byte)))
realArgs = append(realArgs, CStringToString(arg.([]byte)))
} else {
realArgs = append(realArgs, arg)
}
}

n, err := fmt.Fprintf(f.OsFile, NullTerminatedByteSlice(format), realArgs...)
n, err := fmt.Fprintf(f.OsFile, CStringToString(format), realArgs...)
if err != nil {
return -1
}
Expand All @@ -384,7 +384,7 @@ func Fprintf(f *File, format []byte, args ...interface{}) int {
func Fscanf(f *File, format []byte, args ...interface{}) int {
realArgs := prepareArgsForScanf(args)

n, err := fmt.Fscanf(f.OsFile, NullTerminatedByteSlice(format), realArgs...)
n, err := fmt.Fscanf(f.OsFile, CStringToString(format), realArgs...)
if err != nil {
panic(err)
return -1
Expand Down Expand Up @@ -621,13 +621,13 @@ func Printf(format []byte, args ...interface{}) int {
typeOfByteSlice := reflect.TypeOf([]byte(nil))
for _, arg := range args {
if reflect.TypeOf(arg) == typeOfByteSlice {
realArgs = append(realArgs, NullTerminatedByteSlice(arg.([]byte)))
realArgs = append(realArgs, CStringToString(arg.([]byte)))
} else {
realArgs = append(realArgs, arg)
}
}

n, _ := fmt.Printf(NullTerminatedByteSlice(format), realArgs...)
n, _ := fmt.Printf(CStringToString(format), realArgs...)

return n
}
Expand All @@ -645,7 +645,7 @@ func Printf(format []byte, args ...interface{}) int {
// destination, but it also appends a newline character at the end automatically
// (which fputs does not).
func Puts(str []byte) int {
n, _ := fmt.Println(NullTerminatedByteSlice(str))
n, _ := fmt.Println(CStringToString(str))

return n
}
Expand All @@ -660,7 +660,7 @@ func Puts(str []byte) int {
// string.
func Scanf(format []byte, args ...interface{}) int {
realArgs := prepareArgsForScanf(args)
n, _ := fmt.Scanf(NullTerminatedByteSlice(format), realArgs...)
n, _ := fmt.Scanf(CStringToString(format), realArgs...)
finalizeArgsForScanf(realArgs, args)

return n
Expand Down
2 changes: 1 addition & 1 deletion noarch/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ func Strlen(a []byte) int {
// TODO: The transpiler should have a syntax that means this proxy function
// does not need to exist.

return len(NullTerminatedByteSlice(a))
return len(CStringToString(a))
}
4 changes: 2 additions & 2 deletions noarch/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"reflect"
)

// NullTerminatedByteSlice returns a string that contains all the bytes in the
// CStringToString returns a string that contains all the bytes in the
// provided C string up until the first NULL character.
func NullTerminatedByteSlice(s []byte) string {
func CStringToString(s []byte) string {
if s == nil {
return ""
}
Expand Down
4 changes: 2 additions & 2 deletions noarch/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestNullTerminatedByteSlice(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NullTerminatedByteSlice(tt.args.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NullTerminatedByteSlice() = %v, want %v", got, tt.want)
if got := CStringToString(tt.args.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("CStringToString() = %v, want %v", got, tt.want)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions transpiler/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func transpileCallExpr(n *ast.CallExpr, p *program.Program) (
if arraySize != -1 {
p.AddImport("github.com/elliotchance/c2go/noarch")
e = util.NewCallExpr(
"noarch.NullTerminatedByteSlice",
"noarch.CStringToString",
&goast.SliceExpr{X: e},
)
}
Expand All @@ -134,7 +134,7 @@ func transpileCallExpr(n *ast.CallExpr, p *program.Program) (
if i > len(functionDef.ArgumentTypes)-1 &&
(eType == "char *" || eType == "char*") {
p.AddImport("github.com/elliotchance/c2go/noarch")
e = util.NewCallExpr("noarch.NullTerminatedByteSlice", e)
e = util.NewCallExpr("noarch.CStringToString", e)
}
}

Expand Down

0 comments on commit 19497b2

Please sign in to comment.