Преглед изворни кода

More QByteArray manipulation stuff

Neal Wilson пре 10 година
родитељ
комит
a9c19462b7
1 измењених фајлова са 108 додато и 12 уклоњено
  1. 108
    12
      src/typica.w

+ 108
- 12
src/typica.w Прегледај датотеку

@@ -2051,7 +2051,9 @@ which returns the |QByteArray| instead of converting this to a |QString|.
2051 2051
 QScriptValue QIODevice_readBytes(QScriptContext *context, QScriptEngine *engine)
2052 2052
 {
2053 2053
 	QIODevice *self = getself<QIODevice *>(context);
2054
-	return QScriptValue(engine->toScriptValue<QByteArray>(self->readAll()));
2054
+	QScriptValue value = engine->toScriptValue<QByteArray>(self->readAll());
2055
+	setQByteArrayProperties(value, engine);
2056
+	return value;
2055 2057
 }
2056 2058
 
2057 2059
 @ Wrappers around |peek()| and |read()| are also provided.
@@ -2060,15 +2062,19 @@ QScriptValue QIODevice_readBytes(QScriptContext *context, QScriptEngine *engine)
2060 2062
 QScriptValue QIODevice_peek(QScriptContext *context, QScriptEngine *engine)
2061 2063
 {
2062 2064
 	QIODevice *self = getself<QIODevice *>(context);
2063
-	return QScriptValue(engine->toScriptValue<QByteArray>(
2064
-		self->peek(argument<int>(0, context))));
2065
+	QScriptValue value = engine->toScriptValue<QByteArray>(
2066
+		self->peek(argument<int>(0, context)));
2067
+	setQByteArrayProperties(value, engine);
2068
+	return value;
2065 2069
 }
2066 2070
 
2067 2071
 QScriptValue QIODevice_read(QScriptContext *context, QScriptEngine *engine)
2068 2072
 {
2069 2073
 	QIODevice *self = getself<QIODevice *>(context);
2070
-	return QScriptValue(engine->toScriptValue<QByteArray>(
2071
-		self->read(argument<int>(0, context))));
2074
+	QScriptValue value = engine->toScriptValue<QByteArray>(
2075
+		self->read(argument<int>(0, context)));
2076
+	setQByteArrayProperties(value, engine);
2077
+	return value;
2072 2078
 }
2073 2079
 
2074 2080
 @ In order to work with |QByteArray| this should also be exposed to the host
@@ -2084,6 +2090,12 @@ QScriptValue QByteArray_getAt(QScriptContext *context, QScriptEngine *engine);
2084 2090
 QScriptValue QByteArray_setAt(QScriptContext *context, QScriptEngine *engine);
2085 2091
 QScriptValue QByteArray_appendBytes(QScriptContext *context, QScriptEngine *engine);
2086 2092
 QScriptValue QByteArray_appendString(QScriptContext *context, QScriptEngine *engine);
2093
+QScriptValue QByteArray_size(QScriptContext *context, QScriptEngine *engine);
2094
+QScriptValue QByteArray_left(QScriptContext *context, QScriptEngine *engine);
2095
+QScriptValue QByteArray_right(QScriptContext *context, QScriptEngine *engine);
2096
+QScriptValue QByteArray_mid(QScriptContext *context, QScriptEngine *engine);
2097
+QScriptValue QByteArray_chop(QScriptContext *context, QScriptEngine *engine);
2098
+QScriptValue QByteArray_remove(QScriptContext *context, QScriptEngine *engine);
2087 2099
 
2088 2100
 @ First, we provide some functionns for moving array data across the
2089 2101
 language barrier.
@@ -2130,6 +2142,12 @@ void setQByteArrayProperties(QScriptValue value, QScriptEngine *engine)
2130 2142
 	value.setProperty("setAt", engine->newFunction(QByteArray_setAt));
2131 2143
 	value.setProperty("appendBytes", engine->newFunction(QByteArray_appendBytes));
2132 2144
 	value.setProperty("appendString", engine->newFunction(QByteArray_appendString));
2145
+	value.setProperty("size", engine->newFunction(QByteArray_size));
2146
+	value.setProperty("left", engine->newFunction(QByteArray_left));
2147
+	value.setProperty("right", engine->newFunction(QByteArray_right));
2148
+	value.setProperty("mid", engine->newFunction(QByteArray_mid));
2149
+	value.setProperty("chop", engine->newFunction(QByteArray_chop));
2150
+	value.setProperty("remove", engine->newFunction(QByteArray_remove));
2133 2151
 }
2134 2152
 
2135 2153
 @ Perhaps the easiest way to deal with fixed byte strings for serial
@@ -2141,7 +2159,9 @@ QScriptValue QByteArray_fromHex(QScriptContext *context, QScriptEngine *engine)
2141 2159
 	QByteArray self = getself<QByteArray>(context);
2142 2160
 	QByteArray retval;
2143 2161
 	retval = self.fromHex(argument<QString>(0, context).toUtf8());
2144
-	return engine->toScriptValue<QByteArray>(retval);
2162
+	QScriptValue value = engine->toScriptValue<QByteArray>(retval);
2163
+	setQByteArrayProperties(value, engine);
2164
+	return value;
2145 2165
 }
2146 2166
 
2147 2167
 @ A pair of methods is provided for getting and setting values at a particular
@@ -2158,6 +2178,7 @@ QScriptValue QByteArray_setAt(QScriptContext *context, QScriptEngine *)
2158 2178
 {
2159 2179
 	QByteArray self = getself<QByteArray>(context);
2160 2180
 	self[argument<int>(0, context)] = (char)(argument<int>(1, context));
2181
+	return QScriptValue();
2161 2182
 }
2162 2183
 
2163 2184
 @ Methods are provided for appending either another |QByteArray| or a string
@@ -2168,15 +2189,84 @@ argument type.
2168 2189
 QScriptValue QByteArray_appendBytes(QScriptContext *context, QScriptEngine *engine)
2169 2190
 {
2170 2191
 	QByteArray self = getself<QByteArray>(context);
2171
-	return engine->toScriptValue<QByteArray>(
2172
-		self.append(argument<QByteArray>(0, context)));
2192
+	QScriptValue value =
2193
+		engine->toScriptValue<QByteArray>(
2194
+			self.append(argument<QByteArray>(0, context)));
2195
+	setQByteArrayProperties(value, engine);
2196
+	return value;
2173 2197
 }
2174 2198
 
2175 2199
 QScriptValue QByteArray_appendString(QScriptContext *context, QScriptEngine *engine)
