示例#1
0
文件: file.cpp 项目: ShenTensen/circa
void read_text_file(const char* filename, Value* contentsOut)
{
    if (!file_exists(filename)) {
        set_null(contentsOut);
        return;
    }

    FILE* fp = fopen(filename, "r");

    if (fp == NULL) {
        // Unlikely to reach here since we just checked file_exists. But possible.
        set_null(contentsOut);
        return;
    }

    // Get file size
    fseek(fp, 0, SEEK_END);
    size_t file_size = ftell(fp);
    rewind(fp);

    // Read raw data.
    touch(contentsOut);
    set_blob(contentsOut, int(file_size));

    size_t bytesRead = fread(as_blob(contentsOut), 1, file_size, fp);
    
    if (bytesRead < file_size)
        string_resize(contentsOut, (int) bytesRead);

    fclose(fp);
}
示例#2
0
void test_user_defined_type()
{
    Type* type = create_type();
    handle_t::setup_type<MyType>(type);

    // Create a value and then free it.
    test_assert(MyType_allocated == 0);

    caValue value;
    handle_t::set(&value, type, new MyType());
    test_assert(MyType_allocated == 1);

    set_null(&value);
    test_assert(MyType_allocated == 0);

    // Overwrite a value with a new value
    handle_t::set(&value, type, new MyType());
    test_assert(MyType_allocated == 1);
    handle_t::set(&value, type, new MyType());
    test_assert(MyType_allocated == 1);
    handle_t::set(&value, type, new MyType());
    test_assert(MyType_allocated == 1);
    set_null(&value);
    test_assert(MyType_allocated == 0);
};
示例#3
0
void test_simple()
{
    Branch branch;
    setup(branch);

    test_equals(&g_slots, "[false, false, false]");

    caValue handle;

    assign(&handle, 0);
    test_equals(&g_slots, "[true, false, false]");

    set_null(&handle);

    test_equals(&g_slots, "[false, false, false]");

    assign(&handle, 0);
    test_equals(&g_slots, "[true, false, false]");
    assign(&handle, 1);
    test_equals(&g_slots, "[false, true, false]");
    assign(&handle, 2);
    test_equals(&g_slots, "[false, false, true]");

    caValue handle2;
    caValue handle3;
    assign(&handle2, 0);
    test_equals(&g_slots, "[true, false, true]");
    assign(&handle3, 1);
    test_equals(&g_slots, "[true, true, true]");
    set_null(&handle2);
    test_equals(&g_slots, "[false, true, true]");
    set_null(&handle);
    test_equals(&g_slots, "[false, true, false]");
}
示例#4
0
 ReweightOptions() {
   head_normalize = fsm_normalize = false;
   set_null(set);
   set_null(random_add);
   set_null(add);
   set_null(scale);
   clearFeatures = weightsAdd = false;
 }
