Exemple #1
0
void test_json_iterator ()
{
    {
        ADD_TESTS(11);

        pfs::json::json json;

        json["Image"]["Width"]               = 800;
        json["Image"]["Height"]              = 600;
        json["Image"]["Title"]               = _u8("View from 15th Floor");
        json["Image"]["Thumbnail"]["Url"]    = _u8("http://www.example.com/image/481989943");
        json["Image"]["Thumbnail"]["Height"] = 125;
        json["Image"]["Thumbnail"]["Width"]  = _u8("100");

        json["Image"]["IDs"].push_back(116);
        json["Image"]["IDs"].push_back(943);
        json["Image"]["IDs"].push_back(234);
        json["Image"]["IDs"].push_back(38793);

        TEST_OK(json.size() == 1);
        TEST_OK(json.find("Image") != json.end());
        TEST_OK(json["Image"].find("Title") != json["Image"].end());
        TEST_OK(json["Image"].find("--unknown--") == json["Image"].end());
        TEST_OK(json["Image"]["Thumbnail"].contains("Width"));
        TEST_OK(not json["Image"]["Thumbnail"].contains("--unknown--"));

        pfs::json::json::const_iterator it = json.cbegin();

        TEST_OK(it->is_object());
        TEST_OK(it->size() == 5);
        TEST_OK(++it == json.cend());
        TEST_OK((--it)->is_object());

        pfs::json::json::const_iterator it_image = it->cbegin();
        TEST_OK(it_image->size() == 1);
    }

    {
        ADD_TESTS(2);

        pfs::json::json json;
        json["id"] = 1;
        pfs::json::json::const_iterator it = json.find("id");
        TEST_OK(it != json.cend());
        TEST_OK(it != json.end());
    }
}
Exemple #2
0
static void __init_print_spec (print_spec & pspec, json::print_format const & format)
{
    pspec.new_line           = _u8("\n");
    pspec.key_separator      = _u8(":");
    pspec.value_separator    = _u8(",");
    pspec.open_object_brace  = _u8("{");
    pspec.close_object_brace = _u8("}");
    pspec.open_array_brace   = _u8("[");
    pspec.close_array_brace  = _u8("]");
    
    pspec.ws_symbol             = format.ws_symbol;     //_u8(" ")
    pspec.base_indent           = format.base_indent;   // 4
    pspec.brace_indent          = format.brace_indent;  // 0;
    pspec.first_item_indent     = format.first_item_indent; // 0
    pspec.ws_before_vseparator  = __repeat(format.ws_symbol, format.ws_before_vseparator);
    pspec.ws_after_vseparator   = __repeat(format.ws_symbol, format.ws_after_vseparator);
    pspec.ws_before_kvseparator = __repeat(format.ws_symbol, format.ws_before_kvseparator);
    pspec.ws_after_kvseparator  = __repeat(format.ws_symbol, format.ws_after_kvseparator);
    pspec.brace_position        = format.brace_position;        // json::brace_same_line;
    pspec.comma_position        = format.comma_position;        // json::comma_same_line;
}
Exemple #3
0
string to_string (json::value const & v, json::print_style_enum style)
{
    json::print_format format;

    format.ws_symbol = _u8(" ");
    format.base_indent           = 0;
    format.brace_indent          = 0;
    format.first_item_indent     = 0;
    format.ws_before_vseparator  = 0;
    format.ws_after_vseparator   = 0;
    format.ws_before_kvseparator = 0;
    format.ws_after_kvseparator  = 0;

    switch (style) {
    // Kernighan and Ritchie
    //-------------------
    // while (x == y) {
    //     something();
    //     somethingelse();
    // }
    case json::style_kr:
        format.base_indent = 4;
        format.ws_after_kvseparator = 1;
        format.brace_position = json::brace_same_line;
        format.comma_position = json::comma_same_line;
        break;
        
    // BSD (Allman)
    //-------------------
    // while (x == y)
    // {
    //     something();
    //     somethingelse();
    // }
    case json::style_bsd:
        format.base_indent = 4;
        format.ws_after_kvseparator = 1;
        format.brace_position = json::brace_next_line;
        format.comma_position = json::comma_same_line;
        break;
        
    // GNU
    //-------------------
    // while (x == y)
    //   {
    //     something();
    //     somethingelse();
    //   }
    case json::style_gnu:
        format.base_indent = 2;
        format.brace_indent = 2;
        format.ws_after_kvseparator = 1;
        format.brace_position = json::brace_next_line;
        format.comma_position = json::comma_same_line;
        break;
    
    // Whitesmiths
    //-------------------
    // while (x == y)
    //     {
    //     something();
    //     somethingelse();
    //     }
    case json::style_whitesmiths:
        format.base_indent = 0;
        format.brace_indent = 4;
        format.ws_after_kvseparator = 1;
        format.brace_position = json::brace_next_line;
        format.comma_position = json::comma_same_line;
        break;
        
    case json::style_favorite:
        format.base_indent = 4;
        format.ws_after_kvseparator = 1;
        format.ws_after_vseparator  = 1;
        format.first_item_indent    = 2;
        format.brace_position = json::brace_same_line;
        format.comma_position = json::comma_next_line;
        break;
        
    case json::style_plain:       // all in one line
    default:
        format.brace_position = json::brace_same_line;
        format.comma_position = json::comma_same_line;
        break;
    }
    
    json::print_spec pspec;
    json::__init_print_spec(pspec, format);
    
    if (style == json::style_plain)
        pspec.new_line = _u8("");
    
    return json::__to_string(v, pspec);
}
void CodeGen_X86::visit(const Cast *op) {

    if (!op->type.is_vector()) {
        // We only have peephole optimizations for vectors in here.
        CodeGen_Posix::visit(op);
        return;
    }

    vector<Expr> matches;

    struct Pattern {
        bool needs_sse_41;
        bool wide_op;
        Type type;
        string intrin;
        Expr pattern;
    };

    static Pattern patterns[] = {
        {false, true, Int(8, 16), "llvm.x86.sse2.padds.b",
         _i8(clamp(wild_i16x_ + wild_i16x_, -128, 127))},
        {false, true, Int(8, 16), "llvm.x86.sse2.psubs.b",
         _i8(clamp(wild_i16x_ - wild_i16x_, -128, 127))},
        {false, true, UInt(8, 16), "llvm.x86.sse2.paddus.b",
         _u8(min(wild_u16x_ + wild_u16x_, 255))},
        {false, true, UInt(8, 16), "llvm.x86.sse2.psubus.b",
         _u8(max(wild_i16x_ - wild_i16x_, 0))},
        {false, true, Int(16, 8), "llvm.x86.sse2.padds.w",
         _i16(clamp(wild_i32x_ + wild_i32x_, -32768, 32767))},
        {false, true, Int(16, 8), "llvm.x86.sse2.psubs.w",
         _i16(clamp(wild_i32x_ - wild_i32x_, -32768, 32767))},
        {false, true, UInt(16, 8), "llvm.x86.sse2.paddus.w",
         _u16(min(wild_u32x_ + wild_u32x_, 65535))},
        {false, true, UInt(16, 8), "llvm.x86.sse2.psubus.w",
         _u16(max(wild_i32x_ - wild_i32x_, 0))},
        {false, true, Int(16, 8), "llvm.x86.sse2.pmulh.w",
         _i16((wild_i32x_ * wild_i32x_) / 65536)},
        {false, true, UInt(16, 8), "llvm.x86.sse2.pmulhu.w",
         _u16((wild_u32x_ * wild_u32x_) / 65536)},
        {false, true, UInt(8, 16), "llvm.x86.sse2.pavg.b",
         _u8(((wild_u16x_ + wild_u16x_) + 1) / 2)},
        {false, true, UInt(16, 8), "llvm.x86.sse2.pavg.w",
         _u16(((wild_u32x_ + wild_u32x_) + 1) / 2)},
        {false, false, Int(16, 8), "packssdwx8",
         _i16(clamp(wild_i32x_, -32768, 32767))},
        {false, false, Int(8, 16), "packsswbx16",
         _i8(clamp(wild_i16x_, -128, 127))},
        {false, false, UInt(8, 16), "packuswbx16",
         _u8(clamp(wild_i16x_, 0, 255))},
        {true, false, UInt(16, 8), "packusdwx8",
         _u16(clamp(wild_i32x_, 0, 65535))}
    };

    for (size_t i = 0; i < sizeof(patterns)/sizeof(patterns[0]); i++) {
        const Pattern &pattern = patterns[i];

        if (!target.has_feature(Target::SSE41) && pattern.needs_sse_41) {
            continue;
        }

        if (expr_match(pattern.pattern, op, matches)) {
            bool match = true;
            if (pattern.wide_op) {
                // Try to narrow the matches to the target type.
                for (size_t i = 0; i < matches.size(); i++) {
                    matches[i] = lossless_cast(op->type, matches[i]);
                    if (!matches[i].defined()) match = false;
                }
            }
            if (match) {
                value = call_intrin(op->type, pattern.type.lanes(), pattern.intrin, matches);
                return;
            }
        }
    }


    #if LLVM_VERSION >= 38
    // Workaround for https://llvm.org/bugs/show_bug.cgi?id=24512
    // LLVM uses a numerically unstable method for vector
    // uint32->float conversion before AVX.
    if (op->value.type().element_of() == UInt(32) &&
        op->type.is_float() &&
        op->type.is_vector() &&
        !target.has_feature(Target::AVX)) {
        Type signed_type = Int(32, op->type.lanes());

        // Convert the top 31 bits to float using the signed version
        Expr top_bits = cast(signed_type, op->value / 2);
        top_bits = cast(op->type, top_bits);

        // Convert the bottom bit
        Expr bottom_bit = cast(signed_type, op->value % 2);
        bottom_bit = cast(op->type, bottom_bit);

        // Recombine as floats
        codegen(top_bits + top_bits + bottom_bit);
        return;
    }
    #endif


    CodeGen_Posix::visit(op);
}
Exemple #5
0
bool serial::setFormat (const pfs::string & fmt)
{
	bool r = true;

	if (fmt.length() != 3) {
		addError(string()
		        << _u8("bad format string")
		        << ":\"" << fmt << "\"");
		return false;
	}

	if (r) {
		ucchar ch = fmt.charAt(0);

		if (ch == pfs::ucchar('5'))
			r &= setDatabits(serial::DB5);
		else if (ch == pfs::ucchar('6'))
			r &= setDatabits(serial::DB6);
		else if (ch == pfs::ucchar('7'))
			r &= setDatabits(serial::DB7);
		else if (ch == pfs::ucchar('8'))
			r &= setDatabits(serial::DB8);
		else {
			addError(string()
			        << _u8("bad data bits value in string format")
			        << ": \"" << fmt << "\"");
			return false;
		}
	}

	if (r) {
		pfs::ucchar ch = fmt.charAt(1);

		if (ch == pfs::ucchar('N'))
			r &= setParity(serial::ParityNone);
		else if (ch == pfs::ucchar('O'))
			r &= setParity(serial::ParityOdd);
		else if (ch == pfs::ucchar('E'))
			r &= setParity(serial::ParityEven);
		else {
			addError(string()
			        << _u8("bad parity value in string format")
			        << ": " << fmt << "\"");
			return false;
		}
	}

	if (r) {
		pfs::ucchar ch = fmt.charAt(2);

		if (ch == pfs::ucchar('1'))
			r &= setStopbits(serial::SB1);
		else if (ch == pfs::ucchar('2'))
			r &= setStopbits(serial::SB2);
		else {
			addError(string()
			        << _u8("bad stop bits value in string format")
			        << ": " << fmt << "\'");
			return false;
		}
	}

	return r;
}
Exemple #6
0
int main(int argc, char *argv[])
{
	bool is_server = false;
	bool port_ok = true;

    if (argc < 3) {
    	std::cerr <<  "Too few arguments" << std::endl;
    	usage(argv[0]);
    	return -1;
    }

    if (strcmp(argv[1], "-server") == 0)
    	is_server = true;

    pfs::io::serial port;

    port.open(_u8(argv[2]), pfs::io::device::ReadWrite | pfs::io::device::NonBlocking);

//    port.open(_u8(argv[2]), cwt::io::device::ReadWrite
//    		, 115200, 8, 1, cwt::io::serial::ParityNone);

    if (!port.opened()) {
    	return -1;
    }

    if (argc > 3) {
    	pfs::string br_str(argv[3]);
    	bool ok = true;
    	unsigned int br = br_str.toUnsignedInt(&ok);

    	if (!ok) {
    		std::cerr << "Bad baud rate value" << std::endl;
    		port.close();
    		return -1;
    	}
    	port_ok |= port.setBaudrate(br);
    } else {
    	port_ok |= port.setBaudrate(pfs::io::serial::BR9600);
    }

    if (argc > 4)
    	port_ok |= port.setFormat(_u8(argv[4]));
    else
    	port_ok |= port.setFormat(pfs::io::serial::Format8N1);

    if (!port_ok) {
    	return -1;
    }

    std::cerr << "Port parameters: " << port.toString() << std::endl;

    if (is_server) {
    	//printf("Print text and press 'Enter' to send it to serial port or 'Q' to exit loop ...\n");
    	std::cout << "Press 'Ctrl+C' to exit loop ..." << std::endl;
    	while(true) {
    		char bytes[128];
    		ssize_t nbytes = port.read(bytes, 127);
    		if (nbytes < 0) {
    			port.logErrors();
    			break;
    		}
    		if (nbytes > 0) {
    			pfs::byte_string hex(__to_hex(bytes, size_t(nbytes)));
    			//bytes[nbytes] = '\0';
    			std::cout << "bytes read: " << nbytes << "[" << bytes << "][" << hex << ']' << std::endl;
    		}
    	}
    } else {
    	pfs::byte_string bytes("Hello, World!");
    	port.write(bytes);
    }

    port.close();

    return 0;
}