I'm interested in something like strstr() function but that I could pass a formatted string as argument, like what I pass to printf(). To be clear, let's get an example:
Suppose that I want to find this text: "abc:123" where abc could be any string with any size followed by ':' and then followed by some integer number. I suppose a good function could receive as argument something like this: "%s:%d".
Something else, I want to use this embedded, so I can't get big and/or esoteric libraries.
Thanks and best regards!
You can use sscanf. It takes a string and a format as input and you fill variables as a result. Regular expressions are also something to consider
Use should use regular expressions.
This thread may help you: Compiling/Matching POSIX Regular Expressions in C
Gentleman,
I found this CRX and it is exactly what I wanted. Thanks everybody!
Alternatively, you can directly use printf function. The function outputs strings on default uart (mine is uart0).
But prior to using printf, proper set-up and configuration is required.
Related
I'm working in my C program and I have a doubt about how to use the sscanf function.
I have to get from a string like something$HOME/something2 three strings: the first one has to contain "something", the second one "HOME" and the third one "/something2".
But also it has to be able to split $HOME or $HOME/something, or something$HOME...
What I wrote was something like that:
sscanf(str,"%m[^$]$%m[_a-zA-Z0-9]%*m", &ant, &act, &sig);
But for the cases $HOME, $HOME/something, something$HOME... it takes very strange strings. Any ideas of what can I put on my sscanf to get the other values by NULL in case they don't exist?
Sorry, but you should not use sscanf for that. Your input seems to be a shell command line argument and you seem to want implement variable expansion.
For that, you have to write your own parser that would parse the expression, identify and do variable expansion. It's not a job for scanf - it's a job for your custom loop where you inspect each and every character in a string.
There's a utility with like exactly that functionality called envsubst. You could inspect its source gettext/envsubst.c. You could also take a look at wordexp; however, wordexp function is a way broader function.
string.format("%q", foo_str) will format a string to add the appropriate escape chars to make it safe to read back into the Lua interpreter. How can I best use this function to format a Lua_Buffer from the C-API? More generally, how can I access the string.* functions from the C-API? I could use lua_pcall("string.format", ...), but curious if there is a more direct way.
The format function is defined as static in the lstrlib.c module, so AFAIK the only way to get to it is through the string table.
I suppose you could look at addquoted in lstrlib.c and adjust it for your use, but probably easier to just call string.format.
I'm trying to regexp match a C function, e.g.
func(blah blah);
The match can include newlines.
I've tried:
func([.+]);
which didn't do newlines, and:
func([...]);
func([^...]);
neither of which seemed to do anything. I guess I'm looking for the part of a regexp that will match any number/type of characters between my opening func( and );.
You could try func[[:space:]]*([^)]*). Nested parens in calls will confuse it though.
I think that the general case is not feasible with regular expressions, because the nested function calls are not a regular language.
While Maxim's answer is specific, I'm going to guess you are looking to do something with the matched function you found. To do serious code processing, you can't beat the semantic parser that is a part of CEDET's suite of tools. http://cedet.sf.net is also part of Emacs.
If you use the semantic parser in emacs, you can:
M-x semantic-mode RET
and then in code:
(semantic-fetch-tags)
or
(semantic-current-tag)
to get the current tag. Once you have the tag, you can call:
(semantic-tag-function-arguments mytag)
to get the arguments, which are tags. For one of those, use semantic-tag-name to get the name, or semantic-tag-type to get the data type.
Once you've got your tag data, you can always write out new code with SRecode, which is a code generator which will take in tags, and spit out code, such as function declarations.
I have a following string format.
({'BTAddress': <'00:22:58:07:76:9B'>, 'Name': <'Some-Name'>, 'Alias': <'some-Name'>, 'Class': <uint32 12316>, 'Adapter': <objectpath '/org/bluez/3196/hci0'>},)
I would like to parse the above string in C. Instead of doing raw parsing, any advice to use pattern wise parsing ?
Thanks for the help.
You can use strtok : http://www.cplusplus.com/reference/clibrary/cstring/strtok/.
You'll have to do some hand work to make it work, but it works fine.
Note: the char pointer that you pass will be manipulated!!
Any POSIX-compliant C compiler should provide <regex.h>, giving you regular expressions that should be able to help with this.
A regular expression library is probably what you need. If you are using the GNU C library that has a regular expression library, for pattern matching. For Win32 compilers, you can use Regex for Windows port of the GNU code.
My suggestions are as follows :
A) try using strtok to split the text at ',' and then split the resulting subtexts only once at ':' ,that way you would get the key,value pairs you desire.
B) use a regEx api under C that would basically solve your issue.
Also i might suggest looking for a key value store under C for your parsed text (C++ has one but i really don't know about C)
How to call a varargs function via dbus, like printf?
The server is written in C, the client is written in Python.
My questions are how to write the XML spec file? And how to write my GObject?
Thanks in advance.
[I use DBus-GLib.]
There is no way to call a variadic function programmatically with varying number and type of arguments. You could use the corresponding "v" functions (vprintf etc.) but there's no way to make the va_list to pass to them from foreign code and pass it over DBus.
In any case you really need to rethink your design. It's unsafe to call printf with a format string that's not 100% controlled by your program. Passing a client-provided format string is a formula for disaster, because printf invokes undefined behavior if the format string does not match the arguments. In nearly all real-world situations, this will mean privilege elevation vulns!