📄 logformat_trace_inputlog.c
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- *//* $Id: logformat_trace_InputLog.c,v 1.2 2002/09/27 21:11:14 toonen Exp $ * * (C) 2001 by Argonne National Laboratory. * See COPYRIGHT in top-level directory. *//* * @author Anthony Chan */#include <jni.h>#include <stdio.h>#include "logformat_trace_InputLog.h"#include "trace_input.h"#include <stdlib.h>#define ACHAR_LENGTH 256#define AINT_LENGTH 100#define ADOUBLE_LENGTH 100/* File descriptor for the output stream */#define outfile stdout /* File descriptor for the error stream */#define errfile stderr/* JNI Global Reference Variables */ jfieldID fid4filehandle = NULL;static jclass cid4String = NULL;static jclass cid4DobjDef = NULL; /* for Category */static jmethodID mid4NewDobjDef = NULL;static jclass cid4YMap = NULL; /* for YCoordMap */static jmethodID mid4NewYMap = NULL;static jclass cid4Prime = NULL; /* for Primitive */static jmethodID mid4NewPrime = NULL;static jclass cid4Cmplx = NULL; /* for Composite */static jmethodID mid4NewCmplx = NULL;JNIEXPORT void JNICALLJava_logformat_trace_InputLog_initIDs( JNIEnv *env , jclass myclass ){ fid4filehandle = (*env)->GetFieldID( env, myclass, "filehandle", "J" );}/* * tracefile != NULL, ierr == any => Continue * tracefile == NULL, ierr == 0 => help message, exit normally, TRUE * tracefile == NULL, ierr != 0 => error message, exit w/ error, FALSE */JNIEXPORT jboolean JNICALLJava_logformat_trace_InputLog_open( JNIEnv *env, jobject this ){ jclass myclass; jfieldID fid4filespec; jstring j_filespec; const char *c_filespec; TRACE_file tracefile; int ierr; myclass = (*env)->GetObjectClass( env, this ); fid4filespec = (*env)->GetFieldID( env, myclass, "filespec", "Ljava/lang/String;" ); if ( fid4filespec == NULL ) (*env)->SetLongField( env, this, fid4filehandle, (jlong) 0 ); j_filespec = (*env)->GetObjectField( env, this, fid4filespec ); c_filespec = (*env)->GetStringUTFChars( env, j_filespec, NULL ); /* c_filespec = JNU_GetStringNativeChars( env, jfilespec ); */ ierr = TRACE_Open( c_filespec, &tracefile ); /* Set the private variable "filehandle" in java class */ if ( tracefile != NULL ) { fprintf( outfile, "C: Opening trace %s ..... \n", c_filespec ); fflush( outfile ); (*env)->SetLongField( env, this, fid4filehandle, (jlong) tracefile ); return JNI_TRUE; } else { if ( ierr == 0 ) { /* if ( tracefile == NULL && ierr == 0 ) */ (*env)->SetLongField( env, this, fid4filehandle, (jlong) 0 ); fprintf( outfile, "%s\n", TRACE_Get_err_string( ierr ) ); fflush( outfile ); return JNI_TRUE; } else { /* if ( tracefile == NULL && ierr != 0 ) */ (*env)->SetLongField( env, this, fid4filehandle, (jlong) 0 ); fprintf( errfile, "%s\n", TRACE_Get_err_string( ierr ) ); fflush( errfile ); return JNI_FALSE; } }}JNIEXPORT jboolean JNICALLJava_logformat_trace_InputLog_close( JNIEnv *env, jobject this ){ TRACE_file tracefile; jlong filehandle; int ierr; /* Clean up all created Global References */ if ( cid4String != NULL ) { (*env)->DeleteGlobalRef( env, cid4String ); } if ( cid4DobjDef != NULL ) { (*env)->DeleteGlobalRef( env, cid4DobjDef ); /* if ( mid4NewDobjDef != NULL ) (*env)->DeleteGlobalRef( env, mid4NewDobjDef ); */ } if ( cid4YMap != NULL ) { (*env)->DeleteGlobalRef( env, cid4YMap ); /* if ( mid4NewYMap != NULL ) (*env)->DeleteGlobalRef( env, mid4NewYMap ); */ } if ( cid4Prime != NULL ) { (*env)->DeleteGlobalRef( env, cid4Prime ); /* if ( mid4NewPrime != NULL ) (*env)->DeleteGlobalRef( env, mid4NewPrime ); */ } if ( cid4Cmplx != NULL ) { (*env)->DeleteGlobalRef( env, cid4Cmplx ); /* if ( mid4NewCmplx != NULL ) (*env)->DeleteGlobalRef( env, mid4NewCmplx ); */ } filehandle = (*env)->GetLongField( env, this, fid4filehandle ); if ( filehandle == 0 ) { fprintf( errfile, "Java_logformat_trace_InputLog_close(): " "Inaccessible filehandle in Java side\n" ); return JNI_FALSE; } tracefile = (TRACE_file) filehandle; fprintf( outfile, "C: Closing trace ..... \n" ); fflush( outfile ); ierr = TRACE_Close( &tracefile ); if ( ierr == 0 || tracefile == NULL ) return JNI_TRUE; else { fprintf( errfile, "%s\n", TRACE_Get_err_string( ierr ) ); fflush( errfile ); return JNI_FALSE; }}JNIEXPORT jint JNICALL Java_logformat_trace_InputLog_peekNextKindIndex( JNIEnv *env, jobject this ){ TRACE_file tracefile; jlong filehandle; TRACE_Rec_Kind_t next_kind; int ierr; filehandle = (*env)->GetLongField( env, this, fid4filehandle ); if ( filehandle == 0 ) { fprintf( errfile, "Java_logformat_trace_InputLog_peekNextKindIndex(): " "Inaccessible filehandle in Java side\n" ); return TRACE_EOF; } tracefile = (TRACE_file) filehandle; ierr = TRACE_Peek_next_kind( tracefile, &next_kind ); if ( ierr != 0 ) { fprintf( errfile, "%s\n", TRACE_Get_err_string( ierr ) ); fflush( errfile ); return TRACE_EOF; } return next_kind;}JNIEXPORT jobject JNICALL Java_logformat_trace_InputLog_getNextCategory( JNIEnv *env, jobject this ){ TRACE_file tracefile; jlong filehandle; TRACE_Category_head_t type_head; int legend_sz, legend_pos, legend_max; char *legend_base; jstring jlegend; int label_sz, label_pos, label_max; char *label_base; jstring jlabel; int methods_sz, methods_pos, methods_max; int *methods_base; jintArray jmethods; jclass cid_local; jobject objdef; int ierr; char slegend_base[ACHAR_LENGTH]; char slabel_base[ACHAR_LENGTH]; int smethods_base[AINT_LENGTH]; filehandle = (*env)->GetLongField( env, this, fid4filehandle ); if ( filehandle == 0 ) { fprintf( errfile, "Java_logformat_trace_InputLog_getNextCategory(): " "Inaccessible filehandle in Java side\n" ); return NULL; } tracefile = (TRACE_file) filehandle; label_sz = 0; legend_sz = 0; methods_sz = 0; ierr = TRACE_Peek_next_category( tracefile, &legend_sz, &label_sz, &methods_sz ); if ( ierr != 0 || legend_sz <= 0 ) { fprintf( errfile, "%s\n", TRACE_Get_err_string( ierr ) ); fflush( errfile ); return NULL; } legend_pos = 0; if ( legend_sz ) { legend_max = legend_sz+1; if (legend_max > ACHAR_LENGTH) legend_base = (char *) malloc( legend_max * sizeof( char ) ); else legend_base = slegend_base; } else legend_base = NULL; label_pos = 0; if ( label_sz > 0 ) { label_max = label_sz+1; if (label_max > ACHAR_LENGTH) label_base = (char *) malloc( label_max * sizeof( char ) ); else label_base = slabel_base; } else label_base = NULL; methods_pos = 0; if ( methods_sz > 0 ) { methods_max = methods_sz; if (methods_max > AINT_LENGTH) methods_base = (int *) malloc( methods_max * sizeof( int ) ); else methods_base = smethods_base; } else methods_base = NULL; ierr = TRACE_Get_next_category( tracefile, &type_head, &legend_sz, legend_base, &legend_pos, legend_max, &label_sz, label_base, &label_pos, label_max, &methods_sz, methods_base, &methods_pos, methods_max ); if ( ierr != 0 || legend_pos <= 0 ) { fprintf( errfile, "%s\n", TRACE_Get_err_string( ierr ) ); fflush( errfile ); if ( legend_base != NULL && legend_base != slegend_base ) free( legend_base ); if ( label_base != NULL && label_base != slabel_base ) free( label_base ); if ( methods_base != NULL && methods_base != smethods_base ) free( methods_base ); return NULL; } /* Obtain various array references for calling DobjDef's constructor */ if ( legend_base != NULL && legend_pos > 0 ) { legend_base[ legend_pos ] = '\0'; jlegend = (*env)->NewStringUTF( env, legend_base ); } else jlegend = NULL; if ( label_base != NULL && label_pos > 0 ) { label_base[ label_pos ] = '\0'; jlabel = (*env)->NewStringUTF( env, label_base ); } else jlabel = NULL; if ( methods_base != NULL && methods_pos > 0 ) { jmethods = (*env)->NewIntArray( env, methods_sz ); (*env)->SetIntArrayRegion( env, jmethods, 0, methods_sz, (jint *) methods_base ); } else jmethods = NULL; /* Cache DobjDef Class's constructor as Global Reference */ if ( cid4DobjDef == NULL ) { cid_local = (*env)->FindClass( env, "logformat/trace/DobjDef" ); if ( cid_local != NULL ) { cid4DobjDef = (*env)->NewGlobalRef( env, cid_local ); (*env)->DeleteLocalRef( env, cid_local ); /* avoid memory leak */ mid4NewDobjDef = (*env)->GetMethodID( env, cid4DobjDef, "<init>", "(ILjava/lang/String;IIIIIILjava/lang/String;[I)V" ); } } /* Call DobjDef's constructor */ objdef = (*env)->NewObject( env, cid4DobjDef, mid4NewDobjDef, type_head.index, jlegend, type_head.shape, type_head.red, type_head.green, type_head.blue, type_head.alpha, type_head.width, jlabel, jmethods ); /* Clean up the unused reference and free local memory */ if ( jlegend != NULL ) (*env)->DeleteLocalRef( env, jlegend ); if ( legend_base != NULL && legend_base != slegend_base ) free( legend_base ); if ( jlabel != NULL ) (*env)->DeleteLocalRef( env, jlabel ); if ( label_base != NULL && label_base != slabel_base ) free( label_base ); if ( jmethods != NULL ) (*env)->DeleteLocalRef( env, jmethods ); if ( methods_base != NULL && methods_base != smethods_base ) free( methods_base ); return objdef;}JNIEXPORT jobject JNICALLJava_logformat_trace_InputLog_getNextYCoordMap( JNIEnv *env, jobject this ){ TRACE_file tracefile; jlong filehandle; int nrows, ncolumns; int max_column_name, max_title_name; char *title_name; char **column_names; int coordmap_sz, coordmap_pos, coordmap_max; int *coordmap_base; jintArray j_coordmap_elems; int methods_sz, methods_pos, methods_max; int *methods_base; jintArray jmethods; jclass cid_local; jstring jtitle; jstring jcolname; jobjectArray jcolnames; jobject ycoordmap; int icol; int ierr; filehandle = (*env)->GetLongField( env, this, fid4filehandle ); if ( filehandle == 0 ) { fprintf( errfile, "Java_logformat_trace_InputLog_getNextYCoordMap(): " "Inaccessible filehandle in Java side\n" ); return NULL; } tracefile = (TRACE_file) filehandle; nrows = 0; ncolumns = 0; max_column_name = 0; max_title_name = 0; methods_sz = 0; ierr = TRACE_Peek_next_ycoordmap( tracefile, &nrows, &ncolumns, &max_column_name, &max_title_name, &methods_sz );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -