Beispiel #1
0
static int
get_number (string s, int& pos) {
  int n= N(s);
  pos= n-1;
  while (pos > 0 && s[pos] != '-') pos--;
  if (pos > 0 && s[pos-1] == '-') pos--;
  return as_int (s (pos+1, n-1));
}
Beispiel #2
0
void
imlib2_image_size (url u, int& w, int& h) {
  if (imlib2_size_cache->contains (u->t)) {
    w= as_int (imlib2_size_cache[u->t][0]);
    h= as_int (imlib2_size_cache[u->t][1]);
  }
  else {
    Imlib_Image image= imlib2_load_image (u);
    if (image) {
      IMLIB2_context_set_image (image);
      w= IMLIB2_image_get_width ();
      h= IMLIB2_image_get_height ();
      IMLIB2_free_image ();
      imlib2_size_cache (u->t)= tuple (as_string (w), as_string (h));
    }
  }
}
Beispiel #3
0
static std::shared_ptr<MetadataStream::VideoSegment> parseVideoSegmentFromNode(JSONNode& segmentNode)
{
    auto segmentTitleIter = segmentNode.find(ATTR_SEGMENT_TITLE);
    auto segmentFPSIter = segmentNode.find(ATTR_SEGMENT_FPS);
    auto segmentTimeIter = segmentNode.find(ATTR_SEGMENT_TIME);
    auto segmentDurationIter = segmentNode.find(ATTR_SEGMENT_DURATION);
    auto segmentWidthIter = segmentNode.find(ATTR_SEGMENT_WIDTH);
    auto segmentHeightIter = segmentNode.find(ATTR_SEGMENT_HEIGHT);

    if(segmentTitleIter == segmentNode.end())
	VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has no title");
    if(segmentFPSIter == segmentNode.end())
	VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has no fps value");
    if(segmentTimeIter == segmentNode.end())
	VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has no time value");

    std::string title = segmentTitleIter->as_string();
    double fps = segmentFPSIter->as_float();
    long long timestamp = segmentTimeIter->as_int();

    if(title.empty())
	VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has invalid title");
    if(fps <= 0)
	VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has invalid fps value");
    if(timestamp < 0)
	VMF_EXCEPTION(vmf::InternalErrorException, "JSON element has invalid time value");

    std::shared_ptr<MetadataStream::VideoSegment> spSegment(new MetadataStream::VideoSegment(title, fps, timestamp));

    if(segmentDurationIter != segmentNode.end())
    {
	long long duration = segmentDurationIter->as_int();
	if(duration > 0)
	    spSegment->setDuration(duration);
    }
    if(segmentWidthIter != segmentNode.end() && segmentHeightIter != segmentNode.end())
    {
	long width = segmentWidthIter->as_int(), height = segmentHeightIter->as_int();
	if(width > 0 && height > 0)
	    spSegment->setResolution(width, height);
    }

    return spSegment;

}
static path
as_path (tree t) {
  ASSERT (is_tuple (t), "invalid path");
  path p;
  int i, n= N(t);
  for (i=n-1; i>=0; i--)
    p= path (as_int (t[i]), p);
  return p;
}
Beispiel #5
0
    void format_source(caValue* source, Term* term)
    {
        std::stringstream strm;
        if (term->stringProp("syntax:integerFormat", "dec") == "hex")
            strm << "0x" << std::hex;

        strm << as_int(term_value(term));
        append_phrase(source, strm.str(), term, tok_Integer);
    }
Beispiel #6
0
 static void readIntVector(const JSONNode& nd, int vecsz, int * x) {
     int k = 0;
     for (auto i = nd.begin(); i != nd.end(); i++) {
         int u = i->as_int();
         BNB_ASSERT(k < vecsz);
         x[k++] = u;
     }
     BNB_ASSERT(k == vecsz);
 }
