コード例 #1
0
ファイル: jsamaroo.c プロジェクト: jns0987654321/CIS_2500
int main(int argc, char *argv[]) {
	int *array;
	int width, height;
	int value;
	char **stringPtr;
	int **matrix;
	
	/* testing for part 1 */
	printf("Testing Part 1\n");
	width = 5;
	height = 6;
	
	array = create2DArray(height, width);
	
	printf("Store value 7 at [3,4].\n");
	set2DElement(array, 3, 4, 7);
	
	value = get2DElement(array, 3, 4);
	printf("Retrieve value %d from [3,4]\n\n", value);
	
	free2DArray(array);
	
	
	/* testing for part 2 */
	printf("Testing Part 2\n");
	stringPtr = createStringArray(100);
	
	printf("Store string - fred\n");
	setStringArray(stringPtr, 44, "fred");
	printf("Store string - barney\n");
	setStringArray(stringPtr, 80, "barney");
	
	printf("Get string - %s\n", getStringArray(stringPtr, 44));
	printf("Get string - %s\n", getStringArray(stringPtr, 80));
	/* test with NULL string */
	printf("Get string - %s\n\n", getStringArray(stringPtr, 3));
	
	freeStringArray(stringPtr, 100);
	
	
	/* testing for part 3 */
	printf("Testing Part 3\n");
	matrix = createArray(100, 100);
	
	printf("Store 33 44 55\n");
	matrix[22][76] = 33;
	matrix[83][29] = 44;
	matrix[99][65] = 55;
	
	printf("Retrieve %d %d %d\n", matrix[22][76], matrix[83][29],
		   matrix[99][65]);
	
	freeArray(matrix, 100);
	
	return(1);
}
コード例 #2
0
ファイル: cmd_parse.c プロジェクト: kyuh/ChingOS
TentativeCmdInfo parseSingleCmd(char *cmd_blob){
    StringArray sa_prelim_argv = sepStringWithQuotes(cmd_blob, ' ', true);

    TentativeCmdInfo tci;
    tci.inputFilename = NULL;
    tci.outputFilename = NULL;

    StringArray sa_argv = createStringArray(10);

    int i = 0;
    while (i < sa_prelim_argv.size){
        char *cur_str = stringArrayGet(&sa_prelim_argv, i);

        if (cur_str[0] == '<'){

            // If this is the only character in the token,
            // then the next string is the item to be redirected
            if (cur_str[1] == '\0'){
                if (i == sa_prelim_argv.size - 1){
                    fprintf(stderr, "Reached end of line while parsing\n");
                    exit(-1);
                }

                tci.inputFilename = strdup(stringArrayGet(&sa_prelim_argv, i+1));
                i += 2;
            } else {
                tci.inputFilename = strdup(cur_str + 1);    //lop off the <
                i += 1;
            }

        } else if (cur_str[0] == '>'){
            if (cur_str[1] == '\0'){
                if (i == sa_prelim_argv.size - 1){
                    fprintf(stderr, "Reached end of line while parsing\n");
                    exit(-1);
                }

                tci.outputFilename = strdup(stringArrayGet(&sa_prelim_argv, i+1));
                i += 2;
            } else {
                tci.outputFilename = strdup(cur_str + 1);    //lop off the <
                i += 1;
            }

        } else {
            stringArrayInsert(&sa_argv, cur_str);
            i += 1;
        }
    }

    tci.argv = stringArrayToNormalPlusNull(&sa_argv);
    tci.argc = sa_argv.size;

    return tci;
}
コード例 #3
0
ファイル: util.c プロジェクト: ClarkGuan/JSqlite
jobject newTableResult(JNIEnv* env, char** result, int row, int col)
{
  jobjectArray jresult = createStringArray(env, result, row * col);
  jclass cls = (*env)->FindClass(env, "com/baidu/javalite/TableResult");
  jobject rs = (*env)->NewObject(env, cls, getTableResultInit(env), jresult,
                                 row, col);

  (*env)->DeleteLocalRef(env, jresult);
  (*env)->DeleteLocalRef(env, cls);
  return rs;
}
コード例 #4
0
ファイル: InitObjects.c プロジェクト: Macisia/scilab
int InitAxesModel()
{
    int cubeScaling = 0;
    int logFlag = 0;
    int ticksColor = 0;
    int axisLocation = 0;
    int boxType = 0;
    int filled = 0;
    int gridColor = 0;
    int gridPosition = 0;
    int view = 0;
    int axisReverse = 0;
    int axisVisible = 0;
    int defaultNumberTicks = 0;
    int autoTicks = 0;
    int autoSubticks = 0;
    int nbSubticks = 0;
    int hiddenAxisColor = 0;
    int hiddenColor = 0;
    int isoview = 0;
    int visible = 0;
    int *piVisible = &visible;
    int clipState = 0;
    int tightLimits = 0;
    int arcDrawingMethod = 0;
    int labelsFontStyle = 6;
    int labelsFontColor = -1;
    int i = 0;
    double labelsFontSize = 1;
    double axesBounds[4];
    double dataBounds[6];
    double rotationAngles[2];
    double tab[] = { 0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. }; /* graduations init. tmptab */
    /* z-axis initial ticks locations */
    double tabZTicksLocations[] = { -1.0, -0.8, -0.6, -0.4, -0.2, 0.0, 0.2, 0.4, 0.6, 0.8, 1.0 };
    char labelBuffer[LABEL_BUFFER_LENGTH];
    char **stringVector = NULL;

    int lineColor = -1;
    int background = -2;
    int foreground = -1;
    double lineWidth = 1.0;

    /* 0: solid */
    int lineStyle = 0;

    int markMode = 0;
    int lineMode = 1;
    int fillMode = 1;
    int markStyle = 0;
    int markSize = 0;

    /* 0: point, 1: tabulated */
    int markSizeUnit = 1;

    int iLabelUID = 0;

    int iFiguremdlUID = getFigureModel();
    int iAxesmdlUID = getAxesModel();

    sciInitGraphicMode(iAxesmdlUID);

    setGraphicObjectProperty(iAxesmdlUID, __GO_BACKGROUND__, &background, jni_int, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_LINE_COLOR__, &foreground, jni_int, 1);

    setGraphicObjectProperty(iAxesmdlUID, __GO_FONT_SIZE__, &labelsFontSize, jni_double, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_FONT_COLOR__, &labelsFontColor, jni_int, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_FONT_STYLE__, &labelsFontStyle, jni_int, 1);

    setGraphicObjectProperty(iAxesmdlUID, __GO_LINE_THICKNESS__, &lineWidth, jni_double, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_LINE_STYLE__, &lineStyle, jni_int, 1);

    setGraphicObjectProperty(iAxesmdlUID, __GO_MARK_MODE__, &markMode, jni_bool, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_LINE_MODE__, &lineMode, jni_bool, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_FILL_MODE__, &fillMode, jni_bool, 1);

    setGraphicObjectProperty(iAxesmdlUID, __GO_MARK_STYLE__, &markStyle, jni_int, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_MARK_SIZE__, &markSize, jni_int, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_MARK_SIZE_UNIT__, &markSizeUnit, jni_int, 1);

    setGraphicObjectProperty(iAxesmdlUID, __GO_MARK_BACKGROUND__, &background, jni_int, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_MARK_FOREGROUND__, &foreground, jni_int, 1);

    cubeScaling = 0;
    setGraphicObjectProperty(iAxesmdlUID, __GO_CUBE_SCALING__, &cubeScaling, jni_bool, 1);

    /* Log flags set to linear for the 3 axes */
    logFlag = 0;

    setGraphicObjectProperty(iAxesmdlUID, __GO_X_AXIS_LOG_FLAG__, &logFlag, jni_bool, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Y_AXIS_LOG_FLAG__, &logFlag, jni_bool, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Z_AXIS_LOG_FLAG__, &logFlag, jni_bool, 1);

    ticksColor = -1;
    setGraphicObjectProperty(iAxesmdlUID, __GO_TICKS_COLOR__, &ticksColor, jni_int, 1);

    nbSubticks = 1;
    setGraphicObjectProperty(iAxesmdlUID, __GO_X_AXIS_SUBTICKS__, &nbSubticks, jni_int, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Y_AXIS_SUBTICKS__, &nbSubticks, jni_int, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Z_AXIS_SUBTICKS__, &nbSubticks, jni_int, 1);

    /* 0 corresponds to bottom position */
    axisLocation = 0;
    setGraphicObjectProperty(iAxesmdlUID, __GO_X_AXIS_LOCATION__, &axisLocation, jni_int, 1);

    /* 4 corresponds to left position */
    axisLocation = 4;
    setGraphicObjectProperty(iAxesmdlUID, __GO_Y_AXIS_LOCATION__, &axisLocation, jni_int, 1);

    /* 0 corresponds to OFF */
    boxType = 0;
    setGraphicObjectProperty(iAxesmdlUID, __GO_BOX_TYPE__, &boxType, jni_int, 1);

    filled = 1;
    setGraphicObjectProperty(iAxesmdlUID, __GO_FILLED__, &filled, jni_bool, 1);

    gridColor = -1;
    setGraphicObjectProperty(iAxesmdlUID, __GO_X_AXIS_GRID_COLOR__, &gridColor, jni_int, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Y_AXIS_GRID_COLOR__, &gridColor, jni_int, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Z_AXIS_GRID_COLOR__, &gridColor, jni_int, 1);

    /* 0: background */
    setGraphicObjectProperty(iAxesmdlUID, __GO_LINE_COLOR__, &lineColor, jni_int, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_BACKGROUND__, &background, jni_int, 1);

    gridPosition = 0;

    setGraphicObjectProperty(iAxesmdlUID, __GO_GRID_POSITION__, &gridPosition, jni_int, 1);

    rotationAngles[0] = 0.0;
    rotationAngles[1] = 270.0;

    setGraphicObjectProperty(iAxesmdlUID, __GO_ROTATION_ANGLES__, rotationAngles, jni_double_vector, 2);

    /* 0: 2D view */
    view = 0;
    setGraphicObjectProperty(iAxesmdlUID, __GO_VIEW__, &view, jni_int, 1);

    /* Must be set after VIEW, since setting VIEW to 2D overwrites the 3D rotation angles */
    rotationAngles[0] = 45.0;
    rotationAngles[1] = 215.0;

    setGraphicObjectProperty(iAxesmdlUID, __GO_ROTATION_ANGLES_3D__, rotationAngles, jni_double_vector, 2);

    axisVisible = 0;
    setGraphicObjectProperty(iAxesmdlUID, __GO_X_AXIS_VISIBLE__, &axisVisible, jni_bool, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Y_AXIS_VISIBLE__, &axisVisible, jni_bool, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Z_AXIS_VISIBLE__, &axisVisible, jni_bool, 1);

    axisReverse = 0;
    setGraphicObjectProperty(iAxesmdlUID, __GO_X_AXIS_REVERSE__, &axisReverse, jni_bool, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Y_AXIS_REVERSE__, &axisReverse, jni_bool, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Z_AXIS_REVERSE__, &axisReverse, jni_bool, 1);

    /* Corresponds to the MVC AUTO_SUBTICKS property (!flagNax is equivalent to AUTO_SUBTICKS) */
#if 0
    ppaxesmdl->flagNax = FALSE;
#endif

    autoSubticks = 1;
    setGraphicObjectProperty(iAxesmdlUID, __GO_AUTO_SUBTICKS__, &autoSubticks, jni_bool, 1);

    /* To be corrected when the equivalent of flagnax is implemented within the MVC */
    nbSubticks = 1;
    setGraphicObjectProperty(iAxesmdlUID, __GO_X_AXIS_SUBTICKS__, &nbSubticks, jni_int, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Y_AXIS_SUBTICKS__, &nbSubticks, jni_int, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Z_AXIS_SUBTICKS__, &nbSubticks, jni_int, 1);

    /*
     * The code creating default ticks labels and positions should be put
     * into the Model. Ticks positions and locations should be computed using the default data bounds
     * instead of using pre-defined values.
     * Note that the pre-MVC ticks labels creation code is implemented in the C++ Renderer module
     * and should be moved to the Java Model's relevant parts (TicksProperty).
     */

    /*
     * Automatic ticks computation must be activated for the 3 axes before setting ticks values to
     * ensure that the ticks values set are the automatic ticks' ones.
     */
    autoTicks = 1;
    setGraphicObjectProperty(iAxesmdlUID, __GO_X_AXIS_AUTO_TICKS__, &autoTicks, jni_bool, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Y_AXIS_AUTO_TICKS__, &autoTicks, jni_bool, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Z_AXIS_AUTO_TICKS__, &autoTicks, jni_bool, 1);

    defaultNumberTicks = 11;

    setGraphicObjectProperty(iAxesmdlUID, __GO_X_AXIS_TICKS_LOCATIONS__, tab, jni_double_vector, defaultNumberTicks);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Y_AXIS_TICKS_LOCATIONS__, tab, jni_double_vector, defaultNumberTicks);

    stringVector = createStringArray(defaultNumberTicks);

    if (stringVector == NULL)
    {
        return -1;
    }

    /*
     * A proper format should be used (ChoixFormatE function)
     */
    for (i = 0; i < defaultNumberTicks; i++)
    {
        sprintf(labelBuffer, "%.1f", tab[i]);
        stringVector[i] = os_strdup(labelBuffer);

        if (stringVector[i] == NULL)
        {
            return -1;
        }
    }

    setGraphicObjectProperty(iAxesmdlUID, __GO_X_AXIS_TICKS_LABELS__, stringVector, jni_string_vector, defaultNumberTicks);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Y_AXIS_TICKS_LABELS__, stringVector, jni_string_vector, defaultNumberTicks);

    /*
     * The same number of ticks is used now for the x,y and z axes.
     * Previously, the z-axis contained only 3 ticks (-1, 0, 1). However, the renderer module was
     * overriding this default number (3) by creating an 11-tick z-axis when required (3D view).
     * Ticks locations and labels are however different for the z-axis (from -1 to +1 instead of 0 to 1).
     */
    setGraphicObjectProperty(iAxesmdlUID, __GO_Z_AXIS_TICKS_LOCATIONS__, tabZTicksLocations, jni_double_vector, defaultNumberTicks);

    /* ChoixFormatE should be used */
    for (i = 0; i < defaultNumberTicks; i++)
    {
        FREE(stringVector[i]);

        sprintf(labelBuffer, "%.1f", tabZTicksLocations[i]);
        stringVector[i] = os_strdup(labelBuffer);

        if (stringVector[i] == NULL)
        {
            return -1;
        }
    }

    setGraphicObjectProperty(iAxesmdlUID, __GO_Z_AXIS_TICKS_LABELS__, stringVector, jni_string_vector, defaultNumberTicks);

    destroyStringArray(stringVector, defaultNumberTicks);

    /*
     * Indicates the direction of projection (0 for the axis corresponding to the direction,
     * 1 for the others).
     * To be implemented within the MVC if determined to be useful.
     */
#if 0
    ppaxesmdl->project[0] = 1;
    ppaxesmdl->project[1] = 1;
    ppaxesmdl->project[2] = 0;
#endif

    hiddenAxisColor = 4;
    setGraphicObjectProperty(iAxesmdlUID, __GO_HIDDEN_AXIS_COLOR__, &hiddenAxisColor, jni_int, 1);

    hiddenColor = 4;
    setGraphicObjectProperty(iAxesmdlUID, __GO_HIDDEN_COLOR__, &hiddenColor, jni_int, 1);

    isoview = 0;
    setGraphicObjectProperty(iAxesmdlUID, __GO_ISOVIEW__, &isoview, jni_bool, 1);

    /* Axes bounds set to fill the whole drawing area */
    axesBounds[0] = 0.0;
    axesBounds[1] = 0.0;
    axesBounds[2] = 1.0;
    axesBounds[3] = 1.0;
    setGraphicObjectProperty(iAxesmdlUID, __GO_AXES_BOUNDS__, axesBounds, jni_double_vector, 4);

    /* xmin, xmax */
    dataBounds[0] = 0.0;
    dataBounds[1] = 1.0;
    /* ymin, ymax */
    dataBounds[2] = 0.0;
    dataBounds[3] = 1.0;
    /* zmin, zmax */
    dataBounds[4] = -1.0;
    dataBounds[5] = 1.0;

    setGraphicObjectProperty(iAxesmdlUID, __GO_DATA_BOUNDS__, dataBounds, jni_double_vector, 6);

    /* visible */
    getGraphicObjectProperty(iFiguremdlUID, __GO_VISIBLE__, jni_bool, (void**)&piVisible);
    setGraphicObjectProperty(iAxesmdlUID, __GO_VISIBLE__, &visible, jni_bool, 1);

    /* 0: clipping off */
    clipState = 0;
    setGraphicObjectProperty(iAxesmdlUID, __GO_CLIP_STATE__, &clipState, jni_int, 1);

    /* "real data bounds" and "data bounds" are initially the same */
    setGraphicObjectProperty(iAxesmdlUID, __GO_REAL_DATA_BOUNDS__, dataBounds, jni_double_vector, 6);

    tightLimits = 0;
    setGraphicObjectProperty(iAxesmdlUID, __GO_X_TIGHT_LIMITS__, &tightLimits, jni_bool, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Y_TIGHT_LIMITS__, &tightLimits, jni_bool, 1);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Z_TIGHT_LIMITS__, &tightLimits, jni_bool, 1);

    /* Sets the default arc drawing method to lines (1), which is faster */
    arcDrawingMethod = 1;
    setGraphicObjectProperty(iAxesmdlUID, __GO_ARC_DRAWING_METHOD__, &arcDrawingMethod, jni_int, 1);

    /* Creates the Axes model's labels and sets the model as their parent */
    iLabelUID = initLabel(iAxesmdlUID);
    setGraphicObjectProperty(iAxesmdlUID, __GO_TITLE__, &iLabelUID, jni_int, 1);

    iLabelUID = initLabel(iAxesmdlUID);
    setGraphicObjectProperty(iAxesmdlUID, __GO_X_AXIS_LABEL__, &iLabelUID, jni_int, 1);

    iLabelUID = initLabel(iAxesmdlUID);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Y_AXIS_LABEL__, &iLabelUID, jni_int, 1);

    iLabelUID = initLabel(iAxesmdlUID);
    setGraphicObjectProperty(iAxesmdlUID, __GO_Z_AXIS_LABEL__, &iLabelUID, jni_int, 1);

    return 0;
}
コード例 #5
0
ファイル: Main.cpp プロジェクト: DougCS/symdroid
int main(int argc, char* const argv[])
{
    JavaVM* vm = NULL;
    JNIEnv* env = NULL;
    JavaVMInitArgs initArgs;
    JavaVMOption* options = NULL;
    char* slashClass = NULL;
    int optionCount, curOpt, i, argIdx;
    int needExtra = JNI_FALSE;
    int result = 1;

    setvbuf(stdout, NULL, _IONBF, 0);

    /* ignore argv[0] */
    argv++;
    argc--;

    optionCount = argc;

    options = (JavaVMOption*) malloc(sizeof(JavaVMOption) * optionCount);
    memset(options, 0, sizeof(JavaVMOption) * optionCount);

    for (curOpt = argIdx = 0; argIdx < argc; argIdx++) {
        if (argv[argIdx][0] != '-' && !needExtra)
            break;
        options[curOpt++].optionString = strdup(argv[argIdx]);

        needExtra = JNI_FALSE;
        if (strcmp(argv[argIdx], "-classpath") == 0 ||
            strcmp(argv[argIdx], "-cp") == 0)
            /* others? */
        {
            needExtra = JNI_TRUE;
        }
    }

    if (needExtra) {
        fprintf(stderr, "Dalvik VM requires value after last option flag\n");
        goto bail;
    }


    assert(curOpt <= optionCount);

    initArgs.version = JNI_VERSION_1_4;
    initArgs.options = options;
    initArgs.nOptions = curOpt;
    initArgs.ignoreUnrecognized = JNI_FALSE;

    //printf("nOptions = %d\n", initArgs.nOptions);

    blockSigpipe();

    if (JNI_CreateJavaVM(&vm, &env, &initArgs) < 0) {
        fprintf(stderr, "Dalvik VM init failed (check log file)\n");
        goto bail;
    }

    if (argIdx == argc) {
        fprintf(stderr, "Dalvik VM requires a class name\n");
        goto bail;
    }

    jobjectArray strArray;
    strArray = createStringArray(env, &argv[argIdx+1], argc-argIdx-1);
    if (strArray == NULL)
        goto bail;

    jclass startClass;
    jmethodID startMeth;
    char* cp;

    slashClass = strdup(argv[argIdx]);
    for (cp = slashClass; *cp != '\0'; cp++)
        if (*cp == '.')
            *cp = '/';

    startClass = env->FindClass(slashClass);
    if (startClass == NULL) {
        fprintf(stderr, "Dalvik VM unable to locate class '%s'\n", slashClass);
        goto bail;
    }

    startMeth = env->GetStaticMethodID(startClass,
                    "main", "([Ljava/lang/String;)V");
    if (startMeth == NULL) {
        fprintf(stderr, "Dalvik VM unable to find static main(String[]) in '%s'\n",
            slashClass);
        goto bail;
    }

    if (!methodIsPublic(env, startClass, startMeth))
        goto bail;

    env->CallStaticVoidMethod(startClass, startMeth, strArray);

    if (!env->ExceptionCheck())
        result = 0;

bail:
    /*printf("Shutting down Dalvik VM\n");*/
    if (vm != NULL) {
        if (vm->DetachCurrentThread() != JNI_OK) {
            fprintf(stderr, "Warning: unable to detach main thread\n");
            result = 1;
        }

        if (vm->DestroyJavaVM() != 0)
            fprintf(stderr, "Warning: Dalvik VM did not shut down cleanly\n");
        /*printf("\nDalvik VM has exited\n");*/
    }

    for (i = 0; i < optionCount; i++)
        free((char*) options[i].optionString);
    free(options);
    free(slashClass);
    /*printf("--- VM is down, process exiting\n");*/
    return result;
}
コード例 #6
0
/**
 OUTPUTHANDLER::Gets the list of goals
*/
jobjectArray OutputHandler::getGoals(JNIEnv *env)
{
	debugPrintInt(myGoalCount);
	jobjectArray result = createStringArray(env, myGoals, myGoalCount);
	return result;
}
コード例 #7
0
ファイル: cmd_parse.c プロジェクト: kyuh/ChingOS
StringArray sepStringWithQuotes(char *buf, char separator, bool exclude_quote_mark){
    StringArray stArr = createStringArray(10);

    bool quote_entered = false;
    char *last_token_pos = buf;
    char *cur_pos = buf;

    while (true){
        /* Note start of a quoted phrase,
        and get rid of the quotation mark if so chosen
        (i.e. when parsing out the space-separated tokens in practice).

        Handle the beginning quotation mark here,
        handle the end quote in the next if-statement clause.

        (Assume the quoted phrases are separated from the rest of the text
        by another space, e.g. 
            grep "array" >out.txt       is valid
            grep"array">out.txt         is not
        */

        if (*cur_pos == '\"'){
            quote_entered = !quote_entered;

            if (exclude_quote_mark && quote_entered){
                last_token_pos = cur_pos + 1;
            }
        } else if ((*cur_pos == separator && !quote_entered) || *cur_pos == '\n' || *cur_pos == '\0') {


            // Exclude the ending quotation mark of the previous
            // statement, if necessary
            int token_nChars;
            if (exclude_quote_mark && *(cur_pos - 1) == '\"'){
                token_nChars = (cur_pos - 1) - last_token_pos;
            } else {
                token_nChars = cur_pos - last_token_pos;
            }


            // Account for null character
            int token_alloc_size = token_nChars + 1;

            char *token_string = safeMalloc(token_alloc_size * sizeof(char));
            memcpy(token_string, last_token_pos, token_nChars * sizeof(char));
            token_string[token_nChars] = '\0';

            stringArrayInsert(&stArr, token_string);

            if (*cur_pos == '\n' || *cur_pos == '\0'){

                // Check for unclosed quotation
                if (quote_entered){
                    fprintf(stderr, "Unclosed quote in command\n");
                    exit(-1);
                }

                break;
            }

            // Mark the start of new command
            last_token_pos = cur_pos + 1;
        }
        cur_pos++;
    }

    // Filter out any empty tokens
    StringArray stArr_filtered = createStringArray(10);
    for (int i = 0; i < stArr.size; i++){
        char *s = stringArrayGet(&stArr, i);
        if (s[0]){
            stringArrayInsert(&stArr_filtered, s);
        }
    }

    //printf("STARR SIZE: %d\n", stArr.size);
    //printf("STARR FILTERED SIZE: %d\n", stArr_filtered.size);

    return stArr_filtered;
}
コード例 #8
0
ファイル: Main.cpp プロジェクト: zhaohuakai/dalvikvm
/*
 * Parse arguments.  Most of it just gets passed through to the VM.  The
 * JNI spec defines a handful of standard arguments.
 */
