How to check if a Kconfig string is empty using Zephyr? - c-preprocessor

Given this Kconfig:
config MY_STR
string "A string"
The directive #if defined(CONFIG_MY_STR) will eval to true for the default empty string.
How to check if CONFIG_MY_STR is an empty string at compile time? Is it a better practice to use a second boolean value (e.g. CONFIG_USE_MY_STR) like the following?
config MY_STR
string "A string"
depends on USE_MY_STR
config USE_MY_STR
bool "Enable MY_STR"

Since string symbols implicitly default to the empty string the BUILD_ASSERT() can be used to perform compile time check:
BUILD_ASSERT(1 != sizeof(CONFIG_SOMEPROPERTY), "SOMEPROPERTY required");
And pass it during the build like
west build -- -DCONFIG_SOMEPROPERTY=\"1.0\" [other arguments]

Related

How to pass a Swift String that just contains white spaces to a C method?

I wrote a Swift wrapper around the C LibSass library and have some trouble with passing a non-empty String to a C method.
The LibSass library provides a method to set a custom indent value. Because this method expects a const char* I know that I can just pass my Swift String to this method and Swift will convert it. Because it is the indent, this String just contains white spaces or \t, but when I check the value of the getter for the indent, I receive just an empty String. I test this value as follows:
XCTAssertEqual( sassOptions.indent, "\t" )
Where sassOptions.indent returns the indent that should be set.
The indent property is set with func setIndent(_: String) that calls the following LibSass method:
void sass_option_set_indent(struct Sass_Options* options, const char* indent)
The expected options argument is stored as an internal property and given to the C method inside the setIndent(_:String) Swift function.
I installed the LibSass library with Homebrew (brew install --HEAD libsass) and linked the library to my project with this dependency. Everything else is working as I expected.
Does anybody know whether Swift converts a String, that just contains white spaces, to an empty String and how to avoid this behaviour?
My setIndent function:
public func setIndent( _ indent: String ) {
sass_option_set_indent( self.options, indent )
}
See also the GitHub repository for the whole code and tests.

Remark: unrecognized token warning for the macro concatenation

#define DATA_VAR_FILENAME(PROJECT_ID) QUOTES(..\ ## PROJECT_ID ## _data_var.h)
or
#define DATA_VAR_FILENAME(PROJECT_ID) QUOTES(..\##PROJECT_ID##_data_var.h)
for the above line I got below warning
remark: unrecognized token
How to eliminate the warning ?
From the look of it, it looks that you are trying to compose a filename in the preprocessor. This is not possible like that, since the strings that form path names are not tokens for the preprocessor.
Usually there is no need for such a manipulation. String literals are simply concatenated too larger string literals in a later compilation phase. Something like
"../" "my_file.h"
should do the trick.
You also seem to have the difficulty that on your platform \ is the path separator. So you'd have to be careful to escape it in string literals, something like "..\\"

Dot in regular expression (regex)

