Author Topic: Insert GUID plugin  (Read 3719 times)


  • Guest
Insert GUID plugin
« on: August 19, 2010, 02:06:17 am »
Hi all,

I'm a pretty new C::B user, so please bear with me if I violate any forum protocol. :-)

I would like to write a plugin that makes it easy to insert a GUID/UUID into the source code. There are many uses for this, one of which is to ensure my header file multiple-include protectors are unique, like this:

In MyClass.h:
Code: [Select]
#ifndef MY_CLASS_H_c624d840_ab24_11df_94e2_0800200c9a66
#define MY_CLASS_H_c624d840_ab24_11df_94e2_0800200c9a66

class MyClass

#endif //MY_CLASS_H_c624d840_ab24_11df_94e2_0800200c9a66

I guess I would like the plugin to also be able to automate this too, as in it would see the file name is "MyClass.h", and it would put in the #ifndef/#define/#endif automatically, using the file naming and a fresh GUID.

I was just wondering if a plugin like this exists or is already in development. If not, I could use some general pointers on how to get started.

Chris Anderson

P.S. Code::Blocks rules!!!   :D

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5253
Re: Insert GUID plugin
« Reply #1 on: August 19, 2010, 12:28:37 pm »

When you have CB create classes for you, it will place guards there for you (but not based on guid). In our repo we have a plug-in experiment (it is not included in CB builds), you can have a look at it, this one is generating a guard which might be unique.

The core code :
Code: [Select]
void HeaderGuard::OnSave(CodeBlocksEvent& event)
cbEditor* ed = (cbEditor*) event.GetEditor();

wxString n = ed->GetFilename();

if(!n.Right(2).Lower().IsSameAs(_T(".h")) && !n.Right(3).Lower().IsSameAs(_T(".hpp")) && !n.Right(3).Lower().IsSameAs(_T(".hxx")))

// Header guards should appear before any comments or code to ensure the compiler can perform header guard optimisation,
// therefore we don't really need to scan the full text, checking the first line is good enough and much faster, both here and in build.
// It also doesn't choke on external header guards that some people still use.
// We're omitting a check for "#pragma once", as that's not standard compliant. It doesn't hurt to add guards around "#pragma once", either.
const wxString s(ed->GetControl()->GetLine(0).Trim(true).Trim(false));
if(s.StartsWith(_T("#ifndef")) && ( s.Contains(_T("HEADER")) || s.Right(2).IsSameAs(_T("_H")) || s.Contains(_T(" H_")) || s.Contains(_T("_H_")) || s.Contains(_T("_INCLUDED")) ))

unsigned int s1 = 257, s2 = 123, b = 9737333, c = 174440041;
for(unsigned int i = 0; i < n.length(); ++i)
b = (b*33) + (s1 += n[i]);
(c *= 47) += s2;
s2 += s1;
b ^= (((s1%255)<<10) | (s2&255));
c ^= (c << 14); b ^= (b << 21); c ^= (c >> 5);
b ^= (b >> 17); c ^= (c << 11); b ^= (b << 9);

n.Printf(_T("#ifndef HEADER_%X%X\n#define HEADER_%X%X\n\n"), b, c, b, c);
ed->GetControl()->InsertText (0, n);
ed->GetControl()->InsertText (ed->GetControl()->GetLength(), _T("\n#endif // header guard \n"));

Maybe this can be a starting point ?