Skip to content

Instantly share code, notes, and snippets.

@sancarn
Last active February 20, 2018 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sancarn/011ea23901e3fbc0f7478f1c51f4cd57 to your computer and use it in GitHub Desktop.
Save sancarn/011ea23901e3fbc0f7478f1c51f4cd57 to your computer and use it in GitHub Desktop.

MapBasic - Comments in MapInfo Project File.md

2 years ago I visited the MapInfo User Group UK conference held at the Institution of Engineering and Technology (IET), Birmingham. At the conference I met Peter Horsbøll Møller. At the time I hadn't had much experience with MapBasic Project (.mbp) files. A MapBasic project is often required when working on large projects or using libraries that others have made. The basic layout of a MapBasic project is as follows:

[Link]
Application = UpdateFromAmp5.mbx
Module = UpdateFromAmp5.MBO  
Module = FULL_RIBBON.MBO

In the above scenario we tell the MapBasic engine that we have 2 modules, or libraries, "UpdateFromAmp5.MBO" and "FULL_RIBBON.MBO", and that we want to combine these 2 modules into the application named "UpdateFromAmp5.mbx". FULL_RIBBON is a precompiled version of the SimpleRibbonButtons library of functions I built and published on GitHub. These functions help you build a simple ribbon interface without exposing the full ribbon API. The library is primarily built on top of another great library created by Peter, named RIBBONLib. The 2nd module listed, "UpdateFromAmp5.MBO", is my main module which contains all the code to execute the MapBasic application, including a Main() routine.

One of my questions to Peter at MUGUK was the following - Is there a way to add comments to the .mbp file? I have a terrible memory, so any comments within the mapbasic project file which aided my memory would have been helpful. At the time we tested it together and indeed it turned out you couldn't... Or at least, that's what we thought!

Comments in MapBasic Projects

If you don't already know, most programmers don't enjoy re-inventing the wheel, or if they do, they don't have time to do so. For this reason many programmers often build their own file formats around existing ones.

The same is true for the .mbp file format. It's a little known fact that the .mbp file format is actually based on the .ini format. The .ini file format is a common 'initialisation' file format used in many Windows applications. It is certainly true that the .mbp file format likely differs to the .ini file format in some ways, however the structures, at least, are identical:

[Section-Header]
Key1=Value1
Key2=Value2
...

For this reason we can make some assumptions about the file format, and then test these assumptions later in the MapBasic application. One assumption we might make is that the MapBasic applicaton uses the standard .ini functions provided by Microsoft to parse the .mbp file.

To check whether we are correct we can use API Monitor

Given that the MapBasic application uses the standard functions provided by Microsoft to parse the .mbp file, we can assume that the parser looks for the section header [Link] and then reads from there. To test this we can add new headers and see if the compiler fails to link the file:

[SomeHeader]
[Link]
Application = UpdateFromAmp5.mbx
Module = UpdateFromAmp5.MBO  
Module = FULL_RIBBON.MBO
[SomeOtherHeader]

When we run the above example in MapBasic, the application links correctly therefore we can assume that it will ignore the contents of these headers as well, and this is where we can put our comment. For example:

[Comment]
Some awesome comment
[Link]
Application = UpdateFromAmp5.mbx
Module = UpdateFromAmp5.MBO  
Module = FULL_RIBBON.MBO

The above example also links correctly which leaves us to believe that we can use this as a comment system in MapBasic projects. Here's an example I made earlier:

[Comment]
How to build an mbp file:

Application = The name of the final application produced by the project.
Module      = The main source codes
Module      = Library used (always in .MBO format)
Module      = Library used (always in .MBO format)
...

Any sections [...] specified on an own line, which are not [Link], will be ignored by the ini parser (A .mbp file is simply an .ini file)

This allows our comment system to work.

Note:
Be aware that common INI file conventions such as comments:
   #This is a comment...
   ;This is a comment...
Do not work in the link section, as MapBasic errors on all 'non existing files'.

[Link]
Application = UpdateFromAmp5.mbx
Module = UpdateFromAmp5.MBO  
Module = FULL_RIBBON.MBO

So next time you write a MapBasic project file, you might find it useful to right a comment header to your file explaining what each file in your project does. Or maybe you have other ideas where this would be useful.

Happy coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment