Tcl_GetInt(3tcl)

๐Ÿ“ฆ
version 9.0.4 ยท 3 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

๐Ÿ’– NAME ๐Ÿ’–

Tcl_GetInt, Tcl_GetDouble, Tcl_GetBoolean โ€” bestie, these three are the glow-up queens that convert a string into an integer, a double, or a boolean. That's it. That's the job. โœจ

This whole page lives in the Tcl Library Procedures manual, and yes we're talking about Tcl_GetInt(3tcl) today. ๐Ÿซถ


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

#include <tcl.h>

int
Tcl_GetInt(interp, src, intPtr)

int
Tcl_GetDouble(interp, src, doublePtr)

int
Tcl_GetBoolean(interp, src, intPtr)

int
Tcl_GetBool(interp, src, flags, charPtr)

๐ŸŒธ ARGUMENTS ๐ŸŒธ

  • Tcl_Interp *interp (in) โ€” the interpreter we blame when something goes wrong. She's the one who catches the error message. ๐Ÿ’…
  • const char *src (in) โ€” the actual text value you want converted. The raw material, the main character. ๐Ÿฆ‹
  • int *intPtr (out) โ€” a lil pointer to the spot where the integer converted from src gets stored. โœจ
  • double *doublePtr (out) โ€” pointer to where the double-precision floating-point value converted from src goes. So fancy. ๐ŸŽ€
  • char *charPtr (out) โ€” pointer to where the boolean value (0 or 1) converted from src gets parked. ๐Ÿ’œ
  • int flags (in) โ€” either 0 or TCL_NULL_OK. If you use TCL_NULL_OK, then the empty string or NULL will make Tcl_GetBool return TCL_OK, with *charPtr filled with the value '\xFF'. Not a typo, not a vibe, that's literally the spec. ๐Ÿ˜ญ

๐ŸŒˆ DESCRIPTION ๐ŸŒˆ

Okay so here's the tea. โ˜• These procedures convert strings into integers, or double-precision floating-point values, or booleans (which get represented as 0- or 1-valued integers). Each one takes a src argument, converts it into the internal form of one particular type, and stores the converted value at the location that the procedure's third argument is pointing at. If everything goes smoothly โ€” and we love that for us โ€” each procedure returns TCL_OK. ๐Ÿ’š

But if src doesn't have the proper syntax for the type you asked for? Then TCL_ERROR comes back, an error message gets left in the interpreter's result, and nothing gets stored at *intPtr or *doublePtr. Empty-handed. Devastating. ๐Ÿ™ˆ


๐Ÿงฎ Tcl_GetInt

Tcl_GetInt expects src to be a collection of integer digits, optionally signed, and optionally preceded and followed by white space. Now here's the base-detective part, so stay with me bestie:

  • If the first two characters of src, after the optional white space and sign, are 0x, then src is expected to be in hexadecimal form. ๐Ÿ”ฅ
  • Otherwise, if the first such characters are 0d, then src is expected to be in decimal form.
  • Otherwise, if the first such characters are 0o, then src is expected to be in octal form.
  • Otherwise, if the first such characters are 0b, then src is expected to be in binary form.
  • Otherwise, src is expected to be in decimal form. The default. The safe choice. The vanilla of number bases. ๐Ÿฆ

๐ŸŽ€ Tcl_GetDouble

Tcl_GetDouble expects src to be a floating-point number, and a floating-point number is, in order:

  1. white space
  2. a sign
  3. a sequence of digits
  4. a decimal point "."
  5. a sequence of digits
  6. the letter "e"
  7. a signed decimal exponent
  8. and more white space

Any of those fields may be omitted โ€” EXCEPT the digits either before or after the decimal point must be present, and if the "e" is present then it must be followed by the exponent number. No half-finished vibes here. ๐Ÿ˜ค

If there are no fields apart from the sign and initial sequence of digits (i.e., no decimal point or exponent indicator), then that initial sequence of digits should take one of the forms that Tcl_GetInt supports, described above. โฌ†๏ธ

Heads up bestie: the use of "," as a decimal point is not supported, and no other sort of inter-digit separator should be present either. Sorry to my European girlies, the comma is not invited. ๐Ÿ’”

โœ… Tcl_GetBoolean

Tcl_GetBoolean expects src to specify a boolean value.

  • If src is any of 0, false, no, or off, then Tcl_GetBoolean stores a zero value at *intPtr. ๐Ÿšซ
  • If src is any of 1, true, yes, or on, then 1 is stored at *intPtr. ๐Ÿ’ฏ

Any of these values may be abbreviated, and upper-case spellings are also acceptable. So go off, TRUE, YeS, we see you. ๐Ÿ‘‘


๐Ÿซถ Tcl_GetBool

Tcl_GetBool functions almost the same as Tcl_GetBoolean, but it has an additional parameter, flags, which can be used to specify whether the empty string or NULL is accepted as valid. That's the whole difference. Iconic, lowkey. โœจ


๐Ÿ’ซ KEYWORDS ๐Ÿ’ซ

boolean, conversion, double, floating-point, integer


Okay that's the whole page, bestie, you ate it. ๐Ÿ’… This one's straight outta the Tcl source and it's filed under Tcl_GetInt(3tcl) โ€” go convert something cute. Byeee! ๐Ÿฆ‹โœจ

๐Ÿ“ฆ where this page comes from