ProFactor Software Limited

Home > Products > StyleManager > Examples

Icon Download

ProFactor StyleManager Your Code: Your Way.

StyleManager Examples

These examples are based on the freeware Base16/32/64 encoder code from Cyotec Systems Limited. The original code can be found here, and contains the following important notice:

This source code may be used, modified, compiled, duplicated, and/or distributed without restriction provided this copyright notice remains intact. Cyotec Systems Limited and/or its employees cannot be held responsible for any direct or indirect damage or loss of any kind that may result from using this code, and provides no warranty, guarantee, or support.

Stroustrup

Taking the Base16Encode() function as an example, we can see that this code is formatted in a style quite similar to the Stroustrup formatting standard to begin with:

unsigned long CyoEncode::Base16Encode( void* dest, const void* src,
    unsigned long size )
{
    if (!dest || !src)
        throw std::runtime_error( c_pszErrorInvalidParameter );

    ///

    LPBYTE pSrc = (LPBYTE)src;
    LPBYTE pDest = (LPBYTE)dest;
    DWORD dwSrcSize = size;
    DWORD dwDestSize = 0;

    while (dwSrcSize >= 1)
    {
        // 1 input...
        BYTE ch = *pSrc++;
        dwSrcSize -= BASE16_INPUT; //1

        // 2 outputs...
        *pDest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ];
        *pDest++ = BASE16_TABLE[ (ch & 0x0f)      ];
        dwDestSize += BASE16_OUTPUT; //2
    }

    return dwDestSize;
}

Ellemtel

If your organisation does not use the Stroustrup style, and you chose to import this code into your project, you would very likely benefit from reformatting the code to match the rest of your project. Suppose for a moment that your project is formatted in a style similar to the Ellemtel style, which specifies - among other things - that function arguments should be listed one-per-line, that braces should be outer-scope aligned, and that 3-character indentation should be used throughout.

To format this block of code you could try Visual Studio's built-in formatter, but you'll quickly find that this does little more than change the tabulation of the braces. You could also format it by hand, but that would be quite a chore, and you could easily miss out an important rule.

Putting StyleManager to the task, we can see several changes:

unsigned long CyoEncode::Base16Encode( void* dest,            // Arguments are
                                       const void* src,       // each on their
                                       unsigned long size )   // own line
{
   if ( !dest || !src )
      throw std::runtime_error ( c_pszErrorInvalidParameter );

   ///

   LPBYTE pSrc = ( LPBYTE )src;        // Indentation is in 3-space steps
   LPBYTE pDest = ( LPBYTE )dest;
   DWORD dwSrcSize = size;
   DWORD dwDestSize = 0;

   while ( dwSrcSize >= 1 )            // Extra spacing on statement brackets
   {
      // 1 input...
      BYTE ch = *pSrc++;
      dwSrcSize -= BASE16_INPUT; //1

      // 2 outputs...
      *pDest++ = BASE16_TABLE[ ( ch & 0xf0 ) >> 4 ];
      *pDest++ = BASE16_TABLE[ ( ch & 0x0f )      ];
      dwDestSize += BASE16_OUTPUT; //2
   }

   return dwDestSize;
}

GNU

Now let's take a look at what happens when we apply the GNU styling rules. With StyleManager, this is simply a case of selecting this style and reviewing the results in the Style Previewer. This time, we see several different changes to the coding style:

unsigned long                          // Return type on its own line
CyoEncode::Base16Encode ( void *dest, const void *src, unsigned long size )
{                          // ^^^ Asterisks attach to the variable name
  if (! dest || ! src)
    throw std::runtime_error ( c_pszErrorInvalidParameter );

  ///

  LPBYTE pSrc = (LPBYTE)src;           // 2-space indents
  LPBYTE pDest = (LPBYTE)dest;
  DWORD dwSrcSize = size;
  DWORD dwDestSize = 0;

  while (dwSrcSize >= 1)
    {                                  // Braces indent separately
      // 1 input...
      BYTE ch = *pSrc++;
      dwSrcSize -= BASE16_INPUT; //1

      // 2 outputs...
      *pDest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ];
      *pDest++ = BASE16_TABLE[ (ch & 0x0f)      ];
      dwDestSize += BASE16_OUTPUT; //2
    }

  return dwDestSize;
}

You can try these examples for yourself, and also try out these style changes on your own code, by downloading the full product from our Downloads section.

Take a look at some screenshots for more examples of how easy it is to pretty print your code with StyleManager.

< Prev | Next >









(c) 2004-2014 - ProFactor Software Ltd.