Compile Options
To use Artistic Style with a Graphical User Interface (GUI) it is no longer necessary to remove the source module astyle_main.cpp and write embedded code to call the formatter. AStyle can now be compiled as a static library or a shared library (DLL). The entire source code can also be included in the project with the GUI source.
To compile AStyle for use in a GUI the compile option ASTYLE_LIB must be defined (e.g.-DASTYLE_LIB). Then AStyle will accept the files and options as parameters from a function call. It is the responsibility of the GUI to read the source files and accept the options from the user via the GUI. These are then passed to AStyle via the function call described below. After the source files are formatted they will be returned to the GUI. The GUI must then save the source file and take other appropriate action.
AStyleMain Function
This function is called to format the source code.
Syntax
extern "C" EXPORT char* STDCALL AStyleMain ( char* pSourceIn, char* pOptions, void (STDCALL *fpError)(int, char*), char* (STDCALL *fpAlloc)(unsigned long) );Parameters
pSourceIn
A pointer to the source file to be formatted.pOptions
A pointer to the formatting options. They should be in the same format as in the default options file. The options may be set apart by new-lines, tabs or spaces. The long options do not need the '--' prefix.fpError
A pointer to the error handling function. If there are errors in the parameters pSourceIn or pOptions, this function is called. It should display an error message and then either abort or continue the program depending on the error. The first parameter is a number identifying the error. The second parameter is a pointer to a standard error message.Error messages numbered 100-199 are errors that prevent the file from being formatted. A NULL pointer is returned to the calling program. Error messages numbered 200-299 are errors that do NOT prevent the file from being formatted. A valid pointer and a formatted file are returned. This will occur if an invalid option is sent to AStyleMain. The calling program has the option of accepting or rejecting the formatted file.
fpAlloc
A pointer to the memory allocation function. The calling program must allocate memory for the output source file. This function will be called when the memory is needed. The parameter is the amount of memory that should be allocated. The calling program is responsible for freeing the allocated memory when it is no longer required.Return Value
If the function succeeds, the return value is a pointer to the formatted source code.
If the function fails, the return value is NULL.
This function typically fails for one of the following reasons:
- an invalid parameter value, usually a NULL pointer.
- the memory allocation function (fpAlloc) returns a NULL.
Remarks
The EXPORT and STDCALL macros are defined for Windows as __stdcall and __declspec(dllexport) respectively. They are needed if AStyle is compiled as a Windows static library or DLL. For Linux they are defined but do not have a value.
The calling program is responsible for freeing the memory allocated by fpAlloc when it is no longer needed.
Example
The following example formats source files by calling the Artistic Style formatter. It is a console application but the procedure for a GUI is the same. The example can be copied and pasted into a source file. The Artistic Style source code must be added to the project and compiled with the option ASTYLE_LIB. The AStyle source code can also be compiled as a static or shared library (DLL), with the option ASTYLE_LIB, and linked to the project.
// Example.cpp /* This program calls the Artistic Style GUI formatter (AStyleMain) * to format the astyle source files in a 'test' directory. */ #include#include using namespace std; // allow for different calling conventions in Linux and Windows #ifdef _WIN32 #define STDCALL __stdcall #define EXPORT __declspec(dllexport) #else #define STDCALL #define EXPORT #endif // functions to call AStyleMain extern "C" EXPORT char* STDCALL AStyleMain(char* pSourceIn, char* pOptions, void(STDCALL *fpError)(int, char*), char*(STDCALL *fpAlloc)(unsigned long)); void STDCALL ASErrorHandler(int errorNumber, char* errorMessage); char* STDCALL ASMemoryAlloc(unsigned long memoryNeeded); // other functions void error(const char *why, const char* what); int main(int, char **) { char testFileName[][30] = { "../../test/ASBeautifier.cpp", "../../test/ASEnhancer.cpp" , "../../test/ASFormatter.cpp" , "../../test/ASResource.cpp" , "../../test/astyle_main.cpp" , "../../test/astyle.h" }; char* options = "-atP"; size_t arraySize = sizeof(testFileName) / sizeof(testFileName[0]); for (size_t i = 0; i < arraySize; i++) { char* originalFileName = testFileName[i]; char* inFileName = new char [strlen(originalFileName) + 6]; strcpy(inFileName, originalFileName); strcat(inFileName, ".orig"); remove(inFileName); if (rename(originalFileName, inFileName) < 0) error("Could not open input file", originalFileName); ifstream in(inFileName); if (!in) error("Could not open input file", inFileName); // get length of buffer in.seekg(0, ifstream::end); int bufferSizeIn = in.tellg(); in.seekg(0, ifstream::beg); // allocate memory char* bufferIn = new char [bufferSizeIn]; // read data as a block in.read(bufferIn, bufferSizeIn); in.close(); // get actual size - will be smaller than buffer size int textSizeIn = in.gcount(); bufferIn[textSizeIn-1] = '\0'; ofstream out(originalFileName); if (!out) error("Could not open output file", originalFileName); cout << "formatting " << originalFileName << endl; // call the Artistic Style formatting function char* bufferOut = AStyleMain(bufferIn, options, ASErrorHandler, ASMemoryAlloc);// if an error message occurred it was displayed by ASErrorHandler if (bufferOut == NULL) exit(1); int textSizeOut = strlen(bufferOut); out.write(bufferOut, textSizeOut); out.close(); delete [] inFileName; delete [] bufferIn; delete [] bufferOut; } } // Error handler for the Artistic Style formatter void STDCALL ASErrorHandler(int errorNumber, char* errorMessage) { cout << "error " << errorNumber << " - " << errorMessage << '\n' << endl; } // Allocate memory for the Artistic Style formatter char* STDCALL ASMemoryAlloc(unsigned long memoryNeeded) { // error condition is checked after return from AStyleMain char* buffer = new char [memoryNeeded]; return buffer; } // Error message function for this example void error(const char *why, const char* what) { cout << '\n' << why << ' ' << what << '\n' << endl; exit(1); }