home *** CD-ROM | disk | FTP | other *** search
- //
- // Copyright (c) 1997 Vermeer Technologies, Inc., a wholly owned
- // subsidiary of Microsoft Corp. All Rights Reserved
-
- #include <fstream.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <string.h>
- #include <stdio.h>
- #include <math.h>
-
- #ifdef WIN32
- #include <io.h>
- #include <strstrea.h>
- #define IOS_BINARY ios::binary
- #endif
-
- #ifdef UNIX
- #include <strstream.h>
- #define IOS_BINARY ios::bin
- #endif
-
- #include "votefile.h"
- #include "votecount.h"
-
- // Parse the query string. It looks for the keys "Page"
- // and "Vote". It does a case-sensitive search.
- // WARNING: do not use the params string after calling ParseQueryString
- // because the parse method mangles the params string in the process.
- // this is done for efficiency to avoid copying strings.
- void ParseQueryString(char *params,
- char **VoteFileName,
- char **VoteResult
- )
- {
-
- char *pKeyBegin = params;
- char *pKeyEnd = NULL;
- for (char *p = params; *p;)
- {
- if (*p == '=')
- pKeyEnd = p;
- // If it's a key divider, or the last char of the string,
- // parse the entire key/value pair. But if we're at the end
- // of the string, move p there so that the rest of the code
- // works as it is expecting.
- if (*(p+1) == '\0')
- p++;
- if (*p == '&' || *p == '\0')
- {
- // At this point, key is from pKeyBegin to pKeyEnd-1,
- // and value is from pKeyEnd+1 to p-1.
-
- if (pKeyEnd)
- {
- int keyLen = pKeyEnd - pKeyBegin;
- if (keyLen >0)
- {
- // Replace the | seperator with a '\0' so we can just break
- // up this string to return the voteFileName and the
- // VoteResult strings. That way, we don't need to
- // do a strcpy.
- *p = '\0';
- if (strncmp("Page", pKeyBegin, keyLen) == 0)
- *VoteFileName = pKeyEnd+1;
- else if(strncmp("Vote", pKeyBegin, keyLen) == 0)
- *VoteResult = pKeyEnd+1;
- }
- }
- // Reset the pointers:
- pKeyBegin = p+1;
- pKeyEnd = NULL;
- }
- p++;
- }
-
- }
-
-
- const int QueryNumDecodes = 2;
- const char QueryFromDecode[QueryNumDecodes] = {'+', '/'};
- const char QueryToDecode[QueryNumDecodes] = {' ', DIR_SEPARATOR};
-
- // Performs in-place decoding of the query string
- char *QueryDecode(char *queryString)
- {
- // Simple char by char decoding.
- if (queryString == NULL)
- return NULL;
- for (char *p = queryString; *p; p++)
- {
- for (int i=0; i<QueryNumDecodes; i++)
- {
- if (*p == QueryFromDecode[i])
- {
- *p = QueryToDecode[i];
- break;
- }
- }
- }
- return queryString;
- }
-
-
- // argc and argv are unused, so they are commented out to prevent compiler
- // warnings.
- int main(
- int /* argc */,
- char ** /* argv */)
- {
- div_t percentage; // used to calculate percentage of popular opinion
- char *params = QueryDecode(getenv("QUERY_STRING"));
- char *path = getenv("PATH_TRANSLATED");
- if (path == NULL)
- path = "";
-
- char *pageFileName = NULL;
- char *VoteResult = NULL;
-
- if (params == NULL)
- return 1;
-
- // Parse the query string. Fills imageNum with -1 if it's a
- // custom image
- ParseQueryString(params, &pageFileName, &VoteResult );
-
- // If we didn't get a Page filename, just give up.
- if (pageFileName == NULL || strlen(pageFileName) == 0)
- return 1;
-
- // Create full votefile filename and custom image filename by
- // concatenating the path and the relative filenames.
- char *fullVoteFileName = new char[strlen(path) +
- 9 + // +9 for "_private/"
- strlen(pageFileName) +
- 5]; // +4 for ".vot"+'\0'
- sprintf(fullVoteFileName, "%s_private%c%s.vot",
- path, DIR_SEPARATOR, pageFileName);
-
- // The constructor waits until the file is no longer locked, then locks it.
- CVote *VoteCntFile = new CVote(fullVoteFileName);
-
- if (!strcmp(VoteResult,"V1"))
- { VoteCntFile->Increment("vote1type");
- percentage = div ((VoteCntFile->GetVoteCount("vote1type")* 100),
- (VoteCntFile->GetVoteCount("vote1type") +
- VoteCntFile->GetVoteCount("vote2type")));
- };
-
- if (!strcmp(VoteResult,"V2"))
- { VoteCntFile->Increment("vote2type");
- percentage = div ((VoteCntFile->GetVoteCount("vote2type")* 100),
- (VoteCntFile->GetVoteCount("vote1type") +
- VoteCntFile->GetVoteCount("vote2type")));
- };
- // percentage.quot * 100 == final percentage.
-
- // Destruct as soon as possible to unlock the file so others can access it.
- delete VoteCntFile;
-
- // For now, just suppose we are doing a memory file
-
- #ifdef WIN32
- _setmode( _fileno( stdout ), _O_BINARY ); // Make stdout binary
- #endif
- #ifdef UNIX
- fdopen( fileno(stdout), "wb"); // Make stdout binary
- #endif
-
- // output MIME header so browsers know that we're HTML.
-
- fprintf(stdout, "Content-type: text/html\n\n");
-
- fflush(stdout);
-
- cout<< "<html>\n"
- << "<head>\n"
- << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=None\">"
- << "\n<title>Vote Confirmation</title>\n</head>\n<body>\n"
- << "<h1 align=\"center\"><strong>Vote Confirmation "
- << "</strong></h1>\n\n<p align=\"center\"> </p>"
- << "<p align=\"center\"><font size=\"3\">\n"
- << "Your ballot has been cast and your vote "
- << "has been registered.</p>\n"
- << "<p align=\"center\">Currently, " << (percentage.quot)
- << "% of the voting population agrees with your vote.</font></p>"
- << "\n</body>\n\n</html>";
-
- fflush(stdout);
-
- delete fullVoteFileName;
-
- return 0;
- }
-