Example #1
0
File: net_pro.c Project: kota395/np
struct profile *new_profile(struct profile *p, char *csv)
{//入力をstruct profileに格納する
  char *ptr[5];
  if (split(csv, ptr, ',', 5) != 5){
    printf("Failed to execute split in new_profile function.\n");
    return NULL;  //Error
  }
  
  p->id = atoi(ptr[0]);
  if (p->id == 0 && csv[0] != '0'){
    printf("Failed to enter ID in new_profile function.\n");
    return NULL; //Error
  }
  
  strncpy(p->name, ptr[1], MAX_STR_LEN);
  p->name[MAX_STR_LEN] = '\0';
  
  if (new_date(&p->birthday, ptr[2]) == NULL){
    printf("Failed to execute new_date in new_profile function.\n");
    return NULL; //Error
  }
  
  strncpy(p->home, ptr[3], MAX_STR_LEN);
  p->home[MAX_STR_LEN] = '\0';
  
  p->comment = (char *)malloc(sizeof(char)* (strlen(ptr[4]) + 1));
  strcpy(p->comment, ptr[4]);
  
  return p;
}
Example #2
0
/**
 * Create a new profile into P from CSV string like
 *  "0,Takahashi Kazuyuki,1977-04-27,Saitama,Fukuoka Softbank Hawks".
 * return: struct profile *P itself
 */
struct profile *new_profile(struct profile *p, char *csv,int edt)
 {
   char *ptr[5];
 
   if (split(csv, ptr, ',', 5) != 5){
     fprintf(stderr,"error: invalid style of input or data\n\n");
     return NULL;
   }

   p->id = atoi(ptr[0]); 
   if(check_id(p) == NULL){
     if(edt!=1){
       fprintf(stderr,"error: ID already exists.\n To edit the data, use E command\n");
       return NULL;
     }
   }

   strncpy(p->name, ptr[1], MAX_STR_LEN);  
   p->name[MAX_STR_LEN] = '\0';

   if (new_date(&p->birth, ptr[2]) == NULL ){
     fprintf(stderr,"invalid style of input");
     return NULL; 
   }
   if(check_date(&p->birth) == NULL){
     fprintf(stderr,"error: %04d-%02d-%02d is invalid date\n\n",(p->birth).y, (p->birth).m, (p->birth).d);
     return NULL;
   }
  
   strncpy(p->home, ptr[3], MAX_STR_LEN); 
   p->home[MAX_STR_LEN] = '\0';
 
   p->comment = (char *)malloc(sizeof(char) * (strlen(ptr[4])+1));
   strcpy(p->comment, ptr[4]);
 
   profile_data_nitems++;
   
   return p;
}