示例#5
0
文件: symbols.cpp 项目: arn-e/circa
void symbol_deinitialize_global_table()
{
    set_null(g_runtimeSymbolMap);
    delete g_runtimeSymbolMap;
    g_runtimeSymbolMap = NULL;
    set_null(g_runtimeSymbolTable);
    delete g_runtimeSymbolTable;
    g_runtimeSymbolTable = NULL;
}
示例#6
0
void test_release()
{
    Block block;
    block.compile("type T = handle_type()");
    block.compile("def T.release(self)");

    install_function(&block, "T.release", my_release_func);

    Type* T = find_type(&block, "T");
    test_assert(T != NULL);
    test_assert(find_method(NULL, T, "release") != NULL);

    gTimesReleaseCalled = 0;

    Value value;
    make(T, &value);
    test_assert(is_handle(&value));
    test_equals(gTimesReleaseCalled, 0);

    set_int(handle_get_value(&value), 5);
    set_null(&value);

    test_equals(gTimesReleaseCalled, 1);

    gTimesReleaseCalled = 0;

    Value value2;
    make(T, &value);
    copy(&value, &value2);

    test_assert(gTimesReleaseCalled == 0);

    set_null(&value2);
    test_assert(gTimesReleaseCalled == 0);

    set_null(&value);
    test_assert(gTimesReleaseCalled == 1);

    gTimesReleaseCalled = 0;
    make(T, &value);
    make(T, &value2);

    set_null(&value);
    test_assert(gTimesReleaseCalled == 1);

    set_null(&value2);
    test_assert(gTimesReleaseCalled == 2);
}
示例#7
0
void move(caValue* source, caValue* dest)
{
    set_null(dest);
    dest->value_type = source->value_type;
    dest->value_data = source->value_data;
    initialize_null(source);
}
示例#8
0
void erase_term(Term* term)
{
    pre_erase_term(term);

    set_null(term_value(term));
    set_inputs(term, TermList());
    change_function(term, NULL);
    term->type = NULL;
    remove_nested_contents(term);

    // for each user, clear that user's input list of this term
    remove_from_any_user_lists(term);
    clear_from_dependencies_of_users(term);

    if (term->owningBlock != NULL) {
        // remove name binding if necessary
        term->owningBlock->removeNameBinding(term);

        // index may be invalid if something bad has happened
        ca_assert(term->index < term->owningBlock->length());
        term->owningBlock->_terms.setAt(term->index, NULL);

        term->owningBlock = NULL;
        term->index = -1;
    }

    dealloc_term(term);
}
示例#9
0
int		init_SDL(t_graph *graph)
{
  graph->pos = (HEIGHT / 2) - (HEIGHT / 4);
  graph->poscurs = 0;
  set_null(graph);
  SDL_WM_SetCaption(NAME, NULL);
  graph->charac_num = 0;
  if (TTF_Init() == -1)
    return (-1);
  init_font(graph);
  if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
    return (-1);
  graph->screen = SDL_SetVideoMode(WIDTH, HEIGHT, BPP,
				   SDL_SWSURFACE | SDL_FULLSCREEN);
  if (graph->screen == NULL)
    return (-1);
  graph->quit = 0;
  set_character(graph);
  init_spaceship(graph);
  graph->background = load_image("img/space.bmp");
  graph->spacefactor = 1;
  init_joystick(graph);
  intro_music(graph);
  return (0);
}
示例#10
0
void get_relative_name_as_list(Term* term, Block* relativeTo, Value* nameOutput)
{
    set_list(nameOutput, 0);

    // Walk upwards and build the name, stop when we reach relativeTo.
    // The output list will be reversed but we'll fix that.

    while (true) {
        set_value(list_append(nameOutput), unique_name(term));

        if (term->owningBlock == relativeTo) {
            break;
        }

        term = parent_term(term);

        // If term is null, then it wasn't really a child of relativeTo
        if (term == NULL) {
            set_null(nameOutput);
            return;
        }
    }

    // Fix output list
    list_reverse(nameOutput);
}
示例#11
0
struct jstruct_result
_jstruct_import(struct json_object *obj, const void *data,
        const struct jstruct_object_property *properties, struct json_object *errors) {
    _init_importers();
    if (errors != NULL && json_object_get_type(errors) != json_type_array) {
        return jstruct_error_new(jstruct_error_errors_not_array_or_null, NULL, json_object_get_type(errors));
    }
    const struct jstruct_object_property *property;
    struct json_object *prop;
    struct jstruct_result result = JSTRUCT_OK;
    result.allocated = array_list_new(jstruct_allocated_free);
    for (property = properties; property->name; ++property) {
        void *ptr = jstruct_prop_ptr(data, property, JSTRUCT_PROP_PTR_GET_NO_DEREF);
        struct jstruct_result err;
        if (json_object_object_get_ex(obj, property->name, &prop)) {
            if (json_object_get_type(prop) != property->type.json) {
                err = jstruct_error_new(jstruct_error_incorrect_type, property->name, json_object_get_type(prop));
            } else {
                jstruct_import_importer import = importers[json_type_index(property->type.json)];
                err = import(prop, data, ptr, property);
            }
        } else {
            if (!set_null(ptr, property)) {
                err = jstruct_error_array_add(errors, jstruct_error_not_nullable, property->name, 0);
            }
        }
        jstruct_error_consume(&result, &err, errors, property->name, -1);
    }
    if (result.allocated->length == 0) {
        array_list_free(result.allocated);
        result.allocated = NULL;
    }
    return result;
}
示例#12
0
void file_source_read_file(Value* file_source, Value* name, Value* contents)
{
    if (file_source_is_map(file_source)) {
        Value* entry = hashtable_get(file_source, name);
        if (entry == NULL)
            set_null(contents);
        else
            copy(list_get(entry, 1), contents);
        return;
    }

    else if (file_source_is_filesystem_backed(file_source)) {
        Value fullPath;
        Value* rootDir = list_get(file_source, 1);
        copy(rootDir, &fullPath);
        join_path(&fullPath, name);
        read_text_file(as_cstring(&fullPath), contents);
        return;
    }

    else if (file_source_is_tarball_backed(file_source)) {
        Value* tarball = list_get(file_source, 1);
        tar_read_file(tarball, as_cstring(name), contents);
        return;
    }
    internal_error("file_source_read_file: file_source type not recognized");
}
示例#13
0
文件: oid.cpp 项目: asir6/Colt
//=============[Oid::Oid( const char *dotted_string ]=====================
// constructor using a dotted string
//
// do a string to oid using the string passed in
Oid::Oid( const char * dotted_oid_string, size_t size)
{
  // can't init enum SmiValue so just memset it clean
  set_null();

  size_t z;
  if ((z = ACE_OS::strlen(dotted_oid_string)) == 0) {
    set_invalid();
    return;
  }

  if (size == (unsigned int)-1)
    size = z;
  if (size > z)
    size = z;

  char *ptr = (char *)dotted_oid_string;;
  if (size < z) {
    // create new buffer if needed
    ACE_NEW(ptr, char [size]);

    // sz should be in StrToOid?
    ACE_OS::memcpy( (void *)ptr, dotted_oid_string, size);
  }

  size_t byte_counter;
  if (StrToOid( (char *) ptr, &smival.value.oid, byte_counter) < 0)
    set_invalid();
  if (ptr != dotted_oid_string)
    delete [] ptr;
}
示例#14
0
文件: rtree.c 项目: jbmikk/dstlib
static void _compact_nodes(Node *node)
{
	if (bmap_node_count(&node->children) != 1 || node->data) {
		return;
	}

	//TODO: Add bmap_get_first
	BMapEntryNode *child = (BMapEntryNode *)node->children.bmap.entries;

	//Join arrays
	int joined_size = node->size + 1 + child->node.size;
	int end = node->size;

	_node_set_array(node, joined_size);

	node->array[end++] = child->key;

	memcpy(node->array+end, child->node.array, child->node.size);

	//Replace child
	if(child->node.array) {
		free(child->node.array);
		set_null(child->node.array);
	}

	Node cont = child->node;

	bmap_node_delete(&node->children, child->key);

	trace("new size: %i", joined_size);

	_node_init(node, cont.children, cont.data);
}
示例#15
0
void after_each_test()
{
    if (gTempValue != NULL)
        set_null(gTempValue);

    caWorld* world = global_world();
    world_clear_file_sources(world);
}
示例#16
0
文件: handle.cpp 项目: arn-e/circa
void handle_copy(Type* type, caValue* source, caValue* dest)
{
    set_null(dest);

    as_handle(source)->refcount++;
    dest->value_type = source->value_type;
    dest->value_data.ptr = source->value_data.ptr;
}
示例#17
0
文件: kernel.cpp 项目: whunmr/circa
void test_oracle(caStack* stack)
{
    if (list_length(g_oracleValues) == 0)
        set_null(circa_output(stack, 0));
    else {
        copy(list_get(g_oracleValues, 0), circa_output(stack, 0));
        list_remove_index(g_oracleValues, 0);
    }
}
示例#18
0
文件: kernel.cpp 项目: whunmr/circa
void Type__property(caStack* stack)
{
    Type* type = as_type(circa_input(stack, 0));
    const char* str = as_cstring(circa_input(stack, 1));
    caValue* prop = get_type_property(type, str);
    if (prop == NULL)
        set_null(circa_output(stack, 0));
    else
        copy(prop, circa_output(stack, 0));
}
示例#19
0
void reset(caValue* value)
{
    // Check for NULL. Most caValue functions don't do this, but reset() is
    // a convenient special case.
    if (value->value_type == NULL)
        return set_null(value);

    Type* type = value->value_type;

    // Check if the reset() function is defined
    if (type->reset != NULL) {
        type->reset(type, value);
        return;
    }

    // Default behavior: assign this value to null and create a new one.
    set_null(value);
    make(type, value);
}
示例#20
0
void string_copy(Type* type, caValue* source, caValue* dest)
{
    set_null(dest);
    StringData* data = (StringData*) source->value_data.ptr;
    if (data != NULL)
        incref(data);
    dest->value_type = source->value_type;
    dest->value_data.ptr = data;

    INCREMENT_STAT(StringSoftCopy);
}
示例#21
0
void free_list(ListData* data)
{
    if (data == NULL)
        return;

    // Release all elements
    for (int i=0; i < data->count; i++)
        set_null(&data->items[i]);
    free(data);
    debug_unregister_valid_object(data, LIST_OBJECT);
}
示例#22
0
ListData* list_remove_index(ListData* original, int index)
{
    ca_assert(index < original->count);
    ListData* result = list_touch(original);

    for (int i=index; i < result->count - 1; i++)
        swap(&result->items[i], &result->items[i+1]);
    set_null(&result->items[result->count - 1]);
    result->count--;
    return result;
}
示例#23
0
void if_block_turn_common_rebinds_into_outputs(Term* ifCall)
{
    // Find names which are bound in every branch (and not already outputs)
    Branch* contents = nested_contents(ifCall);

    bool firstBranch = true;
    List names;

    for (CaseIterator it(contents); it.unfinished(); it.advance()) {
        Branch* caseBranch = nested_contents(it.current());

        if (firstBranch) {
            firstBranch = false;
            write_all_names_to_list(caseBranch, &names);

            // remove names that are already outputs
            for (int i=0; i < names.length(); i++) {
                Term* existing = contents->get(as_cstring(names[i]));
                if (existing != NULL && existing->function == FUNCS.output)
                    set_null(names[i]);
            }

            continue;
        }

        // search through 'names' and remove any not in this branch.
        for (int i=0; i < names.length(); i++) {
            if (is_null(names[i]))
                continue;
            if (caseBranch->get(as_cstring(names[i])) == NULL)
                set_null(names[i]);
        }
    }

    names.removeNulls();
    // std::cout << names.toString() << std::endl;

    for (int i=0; i < names.length(); i++) {
        if_block_add_output_for_name(ifCall, as_cstring(names[i]));
    }
}
示例#24
0
void fakefs_read_file(const char* filename, caValue* contentsOut)
{
    std::map<std::string, std::string>::const_iterator it =
        g_fileData.find(filename);

    if (it == g_fileData.end()) {
        set_null(contentsOut);
        return;
    }

    set_string(contentsOut, it->second.c_str());
}
示例#25
0
void list_remove_and_replace_with_last_element(ListData** data, int index)
{
    *data = list_touch(*data);
    ca_assert(index < (*data)->count);

    set_null(&(*data)->items[index]);

    int lastElement = (*data)->count - 1;
    if (index < lastElement)
        swap(&(*data)->items[index], &(*data)->items[lastElement]);

    (*data)->count--;
}
示例#26
0
// Unpack a state value. Input 1 is the "identifying term" which is used as a key.
void unpack_state(caStack* stack)
{
    caValue* container = circa_input(stack, 0);
    Term* identifyingTerm = (Term*) circa_caller_input_term(stack, 1);

    caValue* element = get_field(container, unique_name(identifyingTerm));

    if (element == NULL) {
        set_null(circa_output(stack, 0));
    } else {
        copy(element, circa_output(stack, 0));
    }
}
示例#27
0
文件: void.cpp 项目: levelplane/circa
    void cast(CastResult* result, caValue* source, Type* type,
        caValue* dest, bool checkOnly)
    {
        if (!is_null(source)) {
            result->success = false;
            return;
        }

        if (checkOnly)
            return;

        set_null(dest);
    }