Beispiel #7
0
void Term__asint(VM* vm)
{
    Term* t = as_term_ref(vm->input(0));
    if (t == NULL)
        return vm->throw_str("NULL term");
    if (!is_int(term_value(t)))
        return vm->throw_str("Not an int");
    set_int(vm->output(), as_int(term_value(t)));
}
Beispiel #8
0
static lcore_t *
new_lcore(const char *line, lcore_t *rest)
{
	lcore_t *lc = calloc(1, sizeof *lc);

	lc->next = rest;
	lc->u.s.id   = as_int(line);
	return lc;
}
Beispiel #9
0
int to_int(caValue* value)
{
    if (is_int(value))
        return as_int(value);
    else if (is_float(value))
        return (int) as_float(value);

    internal_error("In to_float, type is not an int or float");
    return 0;
}
Beispiel #10
0
static int
large_size (string s) {
  int pos= search_backwards ("-", N(s), s);
  if (pos > 6) {
    if (s[pos-1] == '-') pos--;
    string r= s (pos + 1, N(s) - 1);
    return as_int (r);
  }
  return 0;
}
Beispiel #11
0
int
galleryd::queue::query_category_id_(
    const std::string &category)
{
    auto stmt = db.prepare("SELECT id FROM category WHERE name=?");
    stmt.bind(1, category);

    auto row = stmt.begin();
    return row != stmt.end() ? row.as_int(0) : 0;
}
int main(void)
{

  char *str = "123";
  printf("%s\n", "Test");
  printf("%d\n", as_int("123"));

  
  return 0;
}
Beispiel #13
0
void
edit_main_rep::print_buffer (string first, string last) {
  // in Qt this is the main entry point to the printing subsystem.
  // the other routines (print_to_file, ...) are overriden since all fine tuning 
  // is made here via the Qt print dialog
  bool to_file, landscape;
  url name = url_none();
  string printer;
  string paper_type;
  if (qt_print (to_file, landscape, printer, name, first, last, paper_type)) {
      if (!to_file) name = url_temp (".ps");
      print (name, false, as_int (first), as_int (last));
      if (!to_file) {
        string cmd = printing_cmd * " -P" * printer;
        system (cmd, name);  
        ::remove (name);
      }
  }
}
variant variant::operator%(const variant& v) const
{
	const int numerator = as_int();
	const int denominator = v.as_int();
	if(denominator == 0) {
		throw type_error((formatter() << "divide by zero error").str());
	}

	return variant(numerator%denominator);
}
Beispiel #15
0
static int
get_ex (string family, string variant, string series, string shape,
	int attempt) {
  array<string> lfn= logical_font (family, variant, series, shape);
  array<string> pfn= search_font (lfn, attempt);
  array<string> chs= font_database_characteristics (pfn[0], pfn[1]);
  string ex= find_attribute_value (chs, "ex");
  if (ex == "") return 0;
  else return as_int (ex);
}
Beispiel #16
0
static bool
message_complete (string s) {
  int start= 0;
  int i, n= N(s);
  if (n>0 && s[0] == '!') start= 1;
  for (i=start; i<n; i++)
    if (s[i] == '\n') break;
  if (i == n) return false;
  return (n - (i+1)) >= as_int (s (start, i));
}
Beispiel #17
0
Keyframe::Keyframe(JSONNode node) {
  auto jt = node.find("t");
  if (jt == node.end()) {
    Logger::log(ERR, "Keyframe has no assigned time. Defaulting to 0.");
    t = 0;
  }
  else {
    t = jt->as_int();
  }

  auto v = node.find("val");
  if (v == node.end()) {
    val = nullptr;
  }
  else {
    val = shared_ptr<LumiverseType>(LumiverseTypeUtils::loadFromJSON(*v));
  }

  auto ucs = node.find("useCurrentState");
  if (ucs == node.end()) {
    useCurrentState = false;
  }
  else {
    useCurrentState = ucs->as_bool();
  }

  auto tid = node.find("timelineID");
  if (tid == node.end()) {
    timelineID = "";
  }
  else {
    timelineID = tid->as_string();
  }

  auto to = node.find("timelineOffset");
  if (to == node.end()) {
    timelineOffset = 0;
  }
  else {
    timelineOffset = to->as_int();
  }
}
void test_state_simple()
{
    Branch branch;
    Stack context;

    // Simple test, condition never changes
    Term* block = branch.compile("if true { state i = 0; i += 1 }");
    evaluate_branch(&context, &branch);

    caValue *i = context.state.getField("_if_block")->getIndex(0)->getField("i");
    test_assert(i != NULL);
    test_assert(as_int(i) == 1);
    evaluate_branch(&context, &branch);
    i = context.state.getField("_if_block")->getIndex(0)->getField("i");
    test_assert(as_int(i) == 2);
    evaluate_branch(&context, &branch);
    i = context.state.getField("_if_block")->getIndex(0)->getField("i");
    test_assert(as_int(i) == 3);

    // Same test with elif
    branch.clear();
    block = branch.compile("if false {} elif true { state i = 0; i += 1 }");
    evaluate_branch(&context, &branch);
    i = context.state.getField("_if_block")->getIndex(1)->getField("i");
    test_assert(as_int(i) == 1);
    evaluate_branch(&context, &branch);
    i = context.state.getField("_if_block")->getIndex(1)->getField("i");
    test_assert(as_int(i) == 2);
    evaluate_branch(&context, &branch);
    i = context.state.getField("_if_block")->getIndex(1)->getField("i");
    test_assert(as_int(i) == 3);

    // Same test with else
    branch.clear();

    Stack context2;
    block = branch.compile("if false {} else { state i = 0; i += 1 }");
    evaluate_branch(&context2, &branch);
    i = context2.state.getField("_if_block")->getIndex(1)->getField("i");
    test_assert(as_int(i) == 1);
    evaluate_branch(&context2, &branch);
    i = context2.state.getField("_if_block")->getIndex(1)->getField("i");
    test_assert(as_int(i) == 2);
    evaluate_branch(&context2, &branch);
    i = context2.state.getField("_if_block")->getIndex(1)->getField("i");
    test_assert(as_int(i) == 3);
}
Beispiel #19
0
void Term__has_input_property(VM* vm)
{
    Term* t = as_term_ref(vm->input(0));
    if (t == NULL)
        return vm->throw_str("NULL reference");

    int index = as_int(vm->input(1));
    Symbol key = as_symbol(vm->input(2));

    set_bool(vm->output(), term_get_input_property(t, index, key) == NULL);
}
void
bridge_argument_rep::my_typeset (int desired_status) {
  string name;
  tree   value;
  path   valip= decorate_right (ip);

  tree r= st[0];
  if (is_compound (r)) value= tree (ERROR, "arg");
  else {
    name = r->label;
    if ((!is_nil (env->macro_arg)) && env->macro_arg->item->contains (r->label)) {
      value= env->macro_arg->item [name];
      if (!is_func (value, BACKUP)) {
	path new_valip= env->macro_src->item [name];
	if (is_accessible (new_valip)) valip= new_valip;
      }
    }
    else value= compound ("src-unknown", name);
  }

  path prefix;
  if (N(st) > 1) {
    int i, n= N(st);
    for (i=1; i<n; i++) {
      tree r= env->exec (st[i]);
      if (!is_int (r)) {
        prefix= path ();
        value= tree (ERROR, "arg " * name);
        valip= decorate_right (ip);
        break;
      }
      int nr= as_int (r);
      if ((!is_compound (value)) || (nr<0) || (nr>=N(value))) {
        prefix= path ();
        value= tree (ERROR, "arg " * name);
        valip= decorate_right (ip);
        break;
      }
      value = value[nr];
      valip = descend (valip, nr);
      prefix= prefix * nr;
    }
  }
  initialize (name, prefix, attach_here (value, valip));

  ttt->insert_marker (body->st, ip);
  list<hashmap<string,tree> > old_var= env->macro_arg;
  list<hashmap<string,path> > old_src= env->macro_src;
  if (!is_nil (env->macro_arg)) env->macro_arg= env->macro_arg->next;
  if (!is_nil (env->macro_src)) env->macro_src= env->macro_src->next;
  body->typeset (desired_status);
  env->macro_arg= old_var;
  env->macro_src= old_src;
}
Beispiel #21
0
bool
load_tex_tfm (string family, int size, int dsize, tex_font_metric& tfm) {
  string var= "tfm:" * family * as_string (size);
  if (is_cached ("font_cache.scm", var))
    if (try_tfm (family, as_int (cache_get ("font_cache.scm", var)->label),
		 size, tfm, false))
      return true;
  if (get_setting ("MAKETFM") != "false")
    if (load_tex_tfm (family, size ,dsize, tfm, false))
      return true;
  return load_tex_tfm (family, size ,dsize, tfm, true);
}
tree
evaluate_lookup (tree t) {
  if (N(t)!=2) return evaluate_error ("bad look up");
  tree t1= evaluate (t[0]);
  tree t2= evaluate (t[1]);
  if (!(is_compound (t1) && is_int (t2)))
    return evaluate_error ("bad look up");
  int i= as_int (t2);
  if (i < 0 || i >= N(t1))
    return evaluate_error ("index out of range in look up");
  return t1[i];
}
tree
evaluate_range (tree t) {
  if (N(t)!=3) return evaluate_error ("bad range");
  tree t1= evaluate (t[0]);
  tree t2= evaluate (t[1]);
  tree t3= evaluate (t[2]);
  if (!(is_int (t2) && is_int (t3))) return evaluate_error ("bad range");
  if (is_compound (t1)) {
    if (is_tuple (t1)) {
      int i1= max (0, as_int (t2));
      int i2= min (N (t1), as_int (t3));
      i2 = max (i1, i2);
      return t1 (i1, i2);
    }
    return evaluate_error ("bad range");
  }
  int i1= max (0, as_int (t2));
  int i2= min (N(t1->label), as_int (t3));
  i2 = max (i1, i2);
  return t1->label (i1, i2);
}
Beispiel #24
0
		static void
		_handle_restore (player *pl, world *w, command_reader& reader)
		{
			if (!pl->has ("command.world.world.restore"))
    		{
    			pl->message (messages::not_allowed ());
    			return;
    		}
    	
    	if (!reader.has_next ())
    		{
    			pl->message ("§c * §7Usage§f: §e/world restore §cbackup-number");
    			return;
    		}
    	
    	int backup_num;
    	auto arg = reader.next ();
    	if (!arg.is_int () || ((backup_num = arg.as_int ()) <= 0))
    		{
    			pl->message ("§c * §7The backup number should a positive non-zero integer§c.");
    			return;
    		}
    	
    	std::string src = w->get_path ();
    	src.erase (0, 12); // remove "data/worlds/" part
    	src.insert (0, "data/backups/" + std::string (w->get_name ()) + "/");
    	std::ostringstream ss;
    	ss << src << "." << backup_num;
    	
    	FILE *f = std::fopen (ss.str ().c_str (), "rb");
    	if (!f)
    		{
    			pl->message ("§c * §7Backup does not exist§c.");
    			return;
    		}
    	std::fclose (f);
    	
    	_copy_file (w->get_path (), ss.str ().c_str ());
    	w->reload_world (w->get_name ());
    	w->get_players ().all (
				[] (player *pl)
					{
						std::cout << "W a" << std::endl;
						pl->rejoin_world ();
						std::cout << "W b" << std::endl;
						pl->message ("§bWorld reloaded");
					});
			
			ss.str (std::string ());
			ss << "§eRestored backup #§b" << backup_num;
			pl->message (ss.str ());
		}
