~ehmry/nim_tox

b4ccf3085508ac06a1ac5faf74a1ebaa4a9934c7 — Emery Hemingway 1 year, 11 months ago 6ca4d35
Use cstring to avoid unsafeAddr when passing strings to C
1 files changed, 20 insertions(+), 20 deletions(-)

M src/toxcore.nim
M src/toxcore.nim => src/toxcore.nim +20 -20
@@ 333,9 333,9 @@ proc savedata_data*(options: Options): ptr uint8 {.ctox_options_get.}
proc savedata_length*(options: Options): csize {.ctox_options_get.}

proc `saveData=`*(options: Options; data: string) =
  proc options_set_savedata_data(options: Options; data: ptr char; length: csize) {.ctoxProc.}
  proc options_set_savedata_data(options: Options; data: cstring; length: csize) {.ctoxProc.}
  # proc options_set_savedata_length(options: Options; length: csize) {.ctoxProc.}
  options.options_set_savedata_data(unsafeAddr data[0], data.len)
  options.options_set_savedata_data(data, data.len)

proc log_callback*(options: Options): tox_log_cb {.ctox_options_get.}
proc `log_callback=`*(options: Options; callback: tox_log_cb) {.


@@ 517,10 517,10 @@ type
proc `name=`*(tox: Tox; name: string) {.tags: [IOEffect].} =
  ## Set the display name of `tox` instance and propagate it to friends.
  proc self_set_name(
    core: Core; name: ptr char; length: csize;
    core: Core; name: cstring; length: csize;
    error: ptr Tox_Err_Set_Info): bool {.ctoxProc.}
  var err = TOX_ERR_SET_INFO_OK
  discard tox.core.self_set_name(unsafeAddr name[0], name.len, nil)
  discard tox.core.self_set_name(name, name.len, nil)
  ctoxAssert(TOX_ERR_SET_INFO_OK, err)

proc name*(tox: Tox): string =


@@ 534,10 534,10 @@ proc name*(tox: Tox): string =
proc `statusMessage=`*(tox: Tox; msg: string) {.tags: [IOEffect].} =
  ## Set the status message of `tox` instance and propagate it to friends.
  proc self_set_status_message(
    core: Core; status_message: ptr char;
    core: Core; status_message: cstring;
    length: csize; error: ptr Tox_Err_Set_Info): bool {.ctoxProc.}
  var err = TOX_ERR_SET_INFO_OK
  discard tox.core.self_set_status_message(unsafeAddr msg[0], msg.len, addr err)
  discard tox.core.self_set_status_message(msg, msg.len, addr err)
  ctoxAssert(TOX_ERR_SET_INFO_OK, err)

proc statusMessage*(tox: Tox): string {.tags: [IOEffect].} =


@@ 546,7 546,7 @@ proc statusMessage*(tox: Tox): string {.tags: [IOEffect].} =
  proc self_get_status_message(core: Core; status_message: ptr char) {.ctoxProc.}
  result = newString(tox.core.self_get_status_message_size)
  if 0 < result.len:
    tox.core.self_get_status_message(unsafeAddr result[0])
    tox.core.self_get_status_message(addr result[0])

proc `status=`*(tox: Tox; status: TOX_USER_STATUS) {.tags: [IOEffect].} =
  ## Set the status of `tox` instance.


@@ 572,10 572,10 @@ type Tox_Err_Friend_Add = enum
proc addFriend*(tox: Tox; id: Address, msg: string): Friend {.tags: [IOEffect].} =
  ## Issue a friend request and return a token for the pending friend.
  proc friend_add(
    core: Core; address: ptr uint8; message: ptr char;
    core: Core; address: ptr uint8; message: cstring;
    length: csize; error: ptr TOX_ERR_FRIEND_ADD): Friend {.ctoxProc.}
  var err = TOX_ERR_FRIEND_ADD_OK
  result = tox.core.friend_add(unsafeAddr id.bytes[0], unsafeAddr msg[0], msg.len, addr err)
  result = tox.core.friend_add(unsafeAddr id.bytes[0], msg, msg.len, addr err)
  ctoxAssert(TOX_ERR_FRIEND_ADD_OK, err)

proc addFriendNoRequest*(tox: Tox; key: PublicKey): Friend {.tags: [IOEffect].} =


@@ 802,10 802,10 @@ proc sendMessage*(tox: Tox; friend: Friend; msg: string; kind = TOX_MESSAGE_TYPE
  ## Send a message to a friend.
  proc friend_send_message(
    core: Core; friend: Friend;
    kind: TOX_MESSAGE_TYPE; message: ptr char;
    kind: TOX_MESSAGE_TYPE; message: cstring;
    length: csize; error: ptr TOX_ERR_FRIEND_SEND_MESSAGE): MessageId {.ctoxProc.}
  var err = TOX_ERR_FRIEND_SEND_MESSAGE_OK
  result = tox.core.friend_send_message(friend, kind, unsafeAddr msg[0], msg.len, addr err)
  result = tox.core.friend_send_message(friend, kind, msg, msg.len, addr err)
  ctoxAssert(TOX_ERR_FRIEND_SEND_MESSAGE_OK, err)

callbackSetter friend_read_receipt:


@@ 922,11 922,11 @@ type Tox_Err_File_Send = enum
proc send*(tox: Tox; friend: Friend; kind: uint32; size: BiggestInt; id: FileId; filename: string): FileTransfer {.tags: [IOEffect].} =
  proc file_send(
    core: Core; friend: Friend; kind: uint32;
    file_size: uint64; file_id: ptr uint8; filename: ptr char;
    filename_length: csize; error: ptr Tox_Err_File_Send): File {.ctoxProc.}
    file_size: uint64; file_id: ptr uint8; filename: cstring;
    filename_length: csize; error: ptr Tox_Err_File_Send): FileTransfer {.ctoxProc.}
  var err: Tox_Err_File_Send
  result = tox.core.file_send(
    friend, kind, size.uint64, unsafeAddr id.bytes[0], unsafeAddr filename[0], filename.len, addr err)
    friend, kind, size.uint64, unsafeAddr id.bytes[0], filename, filename.len, addr err)
  ctoxAssert(TOX_ERR_FILE_SEND_OK, err)

