[Prev][Next][Index][Thread]
Working with C strings in HD 1.2
- 
To: info-dylan@ai.mit.edu 
- 
Subject: Working with C strings in HD 1.2 
- 
From: Chris Double <chris@double.co.nz> 
- 
Date: Wed, 29 Dec 1999 03:00:02 -0500 (EST) 
- 
Organization: None. 
- 
Sender: Chris.Double@DOUBLEC 
- 
User-Agent: Gnus/5.070097 (Pterodactyl Gnus v0.97) Emacs/20.4 
- 
Xref: grapevine.lcs.mit.edu comp.lang.dylan:11371 
Back in October I posted a message with the following problem:
    let buffer  = c-type-cast(<c-unsigned-char*>, pointer);
    let request-string = "Some request data";
    let index = 100;
    for(char in request-string)
      buffer[index] := as(<integer>, char);
      index := index + 1;
    end for;
    buffer[index] := 0;
I wanted a quicker/better way of doing the above when transfering
strings to/from a C memory buffer. I've revisited this and I now do
the following:
    let request-string = "Some request data";
    let dest = make(<c-string>, address: \%+(pointer, 100));
    with-c-string( source = request-string )
      copy-bytes!(dest, source, source.size + 1);
    end with-c-string;
This is an order of magnitude faster in the circumstances where I was
using it (copying strings to and from <string>'s and a rich text
editor buffer using the Windows API.
The other problem I asked about:
  The result I need to get out of the buffer is also a zero terminated
  string. I don't know the length of it, I only know the starting
  location. How best to get the information out of the buffer and into
  a <byte-string> or <c-string>? At the moment I'm doing the rather
  slow:
    let index = 100;
    let count = 0;
    while(buffer[index] ~= 0)
      count := count + 1;
      index := index + 1;
    end while;
    let result = make(<byte-string>, size: count);
    for(index2 from 0 below count)
      result[index2] := $char-map[buffer[index2 + 100]];
    end for;
can be solved as follows:
    make(<c-string>, address: \%+(buffer.pointer-address, 100));
I got no replies when I posted the problem so I thought I'd post my
solution in case anyone else comes across a similar problem.
Chris.
-- 
http://www.double.co.nz/dylan