Beispiel #25
0
static string
message_receive (string& s) {
  int start= 0;
  int i, n= N(s);
  if (n>0 && s[0] == '!') start= 1;
  for (i=start; i<n; i++)
    if (s[i] == '\n') break;
  if (i == n) return "";
  int l= as_int (s (start, i++));
  string r= s (i, i+l);
  s= s (i+l, n);
  return r;
}
void Term__asint(caStack* stack)
{
    Term* t = as_term_ref(circa_input(stack, 0));
    if (t == NULL) {
        circa_output_error(stack, "NULL reference");
        return;
    }
    if (!is_int(term_value(t))) {
        circa_output_error(stack, "Not an int");
        return;
    }
    set_int(circa_output(stack, 0), as_int(term_value(t)));
}
Beispiel #27
0
void variant::write_json(std::ostream& s) const
{
	switch(type_) {
	case TYPE_NULL: {
		s << "null";
		return;
	}
	case TYPE_INT: {
		s << as_int();
		return;
	}
	case TYPE_DECIMAL: {
		s << decimal_value_;
		return;
	}
	case TYPE_MAP: {
		s << "{";
		for(std::map<variant,variant>::const_iterator i = map_->elements.begin(); i != map_->elements.end(); ++i) {
			if(i != map_->elements.begin()) {
				s << ',';
			}
			s << '"' << i->first.as_string() << "\":";
			i->second.write_json(s);
		}

		s << "}";
		return;
	}
	case TYPE_LIST: {
		s << "[";

		for(std::vector<variant>::const_iterator i = list_->elements.begin();
		    i != list_->elements.end(); ++i) {
			if(i != list_->elements.begin()) {
				s << ',';
			}

			i->write_json(s);
		}

		s << "]";
		return;
	}
	case TYPE_STRING: {
		s << "\"" << string_->str << "\"";
		return;
	}
	default:
		throw type_error("illegal type to serialize to json");
	}
}
Beispiel #28
0
		static void
		_handle_regenerate (player *pl, world *w, command_reader& reader)
		{
			if (!pl->has ("command.world.world.regenerate"))
    		{
    			pl->message (messages::not_allowed ());
    			return;
    		}
    	
			if (reader.arg_count () < 2 || reader.arg_count () > 3)
				{
					pl->message ("§c * §7Usage§f: §e/world regenerate §cgenerator §8[§cseed§8]");
					return;
				}
			
			const std::string gen_name = reader.next ().as_str ();
			long long gen_seed = w->get_generator ()->seed ();
			if (reader.has_next ())
				{
					auto arg = reader.next ();
					if (!arg.is_int ())
						{
							pl->message ("§c * §7The seed must be an integer§c.");
							return;
						}
					
					gen_seed = arg.as_int ();
				}
			
			world_generator *gen = world_generator::create (gen_name.c_str (), gen_seed);
			if (!gen)
				{
					pl->message ("§c * §7No such world generator§f: §c" + gen_name);
					return;
				}

			w->set_generator (gen);
			w->clear_chunks (false, true);
			w->get_players ().all (
				[] (player *pl)
					{
						pl->rejoin_world ();
						pl->message ("§bWorld reloaded");
					});
			
			/*
			std::ostringstream ss;
			ss << w->get_colored_name () << " §eregenerated §f[§eseed§f: §7" << gen_seed << "§f]";
			pl->message (ss.str ());
			*/
		}
Beispiel #29
0
/**
	Get a value as bool or int

	\param key key
	\param value value to read
	\param is_bool 1 is value is a boolean, otherwise 0
	\returns value
*/
static int as_bool_or_int(const char *key, const char *value, int *is_bool)
{
	*is_bool = 1;
	if (!value)
		return 1;
	if (!*value)
		return 0;
	if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
		return 1;
	if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
		return 0;
	*is_bool = 0;
	return as_int(key, value);
}
void test_import_c()
{
    Branch branch;

    Term* func = import_function(&branch, my_imported_function,
            "my_imported_func(int,int) -> int");

    test_equals(function_get_output_type(func, 0)->name, "int");

    Term* result = branch.compile("my_imported_func(4,5)");
    evaluate_branch(&branch);

    test_assert(as_int(result) == 9);
}