gd_get_string(3)
π gd_get_string β bestie edition π
okay so this is a page from the GETDATA manual (section 3, the library calls section, iykyk) and it's all about gd_get_string, which pulls STRING or SARRAY data out of a Dirfile database. no cap. let's get into it π«Ά
β¨ NAME β¨
gd_get_string β retrieve STRING or SARRAY data from a Dirfile database
β¨ SYNOPSIS β¨
#include <getdata.h>
size_t gd_get_string(DIRFILE *dirfile, const char *field_code, size_t
len, char *data_out);
like. the whole vibe in one function signature. gorgeous. π
β¨ DESCRIPTION β¨
so gd_get_string() goes and queries a dirfile(5) database β the one you point at with dirfile β looking for the string scalar field_code, and listen: that field code should not have a representation suffix on it. just the plain name, bestie. πΈ
the first len characters of that string scalar get dropped into the user-supplied buffer data_out. and if field_code actually refers to a SARRAY field? you get the first element. that's it. that's the tea. β
the dirfile argument has to point to a valid DIRFILE object that you already made by calling gd_open(3). like, a real one. not a fake one. π
now here's the part where it gets spicy πΆοΈ β if len equals zero, or if data_out equals NULL, then literally nothing gets copied into data_out, but gd_get_string() will still hand you back the length of the string scalar. so you can use it as a little measuring tape, periodt. otherwise, data_out has to point to a valid memory location that's big enough to hold at least len characters. and if the string scalar is longer than len? data_out will not be NUL-terminated. π not a vibe. know that.
also quick note: the gd_get_sarray(3) function is another way to grab STRING data, but without you needing to know the length in advance. so this whole thing:
size_t len = gd_get_string(dirfile, field_code, 0, NULL);
char *string = malloc(len);
gd_get_string(dirfile, field_code, len, string);
which is doing the absolute most to make sure the whole string comes back, terminating NUL included β can be replaced with just this:
const char *string;
gd_get_sarray(dirfile, field_code, &string);
and the added benefit?? you don't have to manage the memory for the string. no malloc, no cleanup, no stress. iconic behavior honestly. π
β¨ RETURN VALUE β¨
on success, gd_get_string() returns the actual length of the specified string scalar, including the space for that trailing NUL character. so if the return value is greater than len, that's your signal that the output string is not NUL-terminated. noted. written down. in my notes app. π
on error, this function returns 0 and stashes a negative-valued error code inside the DIRFILE object, which you can grab later with gd_error(3). the possible error codes are:
GD_E_BAD_CODEβ the field specified by field_code was not found in the database. it's not there. it never was. πGD_E_BAD_DIRFILEβ an invalid dirfile was supplied. you handed it something cursed.GD_E_BAD_FIELD_TYPEβ the field_code you gave referred to a field of some type that is not STRING and not SARRAY. wrong department, bestie. πGD_E_INTERNAL_ERRORβ an internal error happened in the library while it was trying to do the task. this means there's a bug in the library. please report the incident to the maintainer. (yes, really. tell them. π«Ά)
and if you want a descriptive error string for whatever went wrong, just call gd_error_string(3). it'll explain it to you nicely. π
β¨ HISTORY β¨
- the
get_string()function first appeared in GetData-0.4.0. a legend was born. β - then in GetData-0.7.0, it got renamed to
gd_get_string(). glow-up era. β¨
β¨ SEE ALSO β¨
gd_get_sarray(3), gd_error(3), gd_error_string(3), gd_open(3), gd_put_string(3), dirfile(5)
bestie this is version 0.10.0, dated 25 December 2016 β happy holidays from the GETDATA crew π go read the other pages, they're all in the same section 3 and they're lowkey slay π π¦
π¦ where this page comes from
- debian bookworm: libgetdata-doc 0.11.0-6
- debian trixie: libgetdata-doc 0.11.0-15
- fedora 43: getdata-devel 0.11.0-13.fc43 Β· GPL-2.0-or-later
- fedora 43: getdata-devel 0.11.0-13.fc43 Β· GPL-2.0-or-later
- fedora 44: getdata-devel 0.11.0-14.fc44 Β· GPL-2.0-or-later
- fedora 44: getdata-devel 0.11.0-14.fc44 Β· GPL-2.0-or-later
- nixos 26.05: getdata 0.11.0
- nixpkgs unstable: getdata 0.11.0
- ubuntu jammy: libgetdata-doc 0.11.0-3ubuntu1
- ubuntu noble: libgetdata-doc 0.11.0-13
- ubuntu questing: libgetdata-doc 0.11.0-15
- ubuntu resolute: libgetdata-doc 0.11.0-17build1
- ubuntu stonking: libgetdata-doc 0.11.0-17build2
- Copyright (C) 2016 D. V. Wiebe
- Copyright (C) 2008, 2009, 2010, 2016 D. V. Wiebe
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms