This document shows the contents of user.cpp if you are starting the tutorial in Part 9. Defining the Remaining User Functions.
IString User::Action() {
IString Action=IString::lowerCase(Req->GetValue("op"));
if (Action=="") return Error("No Action Specified", "The CGI call did not specify an op= argument.");
switch (Action[1]) {
case 'a':
if (Action=="add") return Add();
break;
case 'c':
if (Action=="complete") return Complete();
break;
case 's':
if (Action=="showadd") return ShowAdd();
break;
case 'v':
if (Action=="view") return View();
}
return Error("Unknown Action Specified","The CGI call requested an unknown action, "+Action);
}
IString User::LinkFix() {
// Call ReadFile to read the file pointed to by Request::FilePath
IString Result=ReadFile(Req->GetFilepath());
if (Result=="")
return Error("Could Not Read File",
"The file you requested, "
+Req->GetFilename()
+", was empty, or could not be read.");
// Find the first href= attribute.
int href=Result.indexOf("href=");
// Return if there are no href= tags to fix
if (href==0) return Result;
// Substitute the executable file name and necessary arguments
// in all href= calls. EXE must be defined in macros.hpp
IString LinkInsert=EXE+"?Op=View+Key="+Req->GetKey()+"+file=";
// Allocate a new IString large enough to contain the new string,
// by determining the number of inserts to do.
int NewLength=Result.occurrencesOf("href=")
*LinkInsert.length()
+Result.length()+1;
char* NewResult=new char[NewLength];
NewResult[0]='\0';
int last=0;
while (href>0) {
// Write from last position written up to past the href=,
// and add the link insertion text
strcat(NewResult,
Result.subString(last+1,href+5-last)+
LinkInsert);
last=href+5;
href=Result.indexOf("href=",last);
}
if (last>0) strcat(NewResult,Result.subString(last+1));
return NewResult; // Automatically converted to an IString
}
IString Reviewer::Add() {
return Error("Unimplemented","Reviewer::Add is not yet implemented");
}
IString Reviewer::ShowAdd() {
return Error("Unimplemented","Reviewer::ShowAdd is not yet implemented");
}
IString Reviewer::View() {
// 1. Get the file contents
IString Result=LinkFix();
if (Result=="")
return Error("Could Not Read File",
"The file you requested, "+Req->GetFilename()
+", could not be read, or was empty.");
// 2. Initialize the CommentSection Array.
CreateSections(Result, false);
// 3. Print the CommentSections
Result="";
for (int cs=0;cs<CurCs;cs++) Result+=Section[cs].Print();
return Result;
}
IString Author::View() {
return Error("Unimplemented","Author::View is not yet implemented");
}
IString Author::Complete() {
return Error("Unimplemented","Author::Complete is not yet implemented");
}
IString Reader::View() {
return LinkFix();
}
void Reviewer::CreateSections(IString& Result, IBoolean CompleteLink) {
// Result contains the HTML file with href links already fixed.
// The CompleteLink argument tells CreateSections whether to show
// a link authorizing completion of the comment (Author::View sets
// CompleteLink, while Reviewer::View does not)
// Create a lowercase copy for tag searching
IString result=IString::lowerCase(Result);
// Create a target array of 256 CommentSection objects.
// Track total used so array can be expanded later if more than
// 256 sections are found in document.
// CurCs is declared in class.
CurCs=0;
int MaxCs=256;
// Section is a pointer to an array of CommentSection elements;
// initialize it now.
Section=new CommentSection[MaxCs];
// Search for next paragraph or list item
// MinNonZero() will be defined in func.cpp
int Next=MinNonZero(
NextTag(result,"<p"),
NextTag(result,"<li"));
// If no such tag is found, set next to end of the file,
// so that the first section becomes the entire file.
// Return when done, as no more sections need be created.
if (Next==0) {
Section[CurCs].SetTag(Result);
Section[CurCs].SetReq(Req);
CurCs++;
return;
} else {
// Otherwise, set tag and comment number, and continue
Section[CurCs].SetTag(Result.subString(1,Next-1));
Section[CurCs].SetReq(Req);
Section[CurCs].SetNumber(CurCs);
CurCs++;
}
// Keep adding more sections
while (Next>0) {
// Find the end of the tag and set Tag to it
int eTag=result.indexOf(">",Next);
IString Tag=Result.subString(Next,eTag-Next+1);
// Look for next paragraph or list item
int NextNext=MinNonZero(
NextTag(result,"<p",Next+1),
NextTag(result,"<li",Next+1));
IString Text="";
// Set section to rest of text if no next tag found,
// otherwise set text to rest of this section
if (NextNext==0)
Text=Result.subString(eTag+1);
else
Text=Result.subString(eTag+1,NextNext-eTag-1);
// Expand the Section array if it is too small
if (CurCs>=MaxCs) {
MaxCs+=1;
MaxCs*=1.25;
CommentSection* NewCs=new CommentSection[MaxCs];
for (int cs=0;cs<=CurCs;cs++)
NewCs[cs]=Section[cs];
delete[] Section;
Section=NewCs;
}
// Create the CommentSection
Section[CurCs].SetNumber(CurCs);
Section[CurCs].SetTag(Tag);
Section[CurCs].SetReq(Req);
Section[CurCs].SetText(Text);
Section[CurCs].SetCompleteLink(CompleteLink);
CurCs++;
Next=NextNext;
} // End of While
// Open the comment file and read in any comments
// Comments are delimited by <cmt> and </cmt>
ifstream CommentFile(Req->GetCommentFn());
while (CommentFile) {
IString CommentLine=IString::lineFrom(CommentFile);
IBoolean Completed=false;
if (CommentLine.isLike("<cmt>*")) {
int CmtNum=CommentLine.word(2).asInt();
if (CommentLine.word(3)=="c") Completed=true;
IString User=CommentLine.word(4);
IString Date=CommentLine.word(5);
IString Time=CommentLine.word(6);
IString Text=IString::lineFrom(CommentFile);
IString NextLine=IString::lineFrom(CommentFile);
while (CommentFile && NextLine!="</cmt>") {
Text+=NextLine+"\n";
NextLine=IString::lineFrom(CommentFile);
}
// Add a comment type to this comment section if the comment is valid
if (Text!="" && CmtNum>0 && CmtNum<CurCs && Section[CmtNum].IncrementCount())
Section[CmtNum].SetComment(Text,User,Date,Time,Completed);
} // end outer if
} // end while
} // end function
