[plt-scheme] strings?

From: Mike (mikee at mikee.ath.cx)
Date: Fri Apr 15 09:16:37 EDT 2005

On Thu, 14 Apr 2005, Eli Barzilay might have said:

> On Apr 14, Mike wrote:
> >   For list-related administrative tasks:
> >   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> > 
> > I'm trying to add an extension to mzscheme. In
> > the scheme code I do something like (sqlite:open "dbname").
> > In the extension code I check for SCHEME_CHAR_STRINGP(argv[0]),
> > and then I extract with char *p = SCHEME_BYTE_STR_VAL(argv[0]);
> > I only get the first character of the actual string, the
> > string is needed to pass to sqlite_open().
> 
> SCHEME_CHAR_STRINGP checks for a Scheme string, which contains UCS4
> unicode characters, which usually means you get a single character
> when you try to use it as a char* (since the first character is
> encoded in 4 byts, usually (for ascii) the other three are zeros).
> 
> Use SCHEME_BYTE_STRINGP, and use only byte-strings on the Scheme
> side.  You can then write some Scheme code that will translate normal
> Scheme strings to bytes using utf8.
> 
> (Or you can use the foreign interface that does much of that for you.)

Sorry for being dense on this. I'm using mzscheme 299.100.
I need to convert the argv[0] to a standard, null-terminated
string for passing to sqlite_open(). From my testing last night
I had noticed what you mention, that I'm only getting the first
character from the string. What function do I use to get the full
string, or what function do I use to conver the byte string into
a full string?

(sqlite:open "howard.db")

--

static Scheme_Object *sch_dbopen(int argc, Scheme_Object **argv)
{
    sqlite *db;
    char *msg, *dbname;
    Scheme_Object *so;
    DB *dbo;

    if(!SCHEME_CHAR_STRINGP(argv[0])) {
        scheme_wrong_type("sqlite:open", "string", 0, argc, argv);
    }
    dbname = SCHEME_BYTE_STR_VAL(argv[0]);
(void) fprintf(stderr, "dbname='%s'\n", dbname);
    db = sqlite_open(dbname, 0, &msg);
    if(db) {
        dbo = (DB *) scheme_malloc(sizeof(DB));
        dbo->type = dbtype;
        dbo->db = db;
        return (Scheme_Object *) dbo;
    }
    so = scheme_make_locale_string(msg);
    (void) free(msg);
    return so;
}



Posted on the users mailing list.