File:  [LON-CAPA] / capa / capa51 / pProj / capaSubjective.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Tue Sep 28 21:26:22 1999 UTC (25 years, 1 month ago) by albertel
Branches: capa
CVS tags: start
Created directory structure

#include "capaCommon.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>

char* strtoupper(char* source)
{
  char* result;
  int i,len=strlen(source);
  result=capa_malloc(len,1);
  for(i=0;i<len;i++) result[i]=toupper(source[i]);
  return result;
}

int capa_set_subjective (int set,int problem,char *student_num,char* response)
{
  char buf[FILE_NAME_LENGTH],*upperstunum;
  FILE *responsefile;

  sprintf(buf,"records/set%d",set);
  if( capa_access(buf, F_OK) == -1 ) { 
    if ( mkdir(buf, S_IREAD | S_IWRITE | S_IEXEC ) == -1 ) { return -1; }
  }

  sprintf(buf,"records/set%d/problem%d",set,problem);
  if( capa_access(buf, F_OK) == -1 ) { 
    if ( mkdir(buf, S_IREAD | S_IWRITE | S_IEXEC ) == -1 ) { return -2; }
  }

  upperstunum=strtoupper(student_num);
  sprintf(buf,"records/set%d/problem%d/%s",set,problem,upperstunum);
  if ((responsefile=fopen(buf,"w"))==NULL)  { return -3; }
  
  fwrite(response,strlen(response),1,responsefile);
  fclose(responsefile);
  return 0;
}

char* capa_get_subjective (int set,int problem,char *student_num)
{
  char buf[FILE_NAME_LENGTH],*upperstunum,*response;
  FILE *responsefile;
  long length;

  upperstunum=strtoupper(student_num);
  sprintf(buf,"records/set%d/problem%d/%s",set,problem,upperstunum);
  if ((responsefile=fopen(buf,"r"))==NULL)  { return NULL; }
  fseek(responsefile,0,SEEK_END);
  length=ftell(responsefile);
  rewind(responsefile);
  response=capa_malloc(length+1,1);
  fread(response,length,1,responsefile);
  response[length]='\0';
  return response;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>