Exemplo n.º 1
0
VcStatus readVcFile( FILE *const vcf, VcFile *const filep ){
  
  //Initialize VcFile
  filep->ncards = 0;
  filep->cardp = NULL;
  //other variable declarations
  VcStatus returnStatus = initializeStatus(); //Initalize VcStatus
  VcStatus crrtStatus;
  
  //loop to read in vcards
  while(!feof(vcf)){ 
    Vcard * crrtCard;
    filep->cardp = realloc(filep->cardp, sizeof(Vcard)*(filep->ncards+1));
    crrtStatus = readVcard(vcf, &crrtCard);
    if(crrtStatus.code != OK){
      freeVcFile(filep);
      return crrtStatus;
    }
    else if(crrtCard == NULL){
      break;
    }
    returnStatus.lineto = linecount;
    filep->cardp[filep->ncards] = crrtCard;
    filep->ncards++;
  }//end of loop
  filep->cardp = realloc(filep->cardp, sizeof(Vcard)*(filep->ncards));
  returnStatus.code = OK;
  return returnStatus;
} //end of readVcFile
Exemplo n.º 2
0
void initializeAll(char grid[8][8][33], char status[33], char pieceTranslator[33], char gameVariables[21])
{
    initializeLegal(grid);
    initializeStatus(status);
    initializePieces(pieceTranslator);
    initializeBoard(grid);
    Initialize_Chess_Play();

    return;
}
Exemplo n.º 3
0
int checkSegment(FILE *f, char ending)
{
    struct ht_Table *status = ht_create(NUMBER_OF_SYMBOLS);
    struct ht_Table *opposites = ht_create(NUMBER_OF_OPPOSITES);
    initializeStatus(status);
    initializeOpposites(opposites);
    char CLOSERS[] = {')', '>', ']', '}'};
    char OPENERS[] = {'(', '<', '[', '{'};
    const int LISTS_LENGTH = 4;
    char last = 'a';
    char curr;
    while((curr = fgetc(f)) != EOF)
    {
        printf("%c", curr);
        if(curr == '#')
            skipLineComment(f);
        else if(curr == '*' && last == '/')
            skipBlockComment(f);
        else if(curr == '/' && last == '/')
            skipLineComment(f);
        else if(curr == '\'' || curr == '"')
        {
            if(skipString(f, curr) != 0)
                return 1;
        }
        else if(search(OPENERS, LISTS_LENGTH, curr) != -1)
        {
            (*ht_get(status, curr))++;
        }
        // Struct pointer references screw up the normal algorithm. Don't tell Tim Peters or Linus Torvalds
        else if(curr == '>' && last == '-')
        {}
        else if(search(CLOSERS, LISTS_LENGTH, curr) != -1)
        {
            char correspondingOpener = (char) (*ht_get(opposites, curr));
            int *num = ht_get(status, correspondingOpener);
            if(*num == 0)
            {
                printf("There is an unmatched %c!\n", curr);
                return 1;
            }
            else
            {
                (*num)--;
            }
        }
        last = curr;
    }
    return 0;
}
Exemplo n.º 4
0
int
initializeStatusArmy(tStatusArmy * statusArmy, tTheme * theme)
{
    int i, j, k, error = 0;

    if((*statusArmy = malloc(sizeof(tStatus*)*theme->elemAmount))==NULL)
        error=NO_MEMORY;
    for (i=0; i < theme->elemAmount && !error; i++)
    {
        if (((*statusArmy)[i]= malloc(sizeof(tStatus)*
                                      ((theme->elements)[i].cant)))==NULL)
            error = NO_MEMORY;
        for (j=0; j<((theme->elements)[i].cant) && !error; j++)
        {
            if(((*statusArmy)[i][j].position = malloc(sizeof(tPositionStatus)*
                                               (theme->elements[i].size)))==NULL)
                error=NO_MEMORY;
            if (!error)
            {
                initializeStatus(statusArmy, theme, i, j);
                (*statusArmy)[i][j].alive = 0;
                (*statusArmy)[i][j].dim = 0;
            }
        }
    }

    if (error)
    {
        for (k=0; k<i; k++)
        {
            for (j=0; j<((theme->elements)[k].cant); j++)
            {
                free((*statusArmy)[k][j].position);
            }
            free((*statusArmy)[k]);
        }
        free(*statusArmy);

    }
    return error;
}
Exemplo n.º 5
0
VcStatus getUnfolded( FILE *const vcf, char **const buff ){
  //initialize VcStatus
    VcStatus returnStatus = initializeStatus();
    returnStatus.code = OK;
    //check if SPECIAL call
    if(vcf == NULL){
      if(readahead != NULL){
	free(readahead);
	readahead = NULL;
      }
      //reset static variables and free allocated memory like linecounts and readahead buffers
      return returnStatus;
    }
   //initialize other variables
    char * firstbuff = calloc(FOLD_LEN+2,sizeof(char));
    char * midbuff;
    char * finalbuff = calloc(2, sizeof(char));
    int whitespace = 1, full_line;
    int i, length; 
    char * nullptr;
   //loop for reading file
    //read in line
    if(readahead == NULL){
      fgets(firstbuff, FOLD_LEN+1, vcf);
    }
    else{
      firstbuff = strcpy(firstbuff, readahead);
      free(readahead);
      readahead = NULL;
    }
    
    do{
      if(feof(vcf)){
	*buff = NULL;
	free(firstbuff);
	free(finalbuff);
	return returnStatus;	
      }
      
      returnStatus.lineto++;
      full_line = 1;
      length = strcspn(firstbuff, "\r\n");
      //if(line = whitespace)
      for(i = 0; i < strlen(firstbuff); i++){
	if(!isspace(firstbuff[i])){
	  whitespace = 0;
	  break;
	}
      } //end of for loop to check if line is all whitespace
      if(whitespace == 1){
	whitespace = 1;
	linecount++;
	continue;
      }
      if(length == strlen(firstbuff)){
	full_line = 0;
      }
      if(full_line != 0){
	linecount++;
      }
      midbuff = calloc(length+1,sizeof(char));
      //remove trailing EOL (/r/n)
      strncpy(midbuff, firstbuff, length);
      free(firstbuff);
      //remove initial whitespace
      if(isspace(midbuff[0])){
	for(i = 0; i < strlen(midbuff)-1; i++){
	  midbuff[i] = midbuff[i+1];
	}
	length--;
      }
      //add null pointer
      nullptr = strchr(midbuff, '\0');
      if(nullptr == NULL || strlen(midbuff) > length){
	midbuff[length] = '\0';
      }
      
      //add line to buffer
      finalbuff = realloc(finalbuff, sizeof(char)*(strlen(finalbuff)+strlen(midbuff)+1));
      strcat(finalbuff, midbuff);
      free(midbuff);
      *buff = finalbuff;
      //read in line
      firstbuff = calloc(FOLD_LEN+2,sizeof(char));
      fgets(firstbuff, FOLD_LEN+1, vcf);
    }while(isspace(firstbuff[0]) || full_line == 0);
      
   //clean-up
   returnStatus.linefrom = returnStatus.lineto - returnStatus.linefrom;
   returnStatus.lineto = returnStatus.linefrom;
   readahead = malloc(sizeof(char)*(strlen(firstbuff)+1));
   readahead = strcpy(readahead, firstbuff);
   free(firstbuff);
   return returnStatus;
} //end of getUnfolded function
Exemplo n.º 6
0
VcStatus readVcard( FILE *const vcf, Vcard **const cardp ){
  
   //Initialize Vcard
   (*cardp) = calloc(10, sizeof(Vcard)+(sizeof(VcProp)));
   
   (*cardp)->nprops = 0;
   //Initialize other variables
   int i = 0, numalloc = 10;
   VcStatus returnStatus = initializeStatus();
   VcStatus crrtstatus;
   VcProp tempProp;
   char * buffer;
   VcError error;
   bool end = false;
   
   do{//start loop to read in vcards
      crrtstatus = getUnfolded(vcf, &buffer);
      if(&buffer == NULL || buffer == NULL){
	freeVcard(*cardp);
	(*cardp) = NULL;
	return generateStatus(OK, returnStatus.lineto, returnStatus.lineto+crrtstatus.lineto);
      }//end getUnfolded returned NULL;
      error = parseVcProp(buffer, &tempProp);
      free(buffer);
      if(error != OK){
	freeVcProp(&tempProp);
	freeVcard(*cardp);
	(*cardp) = NULL;
	return generateStatus(error, returnStatus.lineto, returnStatus.lineto+crrtstatus.lineto);
      }
      //check for BEGIN
      if(tempProp.name == VCP_BEGIN && i == 0){
	freeVcProp(&tempProp);
	returnStatus.lineto = returnStatus.linefrom + crrtstatus.lineto;
	i++;
	continue;
      }
      else if((tempProp.name == VCP_BEGIN && i > 0) || (tempProp.name != VCP_BEGIN && i == 0)){
	freeVcProp(&tempProp);
	freeVcard(*cardp);
	(*cardp) = NULL;
	return generateStatus(BEGEND, returnStatus.lineto, returnStatus.lineto+crrtstatus.lineto);
      }
      
      //check for VERSION 
      if(i == 1 && tempProp.name != VCP_VERSION){
	freeVcProp(&tempProp);
	freeVcard(*cardp);
	(*cardp) = NULL;
	return generateStatus(NOPVER, returnStatus.lineto, returnStatus.lineto+crrtstatus.lineto);
      }
      else if((i > 1 && tempProp.name == VCP_VERSION) || 
	(tempProp.name == VCP_VERSION && strcmp(tempProp.value,VCARD_VER) != 0)){
	freeVcProp(&tempProp);
	freeVcard(*cardp);
	(*cardp) = NULL;
	return generateStatus(BADVER, returnStatus.lineto, returnStatus.lineto+crrtstatus.lineto);
      }
      else if(tempProp.name == VCP_VERSION && strcmp(tempProp.value,VCARD_VER) == 0){
	freeVcProp(&tempProp);
	i++;
	returnStatus.lineto = returnStatus.linefrom + crrtstatus.lineto;
	continue;
      }
      //check for END
      if(tempProp.name == VCP_END){
	if(!searchVcard((*cardp)->prop)){
	  freeVcProp(&tempProp);
	  freeVcard(*cardp);
	  (*cardp) = NULL;
	  return generateStatus(NOPNFN, returnStatus.lineto, returnStatus.lineto+crrtstatus.lineto);
	}
	else{
	  end = true;
	  break;
	}
      }
      /*FOR ALL OTHER PROPERTIES*/
      //allocate memory for one more property
      if((*cardp)->nprops >= (numalloc - 1)){
	numalloc++;
	(*cardp) = (Vcard *)realloc((*cardp),sizeof(int)+(sizeof(VcProp)*numalloc));
      }
      
      copyVcProp(&(*cardp)->prop[(*cardp)->nprops], &tempProp);
      freeVcProp(&tempProp);
      (*cardp)->nprops++;
      i++;
      returnStatus.lineto = linecount; 
   }while(error == OK);
   
   
   if(end == false){
     freeVcard(*cardp);
     (*cardp) = NULL;
     return generateStatus(BEGEND, returnStatus.lineto, returnStatus.lineto+crrtstatus.lineto);
   }
   
   //clean-up
   if(numalloc > (*cardp)->nprops){
     (*cardp) = (Vcard *) realloc((*cardp),sizeof(Vcard)+(sizeof(VcProp)*((*cardp)->nprops)));
   }
   freeVcProp(&tempProp);
   returnStatus.code = OK;
   return returnStatus;
} //end of readVcard