Just another WordPress.com site

Visual C++ Code Formatting with AStyle

Visual Studio VBA macro to format the current C++/h document using the open source code formatter AStyle. Simply copy the macro to the “Macros->Module1” is Visual Studio, then hookup a hot key to it.

Download AStyle from http://astyle.sourceforge.net/ and extract to a directory (code will look in the download directory off the user’s home directory). Update the shellCmd string variable with any formatting options AStyle should apply (http://astyle.sourceforge.net/astyle.html).

Format string “-A4Uxpfn”
-A4 = stroustrup style
-U  = unpad paren
-x  = delete empty lines
-p  = pad operators
-f  = break blocks
-n  = do not retain backup

Code Snippet
  1. Public Sub formatDocWithAStyle()
  2. Dim fileName As String
  3. Dim textDocument As TextDocument
  4. Dim startPoint As EnvDTE.EditPoint
  5. Dim endPoint As EnvDTE.EditPoint
  6. Dim text As String
  7. Dim shellCmd As String
  8. Dim procId As Integer
  9. If DTE.ActiveDocument Is Nothing Then
  10. Return
  11. End If
  12. fileName = Environ$(“TEMP”) & “\formattedCode”
  13. shellCmd = “””” & Environ$(“HOMEPATH”) & “\Downloads\AStyle\bin\AStyle” & “””” & ” -A4Uxpfn “ & “””” & fileName & “”””
  14. textDocument = DTE.ActiveDocument.Object
  15. startPoint = textDocument.StartPoint.CreateEditPoint
  16. endPoint = textDocument.EndPoint.CreateEditPoint
  17. text = startPoint.GetText(endPoint)
  18. My.Computer.FileSystem.WriteAllText(fileName, text, False)
  19. procId = Shell(shellCmd, AppWinStyle.Hide, True, 30000)
  20. If procId = 0 Then
  21. text = My.Computer.FileSystem.ReadAllText(fileName)
  22. startPoint.ReplaceText(endPoint, text, vsEPReplaceTextOptions.vsEPReplaceTextTabsSpaces)
  23. End If
  24. End Sub

January 23, 2011 Posted by | Code Formatting | , , , , | 1 Comment

Installing latest gdb on Snow Leopard

Figures there would be a problem installing gdb on Snow Leopard using Macports. The error message was
libbfd.c: In function ‘bfd_get_b_signed_64’:

lipo: can’t figure out the architecture type of: …

Looking at the build log file shows gdb being built for i386 and x86_64. After a little poking around I found the culprit to be in

where when I initially installed Macports had enabled +universal (don’t recall why). So, comment out the +universal line and rebuilt gdb.

sudo port clean gdb
sudo port install gdb

Afterwards, the new gdb will be installed to



October 11, 2010 Posted by | C++ Programming, gcc, Macports | , , , | Leave a comment

Installing latest gcc on Snow Leopard

I decided to install gcc 4.5.1 via Macports and the process didn’t exactly go smoothly. IIRC, the initial error was something about base_file.cc not being find. I’m not sure what caused the problem, however the fix was to temporarily rename the directory /opt/local to /opt/local-temp. The only packages installed in /opt/local/ are python 2.6, git, and Qt4.7.

So the basic steps if you have problems with the install using Macports is

cd /opt
mv local local-temp
sudo port clean gcc45
sudo port selfupdate
sudo port upgrade outdated
sudo port install gcc45

Towards the end of the build process there was another error about libpng being wrong or something. Simply reinstalling libpng via macports resolved the issue and allowed gcc finish building.

sudo port clean libpng
sudo port install libpng
sudo port install gcc45

Afterwards, rename /opt/local-temp to /opt-local.

If you type gcc –version at the command line the version install with XCode will probably run. This is because the Macports version is named gcc-mp-45. Using Macports install gcc_select.

sudo port install gcc_select

gcc_select –l will list the available versions of gcc, and gcc_select mp-gcc45 will make version 4.5 the default. This changes the symbolic link for /opt/local/bin/gcc. I also had to add the new symbolic link to /usr/local/bin/gcc (make a copy of the original) in order to get everything to work correctly.

October 11, 2010 Posted by | C++ Programming, gcc, Macports | , , , , , | 1 Comment

WGS84 Ellipsoid Calculations

(source code links below)
12/7/2010, added support for building as a DLL and using within Microsoft Excel VBA.

In late 2008, I was in negotiations with an aviation company to write some navigation design software. I use to work for the company and was already familiar with their software stacks and engineering processes. Quite a few of the applications the company uses, I had written or helped implement. The chances of actually getting the contract were slim though. Internal growth of the company was fast, and different departments were motivated to go a different direction than the department I was in talks with.

Even though the chances of getting the contract were slim, I wanted to get a jump on some crucial code that would be required from the onset. So I set out on October 15, 2008, to write all the formulas described in FAA Order 8260.54A, Appendix 2, and a test harness for the data published in attachment C of the same document.

Initially I figured it would take 2 to 4 weeks to write the library and test harness based on the completeness of 8260.54A. Almost immediately, I discovered the document to have many errors and omissions, which caused days long investigations into why the formulas didn’t produce the expected test results (the projects code, comments and file Application test Results.txt document some of the problems and how they are resolved).

On December 18, 2008, I finished the work, had no contract, but was proud that I’d resolved each and every formula in Appendix 2 and they produced the expected test results. Now, a year and a half later, I’m releasing the code for the formulas and test harness.

I wrote a standalone WGS84 Geodesic Calculator (beta) for Windows based on the formulas, and some people from the GeoCache community have used it to create and solve complicated puzzles.

The hardest part about releasing the code was which license to choose. Ultimately I decided for the liberal Apache License, Version 2.0, and it’s my contribution to the open source community.

WGS84 Ellipsoid Calculation Types

Arc Intercept Geodesic Locus Intersect Point on LocusP
Course Intersect Geometric Mean Meridional Point to Arc Tangents
Direct Arc Length Locus Arc Intersect Pt is on Arc
Discretized Arc Length Locus Course at Point Pt is on Geodesic
Distance Intersection Locus Intersect Pt is on Locus
Distance to LocusP Locus Perpendicular Intercept Tangent Fixed Radius Arc
Distance to LocusD Locus Tangent Fixed Radius Arc Vincenty Direct
ECEF Perpendicular Intercept Vincenty Inverse
Geodesic Arc Intercept Perpendicular Tangent Points

There are a few other implemented formulas which are support to the ones listed above.  Also, the TerpsTest code for validating the formulas is a good source to see how to use the formulas in your own code.

Available Source Code

The GeoFormulas and TerpsTest (test harness) code is hosted on GitHub, and released under the Apache License, Version 2.0. Note: TerpsTest requires Boost::RegEx. Outside of STL, GeoFormulas is not dependent on external libraries.

The source code for the standalone Windows WGS84 Geodesic Calculator is not available at this time.

July 14, 2010 Posted by | Ellipsoid, FAA, WGS84 | , , , , , , , , | 3 Comments

N-dimension array initialization

In the previous article Container assignment list I show how to assign items to STL containers using a list of items separated by a comma.

This example will show how to do the same with a N-dimensional array, with debug build bounds checking. First an example which shows how to declare and initialize a N-dimensional array of type int or type double with a comma separated list of items.

  1. // define a 3 dimensional array of int's
  2. // and initialize.
  3. MyArray<int, 3> intArray3D;
  4. intArray3D = 1, 2, 3;
  6. // define a 2 diemsional array of double's
  7. // and initialize.
  8. MyArray<double, 2> doubleArray2D;
  9. doubleArray2D = 10.0, 20.0;
  11. // define a 3 x 3 matrix of double and initialize
  12. MyArray<double, 9> doubleMatrix3x3;
  13. doubleMatrix3x3 = 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0;

The N-Dimensional array class implementation

The array class code below only shows enough code to get the assignment and comma operator overloaded. Other useful code and overloads that this class would have is not shown, in order to keep the discussion simple and on topic.

MyArray Class
  1. template<class T, int ArraySize>
  2. class MyArray
  3. {
  4. public:
  5.     MyArray()
  6.     {
  7.         assert(ArraySize != 0);
  8.         // Set items in array to 0
  9.         for(size_t i = 0; i < ArraySize; ++i)
  10.             m_array[i] = 0;
  11.     }
  13.     Initializer<T, ArraySize> operator=(T val)
  14.     {
  15.         // store RHS of = to first array item location
  16.         m_array[0] = val;
  18.         // Call Initializer construct with address of m_array[1],
  19.         // return Initializer object.
  20.         // Allows the overloaded comma operator for Initializer
  21.         // to be called repeatedly.
  22.         return Initializer<T, ArraySize>(&m_array[1]);
  23.     }
  24. private:
  25.     T m_array[ArraySize];
  26. };

MyArray is a template class that needs 2 template parameters, the first is the type of array to create, and the second is the number of elements the array can store.

The default constructor assigns 0 to each element in the array, if this does not make sense for the type object being stored in the array then modify as needed.

The overloaded assignment operator takes the first item in the list, right of the = sign (RHS), and assigns it to first position in the array. It then calls Initializer’s constructor with the address of the second element in the array, and returns the Initializer object.

This sets up Initializer, which overloads the comma operator, to handle the rest of the assignment list. Overloading the assignment and comma operators is discussed in the article Container assignment list.

The Initializer class is simple and documented below.

Initializer Class for MyArray
  1. // Initializer class. Helper class for MyArray,
  2. // keeping track of the current element being
  3. // assigned, with the overloaded comma operator,
  4. template<class T, int ArraySize>
  5. class Initializer
  6. {
  7. public:
  8.     Initializer() : m_ptr(0)
  9.     {
  10.         #ifndef NDEBUG
  11.             m_nCount = 0;
  12.         #endif
  13.     }
  15.     // Constructor called by MyArray operator=
  16.     // ptr will equal MyArray::m_array[1]. Since
  17.     // For debug builds set m_nCount to 1 since
  18.     // MyArray already assigned element [0].
  19.     Initializer(T * ptr) : m_ptr(ptr)
  20.     {
  21.         #ifndef NDEBUG
  22.             m_nCount = 1;
  23.         #endif
  24.     }
  26.     // Overloaded comma operator
  27.     Initializer & operator,(T val)
  28.     {
  29.         #ifndef NDEBUG
  30.             // Debug assert for array overrun
  31.             assert(m_nCount < ArraySize);
  32.             ++m_nCount;
  33.         #endif
  35.         // store value in pointer to array, then
  36.         // increment pointer to array to next location
  37.         *m_ptr++ = val;
  38.         return *this;
  39.     }
  40. private:
  41.     // Pointer to array location
  42.     T * m_ptr;
  43.     #ifndef NDEBUG
  44.         int m_nCount;
  45.     #endif
  46. };

July 12, 2010 Posted by | Arrays, C++ Programming, Templates | , , , , , | Leave a comment