home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / DOCJET.ZIP / data.z / NameSpace Hook.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-18  |  2.8 KB  |  103 lines

  1. /* Copyright (C) 1997-1998, Tall Tree Software Co.
  2.  *
  3.  * This code is provided AS-IS, with no warranty expressed or implied.
  4.  *  in no way shall Tall Tree Software Co. be held liable for damages
  5.  *  brought about by the use of this software.
  6.  */
  7.  
  8.  
  9. #include <afxwin.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <assert.h>
  13.  
  14. #include <Rewriter Hook.h>
  15.  
  16. #include "resource.h"
  17. #include "Settings.h"
  18.  
  19. //  The purpose of this DLL is to remove unnecessary namespace qualifiers.
  20. //  The "Settings" dialog allows users to enter the namespace prefixes they
  21. //  want to remove and OnObjectFound does the deed.
  22.  
  23.  
  24. ///////////////////////////////////////////////////////////////////////
  25. //  Preliminary junk
  26.  
  27. static HookReportFunction nextOnObjectFound;
  28.  
  29. extern "C" DLLEXPORT void SetNextOnObjectFound( HookReportFunction onObjectFound )
  30. {
  31.     nextOnObjectFound = onObjectFound;
  32. }
  33.  
  34. static const char *MatchNS( const char *ns, const char *objname )
  35. {
  36.     while ( *ns != '\0' ) {
  37.         if ( *ns == *objname ) {
  38.             ++ns;
  39.             ++objname;
  40.         }
  41.         else if ( *ns == '.' && objname[0] == ':' && objname[1] == ':' ) {
  42.             ++ns;
  43.             objname += 2;
  44.         }
  45.         else if ( ns[0] == ':' && ns[1] == ':' && objname[0] == '.' ) {
  46.             ns += 2;
  47.             ++objname;
  48.         }
  49.         else if ( ( ns[0] == '.' || ns[0] == ':' ) && objname[0] == '\0' ) {
  50.             return( objname );
  51.         }
  52.         else
  53.           return 0;
  54.     }
  55.     if ( *objname == '.' )
  56.       return( objname+1 );
  57.     else if ( objname[0] == ':' && objname[1] == ':' )
  58.       return( objname+2 );
  59.     else if ( objname[0] == '\0' )
  60.       return objname;
  61.     else
  62.       return( 0 );
  63. }
  64.  
  65. extern "C" DLLEXPORT void OnObjectFound( const char *p_sourceFile,
  66.                      unsigned int p_commentStartLine,
  67.                      unsigned int p_declStartLine,
  68.                      const char *p_objectName,
  69.                      const Subtype &p_subtype,
  70.                      const char *p_superclass,
  71.                      const char *p_declaration,
  72.                      const char *p_comment,
  73.                      const char *p_body,
  74.                      char const **p_extras )
  75. {
  76.     assert( nextOnObjectFound != 0 );
  77.     char *declaration = 0;
  78.  
  79.     for ( char **np = namespaces; *np != 0; ++np ) {
  80.         const char *newName = MatchNS( *np, p_objectName );
  81.         if ( newName != 0 && *newName == '\0' )
  82.           return;
  83.         else if ( newName != 0 ) {
  84.             const char *dp = strstr( p_declaration, p_objectName );
  85.             if ( dp != 0 ) {
  86.                 declaration = strdup( p_declaration );
  87.                 memmove( declaration + (dp-p_declaration),
  88.                      declaration + (dp-p_declaration) +
  89.                        (newName - p_objectName),
  90.                      strlen( p_declaration ) - 
  91.                        ( dp - p_objectName ) - strlen( p_objectName ) );
  92.             }
  93.             p_objectName = newName;
  94.             break;
  95.         }
  96.     }
  97.     (*nextOnObjectFound)( p_sourceFile, p_commentStartLine, p_declStartLine,
  98.                   p_objectName, p_subtype, p_superclass,
  99.                   ( declaration == 0 ? p_declaration : declaration ),
  100.                   p_comment, p_body, p_extras );
  101. }
  102.  
  103.