2176 2200
 {
2177 2201
 	QByteArray self = getself<QByteArray>(context);
2178
-	return engine->toScriptValue<QByteArray>(
2202
+	QScriptValue value = engine->toScriptValue<QByteArray>(
2179 2203
 		self.append(argument<QString>(0, context)));
2204
+	setQByteArrayProperties(value, engine);
2205
+	return value;
2206
+}
2207
+
2208
+@ Checking the size of our byte array frequently a requirement.
2209
+
2210
+@<Functions for scripting@>=
2211
+QScriptValue QByteArray_size(QScriptContext *context, QScriptEngine *)
2212
+{
2213
+	QByteArray self = getself<QByteArray>(context);
2214
+	return QScriptValue(self.size());
2215
+}
2216
+
2217
+@ It is also frequently useful to be able to work with specific parts of a byte
2218
+array, so a few methods are provided for carving these up.
2219
+
2220
+@<Functions for scripting@>=
2221
+QScriptValue QByteArray_left(QScriptContext *context, QScriptEngine *engine)
2222
+{
2223
+	QByteArray self = getself<QByteArray>(context);
2224
+	QScriptValue value = engine->toScriptValue<QByteArray>(
2225
+		self.left(argument<int>(0, context)));
2226
+	setQByteArrayProperties(value, engine);
2227
+	return value;
2228
+}
2229
+
2230
+QScriptValue QByteArray_right(QScriptContext *context, QScriptEngine *engine)
2231
+{
2232
+	QByteArray self = getself<QByteArray>(context);
2233
+	QScriptValue value = engine->toScriptValue<QByteArray>(
2234
+		self.right(argument<int>(0, context)));
2235
+	setQByteArrayProperties(value, engine);
2236
+	return value;
2237
+}
2238
+
2239
+QScriptValue QByteArray_mid(QScriptContext *context, QScriptEngine *engine)
2240
+{
2241
+	QByteArray self = getself<QByteArray>(context);
2242
+	int length = -1;
2243
+	if(context->argumentCount() > 1)
2244
+	{
2245
+		length = argument<int>(1, context);
2246
+	}
2247
+	QScriptValue value = engine->toScriptValue<QByteArray>(
2248
+		self.mid(argument<int>(0, context), length));
2249
+	setQByteArrayProperties(value, engine);
2250
+	return value;
2251
+}
2252
+
2253
+@ We may also want to remove bytes from an array.
2254
+
2255
+@<Functions for scripting@>=
2256
+QScriptValue QByteArray_chop(QScriptContext *context, QScriptEngine *)
2257
+{
2258
+	QByteArray self = getself<QByteArray>(context);
2259
+	self.chop(argument<int>(0, context));
2260
+	return QScriptValue();
2261
+}
2262
+
2263
+QScriptValue QByteArray_remove(QScriptContext *context, QScriptEngine *engine)
2264
+{
2265
+	QByteArray self = getself<QByteArray>(context);
2266
+	QScriptValue value = engine->toScriptValue<QByteArray>(
2267
+		self.remove(argument<int>(0, context), argument<int>(1, context)));
2268
+	setQByteArrayProperties(value, engine);
2269
+	return value;
2180 2270
 }
2181 2271
 
2182 2272
 @ Some protocols require manipulating larger than 8 bit numbers as a sequence
@@ -2206,7 +2296,9 @@ QScriptValue bytesFromInt8(QScriptContext *context, QScriptEngine *engine)
2206 2296
 	QByteArray retval;
2207 2297
 	retval.resize(1);
2208 2298
 	retval[0] = bytes[0];
2209
-	return engine->toScriptValue<QByteArray>(retval);
2299
+	QScriptValue v = engine->toScriptValue<QByteArray>(retval);
2300
+	setQByteArrayProperties(v, engine);
2301
+	return v;
2210 2302
 }
2211 2303
 
2212 2304
 QScriptValue bytesFromInt16(QScriptContext *context, QScriptEngine *engine)
@@ -2217,7 +2309,9 @@ QScriptValue bytesFromInt16(QScriptContext *context, QScriptEngine *engine)
2217 2309
 	retval.resize(2);
2218 2310
 	retval[0] = bytes[0];
2219 2311
 	retval[1] = bytes[1];
2220
-	return engine->toScriptValue<QByteArray>(retval);
2312
+	QScriptValue v = engine->toScriptValue<QByteArray>(retval);
2313
+	setQByteArrayProperties(v, engine);
2314
+	return v;
2221 2315
 }
2222 2316
 
2223 2317
 QScriptValue bytesFromInt32(QScriptContext *context, QScriptEngine *engine)
@@ -2230,7 +2324,9 @@ QScriptValue bytesFromInt32(QScriptContext *context, QScriptEngine *engine)
2230 2324
 	retval[1] = bytes[1];
2231 2325
 	retval[2] = bytes[2];
2232 2326
 	retval[3] = bytes[3];
2233
-	return engine->toScriptValue<QByteArray>(retval);
2327
+	QScriptValue v = engine->toScriptValue<QByteArray>(retval);
2328
+	setQByteArrayProperties(v, engine);
2329
+	return v;
2234 2330
 }
2235 2331
 
2236 2332
 @* Scripting QBuffer.

Loading…
Откажи
Сачувај