def FixFile(filename):
    """ Fixes the C fortran stub files generated by bfort"""
    import re
    ff = open(filename)
    data = ff.read()
    ff.close()
    data = re.subn('\nvoid ', '\nSLEPC_EXTERN void ', data)[0]
    data = re.subn('\nPetscErrorCode ', '\nSLEPC_EXTERN void ', data)[0]
    data = re.subn('Petsc([ToRm]*)Pointer\\(int\\)',
        'Petsc\\1Pointer(void*)', data)[0]
    data = re.subn('PetscToPointer\\(a\\) \\(a\\)',
        'PetscToPointer(a) (*(PetscFortranAddr *)(a))', data)[0]
    data = re.subn('PetscFromPointer\\(a\\) \\(int\\)\\(a\\)',
        'PetscFromPointer(a) (PetscFortranAddr)(a)', data)[0]
    data = re.subn('PetscToPointer\\( \\*\\(int\\*\\)', 'PetscToPointer(', data
        )[0]
    data = re.subn('MPI_Comm comm', 'MPI_Comm *comm', data)[0]
    data = re.subn('\\(MPI_Comm\\)PetscToPointer\\( \\(comm\\) \\)',
        'MPI_Comm_f2c(*(MPI_Fint*)(comm))', data)[0]
    data = re.subn('\\(PetscInt\\* \\)PetscToPointer', '', data)[0]
    data = re.subn('\\(Tao\\* \\)PetscToPointer', '', data)[0]
    data = re.subn('\\(TaoConvergedReason\\* \\)PetscToPointer', '', data)[0]
    data = re.subn('\\(TaoLineSearch\\* \\)PetscToPointer', '', data)[0]
    data = re.subn('\\(TaoLineSearchConvergedReason\\* \\)PetscToPointer',
        '', data)[0]
    match = re.compile(
        '\\b(PETSC|TAO)(_DLL|VEC_DLL|MAT_DLL|DM_DLL|KSP_DLL|SNES_DLL|TS_DLL|FORTRAN_DLL)(EXPORT)'
        )
    data = match.sub('', data)
    ff = open(filename, 'w')
    ff.write(
        """#include "petscsys.h"
#include "petscfix.h"
#include "petsc/private/fortranimpl.h\"
"""
         + data)
    ff.close()
