proc getLen*(filepath: string): int =
const command = """expr $(mediainfo --Inform="Audio;%Duration%" """
return execProcess(command & """"""" & filepath & """") / 1000""").parseInt
I'm executing this command in bash and getting result "347":
expr $(mediainfo --Inform="Audio;%Duration%" "mytrack.ogg") / 1000
Error is:
Traceback (most recent call last)
do.nim(45) do
do.nim(33) getLen
strutils.nim parseInt
Error: unhandled exception: invalid integer: 347
[ValueError]
Nim Compiler Version 0.14.3 (2016-08-07) [Linux: amd64] Ubuntu 15.10Why not use echo to see the content of the string you pass to parseInt?
Note: for bash command expr / indicates integer division. For Nim / is floating point division, you may use div for integer division, or you may round the floating point result. So may guess is that you pass a floating point number to parseInt, which it may not like.
The problem is that the result of execProcess contains trailing whitespace (a newline in this case). Try:
return execProcess(command & """""" & filepath & """") / 1000""").strip.parseInt
@luntik2012 Sorry, saw my mistake short time period after sending the post...
@Flyx For such a case something like a hexdump echo of strings would be nice. I was looking for such proc some months ago -- is there something available?
@luntik2012: execProcess executes the command within a shell, which is probably responsible for the trailing newline.
@Stefan_Salewski: Well, it's easy enough to implement:
import strutils, future
proc hexDump(s: string) =
echo lc[toHex(int8(c)) | (c <- s), string].join(" ")
hexDump("123\n")
yields:
31 32 33 0A
@Flyx For such a case something like a hexdump echo of strings would be nice. I was looking for such proc some months ago -- is there something available?
You can use repr to get a better idea of what characters are contained a string.
Here another hexdump():
import strutils
proc hexDump*(s: string | cstring, cols: int = 16, offs: int = 2): string =
result = newStringOfCap(s.len * 3 - 1 + (s.len div cols + 1) * (offs + 2))
var newLine = true
for idx, c in s:
if idx > 0:
if idx mod cols == 0:
result.add "\n"
newLine = true
else:
result.add ' '
if newLine:
newLine = false
result.add idx.toHex offs
result.add ": "
result.add ord(c).toHex 2
echo hexDump("0123456789ABCDEF\nHello World! The Hexdump!")
Output:
00: 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46
10: 0A 48 65 6C 6C 6F 20 57 6F 72 6C 64 21 20 54 68
20: 65 20 48 65 78 64 75 6D 70 21