00001
00002
00003
00004
00005 using namespace std;
00006
00007 #include<cstdlib>
00008 #include<cstring>
00009 #include<string>
00010 #include<vector>
00011 #include<unistd.h>
00012
00013 #ifndef PARAMETERSINCLUDED
00014 #include "parameters.cpp"
00015 #endif
00016
00017 #define CODEFILEINCLUDED
00018
00019 class codefile
00020 {
00021 private:
00022 string emailid;
00023 string path;
00024 vector<unsigned int> codestream;
00025 unsigned long int curloc;
00026
00027 public:
00028
00029 codefile(string cf_path ,string email_id,unsigned long int size)
00030 {
00031 path=cf_path;
00032 emailid=email_id;
00033 curloc=0;
00034 for(unsigned long int i=0;i<size;i++)
00035 codestream.push_back(0);
00036 FILE* fptr=fopen(path.c_str(),"w+");
00037 fprintf(fptr,"%s",emailid.c_str());
00038 fprintf(fptr,"%c",'\0');
00039 curloc=ftell(fptr);
00040 fclose(fptr);
00041 }
00042
00043
00044 codefile(string cf_path)
00045 {
00046 curloc=0;
00047 path=cf_path;
00048 FILE* fptr=fopen(path.c_str(),"r+");
00049 char c='a';
00050 while((c=fgetc(fptr))!='\0')
00051 {
00052 curloc++;
00053 }
00054 curloc++;
00055 fclose(fptr);
00056 reademailid();
00057 read();
00058 }
00059
00060 int append(unsigned int code)
00061 {
00062 FILE* fptr=fopen(path.c_str(),"a+");
00063 int bytes=fwrite((unsigned int*)&code,sizeof(unsigned int),1,fptr);
00064 curloc=ftell(fptr);
00065 fclose(fptr);
00066 }
00067
00068 unsigned int read(void)
00069 {
00070 unsigned long int size;
00071 FILE* fptr=fopen(path.c_str(),"r+");
00072 if(fptr==NULL)
00073 {
00074 cout<<"Problem with opening the file"<<endl;
00075 exit(1);
00076 }
00077 size=ftell(fptr);
00078 fseek(fptr,0L,SEEK_END);
00079 size=ftell(fptr);
00080 for(unsigned long int i=0;i<(size-emailid.length()-1)/sizeof(unsigned int);i++)
00081 codestream.push_back(0);
00082 fseek(fptr,curloc,SEEK_SET);
00083 int i=0;
00084 unsigned int tempo;
00085 while(fread((unsigned int*)&tempo,sizeof(unsigned int),1,fptr))
00086 {
00087 codestream[i]=tempo;
00088 i++;
00089 }
00090 fclose(fptr);
00091 curloc=0;
00092 return SUCCESS;
00093 }
00094
00095 unsigned int getcode()
00096 {
00097 if(curloc>codestream.size())
00098 return END_OF_CODESTREAM;
00099 return codestream[curloc++];
00100 }
00101
00102 string reademailid(void)
00103 {
00104 char id[257];
00105 FILE* fptr=fopen(path.c_str(),"r+");
00106 fscanf(fptr,"%s",id);
00107 fclose(fptr);
00108 emailid=(string)id;
00109 return emailid;
00110 }
00111
00112 ~codefile() {
00113 }
00114 };