I am using slre (https://code.google.com/p/slre/) for providing a regex library for a c program.
I want to match an IP address with following pattern: "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
I get following compile error: Warning: unknown excape sequence '\.'
I also tried it with '\\.' --> the compile error is gone, but it's still saying it doesn't match.
if (!slre_compile(&slre, settings[i].regex)) {
printf("Error compiling RE: %s\n", slre.err_str);
}
else if (!slre_match(&slre, settings[i].value, strlen(settings[i].value), captures)) {
printf("\nSetting '%s' does not match the regular expression!", settings[i].internName);
}
settings[i].regex is a char* with the regular expression I mentioned above
settings[i].value is a char*
the string I am trying to match is 8.8.8.8
Is there any other way to check for a dot?
Try [.]
Dot isn't special inside character class.
The C compiler is seeing your backslash as an attempt to escape a character in C, in the same way that \n becomes a newline. You need to use a double-backslash:
\\.
The C compiler will turn that into a single backslash and pass that to the regex library.
That's the source of the compiler warning - if it's still not matching after you add the extra backslash then you have a different problem as well.
According to http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx your regex does match 8.8.8.8, so the problem isn't with the regex itself.
Your question is about C, but if you can compile with C++11 you can have a look to literal raw string
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2442.htm
std::string literal_string = R"literal string\. \n";

How to print a Vector2D in unrealscript

I have this function:
function AddImpulse(Vector2D impulse)
{
`log("ADD IMPULSE: " $ impulse);
}
The trouble is that I get the error "Right type is incompatible with '$'. It seems that, although the built-in vector class will automatically coerce to a string, the Vector2D class is just a built-in struct without any operator overloading or automatic conversion.
I wrote an operator overload that help the situation, but unless I put the declaration of the overload in the object class (which I'm lead to believe shouldn't be done) I have to declare it in every class that may use it:
static final operator(40) string $ (string A, Vector2D B)
{
return A $ string(B.x) $ ", " $ string(B.y);
}
Is there a way this can be done generically so that I don't need to do this every time:
`log("ADD IMPULSE: " $ impulse.x $ "," $ impulse.Y);
Although that is not bad in the case of a Vector2D, this will become cumbersome with larger structures or classes.
Your options for generic programming in UnrealScript are limited, unfortunately. One option may be to put your operator overload in an include file and include it in each class that needs it using the `include macro.
If that doesn't work, another option may be to use a macro to invoke a static function in a special class for handling struct to string conversions.
First create a Globals.uci file in your code package's root folder. Globals.uci is a special file which is automatically included by the compiler in all UncrealScript files in the package it is associated with. If your package is called MyPackage, Globals.uci would go in Development/Src/MyPackage/Globals.uci. It should be next to the Classes folder for your package.
Put your macro in Globals.uci:
`define toString(type,name) class'MyStructConversions'.static.`{type}ToString(`{name},"`{name}")
Put your conversion function in MyStructConversions.uc:
class MyStructConversions;
static function string Vector2DToString (const out Vector2D A, string varname)
{
return varname $ ": (" $ A.X $ ", " $ A.Y $ ")";
}
Now when you call `toString(Vector2D, impulse) from anywhere in your package the macro will be replaced at compile time with an invocation to your Vector2DToString function. You can then support more struct types by adding the appropriate definitions to MyStructConversions.uc, and the toString macro will work seamlessly with them.
The documentation on the UnrealScript preprocessor has more information on `include and other macros. Check out Globals.uci in Development/Src/Core for some useful examples as well.

fputs( _("") ) what does the underscore stand for?

I finally got myself to look at some Linux code. I am looking right now at ls.c.
At the function usage() at the bottom I found a lot of these statements:
fputs (_("\
List information about the FILEs (the current directory by default).\n\
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.\n\
\n\
"), stdout);
What does _("") mean? Is it something like L"string" or _T"string" or something completely new? I must also admit I don't know what words to use to search for something like this.
It's a convention used by libintl a.k.a. gettext, for translatable strings. When it runs, gettext function (which _ is aliased to) will return either original or translated string, depending on locale settings and availability of said string.
_ is a macro often used with the GNU gettext package.
GNU gettext is a package that:
takes lists of message strings intended for humans to read, and translations of those strings into other languages, and compiles them into databases;
provides a routine, named gettext(), to look up message strings in that database and return the translation for the message into a particular language.
If a program wanted to print a message in the language selected by the user in an environment variable and picked up by a setlocale() call, it would normally do something such as
fprintf(stderr, gettext("I cannot open the file named %s\n"), filename);
gettext() would look up the appropriate translation of the string "I cannot find the file named %s\n" in the database and return the translated string.
However, that's a bit awkward; as the documentation for GNU gettext notes, many programs use a macro to make just _(string) be an alias for gettext(string).
Function names can, of course, contain an _, and an _ can begin a function name. So, it's possible to name a function simply _.
All that's happening is that a #define or a real function is called _.

Resources