baseline

yet another open-source distributed versioning control system
Log | Files | Refs

objects.c (2362B)


      1 /*
      2  * Copyright (c) 2014 Mohamed Aslan <maslan@sce.carleton.ca>
      3  *
      4  * Permission to use, copy, modify, and distribute this software for any
      5  * purpose with or without fee is hereby granted, provided that the above
      6  * copyright notice and this permission notice appear in all copies.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 #include <stdio.h>
     18 #include <stdlib.h>
     19 
     20 #include "objects.h"
     21 
     22 struct file*
     23 baseline_file_new()
     24 {
     25 	struct file *file = (struct file *)malloc(sizeof(struct file));
     26 	file->id = NULL;
     27 	return file;
     28 }
     29 
     30 void
     31 baseline_file_free(struct file *file)
     32 {
     33 	if (file == NULL)
     34 		return;
     35 	free(file->id);
     36 	if (file->loc == LOC_MEM)
     37 		free(file->buffer);
     38 	free(file);
     39 }
     40 
     41 struct commit*
     42 baseline_commit_new()
     43 {
     44 	struct commit *comm = (struct commit *)malloc(sizeof(struct commit));
     45 	comm->id = NULL;
     46 	comm->dir = NULL;
     47 	comm->author.name = NULL;
     48 	comm->author.email = NULL;
     49 	comm->n_parents = 0;
     50 	comm->message = NULL;
     51 	return comm;
     52 }
     53 
     54 void
     55 baseline_commit_free(struct commit *comm)
     56 {
     57 	int i;
     58 	if (comm == NULL)
     59 		return;
     60 	free(comm->id);
     61 	free(comm->dir);
     62 	for (i=0 ; i<comm->n_parents ; i++)
     63 		free(comm->parents[i]);
     64 	free(comm->message);
     65 	free(comm);
     66 }
     67 
     68 struct dir*
     69 baseline_dir_new()
     70 {
     71 	struct dir *dir = (struct dir *)malloc(sizeof(struct dir));
     72 	dir->id = NULL;
     73 	dir->parent = NULL;
     74 	dir->children = NULL;
     75 	return dir;
     76 }
     77 
     78 void
     79 baseline_dir_free(struct dir *dir)
     80 {
     81 	struct dirent *it, *ptr;
     82 
     83 	if (dir == NULL)
     84 		return;
     85 	if (dir->children == NULL) {
     86 		free(dir);
     87 		return;
     88 	}
     89 	for (it = dir->children ; it != NULL ;) {
     90 		ptr = it;
     91 		it = it->next;
     92 		free(ptr);
     93 	}
     94 	free(dir);
     95 }
     96 
     97 void
     98 baseline_dir_append(struct dir *dir, struct dirent *ent)
     99 {
    100 	struct dirent *it;
    101 
    102 	if (dir == NULL || ent == NULL)
    103 		return;
    104 	if (dir->children == NULL) {
    105 		dir->children = ent;
    106 	}
    107 	else {
    108 		for (it = dir->children ; it->next != NULL ; it = it->next);
    109 		it->next = ent;
    110 	}
    111 	ent->next = NULL;
    112 }
    113