示例#28
0
void make(Type* type, caValue* value)
{
    INCREMENT_STAT(ValueCreates);

    set_null(value);

    value->value_type = type;

    if (type->initialize != NULL)
        type->initialize(type, value);

    type->inUse = true;
}
示例#29
0
void Unit::RemoveFromSystem()
{
    for (unsigned int locind = 0; locind < NUM_COLLIDE_MAPS; ++locind)
        if ( !is_null( this->location[locind] ) ) {
            if (activeStarSystem == NULL) {
                printf( "NONFATAL NULL activeStarSystem detected...please fix\n" );
                activeStarSystem = _Universe->activeStarSystem();
            }
            static bool collidemap_sanity_check =
                XMLSupport::parse_bool( vs_config->getVariable( "physics", "collidemap_sanity_check", "false" ) );
            if (collidemap_sanity_check) {
                if (0) {
                    CollideMap::iterator i;
                    CollideMap::iterator j = activeStarSystem->collidemap[locind]->begin();

                    bool found = false;
                    for (i = activeStarSystem->collidemap[locind]->begin();
                         i != activeStarSystem->collidemap[locind]->end(); ++i) {
                        if (i == this->location[locind]) {
                            printf( "hussah %d\n", *i == *this->location[locind] );
                            found = true;
                        }
                        if (**i < **j) {
                            printf( "(%f %f %f) and (%f %f %f) %f < %f %d!!!",
                                   (**i).GetPosition().i,
                                   (**i).GetPosition().j,
                                   (**i).GetPosition().k,
                                   (**j).GetPosition().i,
                                   (**j).GetPosition().j,
                                   (**j).GetPosition().k,
                                   (**i).GetPosition().MagnitudeSquared(),
                                   (**j).GetPosition().MagnitudeSquared(),
                                   (**i).GetPosition().MagnitudeSquared()
                                   < (**j).GetPosition().MagnitudeSquared() );
                        }
                        j = i;
                    }
                    printf( "fin %d %d ", *(int*) &i, found );
                    activeStarSystem->collidemap[locind]->checkSet();
                    assert( 0 );
                }
            }
            activeStarSystem->collidemap[locind]->erase( this->location[locind] );
            set_null( this->location[locind] );
        }
    for (int j = 0; j < GetNumMounts(); ++j)
        if (mounts[j].type->type == weapon_info::BEAM)
            if (mounts[j].ref.gun)
                mounts[j].ref.gun->RemoveFromSystem( true );
    activeStarSystem = NULL;
}
示例#30
0
void Term__property(VM* vm)
{
    Term* t = as_term_ref(vm->input(0));
    if (t == NULL)
        return vm->throw_str("NULL reference");

    Symbol key = as_symbol(vm->input(1));
    Value* value = term_get_property(t, key);

    if (value == NULL)
        set_null(vm->output());
    else
        circa::copy(value, vm->output());
}