The R Project SVN R-packages

Rev

Rev 3473 | Rev 3700 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3473 Rev 3684
Line 4... Line 4...
4
 
4
 
5
/****************************************************************************
5
/****************************************************************************
6
 * UTILITY FUNCTIONS
6
 * UTILITY FUNCTIONS
7
 ****************************************************************************/
7
 ****************************************************************************/
8
 
8
 
9
/* if user supplied a name, return that name. otherwise, use the 
9
/* if user supplied a name (1st arg), return that name. otherwise, use the 
10
 * default "data" */
10
 * default "data". rname is R name, iname is internal name */
11
static int _check_sdf_name(SEXP name, char **rname, char **iname, int *file_idx) {
11
static int _check_sdf_name(SEXP name, char **rname, char **iname, int *file_idx) {
12
    int namelen = 0;
12
    int namelen = 0;
13
 
13
 
14
    /* check if arg name is supplied */
14
    /* check if arg name is supplied */
15
    if (name == R_NilValue) {
15
    if (name == R_NilValue) {
16
        *rname = "data";
16
        *rname = "data";
17
        namelen = 5;
17
        namelen = 5;
18
        *iname = (char*)R_alloc(13, sizeof(char)); /* data10000.db\0 */
18
        *iname = (char*)R_alloc(13, sizeof(char)); /* .SQLiteDF/data10000.db\0 */
19
        *file_idx = 1;
19
        *file_idx = 1;
20
        sprintf(*iname, "%s%d.db", *rname, *file_idx);
20
        sprintf(*iname, "%s%d.db", *rname, *file_idx);
21
    } else if (IS_CHARACTER(name)) {
21
    } else if (IS_CHARACTER(name)) {
22
        *rname = CHAR_ELT(name,0);
22
        *rname = CHAR_ELT(name,0);
23
        if (!_is_r_sym(*rname)) { 
23
        if (!_is_r_sym(*rname)) { 
24
            Rprintf("Error: supplied name \"%s\"is not a valid R symbol.\n", 
24
            error("supplied name \"%s\"is not a valid R symbol.", *rname); 
25
                    *rname); 
-
 
26
            return FALSE; 
-
 
27
        }
25
        }
28
        namelen = strlen(*rname);
26
        namelen = strlen(*rname);
29
        *iname = (char*)R_alloc(namelen + 9, sizeof(char)); /* <name>10000.db\0 */
27
        *iname = (char*)R_alloc(namelen + 9, sizeof(char)); /* .SQLiteDF/<name>10000.db\0 */
30
        sprintf(*iname, "%s.db", *rname);
28
        sprintf(*iname, "%s.db", *rname);
31
    } else {
29
    } else {
32
        Rprintf("Error: the supplied value for arg name is not a string.\n");
30
        Rprintf("Error: the supplied value for arg name is not a string.\n");
33
    }
31
    }
34
    return namelen;
32
    return namelen;
35
}
33
}
36
 
34
 
37
static int _find_free_filename2(char *rname, char **iname, int *namelen, int *file_idx) {
35
static int _find_free_filename2(char *rname, char *dirname, char **iname, int *namelen, int *file_idx) {
38
    sqlite3_stmt *stmt;
36
    sqlite3_stmt *stmt;
-
 
37
    char *tmp_iname;
39
    sqlite3_prepare(g_workspace, "select 1 from workspace where internal_name=?", -1,
38
    sqlite3_prepare(g_workspace, "select 1 from workspace where internal_name=?", -1,
40
            &stmt, NULL);
39
            &stmt, NULL);
41
    do {
40
    do {
-
 
41
        if (dirname != NULL) {
-
 
42
            sprintf(g_sql_buf[2], "%s/%s", dirname, *iname);
-
 
43
            tmp_iname = g_sql_buf[2];
-
 
44
        } else {
-
 
45
            tmp_iname = *iname;
-
 
46
        }
-
 
47
 
42
        if (!_file_exists(*iname)) {
48
        if (!_file_exists(tmp_iname)) {
43
            sqlite3_reset(stmt);
49
            sqlite3_reset(stmt);
44
            sqlite3_bind_text(stmt, 1, *iname, -1, SQLITE_STATIC);
50
            sqlite3_bind_text(stmt, 1, tmp_iname, -1, SQLITE_STATIC);
45
            if (sqlite3_step(stmt) != SQLITE_ROW) break;
51
            if (sqlite3_step(stmt) != SQLITE_ROW) break;
46
        }
52
        }
47
        *namelen = sprintf(*iname, "%s%d.db", rname, ++(*file_idx)) - 3;
53
        *namelen = sprintf(*iname, "%s%d.db", rname, ++(*file_idx)) - 3;
48
    } while (*file_idx < 10000);
54
    } while (*file_idx < 10000);
49
 
55
 
Line 71... Line 77...
71
 
77
 
72
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
78
    namelen = _check_sdf_name(name, &rname, &iname, &file_idx);
73
 
79
 
74
    if (!namelen) return NULL;
80
    if (!namelen) return NULL;
75
 
81
 
76
    _find_free_filename2(rname, &iname, &namelen, &file_idx);
82
    _find_free_filename2(rname, ".SQLiteDF", &iname, &namelen, &file_idx);
77
 
83
 
78
    if (file_idx >= 10000) { 
84
    if (file_idx >= 10000) { 
79
        Rprintf("Error: cannot find free SDF name.\n");
85
        Rprintf("Error: cannot find free SDF name.\n");
80
        return NULL;
86
        return NULL;
81
    }
87
    }
82
 
88
 
83
    /* add to workspace */
89
    /* add to workspace */
84
    strcpy(g_sql_buf[3], iname);
90
    sprintf(g_sql_buf[3], ".SQLiteDF/%s", iname);
85
    iname[namelen] = 0; /* remove ".db" */
91
    iname[namelen] = 0; /* remove ".db" */
86
    res = _add_sdf1(g_sql_buf[3], iname);
92
    res = _add_sdf1(g_sql_buf[3], iname);
87
    if (_sqlite_error(res)) return NULL;
93
    if (_sqlite_error(res)) return NULL;
88
 
94
 
89
    /* detach SDF's if necessary to attach this one. if file does not
95
    /* detach SDF's if necessary to attach this one. if file does not