IRC channel logs

2023-05-06.log

back to list of logs

<daviid>help :) - i have to call g-bytes-new form core g-golf, and its first arg is a pointer to An array of guint8, as described here - https://docs.gtk.org/glib/ctor.Bytes.new.html
<daviid>now,
<RhodiumToad>ACTION waits with bated breath
<daviid>in the context i need this, the content of the array is actually a template.ui file content, so the content of an xml file - how do/should i open a .ui file in scheme, so what i get is an array of guint8 [uint8]
<daviid>is the question clear?
<RhodiumToad>yes
<RhodiumToad>the file is expected to be small, or at least fitting reasonably into memory?
<daviid>yes
<RhodiumToad>get-bytevector-all looks like what you need
<daviid>RhodiumToad: ok, what about the encoding?
<RhodiumToad>i.e. open the file as a port, get the content as a bytevector, then you can get a C pointer that points to the bytevector content
<daviid>the xml file is full of strings ...
<RhodiumToad>what encoding do you expect?
<daviid>*maybe full of translatable strings - somewhere i did read they expect utf8
<daviid>yes, utf8 -
<RhodiumToad>i.e. whatever you end up passing the GBytes to, is presumably expecting the data to be in some encoding?
<daviid>it's a bit confusing, but of course the template will then be used by the application that derives the GObject type, so it will be 'transformed' ack to what ever it needs to define and present, say, labels for example ...
<daviid>so, though they want a GBytes, full of uint8, in the end, the'll decode it and prsent the strings to gtk/adwata ...
<RhodiumToad>what are you passing the GBytes to?
<daviid>let me find a link
<RhodiumToad>and xml can contain its own charset declaration, are they going to recognize that?
<daviid> https://docs.gtk.org/gtk4/class_method.Widget.set_template.html
<daviid>so, i guess i need to open the file specifying utf8, then call get-bytevector-all? did i understood correctly your suggestion?
<RhodiumToad>not quite, but there's also another wrinkle
<RhodiumToad>ah, never mind, GBytes does track the size
<RhodiumToad>I would open the file in binary mode, not specifying utf8 as such
<daviid>ok, i am a bit confused becaue of the encoding
<RhodiumToad>from what I'm seeing, the file is essentially part of the app, yes? rather than user-supplied data
<daviid>i mean becaue the inadequacy between uint8 and the fact that the xml file is encoded in utf8
<RhodiumToad>so it's the responsibility of the creator to get the encoding right
<daviid>well, g-golf users will of course define their templates ...
<daviid>that's the idea
<daviid>as an example, the adwaita1 demo, produced by the adwaita team of course, starts with "<?xml version="1.0" encoding="UTF-8"?> <interface> ... <template class="AdwDemoWindow" parent="AdwApplicationWindow"> ... ... ... </template> </interface>
<daviid>
<RhodiumToad>the interface seems, from the docs you linked, to be expecting to get just the raw bytes
<RhodiumToad>guint8 is just an unsigned char, you don't have to sweat the difference between signed and unsigned char types, they're all just bytes
<daviid>ok pefect, i forgot how to open a file in binary mode
<daviid>:)
<daviid>and to know its size in uint8
<daviid>RhodiumToad: thanks! i'll figure it out
<RhodiumToad>#:binary #t
<RhodiumToad>probably something like
<RhodiumToad>(call-with-input-file filename (lambda (port) (get-bytevector-all port)) #:binary #t)
<daviid>ok, i was trying just that
<daviid>RhodiumToad: many thanks, it's gona a take a bit more time before i have it workong in g-golf, but will let you know ...
<lloda>all #:binary does is set encoding to iso-8859-1. That only matters if you're reading text, funnily enough. It doesn't do anything if you're reading bytes
<lloda>imo this parameter should be deprecated in all the port functions
<lloda>the manual mentions unget-byte, which doesn't exist. There is get-u8, so maybe we need unget-u8
<daviid>lloda: ok, thanks [for these info]
<old>sneek: botsnack
<sneek>:)
<RhodiumToad>under the hood it's all bytes anyway
<RhodiumToad>what's the best way to remove an optional prefix from a string? e.g. given "foo:bar" return "bar", but given "baz" return it unchanged
<RhodiumToad>(if (string-prefix? pfx str) (string-drop str (string-length pfx)) str)) is the obvious way, not sure if there's a better one
<old>RhodiumToad: (lambda (prefix string) (string-drop (string-prefix-length prefix string))) ?
<old>no need for the predicate I think
<daviid>and if : is (always) the sep, no need for prefix either
<RhodiumToad>that's not quite what I want; it'll drop characters on a partial match
<daviid>ok
<RhodiumToad>I could look for the : but I'm not sure that makes it easier?
<old>(lambda (prefix string) (let ((expected-length (string-length prefix))) (when (= expected-length (string-prefix-length prefix string)) (string-drop string expected-length))))
<old>that would work?
<RhodiumToad>yes, but that's worse than mine
<old>how so?
<daviid>a lot easier, but you said partial match ...
<old>it's equivalent I think
<old>more verbose that's for sure
<RhodiumToad>did you forget to return the string unchanged when the prefix is not there?
<old>true
<RhodiumToad>daviid: it's ok in this case to assume that the prefix will end with : and that : doesn't appear outside the prefix
<daviid>the
<daviid>then (define str "foo:bar")
<daviid>or (and=> (string-index str #\:)
<daviid> (lambda (index) (string-drop str (1+ index))))
<daviid> str)
<daviid>then try using #\- to check
<daviid>properly indented it looks better :)