📄 gen_code.c
字号:
/* TA-LIB Copyright (c) 1999-2007, Mario Fortier * All rights reserved. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * - Neither name of author nor the names of its contributors * may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *//* List of contributors: * * Initial Name/description * ------------------------------------------------------------------- * MF Mario Fortier (mfortier@ta-lib.org) * ST Steve Thames (steve@softlife.com) * AC Angelo Ciceri * RM Robert Meier (talib@meierlim.com) * CM Craig Miller (c-miller@users.sourceforge.net) * RG Richard Gomes * * Change history: * * MMDDYY BY Description * ------------------------------------------------------------------- * 112400 MF First version. * 052403 MF Many modifications related to generate code that works * with the windows .NET Managed C++ compiler. * 092103 MF Now touch files only when there is really a change. * 101303 MF Remove underscore from names. * 020804 MF,ST Fixes to make it work on Linux (Bug#873879). * 022904 MF Add TA_GetLookback * 030604 MF Add generation of "ta_func.swg" * 082004 AC Add generation of candlestick functions declaration * 010405 RM Change createProjTemplate to work with VS03 and VS05 * 031805 MF Add generation of MSVC project file. * 061805 MF Changes related to .NET verifiable code. * 062505 MF Fix 'out' attribute for .NET verifiable code. * 121705 MF Complete Java port. * 012806 MF Add call to Java post-processing. * 093006 MF Add code generation for TA_FunctionDescription * 110206 AC Change volume and open interest to double * 120106 MF Add generation of java_defs.h * 122406 MF Add generation of Makefile.am * 011707 CM Add ta_pragma.h handles VC8 warnings, type conversion of strlen handles VC warning * 021807 MF Add generation of VS2005 project file * 040107 MF,RG Add generation of CoreAnnotated.java *//* Description: * Generates a lot of source code. Run "gen_code ?" for * the list of file. * * The generator use as input the interface definition * of each of the TA functions. The interface is provided * from static data in the TA-Abstract module. * (See the 'table_x.c' files) * * This utility is intended only to people integrating new * TA functions in TA-Lib. * * Note: All directory in this code is relative to the 'bin' * directory. You must run "gen_code" from ta-lib/c/bin. * */#include "ta_pragma.h" /* this must be the first inclusion */#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <ctype.h> #if !defined(__WIN32__) && !defined(__MSDOS__) && !defined(WIN32) #include <unistd.h>#endif#if defined (WIN32) #include <windows.h>#endif#include "ta_common.h"#include "ta_abstract.h"#include "ta_memory.h"extern int mcpp_main( int argc, char ** argv);#define BUFFER_SIZE 16000#define FILE_WRITE 0#define WRITE_ON_CHANGE_ONLY 0#define FILE_READ 0x00000001#define WRITE_ALWAYS 0x00000002#ifndef min #define min(a, b) (((a) < (b)) ? (a) : (b))#endif#ifndef max #define max(a, b) (((a) > (b)) ? (a) : (b))#endiftypedef struct{ FILE *file; FILE *fileTarget; FILE *templateFile; char f1_name[BUFFER_SIZE]; char f2_name[BUFFER_SIZE]; int flags;} FileHandle;FileHandle *gOutFunc_H; /* For "ta_func.h" */FileHandle *gOutFrame_H; /* For "ta_frame.h" */FileHandle *gOutFrame_C; /* For "ta_frame.c" */FileHandle *gOutGroupIdx_C; /* For "ta_group_idx.c" */FileHandle *gOutFunc_C; /* For "ta_x.c" where 'x' is TA function name. */FileHandle *gOutRetCode_C; /* For "ta_retcode.c" */FileHandle *gOutRetCode_CSV; /* For "ta_retcode.csv" */FileHandle *gOutFuncList_TXT; /* For "ta_func_list.txt" */FileHandle *gOutDefs_H; /* For "ta_defs.h" */FileHandle *gOutFunc_SWG; /* For SWIG */FileHandle *gOutFunc_XML; /* For "ta_func_api.xml" */FileHandle *gOutFuncAPI_C; /* For "ta_func_api.c" */FileHandle *gOutMakefile_AM; /* For "Makefile.am" */#ifdef _MSC_VER/* The following files are generated only on Windows platform. */FileHandle *gOutDotNet_H; /* For .NET interface file */FileHandle *gOutCore_Java; /* For Core.Java */FileHandle *gOutProjFile; /* For .NET project file */FileHandle *gOutMSVCProjFile; /* For MSVC project file */FileHandle *gOutVS2005ProjFile; /* For VS2005 project file */FileHandle *gOutExcelGlue_C; /* For "excel_glue.c" */FileHandle *gOutJavaDefs_H; /* For "java_defs.h" */FileHandle *gOutFunc_Annotation; /* For "CoreAnnotated.java" *//* Why these file are not generated from a unix platform? * * The reason is obvious for .NET, Excel and MSVC related files. * * For the Java code, the reason is that I use a C preprocessor * called MCPP and for now I have ported it only on windows. * (see the mcpp.exe included in the package). * If someone get the mcpp or an equivalent to be integrated * in gen_code, then Java code could also be generated from unix. */static void printExcelGlueCode( FILE *out, const TA_FuncInfo *funcInfo );static void genJavaCodePhase1( const TA_FuncInfo *funcInfo );static void genJavaCodePhase2( const TA_FuncInfo *funcInfo );/* To generate CoreAnnotated.java */static void printJavaFunctionAnnotation(const TA_FuncInfo *funcInfo);#endiftypedef void (*TA_ForEachGroup)( const char *groupName, unsigned int index, unsigned int isFirst, /* Boolean */ unsigned int isLast /* Boolean */ );static unsigned int forEachGroup( TA_ForEachGroup forEachGroupfunc, void *opaqueData );static void doForEachFunctionPhase1( const TA_FuncInfo *funcInfo, void *opaqueData );static void doForEachFunctionPhase2( const TA_FuncInfo *funcInfo, void *opaqueData );static void doForEachFunctionXml( const TA_FuncInfo *funcInfo, void *opaqueData );static void doForEachUnstableFunction( const TA_FuncInfo *funcInfo, void *opaqueData );static void doDefsFile( void );static int gen_retcode( void );static void printIndent( FILE *out, unsigned int indent );static void printFunc( FILE *out, const char *prefix, /* Can be NULL */ const TA_FuncInfo *funcInfo, unsigned int prototype, /* Boolean */ unsigned int frame, /* Boolean */ unsigned int semiColonNeeded, /* Boolean */ unsigned int validationCode, /* Boolean */ unsigned int lookbackSignature, /* Boolean */ unsigned int managedCPPCode, /* Boolean */ unsigned int managedCPPDeclaration, /* Boolean */ unsigned int inputIsSinglePrecision, /* Boolean */ unsigned int outputForSWIG, /* Boolean */ unsigned int outputForJava, /* Boolean */ unsigned int lookbackValidationCode, /* Boolean */ unsigned int useSubArrayObject, /* Boolean */ unsigned int arrayToSubArrayCnvt /* Boolean */ );static void printCallFrame ( FILE *out, const TA_FuncInfo *funcInfo );static void printFrameHeader( FILE *out, const TA_FuncInfo *funcInfo, unsigned int lookbackSignature );static void printExternReferenceForEachFunction( const TA_FuncInfo *info, void *opaqueData );static void printFunctionAddress( const TA_FuncInfo *info, void *opaqueData );static void printPerGroupList( const char *groupName, unsigned int index, unsigned int isFirst, unsigned int isLast );static void printGroupListAddress( const char *groupName, unsigned int index, unsigned int isFirst, unsigned int isLast );static void printGroupSize( const char *groupName, unsigned int index, unsigned int isFirst, unsigned int isLast );static void printGroupSizeAddition( const char *groupName, unsigned int index, unsigned int isFirst, unsigned int isLast );static int addUnstablePeriodEnum( FILE *out );static int createTemplate( FileHandle *in, FileHandle *out );static int generateFuncAPI_C( void );#ifdef _MSC_VERstatic int createProjTemplate( FileHandle *in, FileHandle *out );static int createMSVCProjTemplate( FileHandle *in, FileHandle *out );static int createVS2005ProjTemplate( FileHandle *in, FileHandle *out );static void printVS2005FileNode( FILE *out, const char *name );#endifstatic void writeFuncFile( const TA_FuncInfo *funcInfo );static void doFuncFile( const TA_FuncInfo *funcInfo );static void printOptInputValidation( FILE *out, const char *name, const TA_OptInputParameterInfo *optInputParamInfo, int lookbackValidationCode /* Boolean */ );static int skipToGenCode( const char *dstName, FILE *out, FILE *templateFile );static void printDefines( FILE *out, const TA_FuncInfo *funcInfo );static void printFuncHeaderDoc( FILE *out, const TA_FuncInfo *funcInfo, const char *prefix );static void extractTALogic( FILE *inFile, FILE *outFile );static void cnvtToUpperCase( char *str );static void cnvtChar( char *str, char from, char to );static char *trimWhitespace( char *str );/* Return 1 on success */static int copyFile( const char *src, const char *dest );/* Return 1 when identical */static int areFileSame( const char *file1, const char *file2 );static void fileDelete( const char *fileToDelete );static void appendToFunc( FILE *out );static void convertFileToCArray( FILE *in, FILE *out );static void ReplaceReservedXmlCharacters(const char *input, char *output );char gToOpen[BUFFER_SIZE];char gTempBuf[BUFFER_SIZE];char gTempBuf2[BUFFER_SIZE];char gTempBuf3[BUFFER_SIZE];char gTempBufForPrint[BUFFER_SIZE];char gTempDoubleToStr[200];/* Because Microsoft and Borland does not display * the value of a double in the same way (%e), this * function attempts to eliminate difference. This * is done to avoid annoying difference with CVS. */const char *doubleToStr( double value );const char *gCurrentGroupName;static int genCode(int argc, char* argv[]);extern const TA_OptInputParameterInfo TA_DEF_UI_MA_Method;/* Set this variable to 1 whenever you wish to output a * prefix to all generated line. */int genPrefix = 0;void print( FILE *out, const char *text, ... ){ va_list arglist; memset(gTempBufForPrint,0,sizeof(gTempBufForPrint)); va_start(arglist,text); vsprintf(gTempBufForPrint,text,arglist); va_end(arglist); if( strlen(gTempBufForPrint) >= BUFFER_SIZE-strlen("/* Generated */ ") ) { printf( "Lines length exceed internal buffers (%lu,%lu)\n", (unsigned long)strlen(gTempBufForPrint), (unsigned long)(BUFFER_SIZE-strlen("/* Generated */ ")) ); exit(-1); } if( genPrefix ) fprintf( out, "/* Generated */ %s", gTempBufForPrint ); else fprintf( out, "%s", gTempBufForPrint );}static void printIndent( FILE *out, unsigned int indent ){ unsigned int i; if( genPrefix ) fprintf( out, "/* Generated */ " ); for( i=0; i < indent; i++ ) { fprintf( out, " " ); }}int main(int argc, char* argv[]){ int retValue; TA_RetCode retCode; if( argc > 1 ) { /* There is no parameter needed for this tool. */ printf( "\n" ); printf( "gen_code V%s - Updates many TA-Lib source files\n", TA_GetVersionString() ); printf( "\n" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -