Beispiel #1
0
CRB_Value
crb_nv_fgets_proc(CRB_Interpreter *interpreter,
                  CRB_LocalEnvironment *env,
                  int arg_count, CRB_Value *args)
{
    CRB_Value value;
    FILE *fp;
    char buf[LINE_BUF_SIZE];
    char *mb_buf = NULL;
    int ret_len = 0;
    CRB_Char *wc_str;

    check_argument_count(arg_count, 1);

    if (args[0].type != CRB_NATIVE_POINTER_VALUE
        || !check_native_pointer(&args[0])) {
        crb_runtime_error(0, FGETS_ARGUMENT_TYPE_ERR,
                          MESSAGE_ARGUMENT_END);
    }
    fp = args[0].u.native_pointer.pointer;

    while (fgets(buf, LINE_BUF_SIZE, fp)) {
        int new_len;
        new_len = ret_len + strlen(buf);
        mb_buf = MEM_realloc(mb_buf, new_len + 1);
        if (ret_len == 0) {
            strcpy(mb_buf, buf);
        } else {
            strcat(mb_buf, buf);
        }
        ret_len = new_len;
        if (mb_buf[ret_len-1] == '\n')
            break;
    }
    if (ret_len > 0) {
        wc_str = CRB_mbstowcs_alloc(interpreter, env, __LINE__, mb_buf);
        if (wc_str == NULL) {
            MEM_free(mb_buf);
            crb_runtime_error(0,
                              BAD_MULTIBYTE_CHARACTER_ERR,
                              MESSAGE_ARGUMENT_END);
        }
        value.type = CRB_STRING_VALUE;
        value.u.object = CRB_create_crowbar_string(interpreter, env, wc_str);
    } else {
        value.type = CRB_NULL_VALUE;
    }
    MEM_free(mb_buf);

    return value;
}
Beispiel #2
0
ORG_Value org_nv_fgets_proc(ORG_Interpreter *interpreter, int arg_count, ORG_Value *args) {
    ORG_Value value;
    FILE *fp;
    char buf[LINE_BUF_SIZE];
    char *ret_buf = NULL;
    int ret_len = 0;

    if (arg_count < 1) {
        //org_runtime_error(0, ARGUMENT_TOO_FEW_ERR, MESSAGE_ARGUMENT_END);
        exit(1);
    } else if (arg_count > 1) {
        //org_runtime_error(0, ARGUMENT_TOO_MANY_ERR, MESSAGE_ARGUMENT_END);
        exit(1);
    }
    if (args[0].type != ORG_NATIVE_POINTER_VALUE || !check_native_pointer(&args[0])) {
        //org_runtime_error(0, FGETS_ARGUMENT_TYPE_ERR, MESSAGE_ARGUMENT_END);
        exit(1);
    }
    fp = args[0].u.native_pointer.pointer;

    while (fgets(buf, LINE_BUF_SIZE, fp)) {
        int new_len;
        new_len = ret_len + strlen(buf);
        ret_buf = MEM_realloc(ret_buf, new_len + 1);
        if (ret_len == 0) {
            strcpy(ret_buf, buf);
        } else {
            strcat(ret_buf, buf);
        }
        ret_len = new_len;
        if (ret_buf[ret_len-1] == '\n')
            break;
    }
    if (ret_len > 0) {
        value.type = ORG_STRING_VALUE;
        value.u.string_value = org_create_origin_string(interpreter, ret_buf);
    } else {
        value.type = ORG_NULL_VALUE;
    }

    return value;
}
Beispiel #3
0
ORG_Value org_nv_fclose_proc(ORG_Interpreter *interpreter, int arg_count, ORG_Value *args) {
    ORG_Value value;
    FILE *fp;

    value.type = ORG_NULL_VALUE;
    if (arg_count < 1) {
        //org_runtime_error(0, ARGUMENT_TOO_FEW_ERR,MESSAGE_ARGUMENT_END);
        exit(1);
    } else if (arg_count > 1) {
        //org_runtime_error(0, ARGUMENT_TOO_MANY_ERR, MESSAGE_ARGUMENT_END);
        exit(1);
    }
    if (args[0].type != ORG_NATIVE_POINTER_VALUE || !check_native_pointer(&args[0])) {
        //org_runtime_error(0, FCLOSE_ARGUMENT_TYPE_ERR,MESSAGE_ARGUMENT_END);
        exit(1);
    }
    fp = args[0].u.native_pointer.pointer;
    fclose(fp);

    return value;
}
Beispiel #4
0
CRB_Value
crb_nv_fclose_proc(CRB_Interpreter *interpreter,
                   CRB_LocalEnvironment *env,
                   int arg_count, CRB_Value *args)
{
    CRB_Value value;
    FILE *fp;

    check_argument_count(arg_count, 1);

    value.type = CRB_NULL_VALUE;
    if (args[0].type != CRB_NATIVE_POINTER_VALUE
        || !check_native_pointer(&args[0])) {
        crb_runtime_error(0, FCLOSE_ARGUMENT_TYPE_ERR,
                          MESSAGE_ARGUMENT_END);
    }
    fp = args[0].u.native_pointer.pointer;
    fclose(fp);

    return value;
}
Beispiel #5
0
CRB_Value
crb_nv_fputs_proc(CRB_Interpreter *interpreter,
                  CRB_LocalEnvironment *env,
                  int arg_count, CRB_Value *args)
{
    CRB_Value value;
    FILE *fp;

    check_argument_count(arg_count, 2);
    value.type = CRB_NULL_VALUE;
    if (args[0].type != CRB_STRING_VALUE
        || (args[1].type != CRB_NATIVE_POINTER_VALUE
            || !check_native_pointer(&args[1]))) {
        crb_runtime_error(0, FPUTS_ARGUMENT_TYPE_ERR,
                          MESSAGE_ARGUMENT_END);
    }
    fp = args[1].u.native_pointer.pointer;

    CRB_print_wcs(fp, args[0].u.object->u.string.string);

    return value;
}
Beispiel #6
0
ORG_Value org_nv_fputs_proc(ORG_Interpreter *interpreter, int arg_count, ORG_Value *args) {
    ORG_Value value;
    FILE *fp;

    value.type = ORG_NULL_VALUE;
    if (arg_count < 2) {
        //org_runtime_error(0, ARGUMENT_TOO_FEW_ERR, MESSAGE_ARGUMENT_END);
        exit(1);
    } else if (arg_count > 2) {
        //org_runtime_error(0, ARGUMENT_TOO_MANY_ERR, MESSAGE_ARGUMENT_END);
        exit(1);
    }
    if (args[0].type != ORG_STRING_VALUE || (args[1].type != ORG_NATIVE_POINTER_VALUE || !check_native_pointer(&args[1]))) {
        //org_runtime_error(0, FPUTS_ARGUMENT_TYPE_ERR, MESSAGE_ARGUMENT_END);
        exit(1);
    }
    fp = args[1].u.native_pointer.pointer;

    fputs(args[0].u.string_value->string, fp);

    return value;
}