int main(int argc, char* const argv[])
{
	printf("ZHK test printf\n");
	JavaVM* vm = NULL;
    JNIEnv* env = NULL;
    JavaVMInitArgs initArgs;
    JavaVMOption* options = NULL;
    char* slashClass = NULL;
    int optionCount, curOpt, i, argIdx;
    int needExtra = JNI_FALSE;
    int result = 1;

    setvbuf(stdout, NULL, _IONBF, 0);

    /* ignore argv[0] */
    argv++;
    argc--;

    /*
     * If we're adding any additional stuff, e.g. function hook specifiers,
     * add them to the count here.
     *
     * We're over-allocating, because this includes the options to the VM
     * plus the options to the program.
     */
    optionCount = argc;

    options = (JavaVMOption*) malloc(sizeof(JavaVMOption) * optionCount);
    memset(options, 0, sizeof(JavaVMOption) * optionCount);

    /*
     * Copy options over.  Everything up to the name of the class starts
     * with a '-' (the function hook stuff is strictly internal).
     *
     * [Do we need to catch & handle "-jar" here?]
     */
    for (curOpt = argIdx = 0; argIdx < argc; argIdx++) {
        if (argv[argIdx][0] != '-' && !needExtra)
            break;
        options[curOpt++].optionString = strdup(argv[argIdx]);

        /* some options require an additional arg */
        needExtra = JNI_FALSE;
        if (strcmp(argv[argIdx], "-classpath") == 0 ||
            strcmp(argv[argIdx], "-cp") == 0)
            /* others? */
        {
            needExtra = JNI_TRUE;
        }
    }

    if (needExtra) {
        fprintf(stderr, "Dalvik VM requires value after last option flag\n");
        goto bail;
    }

    /* insert additional internal options here */

    assert(curOpt <= optionCount);

    initArgs.version = JNI_VERSION_1_4;
    initArgs.options = options;
    initArgs.nOptions = curOpt;
    initArgs.ignoreUnrecognized = JNI_FALSE;

    //printf("nOptions = %d\n", initArgs.nOptions);

    blockSigpipe();

    /*
     * Start VM.  The current thread becomes the main thread of the VM.
     */
	jint ret_JNI_CreateJavaVM;
	ret_JNI_CreateJavaVM = JNI_CreateJavaVM(&vm, &env, &initArgs);
	//printf("ZHK in Main.cpp:215 ret_JNI_CreateJavaVM = %d\n" ,ret_JNI_CreateJavaVM);
    if (ret_JNI_CreateJavaVM < 0) {
		printf("ZHK in Main.cpp:217 Dalvik VM init failed! (check log file)\n");
        fprintf(stderr, "Dalvik VM init failed! (check log file)\n");
        goto bail;
    }

    /*
     * Make sure they provided a class name.  We do this after VM init
     * so that things like "-Xrunjdwp:help" have the opportunity to emit
     * a usage statement.
     */
    if (argIdx == argc) {
		printf("ZHK in Main.cpp:228 Dalvik VM requires a class name\n");
        fprintf(stderr, "Dalvik VM requires a class name\n");
        goto bail;
    }

    /*
     * We want to call main() with a String array with our arguments in it.
     * Create an array and populate it.  Note argv[0] is not included.
     */
    jobjectArray strArray;
    strArray = createStringArray(env, &argv[argIdx+1], argc-argIdx-1);
    if (strArray == NULL)
        goto bail;

    /*
     * Find [class].main(String[]).
     */
    jclass startClass;
    jmethodID startMeth;
    char* cp;

    /* convert "com.android.Blah" to "com/android/Blah" */
    slashClass = strdup(argv[argIdx]);
    for (cp = slashClass; *cp != '\0'; cp++)
        if (*cp == '.')
            *cp = '/';

    startClass = env->FindClass(slashClass);
    if (startClass == NULL) {
		printf("ZHK in Main.cpp:257 Dalvik VM unable to locate class '%s'\n", slashClass);
        fprintf(stderr, "Dalvik VM unable to locate class '%s'\n", slashClass);
        goto bail;
    }

    startMeth = env->GetStaticMethodID(startClass,
                    "main", "([Ljava/lang/String;)V");
    if (startMeth == NULL) {
		printf("ZHK in Main.cpp:265 Dalvik VM unable to find static main(String[]) in '%s'\n", slashClass);
        fprintf(stderr, "Dalvik VM unable to find static main(String[]) in '%s'\n",
            slashClass);
        goto bail;
    }

    /*
     * Make sure the method is public.  JNI doesn't prevent us from calling
     * a private method, so we have to check it explicitly.
     */
    if (!methodIsPublic(env, startClass, startMeth))
        goto bail;

    /*
     * Invoke main().
     */
    env->CallStaticVoidMethod(startClass, startMeth, strArray);

    if (!env->ExceptionCheck())
        result = 0;

bail:
    /*printf("Shutting down Dalvik VM\n");*/
    if (vm != NULL) {
        /*
         * This allows join() and isAlive() on the main thread to work
         * correctly, and also provides uncaught exception handling.
         */
        if (vm->DetachCurrentThread() != JNI_OK) {
            fprintf(stderr, "Warning: unable to detach main thread\n");
            result = 1;
        }

        if (vm->DestroyJavaVM() != 0)
            fprintf(stderr, "Warning: Dalvik VM did not shut down cleanly\n");
        /*printf("\nDalvik VM has exited\n");*/
    }

    for (i = 0; i < optionCount; i++)
        free((char*) options[i].optionString);
    free(options);
    free(slashClass);
    /*printf("--- VM is down, process exiting\n");*/
    return result;
}