bool json_tree_builder::process_first(const token &token_)
{
    auto jval = to_value(token_);
    if (jval == nullptr || (jval->jtype() != json_type::J_OBJECT && jval->jtype() != json_type::J_ARRAY))
    { // object or array expected
        return false;
    }
    _current = jval; // object or array
    _accept_value = _current->jtype() == json_type::J_ARRAY;

    return true;
}
Beispiel #2
0
void ItemFormatterBase::setJobTypeFld(int index, const QString &fld, bool center)
{
   QByteArray jtype(fld.trimmed().toAscii());
   if (jtype.size()) {
      setTextFld(index, job_type_to_str(jtype[0]), center);
   } else {
      setTextFld(index, "", center);
   }
}
bool json_tree_builder::insert_to_array(const token &token_)
{
    auto arr = std::static_pointer_cast< json_array >(_current);

    switch (token_.type())
    {
    case T_COMMA:
        if (_accept_comma)
        {
            _accept_comma = false;
            _accept_end = false;
            _accept_value = true;

            return true;
        }
        else
        {
            return false;
        }
        break;
    case T_RBRACKET:
        if (_accept_end)
        {
            if (_parents.empty())
            { // parsing successfully finished
                _finished = true;
                return true;
            }

            _current = _parents.top();
            _parents.pop();

            _accept_colon = false;
            _accept_comma = true;
            _accept_end = true;
            _accept_name = false;
            _accept_value = _current->jtype() == json_type::J_ARRAY;

            return true;
        }
        else
        {
            return false;
        }
        break;
    case T_LBRACE:
    case T_LBRACKET:
        if (_accept_value)
        {
            auto new_val = to_value(token_);
            _parents.push(_current);
            _current = new_val;
            arr->insert(new_val);

            _accept_colon = false;
            _accept_comma = false;
            _accept_end = true;
            _accept_name = true;
            _accept_value = new_val->jtype() == json_type::J_ARRAY;

            return true;
        }
        else
        {
            return false;
        }
        break;
    case T_STR:
    case T_NUM:
    case T_TRUE:
    case T_FALSE:
    case T_NULL:
        if (_accept_value)
        {
            arr->insert(to_value(token_));
            _accept_comma = true;
            _accept_end = true;
            _accept_value = false;

            return true;
        }
        else
        {
            return false;
        }
        break;
    default:
        return false; // unexpected token
    }

    return false; // should not get here
}
Beispiel #4
0
static void
genj_writeattr(Generator* generator, Symbol* asym, Bytebuffer* code,
               int rank, size_t* start, size_t* count)
{
    Symbol* basetype = asym->typ.basetype;
    nc_type typecode = basetype->typ.typecode;
    /* default assumption */
    size_t len = asym->data == NULL?0:asym->data->length; 

    codeprintf("%s/* attribute: %s */\n",indented(1),asym->name);

    /* Handle NC_CHAR specially */
    if(typecode == NC_CHAR) {
        /* revise the length count */
        len = bbLength(code);
        if(len == 0) {
	    bbAppend(code,'\0'); len++;
	    bbClear(code);
	    bbCat(code,"\"\"");
	    len++;
	} else
            jquotestring(code,'"');
	bbNull(code);
    } else { /* not NC_CHAR*/
        char* code2;
	commify(code);
        /* Convert to constant */
        code2 = bbDup(code);
        bbClear(code);
        bbprintf0(stmt,"new %s[]",
                jarraytype(typecode));
        bbCatbuf(code,stmt);
        bbCat(code,"{");
        bbCat(code,code2);
        bbCat(code,"}");
        efree(code2);
    }
    switch (typecode) {
    case NC_BYTE:
    case NC_SHORT:
    case NC_INT:
    case NC_FLOAT:
    case NC_DOUBLE:
	codelined(1,"{");
	bbprintf0(stmt,"%sArray data = Array.factory(%s.class, new int[]{%lu}, ",
		indented(1),
		jtype(basetype->typ.typecode),
		len);
	codedump(stmt);
        codedump(code);
	codeline(");");
	if(asym->att.var == NULL) {
            bbprintf0(stmt,"%sncfile.addGlobalAttribute(\"%s\",data);\n",
                indented(1),jescapifyname(asym->name));
	} else {
            bbprintf0(stmt,"%sncfile.addVariableAttribute(\"%s\",\"%s\",data);\n",
		indented(1),
		jescapifyname(asym->att.var->name),
                jescapifyname(asym->name));
	}
        codedump(stmt);
	codelined(1,"}");
        codeflush();
        break;

    case NC_CHAR:
	if(asym->att.var == NULL) {
            bbprintf0(stmt,"%sncfile.addGlobalAttribute(\"%s\",%s);\n",
		indented(1),
                jescapifyname(asym->name),
		bbContents(code));
	} else {
            bbprintf0(stmt,"%sncfile.addVariableAttribute(\"%s\",\"%s\",%s);\n",
		indented(1),
		jescapifyname(asym->att.var->name),
                jescapifyname(asym->name),
		bbContents(code));
	}
        codedump(stmt);
        codeflush();
        break;

    default: break;
    }
    codeflush();
}
Beispiel #5
0
static void
genj_writevar(Generator* generator, Symbol* vsym, Bytebuffer* code,
              int rank, size_t* start, size_t* count)
{
    Dimset* dimset = &vsym->typ.dimset;
    int typecode = vsym->typ.basetype->typ.typecode;
    int i;

    codeline("");
    codelined(1,"{"); /* Enclose in {...} for scoping */

    if(rank == 0) {
        bbprintf0(stmt,"%sArray%s.D0 data = new Array%s.D0();\n",
		indented(1),jtypecap(typecode), jtypecap(typecode));
        codedump(stmt);
        if(typecode == NC_CHAR) {
            /* Construct the data Array */
            jquotestring(code,'\'');
	    bbprintf0(stmt,"%sdata.set((char)%s);\n",
			  indented(1),bbContents(code));
	} else {
	    commify(code);
            bbprintf0(stmt,"%sdata.set((%s)%s);\n",
	 	      indented(1),jtype(typecode),bbContents(code));
        }
	codedump(stmt);
        /* do the actual write */
        bbprintf0(stmt,"%sncfile.write(\"%s\",data);\n",
		indented(1),jescapifyname(vsym->name));
	codedump(stmt);
    } else { /* array */
	Bytebuffer* dimbuf = bbNew();
        /* Construct the dimension set*/
	bbCat(dimbuf,"new int[]{");
	for(i=0;i<rank;i++) {
            Symbol* dsym = dimset->dimsyms[i];
	    char tmp[32];
	    nprintf(tmp,sizeof(tmp),"%lu",dsym->dim.declsize);
	    if(i>0) {bbCat(dimbuf,", ");}
	    bbCat(dimbuf,tmp);
	}
	bbCat(dimbuf,"}");
        /* Construct the data array and capture its index */
	if(typecode == NC_CHAR) {
	    jquotestring(code,'"');
            bbprintf0(stmt,"%sString contents = ",
			indented(1));
	} else {
            bbprintf0(stmt,"%s%s[] contents = new %s[] {",
			indented(1),jtype(typecode),jtype(typecode));
	    commify(code);
	}
	codedump(stmt);
        codedump(code);
        if(typecode != NC_CHAR) codepartial("}");
        codeline(";");
        bbprintf0(stmt,"%sArray%s data = new Array%s(%s);\n",
		indented(1),
		jtypecap(typecode),
		jtypecap(typecode),
		bbContents(dimbuf));
        codedump(stmt);
        codelined(1,"IndexIterator iter = data.getIndexIterator();");
        codelined(1,"int count = 0;");
        codelined(1,"while(iter.hasNext())");
	if(typecode == NC_CHAR)
            bbprintf0(stmt,
			"%siter.setCharNext(contents.charAt(count++));\n",indented(2));
	else
            bbprintf0(stmt,"%siter.set%sNext(contents[count++]);\n",
                    indented(2),jtypecap(typecode));
	codedump(stmt);
        bbFree(dimbuf);
	/* Construct the origin set from the start set */
        bbprintf0(stmt,"%sint[] origin = new int[]{",indented(1));
	for(i=0;i<rank;i++) {
	    bbprintf(stmt,"%s%lu",(i>0?", ":""),start[i]);
	}
	bbCat(stmt,"};\n");
	codedump(stmt);
        /* do the actual write */
        bbprintf0(stmt,"%sncfile.write(\"%s\",origin,data);\n",
		indented(1),jescapifyname(vsym->name));
	codedump(stmt);
    }
    codelined(1,"}"); /* Enclose in {...} for scoping */
    codeflush();
}
Beispiel #6
0
static void
genjstd_definevardata(Symbol* vsym)
{
    Dimset* dimset = &vsym->typ.dimset;
    Symbol* basetype = vsym->typ.basetype;
    int rank = dimset->ndims;
    int isscalar = (dimset->ndims == 0);
    Bytebuffer* code;
    nc_type typecode = basetype->typ.typecode;

    if(vsym->data == NULL) return;

    code = bbNew();

    jlined(1,"{");

    /* Handle special cases first*/
    if(isscalar) {
        /* Construct the data Array */
        nprintf(stmt,sizeof(stmt),"Array%s.D0 data = new Array%s.D0();",
		jtypecap(typecode), jtypecap(typecode));

        jlined(1,stmt);
	/* Fill it */
	genjstd_scalardata(vsym,code);
	if(typecode == NC_CHAR) {
            nprintf(stmt,sizeof(stmt),"data.set(%s.charAt(0));",
		bbContents(code));
	} else {
            nprintf(stmt,sizeof(stmt),"data.set((%s)%s);",
		jtype(typecode),bbContents(code));
	}
	jlined(1,stmt);
     } else { /* Non-scalar*/
	int i;
        Bytebuffer* dimbuf = bbNew();

	/* Store the data */
        genjstd_arraydata(vsym,NULL,code);

        /* Construct the dimension set*/
	bbCat(dimbuf,"new int[]{");
	for(i=0;i<rank;i++) {
            Symbol* dsym = dimset->dimsyms[i];
	    char tmp[32];
	    if(i==0 && dsym->dim.size == NC_UNLIMITED)
	        nprintf(tmp,sizeof(tmp),"%lu",dsym->dim.unlimitedsize);
	    else
	        nprintf(tmp,sizeof(tmp),"%lu",dsym->dim.size);
	    if(i>0) {bbCat(dimbuf,", ");}
	    bbCat(dimbuf,tmp);
	}
	bbCat(dimbuf,"}");
        /* Construct the data array and capture its index */
	if(typecode == NC_CHAR)
            nprintf(stmt,sizeof(stmt),"%sString contents = ",
			indented(1));
	else
            nprintf(stmt,sizeof(stmt),"%s%s[] contents = new %s[] {",
			indented(1),jtype(typecode),jtype(typecode));
	jpartial(stmt);
	commify(code);
        jprint(code);
        if(typecode != NC_CHAR) jpartial("}");
        jline(";");
        nprintf(stmt,sizeof(stmt),"Array%s data = new Array%s(%s);",
		jtypecap(typecode), jtypecap(typecode), bbContents(dimbuf));
        jlined(1,stmt);
        jlined(1,"IndexIterator iter = data.getIndexIterator();");
        jlined(1,"int count = 0;");
        jlined(1,"while(iter.hasNext())");
	if(typecode == NC_CHAR)
            nprintf(stmt,sizeof(stmt),
			"iter.setCharNext(contents.charAt(count++));");
	else
            nprintf(stmt,sizeof(stmt),"iter.set%sNext(contents[count++]);",
                    jtypecap(typecode));
	jlined(2,stmt);
        bbFree(dimbuf);
    }
    /* do the actual write */
    nprintf(stmt,sizeof(stmt),"ncfile.write(\"%s\",data);",
		jescapifyname(vsym->name));
    jlined(1,stmt);
    bbFree(code);    
    jlined(1,"}");
    jflush();
}
Beispiel #7
0
static void
genjstd_primattribute(Symbol* asym, Bytebuffer* code, unsigned long len)
{
    Symbol* basetype = asym->typ.basetype;
    nc_type typecode = basetype->typ.typecode;

    /* Handle NC_CHAR specially */
    if(typecode == NC_CHAR) {
        /* revise the length count */
        len = bbLength(code);
        if(len == 0) {bbAppend(code,'\0'); len++;}
        jquotestring(code,'"');
    } else {
        /* Convert to constant */
        char* code2 = bbDup(code);
        bbClear(code);
        nprintf(stmt,sizeof(stmt),"new %s[]",
                jarraytype(typecode));
        bbCat(code,stmt);
        bbCat(code,"{");
        bbCat(code,code2);
        bbCat(code,"}");
        efree(code2);
    }
    switch (typecode) {
    case NC_BYTE:
    case NC_SHORT:
    case NC_INT:
    case NC_FLOAT:
    case NC_DOUBLE:
	jlined(1,"{");
	nprintf(stmt,sizeof(stmt),"%sArray data = Array.factory(%s.class, new int[]{%lu}, ",
		indented(1),
		jtype(basetype->typ.typecode),
		len);
	jpartial(stmt);
        jprint(code);
	jline(");");
	if(asym->att.var == NULL) {
            nprintf(stmt,sizeof(stmt),"ncfile.addGlobalAttribute(\"%s\",data);",
                jescapifyname(asym->name));
	} else {
            nprintf(stmt,sizeof(stmt),"ncfile.addVariableAttribute(\"%s\",\"%s\",data);",
		jescapifyname(asym->att.var->name),
                jescapifyname(asym->name));
	}
        jlined(1,stmt);
	jlined(1,"}");
        jflush();
        break;

    case NC_CHAR:
	if(asym->att.var == NULL) {
            nprintf(stmt,sizeof(stmt),"ncfile.addGlobalAttribute(\"%s\",%s);",
                jescapifyname(asym->name),
		bbContents(code));
	} else {
            nprintf(stmt,sizeof(stmt),"ncfile.addVariableAttribute(\"%s\",\"%s\",%s);",
		jescapifyname(asym->att.var->name),
                jescapifyname(asym->name),
		bbContents(code));
	}
        jlined(1,stmt);
        jflush();
        break;

    default: break;
    }
}
Beispiel #8
0
/**
 * @brief Performs semantic analysis of root object - tells if given JSON
 * is valid Docker image confuguration:
 * https://github.com/docker/docker/blob/master/image/spec/v1.md#image-json-description
 * @param val_ JSON document
 * @return is valid Docker image spec?
 */
bool
is_valid_root_object (std::shared_ptr<json_value> val_)
{
    bool ret = true;

    /*
     * id -> J_STRING, is_hexadecimal_256bits()
     * created -> J_STRING, is_iso8601_datetime()
     * os -> J_STRING, is_os()
     * parent -> J_STRING, is_hexadecimal_256bits()
     * Size -> J_INT
     * architecture -> J_STRING, is_architecture()
     * author -> J_STRING
     * checksum -> J_STRING
     * config -> J_OBJECT / J_NULL, is_valid_config()
     */

	if (val_->jtype() != json_type::J_OBJECT) {
		return false;
	}

    auto object = std::static_pointer_cast< json_object >(val_); // type std::shared_ptr< json_object >

    // validate id
    auto id = object->find("\"id\""); // id is of type std::shared_ptr< json_value >
    if (id != nullptr) {
        if (id->jtype() != json_type::J_STRING) {
            std::cerr << "id isn't string" << std::endl;
            ret = false;
        } else {
            if (!is_hexadecimal_256bits(id->to_string())) {
                std::cerr << "id isn't valid hexadecimal string" << std::endl;
                ret = false;
            }
        }
    } else {
        std::cerr << "id doesn't exist" << std::endl;
        ret = false;
    }

    // validate created
    auto created = object->find("\"created\"");
    if (created != nullptr) {
        if (created->jtype() != json_type::J_STRING) {
            std::cerr << "created isn't string" << std::endl;
            ret = false;
        } else {
            if (!is_iso8601_datetime(created->to_string())) {
                std::cerr << "created isn't valid datetime string" << std::endl;
                ret = false;
            }
        }
    }

    // validate os
    auto os = object->find("\"os\"");
    if (os != nullptr) {
        if (os->jtype() != json_type::J_STRING) {
            std::cerr << "os isn't string" << std::endl;
            ret = false;
        } else {
            if (!is_os(os->to_string())) {
                std::cerr << "os isn't valid os string" << std::endl;
                ret = false;
            }
        }
    }

    // validate parent
    auto parent = object->find("\"parent\"");
    if (parent != nullptr) {
        if (parent->jtype() != json_type::J_STRING) {
            std::cerr << "parent isn't string" << std::endl;
            ret = false;
        } else {
            if (!is_hexadecimal_256bits(parent->to_string())) {
                std::cerr << "parent isn't valid hexadecimal string" << std::endl;
                ret = false;
            }
        }
    }

    // validate Size
    auto size = object->find("\"Size\"");
    if (size != nullptr) {
        if (size->jtype() != json_type::J_INT) {
            std::cerr << "size isn't int" << std::endl;
            ret = false;
        } else {
            if (std::static_pointer_cast<json_int>(size)->value() < 0) {
                std::cerr << "Size is negative" << std::endl;
				ret = false;
			}
		}
    }

    // validate architecture
    auto architecture = object->find("\"architecture\"");
    if (architecture != nullptr) {
        if (architecture->jtype() != json_type::J_STRING) {
            std::cerr << "architecture isn't string" << std::endl;
            ret = false;
        } else {
            if (!is_architecture(architecture->to_string())) {
                std::cerr << "architecture isn't valid architecture string" << std::endl;
                ret = false;
            }
        }
    }

    // validate author
    auto author = object->find("\"author\"");
    if (author != nullptr) {
        if (author->jtype() != json_type::J_STRING) {
            std::cerr << "author isn't string" << std::endl;
            ret = false;
        }
    }

    // validate checksum
    auto checksum = object->find("\"checksum\"");
    if (checksum != nullptr) {
        if (checksum->jtype() != json_type::J_STRING) {
            std::cerr << "checksum isn't string" << std::endl;
            ret = false;
        }
    }

    // validate config
    auto config = object->find("\"config\"");
    if (config != nullptr) {
        if (config->jtype() == json_type::J_NULL) {
            ; // can be null, is valid
        } else if (config->jtype() == json_type::J_OBJECT) {
            if (!is_valid_config(config)) {
                std::cerr << "config isn't valid" << std::endl;
                ret = false;
            }
        } else {
            std::cerr << "config isn't valid type" << std::endl;
            ret = false;
        }
    }

    return ret;
}
Beispiel #9
0
/**
 * @brief Is valid config?
 * @param val_ config
 * @return is valid?
 */
bool
is_valid_config (std::shared_ptr< json_value > val_)
{
    bool ret = true;

    /*
    * User -> J_STRING, is_user_group()
    * Memory -> J_INT
    * MemorySwap -> J_INT
    * CpuShares -> J_INT
    * ExposedPorts -> J_OBJECT, is_valid_exposedports()
    * Env -> J_ARRAY, is_valid_env()
    * Entrypoint, J_ARRAY, is_content_j_string_array()
    * Cmd, J_ARRAY,is_content_j_string_array()
    * Volumes, J_OBJECT, is_valid_second_object_object()
    * WorkingDir, J_OBJECT
    */

    auto object = std::static_pointer_cast< json_object >(val_);

    // validate Env
    auto Env = object->find("\"Env\"");
    if (Env != nullptr) {
        if (Env->jtype() == json_type::J_ARRAY) {
			if (!is_valid_env(Env)) {
				std::cerr << "Env isn't valid env array" << std::endl;
				ret = false;
			}
        } else if (Env->jtype() == json_type::J_NULL) {
			; // is valid
		} else {
			ret = false;
		}
    }

    // validate ExposedPorts
    auto ExposedPorts = object->find("\"ExposedPorts\"");
    if (ExposedPorts != nullptr) {
        if (ExposedPorts->jtype() == json_type::J_OBJECT) {
            if (!is_valid_exposedports(ExposedPorts)) {
                std::cerr << "ExposedPorts isn't valid exposedports object" << std::endl;
                ret = false;
            }
        } else if (ExposedPorts->jtype() == json_type::J_NULL) {
			; // is valid
		} else {
			ret = false;
		}
    }

    // validate WorkingDir
    auto WorkingDir = object->find("\"WorkingDir\"");
    if (WorkingDir != nullptr) {
        if (WorkingDir->jtype() != json_type::J_STRING && WorkingDir->jtype() != json_type::J_NULL) {
            std::cerr << "WorkingDir isn't object" << std::endl;
            ret = false;
        }
    }

    // validate Volumes
    auto Volumes = object->find("\"Volumes\"");
    if (Volumes != nullptr) {
        if (Volumes->jtype() == json_type::J_OBJECT) {
            if (!is_valid_second_object_object(Volumes)) {
                std::cerr << "Volumes isn't valid volumes object" << std::endl;
                ret = false;
            }
        } else if (Volumes->jtype() == json_type::J_NULL) {
			; // is valid
		} else {
			ret = false;
		}
    }

    // validate Cmd
    auto Cmd = object->find("\"Cmd\"");
    if (Cmd != nullptr) {
        if (Cmd->jtype() == json_type::J_ARRAY) {
            if (!is_content_j_string_array(Cmd)) {
                std::cerr << "Cmd isn't valid array of strings" << std::endl;
                ret = false;
            }
        } else if (Cmd->jtype() == json_type::J_NULL) {
			; // is valid
		} else {
			ret = false;
		}
    }

    // validate Entrypoint
    auto Entrypoint = object->find("\"Entrypoint\"");
    if (Entrypoint != nullptr) {
        if (Entrypoint->jtype() == json_type::J_ARRAY) {
            if (!is_content_j_string_array(Entrypoint)) {
                std::cerr << "Entrypoint isn't valid array of strings" << std::endl;
                ret = false;
            }
        } else if (Entrypoint->jtype() == json_type::J_NULL) {
			; // is valid
		} else {
			ret = false;
		}
    }

    // validate CpuShares
    auto CpuShares = object->find("\"CpuShares\"");
    if (CpuShares != nullptr) {
        if (CpuShares->jtype() == json_type::J_INT) {
			if (std::static_pointer_cast<json_int>(CpuShares)->value() <= 0) {
				std::cerr << "CpuShares isn't positive integer" << std::endl;
				ret = false;
			}
		} else if (CpuShares->jtype() == json_type::J_NULL) {
			; // is valid
		} else {
			ret = false;
		}
    }

    // validate MemorySwap
    auto MemorySwap = object->find("\"MemorySwap\"");
    if (MemorySwap != nullptr) {
        if (MemorySwap->jtype() == json_type::J_INT) {
			if (std::static_pointer_cast<json_int>(MemorySwap)->value() <= 0) {
				std::cerr << "MemorySwap isn't positive integer" << std::endl;
				ret = false;
			}
		} else if (MemorySwap->jtype() == json_type::J_NULL) {
			; // is valid
		} else {
			ret = false;
		}
    }

    // validate Memory
    auto Memory = object->find("\"Memory\"");
    if (Memory != nullptr) {
        if (Memory->jtype() == json_type::J_INT) {
			if (std::static_pointer_cast<json_int>(Memory)->value() <= 0) {
				std::cerr << "Memory isn't positive integer" << std::endl;
				ret = false;
			}
		} else if (Memory->jtype() == json_type::J_NULL) {
			; // is valid
		} else {
			ret = false;
		}
    }

    // validate User
    auto User = object->find("\"User\"");
    if (User != nullptr) {
        if (User->jtype() == json_type::J_STRING) {
            if (!is_user_group(User->to_string())) {
                std::cerr << "User isn't valid user_group string" << std::endl;
                ret = false;
            }
        } else if (User->jtype() == json_type::J_NULL) {
			; // is valid
		} else {
			ret = false;
		}
    }

    return ret;
}