|
Home > Products > StyleManager > Examples
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)
{
BYTE ch = *pSrc++;
dwSrcSize -= BASE16_INPUT;
*pDest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ];
*pDest++ = BASE16_TABLE[ (ch & 0x0f) ];
dwDestSize += BASE16_OUTPUT;
}
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,
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 )
{
BYTE ch = *pSrc++;
dwSrcSize -= BASE16_INPUT;
*pDest++ = BASE16_TABLE[ ( ch & 0xf0 ) >> 4 ];
*pDest++ = BASE16_TABLE[ ( ch & 0x0f ) ];
dwDestSize += BASE16_OUTPUT;
}
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
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)
{
BYTE ch = *pSrc++;
dwSrcSize -= BASE16_INPUT;
*pDest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ];
*pDest++ = BASE16_TABLE[ (ch & 0x0f) ];
dwDestSize += BASE16_OUTPUT;
}
return dwDestSize;
}
You can try these examples for yourself, and also try out these style changes
on your own code, by downloading the StyleManager free trial 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 >
Free Trial
StyleManager will change the way your format your code - but don't just take our word for it. Try before you buy with StyleManager's no obligation, 28-day free trial.
Download the free trial from our Downloads section, or
head straight to our Online Store to buy your copy now.

|