~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/Util_ms.cc

Added the PBMS daemon plugin.

(Augen zu und durch!)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 
2
 *
 
3
 * PrimeBase Media Stream for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Original author: Paul McCullagh
 
20
 * Continued development: Barry Leslie
 
21
 *
 
22
 * 2007-05-25
 
23
 *
 
24
 * H&G2JCtL
 
25
 *
 
26
 * Network interface.
 
27
 *
 
28
 */
 
29
 
 
30
/* I must do this in order to get the MS config.h, which
 
31
 * will give us the correct value for VERSION!
 
32
 */
 
33
#ifdef MYSQL_SERVER
 
34
#undef MYSQL_SERVER
 
35
#endif
 
36
 
 
37
#include "CSConfig.h"
 
38
#include <inttypes.h>
 
39
 
 
40
#include <string.h>
 
41
#include <ctype.h>
 
42
 
 
43
#include "CSGlobal.h"
 
44
#include "CSStrUtil.h"
 
45
 
 
46
#include "Engine_ms.h"
 
47
#include "Util_ms.h"
 
48
 
 
49
/*
 
50
 * A file name has the form:
 
51
 * <text>-<number>[.<ext>]
 
52
 * This function return the number part as a
 
53
 * u_int.
 
54
 */
 
55
uint32_t ms_file_to_table_id(const char *file_name, const char *name_part)
 
56
{
 
57
        uint32_t value = 0;
 
58
 
 
59
        if (file_name) {
 
60
                const char *num = file_name +  strlen(file_name) - 1;
 
61
                
 
62
                while (num >= file_name && *num != '-')
 
63
                        num--;
 
64
                if (name_part) {
 
65
                        /* Check the name part of the file: */
 
66
                        int len = strlen(name_part);
 
67
                        
 
68
                        if (len != num - file_name)
 
69
                                return 0;
 
70
                        if (strncmp(file_name, name_part, len) != 0)
 
71
                                return 0;
 
72
                }
 
73
                num++;
 
74
                if (isdigit(*num))
 
75
                        sscanf(num, "%"PRIu32"", &value);
 
76
        }
 
77
        return value;
 
78
}
 
79
 
 
80
uint32_t ms_file_to_table_id(const char *file_name)
 
81
{
 
82
        return ms_file_to_table_id(file_name, NULL);
 
83
}
 
84
 
 
85
const char *ms_file_to_table_name(size_t size, char *tab_name, const char *file_name)
 
86
{
 
87
        const char      *cptr;
 
88
        size_t          len;
 
89
 
 
90
        file_name = cs_last_name_of_path(file_name);
 
91
        cptr = file_name + strlen(file_name) - 1;
 
92
        while (cptr > file_name && *cptr != '.')
 
93
                cptr--;
 
94
        if (cptr > file_name && *cptr == '.') {
 
95
                if (strncmp(cptr, ".bs", 2) == 0) {
 
96
                        cptr--;
 
97
                        while (cptr > file_name && isdigit(*cptr))
 
98
                                cptr--;
 
99
                }
 
100
        }
 
101
 
 
102
        ret_name:
 
103
        len = cptr - file_name;
 
104
        if (len > size-1)
 
105
                len = size-1;
 
106
 
 
107
        memcpy(tab_name, file_name, len);
 
108
        tab_name[len] = 0;
 
109
 
 
110
        /* Return a pointer to what was removed! */
 
111
        return file_name + len;
 
112
}
 
113
 
 
114
bool ms_parse_blob_url(MSBlobURLPtr blob, const char *url)
 
115
{
 
116
        uint32_t                db_id = 0;
 
117
        uint32_t                tab_id = 0;
 
118
        uint64_t                blob_id = 0;
 
119
        uint64_t                blob_size = 0;
 
120
        uint64_t                blob_ref_id = 0;
 
121
        uint32_t                auth_code = 0;
 
122
        uint32_t                server_id = 0;
 
123
        char            type, junk[5];
 
124
        int                     scanned;
 
125
 
 
126
        junk[0] = 0;
 
127
        
 
128
        //If this changes do not forget to update couldBeURL in pbms.h.
 
129
        scanned = sscanf(url, URL_FMT"%4s", &db_id, &type, &tab_id, &blob_id, &auth_code, &server_id, &blob_ref_id, &blob_size, junk);
 
130
        if (scanned != 8) // If junk is found at the end this will also result in an invalid URL. 
 
131
                return false;
 
132
        
 
133
        if (junk[0] || (type != MS_URL_TYPE_BLOB && type != MS_URL_TYPE_REPO))
 
134
                return false;
 
135
                
 
136
        blob->bu_type = type;
 
137
        blob->bu_db_id = db_id;
 
138
        blob->bu_tab_id = tab_id;
 
139
        blob->bu_blob_id = blob_id;
 
140
        blob->bu_auth_code = auth_code;
 
141
        blob->bu_server_id = server_id;
 
142
        blob->bu_blob_ref_id = blob_ref_id;
 
143
        blob->bu_blob_size = blob_size;
 
144
        return blob->bu_db_id && tab_id && blob_id;
 
145
}
 
146
 
 
147
void ms_build_blob_url(MSBlobURLPtr blob, char *url)
 
148
{
 
149
        snprintf(url, PBMS_BLOB_URL_SIZE, URL_FMT,      blob->bu_db_id, 
 
150
                                                        blob->bu_type, 
 
151
                                                        blob->bu_tab_id, 
 
152
                                                        blob->bu_blob_id, 
 
153
                                                        blob->bu_auth_code, 
 
154
                                                        blob->bu_server_id, 
 
155
                                                        blob->bu_blob_ref_id, 
 
156
                                                        blob->bu_blob_size);
 
157
}
 
158
 
 
159
#define _STR(x) #x
 
160
#define STR(x) _STR(x)
 
161
const char *ms_version()
 
162
{
 
163
        return STR(PBMS_VERSION);
 
164
}
 
165
 
 
166
 
 
167