type Tox_Err_File_Send_Chunk = enum


@@ 1193,10 1193,10 @@ type Tox_Err_Conference_Join = enum

proc join*(tox: Tox; friend: Friend; cookie: string): Conference {.tags: [IOEffect].} =
  proc conference_join(
    core: Core; friend: Friend; cookie: ptr char;
    core: Core; friend: Friend; cookie: cstring;
    length: csize; error: ptr Tox_Err_Conference_Join): Conference {.ctoxProc.}
  var err = TOX_ERR_CONFERENCE_JOIN_OK
  result = tox.core.conference_join(friend, unsafeAddr cookie[0], cookie.len, addr err)
  result = tox.core.conference_join(friend, cookie, cookie.len, addr err)
  ctoxAssert(TOX_ERR_CONFERENCE_JOIN_OK, err)

type Tox_Err_Conference_Send_Message = enum


@@ 1209,12 1209,12 @@ type Tox_Err_Conference_Send_Message = enum
proc sendMessage*(tox: Tox; conf: Conference; kind: TOX_MESSAGE_TYPE; msg: string) {.tags: [IOEffect].} =
  proc conference_send_message(
    core: Core; conf: Conference;
    kind: TOX_MESSAGE_TYPE; message: ptr char;
    kind: TOX_MESSAGE_TYPE; message: cstring;
    length: csize;
    error: ptr Tox_Err_Conference_Send_Message): bool {.ctoxProc.}
  var err = TOX_ERR_CONFERENCE_SEND_MESSAGE_OK
  discard tox.core.conference_send_message(
    conf, kind, unsafeAddr msg[0], msg.len, addr err)
    conf, kind, msg, msg.len, addr err)
  ctoxAssert(TOX_ERR_CONFERENCE_SEND_MESSAGE_OK, err)

type Tox_Err_Conference_Title = enum


@@ 1240,10 1240,10 @@ proc title*(tox: Tox; conf: Conference): string =
proc `title=`*(tox: Tox; conf: Conference; s: string) {.tags: [IOEffect].} =
  proc conference_set_title(
    core: Core; conf: Conference;
    title: ptr char; length: csize;
    title: cstring; length: csize;
    error: ptr Tox_Err_Conference_Title): bool {.ctoxProc.}
  var err = TOX_ERR_CONFERENCE_TITLE_OK
  discard tox.core.conference_set_title(conf, unsafeAddr s[0], s.len, addr err)
  discard tox.core.conference_set_title(conf, s, s.len, addr err)
  ctoxAssert(TOX_ERR_CONFERENCE_TITLE_OK, err)

proc conferences*(tox: Tox): seq[Conference] =