Is code aware of the sketch name?

Hi,

Can the name of a sketch be referenced in code?

I have couple of different projects that use the same library. I'd like the library to behave slightly differently depending on which project it's in. Is there a pre-compiler directive that I can use that will allow me to enable/disable sections of code depending on which project it's part of (or in other words, depending on the name of the top-level sketch)

Thanks

You would have to create a #define yourself. There is the FILE constant that is defined for you, but the pre-processor has no facilities to compare strings.

You could do it in the actual code:

if (strcmp(__FILE__, "myfile.ino") == 0)
{
   //do this if they match
}
else
{
   // do this if they don't
}

but print FILE first to see how much path and stuff is there.

Thanks - just what I was looking for!

Without details this is only speculation, but wanting a library to behave differently depending where it's being used makes me suspect it's designed wrongly.

Also note that the value of FILE evaluates to the name of the source file that the statement is contained in - if you use it in a library, it'll be the name of the library source file which is probably not what you want here.

if you use it in a library, it'll be the name of the library source file

Experimentation indicates that using FILE in a library will return the name of the top-level sketch - which is exactly what I do want.

wanting a library to behave differently depending where it's being used makes me suspect it's designed wrongly.

Generally I would agree with you, in this case it seems to be the easiest option. I have a GPS library, for one project I just want to read and validate each sentence, for another I want to do full parsing of each field. It just seemed to be the quickest option, I guess I should have split it into two libraries.

Cheers

It just seemed to be the quickest option, I guess I should have split it into two libraries.

I doubt that it is the quickest solution. It certainly isn’t the best solution. And, yes you should have, and still could.

I don’t see how you are going to parse anything without reading and validating first. I don’t understand why the parsing would take place until you called a method that needed to return some data that the parsing process would extract from the sentence. I don’t understand why you are not using TinyGPS or TinyGPS+, since they already exist.

Why don't you save a modified copy of the library and call the original from one sketch, and the modded one from the other?

There are also macros for

 __LINE__    // line count; use #line 100 after all header files are read
            // allows you to start numbering the source file at 100

__func__    // which function am I in

BigusDickus: Experimentation indicates that using FILE in a library will return the name of the top-level sketch - which is exactly what I do want.

That's not what I would expect, and not the behaviour I see. The FILE macro is an ANSI standard predefined macro which gives the name of the [u]current source file[/u] i.e. the source file which contains the FILE reference. If the reference is inside a library source file, then the value will be the name of the library source file. You can't use this to determine which sketch is using the library.

Remember, The arduino concatenates all the ino's into one big conglomeration before passing it to gcc, so gcc only sees one file. If you named the other cource files with a .cpp extension it might be a different story.

KeithRB:
Remember, The arduino concatenates all the ino’s into one big conglomeration before passing it to gcc, so gcc only sees one file. If you named the other cource files with a .cpp extension it might be a different story.

Is it possible to implement a library in a .ino file? I’ve never seen it done, and I have no idea whether it’s supported.