void DataOutputStream::writeMatrixd(const osg::Matrixd& mat)
{

    for(int r=0;r<4;r++)
    {
        for(int c=0;c<4;c++)
        {
            writeDouble(mat(r,c));
        }
    }

    if (_verboseOutput) std::cout<<"read/writeMatrix() ["<<mat<<"]"<<std::endl;

}
Beispiel #2
0
static TACommandVerdict __wcstod_internal_cmd(TAThread thread,TAInputStream stream)
{

    wchar_t* st, *endptr;

    size_t size;

    double res;



    size = readInt(&stream);

    st = (wchar_t*)ta_alloc_memory(size * sizeof(wchar_t) + 1);



    readWCharArray(&stream, st, &size);



    st[size] = '\0';



    START_TARGET_OPERATION(thread);


    errno = 0;
    res = __wcstod_internal(st, &endptr, 0);

    END_TARGET_OPERATION(thread);


    writeDouble(thread, res);

    writeInt(thread, (int)( endptr - st));

    writeInt(thread, errno);


    ta_dealloc_memory(st);



    sendResponse(thread);


    return taDefaultVerdict;
}
Beispiel #3
0
int main(int argc, char *argv[]){

	if (argc < 4) {
		puts("Argument Exception!");
		return 1;
	}
	//Load Road Network from File
	loadData(argv[1]);
	
	//1 读入轨迹文件
	FILE* spatial = fopen(argv[2],"rb");
	FILE* stamp = fopen(argv[3],"wb");
	
	int traNumber = readInt(spatial);
	//printf("%d\n",traNumber);
	writeInt(stamp, traNumber);
	
	int i;
	for (i = 0; i < traNumber; ++i) {
		//puts(inPath[i]);
		int spNumber = readInt(spatial);
		
		writeInt(stamp,spNumber);
		int j;
		accDist[0] = 0;
		double roadLen,sideLen;
		
		for (j = 0; j < spNumber; ++j) { 
			path[j] = readInt(spatial);
			//printf("%d ",path[j]);
			nodeToEdgeDistanceAndNodeSide(0,0,path[j],&sideLen,&roadLen);
			writeInt(stamp,path[j]);
			writeDouble(stamp,roadLen);
		}
		
		//调试用
		if (i % 100 ==99) printf("+");
		if (i % 10000 ==9999) printf("\n");
	}
	
	fclose(spatial);
	fclose(stamp);
	
	return 0;
}
Beispiel #4
0
static TACommandVerdict asin_cmd(TAThread thread,TAInputStream stream)
{
    double x, res;
    x = readDouble(&stream);

    START_TARGET_OPERATION(thread);

    errno = 0;
    res = asin(x);

    END_TARGET_OPERATION(thread);

    writeInt(thread, errno);
    writeDouble(thread, res);
    sendResponse(thread);

    return taDefaultVerdict;
}
Beispiel #5
0
 void write(const osgGA::GUIEventAdapter& event)
 {
     writeUInt(event.getEventType());
     writeUInt(event.getKey());
     writeUInt(event.getButton());
     writeInt(event.getWindowX());
     writeInt(event.getWindowY());
     writeUInt(event.getWindowWidth());
     writeUInt(event.getWindowHeight());
     writeFloat(event.getXmin());
     writeFloat(event.getYmin());
     writeFloat(event.getXmax());
     writeFloat(event.getYmax());
     writeFloat(event.getX());
     writeFloat(event.getY());
     writeUInt(event.getButtonMask());
     writeUInt(event.getModKeyMask());
     writeDouble(event.getTime());
 }
Beispiel #6
0
static TACommandVerdict lgamma_r_cmd(TAThread thread,TAInputStream stream)
{
    double x, res;
    int signp;

    x = readDouble(&stream);

    START_TARGET_OPERATION(thread);

    errno = 0;
    res = lgamma_r(x, &signp);

    END_TARGET_OPERATION(thread);

    writeInt(thread, errno);
    writeDouble(thread, res);
    writeInt(thread, signp);
    sendResponse(thread);

    return taDefaultVerdict;
}
static void serializeUntypedTerm(A2PWriter writer, ATerm value){
	int type = ATgetType(value);
	switch(type){
		case AT_INT:
			writeInteger(writer, (ATermInt) value);
			break;
		case AT_REAL:
			writeDouble(writer, (ATermReal) value);
			break;
		case AT_APPL:
			{
				ATermAppl appl = (ATermAppl) value;
				AFun fun = ATgetAFun(appl);
				if(ATisQuoted(fun) == ATfalse){
					A2PType expected = A2PnodeType();
					ATermList annotations = (ATermList) ATgetAnnotations(value);
					if(annotations == NULL){
						writeNode(writer, expected, appl);
					}else{
						if(((A2PNodeType) expected->theType)->declaredAnnotations == NULL){ fprintf(stderr, "Node term has annotations, but none are declared.\n"); exit(1); }
						
						writeAnnotatedNode(writer, expected, appl, annotations);
					}
				}else{
					if(ATgetArity(fun) != 0){ fprintf(stderr, "Quoted appl (assumed to be a string) has a non-zero arity.\n"); exit(1);}
					
					writeString(writer, appl);
				}
			}
			break;
		case AT_LIST:
			writeList(writer, A2PlistType(A2PvalueType()), (ATermList) value);
			break;
		default:
			fprintf(stderr, "Encountered unwriteable type: %d.\n", type);
			exit(1);
	}
}
Beispiel #8
0
static TACommandVerdict cbrt_cmd(TAThread thread,TAInputStream stream)
{
    double x, res;

    // Prepare

    x = readDouble(&stream);
    
    START_TARGET_OPERATION(thread);
    
    // Execute

    res = cbrt(x);
    
    END_TARGET_OPERATION(thread);
    
    // Response
    
    writeDouble(thread, res);

    sendResponse(thread);
    return taDefaultVerdict;
}
void
writeSubspace (Subspace *s, char *training_filename, char *imageList, int argc, char**argv)
{
    int i, j;
    FILE* file;
    char *cutOffModeStr;
    time_t ttt = time(0);

    switch (s->cutOffMode)
    {
    case CUTOFF_NONE:    cutOffModeStr = "NONE";    break;
    case CUTOFF_SIMPLE:  cutOffModeStr = "SIMPLE";  break;
    case CUTOFF_ENERGY:  cutOffModeStr = "ENERGY";  break;
    case CUTOFF_STRETCH: cutOffModeStr = "STRETCH"; break;
    case CUTOFF_DROPVEC: cutOffModeStr = "DROPVEC"; break;
    default:             cutOffModeStr = "UNKNOWN"; break;
    }

    MESSAGE1ARG ("Saving trianing information to file %s", training_filename);

    file = fopen (training_filename, "wb");
    if (!file) {
        printf ("Error: could not open file <%s>\n", training_filename);
        exit (1);
    }

    fprintf (file, "TRAINING_COMMAND =");

    for (i = 0; i < argc; i++)
        fprintf (file, " %s", argv[i]);

    fprintf (file, "\n");
    fprintf (file, "DATE          = %s", ctime(&ttt));
    fprintf (file, "FILE_LIST     = %s\n", imageList);
    fprintf (file, "VECTOR_LENGTH = %d\n", s->basis->row_dim); /* numPixels */
    fprintf (file, "USE_LDA       = %s\n", s->useLDA ? "YES" : "NO" );

    fprintf (file, "CUTOFF_MODE   = %s\n", cutOffModeStr);
    fprintf (file, "CUTOFF_PERCENTAGE  = %f\n", s->cutOff);
    fprintf (file, "BASIS_VALUE_COUNT  = %d\n", s->values->row_dim);   /* basisDim */
    fprintf (file, "BASIS_VECTOR_COUNT = %d\n", s->basis->col_dim);
    fprintf (file, "DROPPED_FROM_FRONT = %d\n", s->dropNVectors);

    fprintf (file, "USE_LPP = %s\n", s->useLPP ? "YES" : "NO" );
    fprintf (file, "NEIGHBOURS = %d\n", s->neighbourCount);
    fprintf (file, "LPP_DIST = %s\n", s->lppDistance);
    fprintf (file, "LPP_VECTORS = %d\n", s->lppKeepNVectors);
    fprintf (file, "ADAPTIVE_K = %d\n", s->useAdaptiveK);

    fprintf (file, "USE_ICA = %s\n", s->useICA ? "YES" : "NO" );
    fprintf (file, "BLOCKSIZE = %d\n", s->blockSize);
    fprintf (file, "LEARNING_RATE = %e\n", s->learningRate);
    fprintf (file, "ITERATIONS = %d\n", s->iterations);
    fprintf (file, "ARCHITECTURE = %d\n", s->arch);

    for (i = 21; i < 256; i++){
        fprintf (file, "\n");
    }

    /**/

    /* write out the pixel count */
    writeInt (file, s->mean->row_dim);

    /* write out the mean vector */
    for (i = 0; i < s->mean->row_dim; i++) {
        writeDouble (file, ME(s->mean,i,0));
    }

    /* write out the number of eigen values */
    writeInt (file, s->values->row_dim);

    /* write out the eigen values */
    for (i = 0; i < s->values->row_dim; i++) {
        writeDouble (file, ME(s->values,i,0));
    }

    /* write out the number of basis vectors */
    writeInt (file,s->basis->col_dim);

    /* write out the eigen basis.  the size is "pixelcount"X"number of vectors"*/
    for (i = 0; i < s->basis->col_dim; i++) {
        for (j = 0; j < s->basis->row_dim; j++) {
            writeDouble (file, ME(s->basis, j, i));
        }
    }

    if(s->useICA)
    {
        /*START: 	Changed by Zeeshan: for ICA*/
        /* write out the number of rows in ica2 basis */
        writeInt (file, s->ica2Basis->row_dim);

        /* write out the number of cols in ica2 basis */
        writeInt (file, s->ica2Basis->col_dim);

        /* write out the ica 2 basis */
        for (i = 0; i < s->ica2Basis->col_dim; i++) {
            for (j = 0; j < s->ica2Basis->row_dim; j++) {
                writeDouble (file, ME(s->ica2Basis, j, i));
            }
        }
    }
    /*END: 		Changed by Zeeshan: for ICA*/

    fclose (file);
}
Beispiel #10
0
inline void nl_writeDouble(char* x, int& y, NLdouble z)		{ writeDouble(x, y, z); }
Beispiel #11
0
        void write(const osg::Matrix& matrix)
        {
            writeDouble(matrix(0,0));
            writeDouble(matrix(0,1));
            writeDouble(matrix(0,2));
            writeDouble(matrix(0,3));

            writeDouble(matrix(1,0));
            writeDouble(matrix(1,1));
            writeDouble(matrix(1,2));
            writeDouble(matrix(1,3));

            writeDouble(matrix(2,0));
            writeDouble(matrix(2,1));
            writeDouble(matrix(2,2));
            writeDouble(matrix(2,3));

            writeDouble(matrix(3,0));
            writeDouble(matrix(3,1));
            writeDouble(matrix(3,2));
            writeDouble(matrix(3,3));

            osg::notify(osg::NOTICE)<<"writeMatrix = "<<matrix<<std::endl;

        }
Beispiel #12
0
bool eaio::writeDouble(IStream* pOS, double_t value, Endian endianDestination)
{
    return writeDouble(pOS, &value, 1, endianDestination);
}
void DataOutputStream::writeVec2d(const osg::Vec2d& v){
    writeDouble(v.x());
    writeDouble(v.y());

    if (_verboseOutput) std::cout<<"read/writeVec2() ["<<v<<"]"<<std::endl;
}
Beispiel #14
0
static bool
output_val(PyObject* output, PyObject* value, TType type, PyObject* typeargs) {
    /*
     * Refcounting Strategy:
     *
     * We assume that elements of the thrift_spec tuple are not going to be
     * mutated, so we don't ref count those at all. Other than that, we try to
     * keep a reference to all the user-created objects while we work with them.
     * output_val assumes that a reference is already held. The *caller* is
     * responsible for handling references
     */

    switch (type) {

    case T_BOOL: {
        int v = PyObject_IsTrue(value);
        if (v == -1) {
            return false;
        }

        writeByte(output, (int8_t) v);
        break;
    }
    case T_I08: {
        int32_t val;

        if (!parse_pyint(value, &val, INT8_MIN, INT8_MAX)) {
            return false;
        }

        writeByte(output, (int8_t) val);
        break;
    }
    case T_I16: {
        int32_t val;

        if (!parse_pyint(value, &val, INT16_MIN, INT16_MAX)) {
            return false;
        }

        writeI16(output, (int16_t) val);
        break;
    }
    case T_I32: {
        int32_t val;

        if (!parse_pyint(value, &val, INT32_MIN, INT32_MAX)) {
            return false;
        }

        writeI32(output, val);
        break;
    }
    case T_I64: {
        int64_t nval = PyLong_AsLongLong(value);

        if (INT_CONV_ERROR_OCCURRED(nval)) {
            return false;
        }

        if (!CHECK_RANGE(nval, INT64_MIN, INT64_MAX)) {
            PyErr_SetString(PyExc_OverflowError, "int out of range");
            return false;
        }

        writeI64(output, nval);
        break;
    }

    case T_DOUBLE: {
        double nval = PyFloat_AsDouble(value);
        if (nval == -1.0 && PyErr_Occurred()) {
            return false;
        }

        writeDouble(output, nval);
        break;
    }

    case T_STRING: {
        Py_ssize_t len = 0;
        if (is_utf8(typeargs) && PyUnicode_Check(value))
            value = PyUnicode_AsUTF8String(value);
        len = PyString_Size(value);

        if (!check_ssize_t_32(len)) {
            return false;
        }

        writeI32(output, (int32_t) len);
        PycStringIO->cwrite(output, PyString_AsString(value), (int32_t) len);
        break;
    }

    case T_LIST:
    case T_SET: {
        Py_ssize_t len;
        SetListTypeArgs parsedargs;
        PyObject *item;
        PyObject *iterator;

        if (!parse_set_list_args(&parsedargs, typeargs)) {
            return false;
        }

        len = PyObject_Length(value);

        if (!check_ssize_t_32(len)) {
            return false;
        }

        writeByte(output, parsedargs.element_type);
        writeI32(output, (int32_t) len);

        iterator =  PyObject_GetIter(value);
        if (iterator == NULL) {
            return false;
        }

        while ((item = PyIter_Next(iterator))) {
            if (!output_val(output, item, parsedargs.element_type, parsedargs.typeargs)) {
                Py_DECREF(item);
                Py_DECREF(iterator);
                return false;
            }
            Py_DECREF(item);
        }

        Py_DECREF(iterator);

        if (PyErr_Occurred()) {
            return false;
        }

        break;
    }

    case T_MAP: {
        PyObject *k, *v;
        Py_ssize_t pos = 0;
        Py_ssize_t len;

        MapTypeArgs parsedargs;

        len = PyDict_Size(value);
        if (!check_ssize_t_32(len)) {
            return false;
        }

        if (!parse_map_args(&parsedargs, typeargs)) {
            return false;
        }

        writeByte(output, parsedargs.ktag);
        writeByte(output, parsedargs.vtag);
        writeI32(output, len);

        // TODO(bmaurer): should support any mapping, not just dicts
        while (PyDict_Next(value, &pos, &k, &v)) {
            // TODO(dreiss): Think hard about whether these INCREFs actually
            //               turn any unsafe scenarios into safe scenarios.
            Py_INCREF(k);
            Py_INCREF(v);

            if (!output_val(output, k, parsedargs.ktag, parsedargs.ktypeargs)
                    || !output_val(output, v, parsedargs.vtag, parsedargs.vtypeargs)) {
                Py_DECREF(k);
                Py_DECREF(v);
                return false;
            }
            Py_DECREF(k);
            Py_DECREF(v);
        }
        break;
    }

    // TODO(dreiss): Consider breaking this out as a function
    //               the way we did for decode_struct.
    case T_STRUCT: {
        StructTypeArgs parsedargs;
        Py_ssize_t nspec;
        Py_ssize_t i;

        if (!parse_struct_args(&parsedargs, typeargs)) {
            return false;
        }

        nspec = PyTuple_Size(parsedargs.spec);

        if (nspec == -1) {
            return false;
        }

        for (i = 0; i < nspec; i++) {
            StructItemSpec parsedspec;
            PyObject* spec_tuple;
            PyObject* instval = NULL;

            spec_tuple = PyTuple_GET_ITEM(parsedargs.spec, i);
            if (spec_tuple == Py_None) {
                continue;
            }

            if (!parse_struct_item_spec (&parsedspec, spec_tuple)) {
                return false;
            }

            instval = PyObject_GetAttr(value, parsedspec.attrname);

            if (!instval) {
                return false;
            }

            if (instval == Py_None) {
                Py_DECREF(instval);
                continue;
            }

            writeByte(output, (int8_t) parsedspec.type);
            writeI16(output, parsedspec.tag);

            if (!output_val(output, instval, parsedspec.type, parsedspec.typeargs)) {
                Py_DECREF(instval);
                return false;
            }

            Py_DECREF(instval);
        }

        writeByte(output, (int8_t)T_STOP);
        break;
    }

    case T_STOP:
    case T_VOID:
    case T_UTF16:
    case T_UTF8:
    case T_U64:
    default:
        PyErr_SetString(PyExc_TypeError, "Unexpected TType");
        return false;

    }

    return true;
}
Beispiel #15
0
void MessageEncoder::writeMap(const qpid::types::Variant::Map& properties, const Descriptor* d, bool large)
{
    void* token = large ? startMap32(d) : startMap8(d);
    for (qpid::types::Variant::Map::const_iterator i = properties.begin(); i != properties.end(); ++i) {
        writeString(i->first);
        switch (i->second.getType()) {
          case qpid::types::VAR_MAP:
          case qpid::types::VAR_LIST:
            //not allowed (TODO: revise, only strictly true for application-properties) whereas this is now a more general method)
            QPID_LOG(warning, "Ignoring nested map/list; not allowed in application-properties for AMQP 1.0");
          case qpid::types::VAR_VOID:
            writeNull();
            break;
          case qpid::types::VAR_BOOL:
            writeBoolean(i->second);
            break;
          case qpid::types::VAR_UINT8:
            writeUByte(i->second);
            break;
          case qpid::types::VAR_UINT16:
            writeUShort(i->second);
            break;
          case qpid::types::VAR_UINT32:
            writeUInt(i->second);
            break;
          case qpid::types::VAR_UINT64:
            writeULong(i->second);
            break;
          case qpid::types::VAR_INT8:
            writeByte(i->second);
            break;
          case qpid::types::VAR_INT16:
            writeShort(i->second);
            break;
          case qpid::types::VAR_INT32:
            writeInt(i->second);
            break;
          case qpid::types::VAR_INT64:
            writeULong(i->second);
            break;
          case qpid::types::VAR_FLOAT:
            writeFloat(i->second);
            break;
          case qpid::types::VAR_DOUBLE:
            writeDouble(i->second);
            break;
          case qpid::types::VAR_STRING:
            if (i->second.getEncoding() == BINARY) {
                writeBinary(i->second);
            } else {
                writeString(i->second);
            }
            break;
          case qpid::types::VAR_UUID:
            writeUuid(i->second);
            break;
        }
    }
    if (large) endMap32(properties.size()*2, token);
    else endMap8(properties.size()*2, token);
}
 cGenericBuffer& operator<< (double Data)          {writeDouble(Data); return *this;}
Beispiel #17
0
        void initFileObject(lua_State* L)
        {
            luaL_newmetatable(L, meta<File>());
            // Duplicate the metatable on the stack.
            lua_pushvalue(L, -1);
            // metatable.__index = metatable
            lua_setfield(L, -2, "__index");

            // Put the members into the metatable.
            const luaL_Reg functions[] = {
                {"__gc", [](lua_State* L) { return script::wrapped<File>(L, 1)->gc(L); }},
                {"__index", [](lua_State* L) { return script::wrapped<File>(L, 1)->index(L); }},
                {"__newindex", [](lua_State* L) { return script::wrapped<File>(L, 1)->newindex(L); }},
                {"__tostring", [](lua_State* L) { return script::wrapped<File>(L, 1)->tostring(L); }},
                {"__pairs", [](lua_State* L) { return script::wrapped<File>(L, 1)->pairs(L); }},
                {"close", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    file->close();
                    return 0;
                }},
                {"readUnsigned8", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);

                    uint8_t value;
                    if(file->readUnsigned8(value))
                    {
                        script::push(L, value);
                        return 1;
                    }
                    return 0;
                }},
                {"readUnsigned16", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);

                    uint16_t value;
                    if(file->readUnsigned16(value))
                    {
                        script::push(L, value);
                        return 1;
                    }
                    return 0;
                }},
                {"readUnsigned32", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);

                    uint32_t value;
                    if(file->readUnsigned32(value))
                    {
                        script::push(L, value);
                        return 1;
                    }
                    return 0;
                }},
                {"readInt8", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);

                    int8_t value;
                    if(file->readInt8(value))
                    {
                        script::push(L, value);
                        return 1;
                    }
                    return 0;
                }},
                {"readInt16", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);

                    int16_t value;
                    if(file->readInt16(value))
                    {
                        script::push(L, value);
                        return 1;
                    }
                    return 0;
                }},
                {"readInt32", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);

                    int32_t value;
                    if(file->readInt32(value))
                    {
                        script::push(L, value);
                        return 1;
                    }
                    return 0;
                }},
                {"readFloat", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);

                    float value;
                    if(file->readFloat(value))
                    {
                        script::push(L, value);
                        return 1;
                    }
                    return 0;
                }},
                {"readDouble", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);

                    double value;
                    if(file->readDouble(value))
                    {
                        script::push(L, value);
                        return 1;
                    }
                    return 0;
                }},
                {"readString", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    auto blockSize = script::get<int>(L, 2);

                    std::string buffer(blockSize, 0);
                    if(file->readString(buffer))
                    {
                        lua_pushlstring(L, buffer.data(), buffer.size());
                        return 1;
                    }
                    return 0;
                }},
                {"readLine", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);

                    std::string value;
                    if(file->readLine(value))
                    {
                        script::push(L, value.c_str());
                        return 1;
                    }
                    return 0;
                }},
                {"readFully", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);

                    // Get current position.
                    auto pos = file->tell();
                    if(pos == -1)
                    {
                        return 0;
                    }

                    // Length to read = position of end - current.
                    auto length = 0;
                    if(!file->seek(pos, FileSeekMode::End))
                    {
                        return 0;
                    }
                    length = file->tell() - pos;
                    if(!file->seek(pos, FileSeekMode::Start))
                    {
                        return 0;
                    }

                    // Read the entire file into a string.
                    std::string buf(length, 0);
                    if(file->readString(buf))
                    {
                        lua_pushlstring(L, buf.data(), buf.size());
                        return 1;
                    }
                    return 0;
                }},
                {"writeUnsigned8", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    auto value = script::get<int>(L, 2);

                    script::push(L, file->writeUnsigned8(value));
                    return 1;
                }},
                {"writeUnsigned16", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    auto value = script::get<int>(L, 2);

                    script::push(L, file->writeUnsigned16(value));
                    return 1;
                }},
                {"writeUnsigned32", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    auto value = script::get<int>(L, 2);

                    script::push(L, file->writeUnsigned32(value));
                    return 1;
                }},
                {"writeInt8", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    auto value = script::get<int>(L, 2);

                    script::push(L, file->writeInt8(value));
                    return 1;
                }},
                {"writeInt16", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    auto value = script::get<int>(L, 2);

                    script::push(L, file->writeInt16(value));
                    return 1;
                }},
                {"writeInt32", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    auto value = script::get<int>(L, 2);

                    script::push(L, file->writeInt32(value));
                    return 1;
                }},
                {"writeFloat", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    auto value = script::get<double>(L, 2);

                    script::push(L, file->writeFloat((float) value));
                    return 1;
                }},
                {"writeDouble", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    auto value = script::get<double>(L, 2);

                    script::push(L, file->writeDouble(value));
                    return 1;
                }},
                {"writeString", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    size_t length = 0;
                    const char* buffer = luaL_checklstring(L, 2, &length);
                    std::string value(buffer, buffer + length);
                    script::push(L, file->writeString(value));
                    return 1;
                }},
                {"writeLine", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    size_t length = 0;
                    const char* buffer = luaL_checklstring(L, 2, &length);
                    std::string value(buffer, buffer + length);
                    script::push(L, file->writeLine(value));
                    return 1;
                }},
                {"tell", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    script::push(L, int(file->tell()));
                    return 1;
                }},
                {"seek", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    auto position = script::get<int>(L, 2);
                    auto mode = FileSeekMode(script::get<int>(L, 3));
                    script::push(L, file->seek(position, mode));
                    return 1;
                }},
                {"flush", [](lua_State* L)
                {
                    auto file = script::ptr<File>(L, 1);
                    file->flush();
                    return 0;
                }},
                {nullptr, nullptr},
            };
            luaL_setfuncs(L, functions, 0);

            lua_pop(L, 1);
                
            // Push plum namespace.
            lua_getglobal(L, "plum");

            // plum.File = <function create>
            script::push(L, "File");
            lua_pushcfunction(L, [](lua_State* L)
            {
                auto filename = script::get<const char*>(L, 1);
                auto mode = FileOpenMode(script::get<int>(L, 2));
                auto f = new File(filename, mode);
                // Failure.
                if(!f->isActive())
                {    
                    delete f;
                    return 0;
                }

                script::push(L, f, LUA_NOREF);
                return 1;
            });
            lua_settable(L, -3);

            // Pop plum namespace.
            lua_pop(L, 1);
        }
Beispiel #18
0
int _tmain(int argc, NLchar **argv)
#endif
{
    NLbyte      buffer[NL_MAX_STRING_LENGTH];
    NLint       count = 0;
    NLbyte      b = (NLbyte)99;
    NLushort    s = 0x1122;
    NLulong     l = 0x11223344;
    NLfloat     f = 12.3141592651f;
    NLdouble    d = 123.12345678901234;
    NLchar      string[NL_MAX_STRING_LENGTH] = TEXT("Hello");
    NLbyte      block[] = {9,8,7,6,5,4,3,2,1,0};

    if(nlInit() == NL_FALSE)
        printErrorExit();

    nlEnable(NL_BIG_ENDIAN_DATA);
    _tprintf(TEXT("nl_big_endian_data = %d\n"), nlGetBoolean(NL_BIG_ENDIAN_DATA));
    _tprintf(TEXT("nlGetString(NL_VERSION) = %s\n\n"), nlGetString(NL_VERSION));
    _tprintf(TEXT("nlGetString(NL_NETWORK_TYPES) = %s\n\n"), nlGetString(NL_NETWORK_TYPES));

    if(nlSelectNetwork(NL_IP) == NL_FALSE)
        printErrorExit();

    _tprintf(TEXT("Short number: %#x, "), s);
    s = nlSwaps(s);
    _tprintf(TEXT("swapped: %#x, "), s);
    s = nlSwaps(s);
    _tprintf(TEXT("swapped back: %#x\n"), s);

    _tprintf(TEXT("Long number: %#lx, "), l);
    l = nlSwapl(l);
    _tprintf(TEXT("swapped: %#lx, "), l);
    l = nlSwapl(l);
    _tprintf(TEXT("swapped back: %#lx\n"), l);

    _tprintf(TEXT("Float number: %.10f, "), f);
    f = nlSwapf(f);
    _tprintf(TEXT("swapped: %.10f, "), f);
    f = nlSwapf(f);
    _tprintf(TEXT("swapped back: %.10f\n"), f);

    _tprintf(TEXT("Double number: %.14f, "), d);
    d = nlSwapd(d);
    _tprintf(TEXT("swapped: %.24f, "), d);
    d = nlSwapd(d);
    _tprintf(TEXT("swapped back: %.14f\n"), d);
    _tprintf(TEXT("\n"));

    _tprintf(TEXT("write byte %d to buffer\n"), b);
    _tprintf(TEXT("write short %#x to buffer\n"), s);
    _tprintf(TEXT("write long %#lx to buffer\n"), l);
    _tprintf(TEXT("write float %f to buffer\n"), f);
    _tprintf(TEXT("write double %.14f to buffer\n"), d);
    _tprintf(TEXT("write string %s to buffer\n"), string);
    _tprintf(TEXT("write block %d%d%d%d%d%d%d%d%d%d to buffer\n"), block[0]
            , block[1], block[2], block[3], block[4], block[5], block[6]
            , block[7], block[8], block[9]);
    _tprintf(TEXT("\n"));
    writeByte(buffer, count, b);
    writeShort(buffer, count, s);
    writeLong(buffer, count, l);
    writeFloat(buffer, count, f);
    writeDouble(buffer, count, d);
    writeString(buffer, count, string);
    writeBlock(buffer, count, block, 10);

    /* reset count to zero to read from start of buffer */
    count = 0;

    readByte(buffer, count, b);
    readShort(buffer, count, s);
    readLong(buffer, count, l);
    readFloat(buffer, count, f);
    readDouble(buffer, count, d);
    readString(buffer, count, string);
    readBlock(buffer, count, block, 10);
    _tprintf(TEXT("read byte %d from buffer\n"), b);
    _tprintf(TEXT("read short %#x from buffer\n"), s);
    _tprintf(TEXT("read long %#lx from buffer\n"), l);
    _tprintf(TEXT("read float %f from buffer\n"), f);
    _tprintf(TEXT("read double %.14f from buffer\n"), d);
    _tprintf(TEXT("read string %s from buffer\n"), string);
    _tprintf(TEXT("read block %d%d%d%d%d%d%d%d%d%d from buffer\n"), block[0]
            , block[1], block[2], block[3], block[4], block[5], block[6]
            , block[7], block[8], block[9]);

    nlShutdown();
    return 0;
}
Beispiel #19
0
void QgsDxfExport::writeGroup( int code, double d )
{
  writeGroupCode( code );
  writeDouble( d );
}
static void doSerialize(A2PWriter writer, A2PType expected, ATerm value){
        DKISIndexedSet sharedValues = writer->valueSharingMap;
        int valueHash = hashValue(value);
        int valueId = DKISget(sharedValues, (void*) value, (void*) expected, valueHash); /* TODO: Fix sharing (check types). */
        if(valueId != -1){
                writeByteToBuffer(writer->buffer, PDB_SHARED_FLAG);
                printInteger(writer->buffer, valueId);
                return;
        }

	switch(expected->id){
		case PDB_VALUE_TYPE_HEADER:
			serializeUntypedTerm(writer, value);
			break;
		case PDB_BOOL_TYPE_HEADER:
			if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Boolean didn't have AT_APPL type.\n"); exit(1); }
			writeBool(writer, (ATermAppl) value);
			break;
		case PDB_INTEGER_TYPE_HEADER:
			if(ATgetType(value) != AT_INT){ fprintf(stderr, "Integer didn't have AT_INT type.\n"); exit(1); }
			writeInteger(writer, (ATermInt) value);
			break;
		case PDB_DOUBLE_TYPE_HEADER:
			if(ATgetType(value) != AT_REAL){ fprintf(stderr, "Double didn't have AT_REAL type.\n"); exit(1); }
			writeDouble(writer, (ATermReal) value);
			break;
		case PDB_STRING_TYPE_HEADER:
			if(ATgetType(value) != AT_APPL || ATisQuoted(ATgetAFun((ATermAppl) value)) == ATfalse){ fprintf(stderr, "String didn't have 'quoted' AT_APPL type.\n"); ATabort(""); exit(1); }
			writeString(writer, (ATermAppl) value);
			break;
		case PDB_SOURCE_LOCATION_TYPE_HEADER:
			if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Source location didn't have AT_APPL type.\n"); exit(1); }
			writeSourceLocation(writer, (ATermAppl) value);
			break;
		case PDB_NODE_TYPE_HEADER:
			if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Node didn't have AT_APPL type.\n"); exit(1); }
			{
				ATermList annotations = (ATermList) ATgetAnnotations(value);
				if(annotations == NULL){
					writeNode(writer, expected, (ATermAppl) value);
				}else{
					if(((A2PNodeType) expected->theType)->declaredAnnotations == NULL){ fprintf(stderr, "Node term has annotations, but none are declared.\n"); exit(1); }
					
					writeAnnotatedNode(writer, expected, (ATermAppl) value, annotations);
				}
			}
			break;
		case PDB_TUPLE_TYPE_HEADER:
			if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Tuple didn't have AT_APPL type.\n"); exit(1); }
			writeTuple(writer, expected, (ATermAppl) value);
			break;
		case PDB_LIST_TYPE_HEADER:
			if(ATgetType(value) != AT_LIST){ fprintf(stderr, "List didn't have AT_LIST type.\n"); exit(1); }
			writeList(writer, expected, (ATermList) value);
			break;
		case PDB_SET_TYPE_HEADER:
			if(ATgetType(value) != AT_LIST){ fprintf(stderr, "Set didn't have AT_LIST type.\n"); exit(1); }
			writeSet(writer, expected, (ATermList) value);
			break;
		case PDB_RELATION_TYPE_HEADER:
			if(ATgetType(value) != AT_LIST){ fprintf(stderr, "Relation didn't have AT_LIST type.\n"); exit(1); }
			writeRelation(writer, expected, (ATermList) value);
			break;
		case PDB_MAP_TYPE_HEADER:
			if(ATgetType(value) != AT_LIST){ fprintf(stderr, "Map didn't have AT_LIST type.\n"); exit(1); }
			writeMap(writer, expected, (ATermList) value);
			break;
		case PDB_CONSTRUCTOR_TYPE_HEADER:
			if(ATgetType(value) != AT_APPL){ fprintf(stderr, "Constructor didn't have AT_APPL type.\n"); exit(1); }
			{
				ATermList annotations = (ATermList) ATgetAnnotations(value);
				if(annotations == NULL){
					writeConstructor(writer, expected, (ATermAppl) value);
				}else{
					if(((A2PConstructorType) expected->theType)->declaredAnnotations == NULL){ fprintf(stderr, "Constructor term has annotations, but none are declared.\n"); exit(1); }
					
					writeAnnotatedConstructor(writer, expected, (ATermAppl) value, annotations);
				}
			}
			break;
		case PDB_ADT_TYPE_HEADER:
			writeADT(writer, expected, value);
			break;
		default:
			fprintf(stderr, "Unserializable type: %d\n.", expected->id);
			exit(1);
	}
	
	DKISstore(sharedValues, (void*) value, (void*) expected, valueHash);
}
BEAST_API bool OutputStream::writeType <double> (double v) { return writeDouble (v); }