Example #1
0
void ExprEvaluator::popOperator() {
  if(isBinary(operators.back())){
    double t1 = pop(operands);
    double t0 = pop(operands);
    operands.push_back(mkNode(pop(operators), t0, t1));
  } else
    operands.push_back(mkNode(pop(operators), pop(operands))); 
}
Example #2
0
int main() {

    int n;
    NodePtr np, head = NULL, prev;
    
    /* Make a list */
    printf("Enter numbers, and 0 to stop.\n");
    if (scanf("%d", &n) != 1) n = 0;
    while (n != 0) {
        np = mkNode(n);
        if (head == NULL) head = np;
        else prev->next = np;
        prev = np;
        if (scanf("%d", &n) != 1) n = 0;
    }

    /* Display list */
    printList(head);

    /* Free list */
    freeList(head);
    printf("\n");
    return 0;

}
Example #3
0
void Snake::tick()
{
    QPoint newCoords = this->m_nodes[SnakeBody].last()->pos
            + orientationPoint(this->m_nodes[SnakeBody].last()->orientation);


    // edge teleportation:
    if (newCoords.x() >= this->size().width())
        newCoords.setX(0);
    if (newCoords.y() >= this->size().height())
        newCoords.setY(0);
    if (newCoords.x() < 0)
        newCoords.setX(this->size().width() - 1);
    if (newCoords.y() < 0)
        newCoords.setY(this->size().height() - 1);

    bool snakeGrowFlag = false;
    if (!this->checkGameOver(newCoords)) {
        // now move to the new coords

        // check if there's an apple at the new position
        if (this->gridLookup.contains(newCoords) &&
                this->gridLookup.value(newCoords)->type == Apple) {
            // delete the apple and make a new one
            this->delNode(newCoords);
            this->newApple(newCoords);
            // not removing a node from the tail (growing)
            snakeGrowFlag = true;
        } else {
            // remove a node at the "tail" only if not growing
            this->delNode(SnakeBody, 0);
            // the new first node now becomes the tail
            this->m_nodes[SnakeBody].first()->attr = AttrSnakeTail;
        }

        // the old head now becomes part of the torso
        if (this->m_nodes[SnakeBody].last()->attr == AttrSnakeHeadFat) {
            this->m_nodes[SnakeBody].last()->attr = AttrSnakeTorsoFat;
        } else {
            this->m_nodes[SnakeBody].last()->attr = AttrSnakeTorso;
        }

        // add a node at the "head"
        this->addNode(mkNode(newCoords, SnakeBody
            , this->m_nodes[SnakeBody].last()->orientation
            , (snakeGrowFlag ? AttrSnakeHeadFat : AttrSnakeHead), BendNone));
        this->lastOrientation = this->m_nodes[SnakeBody].last()->orientation;

        emit refreshNodes();
        if (snakeGrowFlag) {    // fixme, I hate flags
            emit snakeLengthChanged();
        }
    } else {
        // game over
        this->god->stop();
        this->setState(Over);
        emit refreshNodes();
    }
}
Example #4
0
Heap mkHeap(int size){
	Heap new_heap = malloc(sizeof(struct heap_s));
	assert(new_heap != NULL);
	new_heap->size = size;
	new_heap->unsolved = size;
	new_heap->array = malloc(size*sizeof(Node));
	assert(new_heap->array != NULL);
	int i;
	for (i=0; i < size; i++)
		new_heap->array[i] = mkNode(i,i);
	return new_heap;
}
Example #5
0
File: dfs.c Project: bwhite/dfs
int dfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
    pthread_mutex_lock(&treeMut);
    path = narrow_path(path);
    if (remove_create) {
	pthread_mutex_unlock(&treeMut);
	return -EACCES;
    }
    dfs_out("CREATE: '%s'\n", path);

    DfsFile	*f = findFile((char *)path);
    DfsFile	*dir;
    char	*dname, *fname;

    if (f) {
	pthread_mutex_unlock(&treeMut);
	return -EEXIST;
    }

    if (!(fname = strrchr(path, '/'))) {
	pthread_mutex_unlock(&treeMut);
	return -EINVAL;
    }

    dname = strdup(path);
    dname[fname - path] = 0;
    fname++;

    if (!(dir = findFile(dname))) {
	free(dname);
	pthread_mutex_unlock(&treeMut);
	return -EINVAL;
    }

    f = mkNode(path, fname, dir, DEF_FILE_MODE);

    dfs_out("CREATE OUT, now %d children in '%s'\n", dir->num_children, dname);

    free(dname);

    f->version++;

    logFileVersion(f);
    pthread_mutex_unlock(&treeMut);
    return 0;
}
Example #6
0
File: dfs.c Project: bwhite/dfs
int _dfs_mkdir(const char *path, mode_t mode)
{
    /* Assumes treeMut is locked */
    //S_IFDIR
    DfsFile	*dir;
    char	*dname = strdup(path), *fname;

    dfs_out("MKDIR: '%s', %o\n", path, mode);

    if (dname[strlen(dname) - 1] == '/') 
	dname[strlen(dname) - 1] = 0;

    if (!dname[0]) {
	free(dname);
	return -EEXIST;
    }

    if (dir = findFile((char *)dname)) {
	free(dname);
	return -EEXIST;
    }
	
    fname = strrchr(dname, '/');
    dfs_out("STRRCHR '%s', '%s'\n", dname, fname ? fname : "''");
    if (!fname) {
	free(dname);
	return -EINVAL;
    }

    *fname++ = 0;
    dfs_out("dname %x ('%s'), fname %x ('%s')\n", dname, dname, fname, fname);

    if (!(dir = findFile((char *)dname))) {
	free(dname);
	return -EINVAL;
    }
	
    dfs_out("MKDIR2: '%s', '%s'\n", dname, fname);

    mkNode(path, fname, dir, S_IFDIR | mode);

    free(dname);

    return 0;
}
Example #7
0
File: dfs.c Project: bwhite/dfs
// called to reply records from logs returned from server
void playLog(char *buf, int len)
{
    /* Assumes treeMut and replyLogserverMut are locked */
    /* Find first ID of the new entry, go through whole log
       and truncate if that ID or greater is found, then append
       the new log.  It is assumed that the server has successfully
       managed any conflicts and any of that is taken care of. */
    // If we aren't forcing, check if this entry is in the log
    char *data = opLog.data;
    char *end = opLog.data + opLog.used;
    int first_version = ((LogHdr *)buf)->id;
    /* If we already have this version then quit.
       We only check the first record as if we asked
       for the log we have an empty log.  If we get
       pushed then we recieve one log entry and we just
       need to check if we have already committed it.
     */
    if (pending_change_id == first_version) {
      dfs_out("PlayLog: Server sent us what we are waiting to commit!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
      return;
    }
    while (data != NULL && data < end) {
	int version = ((LogHdr *)data)->id;
	if (version == first_version) {
	    dfs_out("PlayLog: Already have element in log\n");
	    return;
	}
	data += ((LogHdr *)data)->len;
    }
    dfs_out("PlayLog: Adding new element\n");
    checkLogSpace(len);
    data = opLog.data;
    dfs_out("PlayLog: Adding new element[%p][%d][%d]\n", opLog.data, opLog.used, opLog.alloced);
    memcpy(data + opLog.used, buf, len);
    opLog.used += len;
    end = opLog.data + opLog.used;
    destroy_tree();
    root = mkNode("", "", NULL, DEF_DIR_MODE);
    int last_id = 1;
    while (data < end) {
	switch ( ((LogHdr *)data)->type ) {
	case LOG_FILE_VERSION:
	    {
		LogFileVersion	*fv = (LogFileVersion *)data;
		char		*recipes = (char *)(fv + 1);
		char		*path = recipes + fv->recipelen;
		DfsFile	*f;
		dfs_out("Push[%s] id[%d]\n", path, fv->hdr.id);
		f = findFile((char *)path);
		if (f == NULL) {
		    DfsFile *dir;
		    char *dname, *fname;
		    fname = strrchr(path, '/');
		    dname = strdup(path);
		    dname[fname - path] = 0;
		    fname++;
		    dir = findFile(dname);
		    f = mkNode(path, fname, dir, DEF_FILE_MODE);
		    free(dname);
		}
		// Update mtime
		f->stat.st_mtime = fv->mtime;
		// Update version
		f->version = fv->hdr.version;
		// Update recipelen
		f->recipelen = fv->recipelen;
		// Update mode
		f->stat.st_mode = fv->flags;
		// Update length
		f->stat.st_size = f->len = fv->flen;
		// Update recipe
		free(f->recipe);
		f->recipe = malloc(fv->recipelen);
		memcpy(f->recipe, recipes, fv->recipelen);
		dfs_out("Push[%s] id[%d] version[%d] rlength[%d] recipe[%s] len[%d] dirty[%d]\n", path, fv->hdr.id, f->version, f->recipelen, recipes, f->len, f->dirty);
	    }
	    break;
	case LOG_UNLINK:
	    {
		char *path = (char *)((LogOther *)data + 1);
		_dfs_unlink(path);
	    }
	    break;
	case LOG_MKDIR:
	    {
		char *path = (char *)((LogOther *)data + 1);
		long mode = ((LogOther *)data)->flags;
		_dfs_mkdir(path, mode);
	    }
	    break;
	case LOG_RMDIR:
	    {
		char *path = (char *)((LogOther *)data + 1);
		_dfs_rmdir(path);
	    }
	    break;
	case LOG_CHMOD:
	    {
		char *path = (char *)((LogOther *)data + 1);
		long mode = ((LogOther *)data)->flags;
		_dfs_chmod(path, mode);
	    }
	    break;
	default:
	    printf("BAD RECORD\n");
	    exit(1);
	}
	last_id = ((LogHdr *)data)->id;
	data += ((LogHdr *)data)->len;
    }
    dfs_out("Last ID[%d]\n", last_id);
    opLog.id = last_id;
}
Example #8
0
File: dfs.c Project: bwhite/dfs
static void * dfs_init(struct fuse_conn_info *conn)
{
    pthread_mutex_lock(&treeMut);
    dfs_out("INIT!\n");
    root = mkNode("", "", NULL, DEF_DIR_MODE);

    comm_register_msgtypes(sizeof(messages) / sizeof(messages[0]), messages);

    if (!(extentSock = comm_client_socket(xname, xport))) 
	dfs_die("NO setup client socket to extent server at %d on '%s'\n", xport, xname);

    if (sname && ((opLog.net_fd = comm_client_socket(sname, sport)) > 0)) {
	// Here is where we auth
        assert(chit);
        // Send the server an OHAI
	Msg *reply = comm_send_and_reply(0, opLog.net_fd, DFS_OHAI_SERVER, NULL);
	char *reply_data = malloc(reply->len + 1);
	memcpy(reply_data, reply->data, reply->len);
	reply_data[reply->len] = '\0';
	char server_nonce = reply_data[0];
	char *server_pk = reply_data + 1;
	printf("Server gave us a nonce [%d] and a pk[%s].  He liked our picture!\n", (int) server_nonce, server_pk);
	// TODO Verify PK
	// Make an AES session key
	cry_create_nonce(16, session_key);
	printf("Sym Key[%x%x%x%x]\n", *(session_key), *(session_key + 4), *(session_key + 8), *(session_key + 12));
	// Encrypt session key with PK
	char *session_key_encrypted;
	int session_key_encrypted_sz;
	cry_asym_encrypt(&session_key_encrypted, &session_key_encrypted_sz, session_key, 16, server_pk);
	cry_sym_init(session_key);
	int out_chit_len = strlen(chit) + 3;
	char *out_chit = malloc(out_chit_len);
	char client_nonce;
	cry_create_nonce(1, &client_nonce);
	printf("client nonce[%d]\n", (int)client_nonce);
	out_chit[0] = client_nonce;
	out_chit[1] = server_nonce;
	strcpy(out_chit + 2, chit);
	out_chit[out_chit_len - 1] = '\0';
	char *out_chit_encrypted;
	int out_chit_encrypted_sz;
	cry_sym_encrypt(&out_chit_encrypted, &out_chit_encrypted_sz, out_chit, out_chit_len);
	reply = comm_send_and_reply(0, opLog.net_fd, DFS_TAKE_CHIT_SERVER, session_key_encrypted,
				    session_key_encrypted_sz, out_chit_encrypted, out_chit_encrypted_sz, NULL);
	char *out_nonce;
	int out_nonce_sz;
	cry_sym_init(session_key);
	cry_sym_decrypt(&out_nonce, &out_nonce_sz, reply->data, reply->len);
	printf("Nonce check[%d][%d]\n", (int)client_nonce, (int)(*out_nonce));
	assert(*out_nonce == (char)(client_nonce + 1));
	free(reply);

	// create a new thread reading this socket
	pthread_t		tid;
	pthread_create(&tid, NULL, listener, &opLog.net_fd);

	// grab current FS from server
	reply = comm_send_and_reply_mutex(1, &replyLogserverMut, &replyLogserverCond, opLog.net_fd, DFS_MSG_GET_LOG, NULL);
	char *log;
	size_t log_sz;
	assert(tuple_unserialize_log(&log, &log_sz, reply->data, reply->len) == 0);
	if (reply) {
	    if (reply->len) {
		playLog(log, log_sz);
		opLog.served = log_sz;
	    }
	    free(reply);
	}
	free(log);
    } else {
      dfs_die("NO setup client socket to log server at %d on '%s'\n", sport, sname);
    }
    pthread_mutex_unlock(&treeMut);
}
Example #9
0
void push(struct stack *s, void *elem){
  assert(s != NULL);
  struct node *newTop = mkNode(elem, s->top);
  s->top = newTop;
}