Nimgen wrapper for Edlib

2019-05-16 Thread ducktape
Hello!

I am trying to make a wrapper for the 
[edlib](https://github.com/Martinsos/edlib) library. It can be found 
[here](https://github.com/sstadick/nimedlib). I'm sure I'm missing something 
simple, but when I try to run the small test in my repo (`nim c -r 
tests/tnimedlib.nim`), I just get an error message


/.nimble/pkgs/nimedlib-0.1.0/nimedlib/edlib.nim(56, 6) Error: 
implementation of 'nimedlib.edlibFreeAlignResult(result: 
EdlibAlignResult)[declared in 
../../../.nimble/pkgs/nimedlib-0.1.0/nimedlib/edlib.nim(56, 5)]' expected


Run

To me this would indicate that the function wasn't in the cpp file, but it is, 
and all the other functions are found just fine it appears. I am not an expert 
in C land by any means though, so perhaps it's not that simple. Any pointers 
are appreciated!


Re: Problem trying to compress the http response data!

2019-05-16 Thread jrfondren
Try miniz instead. 
[https://github.com/treeform/miniz](https://github.com/treeform/miniz)

zip/zlib.nim defines ulong as 32-bit, so it fails with 64-bit zlibs.


Wrapping C++ class with overloaded constructors

2019-05-16 Thread sdwfrost
Dear All,

I'm trying to wrap this C++ header:

[https://github.com/cartoonist/kseqpp/blob/master/src/seqio.h](https://forum.nim-lang.org/postActivity.xml#https-github-com-cartoonist-kseqpp-blob-master-src-seqio-h)

which has a constructor SeqStreamIn( const char* filename )

My Nim code is


# Compile with
# nim cpp --cincludes:. --passL:"-lz" kseqpp

import os

const
seqio = "seqio.h"

type
SeqStreamInObj {.header: seqio, importcpp: "klibpp::SeqStreamIn".} = 
object
SeqStreamIn = ptr SeqStreamInObj

proc newSeqStreamIn*(fn: cstring): SeqStreamInObj {.importcpp: 
"klibpp::SeqStreamIn(#)".}

when isMainModule:
var iss = newSeqStreamIn("sample.fastq".cstring)


Run

However, when I compile, I get an error, error: no matching function for call 
to ‘klibpp::SeqStreamIn::SeqStreamIn()’.

Any ideas of what I'm doing wrong? 


Re: output order

2019-05-16 Thread Araq
Evaluation order is left-to-right as written in the manual. Of course, this 
evaluation order refers to _after_ template/macro expansions.


Re: Trouble when converting c code to nim

2019-05-16 Thread zevv
It would be helpful to others to indicate your exact problem, like why does it 
not compile and why you can not solve this? Your above snippet has problems 
with converting types, because you are trying to mix Nim integers and C 
unsigned chars. Fixing this is not too hard:


proc utf8str_codepoint_len*(s: cstring, utf8len: cint): cint =
  var codepointLen: cint = 0
  let m4 = 128 + 64 + 32 + 16
  let m3 = 128 + 64 + 32
  let m2 = 128 + 64
  var i: cint = 0
  while i < utf8len:
var c = s[i].int
if (c and m4) == m4:
  inc(i, 3)
elif (c and m3) == m3:
  inc(i, 2)
elif (c and m2) == m2:
  inc(i, 1)
inc(i)
inc(codepointLen)
  return codepointLen


Run

As an alternative you might look into the nim unicode module which offers the 
runelen() proc which does almost exactly what your above code does.


Re: Trouble when converting c code to nim

2019-05-16 Thread doofenstein
Your code is mostly fine, the reason it's only compiling, is due to Nim having 
a much stricter type system. For other visitors this is the error message is:


in.nim(3, 34) Error: type mismatch: got but expected 'cuchar = Char'


Run

You're probably better off not using the types prefixed by C, those are mostly 
meant for compability.

This is how I would translate your function into more idiomatic Nim: 


proc utf8str_codepoint_len*(s: string, utf8len: int): int =
  const
m4 = 128'u8 + 64 + 32 + 16
m3 = 128'u8 + 64 + 32
m2 = 128'u8 + 64
  var i = 0
  while i < utf8len:
# in Nim char and uint8/byte are separate types, with the former being 
only used to represent 8 bit
# characters and thus arithmetic functions aren't defined for neither 
it nor cchar.
var c = uint8(s[i])
if (c and m4) == m4:
  i += 3
elif (c and m3) == m3:
  i += 2
elif (c and m2) == m2:
  i += 1
inc result # result is the implicit return value that is like all 
unassigned variables in Nim set to 0 in the beginning


Run


Re: Modern way to pass a sequence to a c function as a void pointer and to cast it back as seq

2019-05-16 Thread zevv
Well, C _can_ understand it. But that does not mean you should _want_ it to :)


{.emit: """

typedef long long int nim_int;

struct nim_seq_hdr {
  nim_int len;
  nim_int reserved;
};

struct nim_string {
  struct nim_seq_hdr seq;
  char *data[0];
};

struct nim_string_seq {
  struct nim_seq_hdr seq;
  struct nim_string *data[0];
};


void foo(void *ptr)
{
  struct nim_string_seq *n = *(struct nim_string_seq **)ptr;
  nim_int i;
  for(i=0; iseq.len; i++) {
struct nim_string *s = *(struct nim_string **)(>data[i]);
printf("%d -> %d: %s\n", i, s->seq.len, s->data);
  }
}


"""}

proc foo(a1: pointer) {.cdecl, importc.}
var a = @["one", "two", "three"]
foo(addr a)


Run


Trouble when converting c code to nim

2019-05-16 Thread milerius
Hello i have the following piece of code that i want to transform in nim:


int utf8str_codepoint_len( char const* s, int utf8len ) {
int codepointLen = 0;
unsigned char m4 = 128 + 64 + 32 + 16;
unsigned char m3 = 128 + 64 + 32;
unsigned char m2 = 128 + 64;
for ( int i = 0; i < utf8len; ++ i, ++ codepointLen ) {
char c = s[i];
if ( ( c & m4 ) == m4 ) {
i += 3;
} else if ( ( c & m3 ) == m3 ) {
i += 2;
} else if ( ( c & m2 ) == m2 ) {
i += 1;
}
}
return ( codepointLen );
}


Run

I have this for the moment but it's doesn't compile:


proc utf8str_codepoint_len*(s: cstring; utf8len: cint): cint =
  var codepointLen: cint = 0
  var m4: cuchar = 128 + 64 + 32 + 16
  var m3: cuchar = 128 + 64 + 32
  var m2: cuchar = 128 + 64
  var i: cint = 0
  while i < utf8len:
var c: char = s[i]
if (c and m4) == m4:
  inc(i, 3)
elif (c and m3) == m3:
  inc(i, 2)
elif (c and m2) == m2:
  inc(i, 1)
inc(i)
inc(codepointLen)
  return codepointLen


Run

Should i keep it in c and import it nim ?


Problem trying to compress the http response data!

2019-05-16 Thread mrhdias
I am trying to compress the responses that are sent by http:


import asynchttpserver, asyncdispatch
import zip/zlib

proc handle(req: Request) {.async.} =
  if req.headers.hasKey("Accept-Encoding") and 
req.headers["Accept-Encoding"] == "gzip":
let headers = newHttpHeaders([("Content-Encoding", "gzip")])
await req.respond(Http200, compress("Yes, it was compressed!"), headers)
  else:
await req.respond(Http200, "Compress me please!...")

var server = newAsyncHttpServer()
waitFor server.serve(Port(8080), handle)


Run

But I get the following error message when trying to run this code!


Exception message: zlib version mismatch!
Exception type: [ZlibStreamError]
Error: execution of an external program failed: '/home/hdias/dev/server '

What's is wrong? and how can I get it to work? (my zlib version: 
zlib-1:1.2.11-3) 


Re: How to create and pass a char*[] to a C function with FFI

2019-05-16 Thread trtt
The header was like this: int argc, char* argv[] and it only worked with ptr 
cstringArray.


Re: new experimental nim plugin for neovim

2019-05-16 Thread miran
> Any recommendations for other plugins that are useful?

Look at plugins by [tpope](https://github.com/tpope/), lots of (must-have) 
goodies there.

Other stuff you'll pick along the way based on your preferences, but here are 
some of mine: fzf.vim, vim-gitgutter, rainbow, supertab, vim-highlightedyank, 
vim-bbye


Re: How to create and pass a char*[] to a C function with FFI

2019-05-16 Thread trtt
ptr cstringArray works.


Re: How to create and pass a char*[] to a C function with FFI

2019-05-16 Thread mratsim
It works, you can pass `ptr Foo` or even `pointer` to any char *. In your case 
you should probably use `pointer` or `ptr pointer` or `ptr 
UncheckArray[pointer]`.

But without the relevant part of the header, I can't say much more. Anyway the 
proper way to interface `char *` is `pointer`, you can look into the `alloc` 
and `alloc0` procs and other low-level proc in `system.nim` as reference.


Re: new experimental nim plugin for neovim

2019-05-16 Thread geotre
Haven't used neovim before but I just installed it and it's nice. Any 
recommendations for other plugins that are useful?


Re: How to create and pass a char*[] to a C function with FFI

2019-05-16 Thread trtt
That's about seq[int] and that technique doesn't work.


Re: How to create and pass a char*[] to a C function with FFI

2019-05-16 Thread kaushalmodi
I believe this is related: 
[https://forum.nim-lang.org/t/4837#30308](https://forum.nim-lang.org/t/4837#30308)


Re: Modern way to pass a sequence to a c function as a void pointer and to cast it back as seq

2019-05-16 Thread trtt
I don't think that C will understand Nim's seq. Maybe you want to create a 
char*[] like me: 
[https://forum.nim-lang.org/t/4851](https://forum.nim-lang.org/t/4851)


How to create and pass a char*[] to a C function with FFI

2019-05-16 Thread trtt
cstringArray and ptr UncheckedArray[ptr cstring] don't work. They do work with 
char** but that's not what I need.


Re: Unexpected error from the C compiler with Option

2019-05-16 Thread rosey12
I am also facing this type of issue and get useful ideas from 
[https://errorcode0x.com/fixed-asus-error-code-55](https://errorcode0x.com/fixed-asus-error-code-55)/
 page. I get some proper solution here. Keep sharing.


Re: Modern way to pass a sequence to a c function as a void pointer and to cast it back as seq

2019-05-16 Thread 2vg
` cFunction(sq[0].unsafeAddr.pointer) `

Run


Modern way to pass a sequence to a c function as a void pointer and to cast it back as seq

2019-05-16 Thread milerius
Hello

I'm looking for help for the following snippet 


let sq = @["hello", "nim"]


proc cFunction(a1: pointer) =
# How to cast the pointer to seq[string]

proc main() =
  # How to call cFunction and send sq as a c void pointer


Run

I have most of the time type mismatch when using


cFunction(cast[pointer](sq[0].addr))

cFunction(sq.unsafeAddr)

cFunction(sq[0].unsafeAddr)


Run


Re: How to set toolchains for cross compilation?

2019-05-16 Thread LeFF
So I finally managed to make it work.

test.nim: 


echo "Hello World!"


Run

test.nim.cfg: 


gcc.path   = r"C:\COMPILERS\x86_64-w64-mingw32-native\bin"
gcc.exe= "x86_64-w64-mingw32-gcc.exe"
gcc.cpp.exe= "x86_64-w64-mingw32-g++.exe"
gcc.linkerexe  = "x86_64-w64-mingw32-gcc.exe"

i386.windows.gcc.path   = 
r"C:\COMPILERS\i686-w64-mingw32-native\bin"
i386.windows.gcc.exe= "i686-w64-mingw32-gcc.exe"
i386.windows.gcc.cpp.exe= "i686-w64-mingw32-g++.exe"
i386.windows.gcc.linkerexe  = "i686-w64-mingw32-gcc.exe"

i386.linux.gcc.path   = r"C:\COMPILERS\i686-linux-musl-cross\bin"
i386.linux.gcc.exe= "i686-linux-musl-gcc.exe"
i386.linux.gcc.cpp.exe= "i686-linux-musl-g++.exe"
i386.linux.gcc.linkerexe  = "i686-linux-musl-gcc.exe"

amd64.linux.gcc.path   = r"C:\COMPILERS\x86_64-linux-musl-cross\bin"
amd64.linux.gcc.exe= "x86_64-linux-musl-gcc.exe"
amd64.linux.gcc.cpp.exe= "x86_64-linux-musl-g++.exe"
amd64.linux.gcc.linkerexe  = "x86_64-linux-musl-gcc.exe"


Run

test.bat: 


nim c -d:release -o:test_win64.exe --os:windows --cpu:amd64 test.nim
nim c -d:release -o:test_win32.exe --os:windows --cpu:i386  test.nim
nim c -d:release -o:test_lin64.elf --os:linux   --cpu:amd64 test.nim
nim c -d:release -o:test_lin32.elf --os:linux   --cpu:i386  test.nim


Run

It seems adding ' _.linkerexe ' and '_.path' and using 'gcc.*' for native 
target fixed everything.


Re: output order

2019-05-16 Thread SolitudeSF
why would it? arguments need to be evaluated first