void GenerateTempFileName ( wchar_t alter * tempFileNameOut, wchar_t const * extension ) { #ifdef _MSC_VER wchar_t temp_dir[_MAX_DIR]; DWORD dir_len = 0; dir_len = GetTempPathW(_MAX_DIR, temp_dir); assert(dir_len != 0); assert(dir_len <= _MAX_DIR); UINT res = 0; res = GetTempFileNameW(temp_dir, L"HOOPS", 0, tempFileNameOut); assert(res != 0); // if extension is specified replace .tmp with user-specified value if (extension) { wchar_t *old_extension = wcsrchr(tempFileNameOut, L'.'); if (extension[0] == L'.') old_extension[0] = 0; else old_extension[1] = 0; wcscat(tempFileNameOut, extension); } #else char temp_template[TEMPFILE_UTILS_BUFFER_SIZE]; if (extension) GenerateTempFileName(temp_template, reinterpret_cast<char const *>(H_UTF8(extension).encodedText())); else GenerateTempFileName(temp_template); if (temp_template[0] == 0) tempFileNameOut[0] = 0; else wcscpy(tempFileNameOut, H_WCS(temp_template).encodedText()); #endif }
REGISTER_TESTS_END // FileExists //------------------------------------------------------------------------------ void TestFileIO::FileExists() const { // generate a process unique file path AStackString<> path; GenerateTempFileName( path ); // ensure doesn't exist FileIO::FileDelete( path.Get() ); // delete in case left over from previous test run TEST_ASSERT( FileIO::FileExists( path.Get() ) == false ); // create it FileStream f; TEST_ASSERT( f.Open( path.Get(), FileStream::WRITE_ONLY ) == true ); f.Close(); // ensure exists TEST_ASSERT( FileIO::FileExists( path.Get() ) == true ); // clean up TEST_ASSERT( FileIO::FileDelete( path.Get() ) == true ); TEST_ASSERT( FileIO::FileExists( path.Get() ) == false ); }
int CreateStdWebCLIPSRouter(struct WebCLIPSRouter *pWCRouter, char *szRouter) { int bResult; //Make space for the name of router being listened to pWCRouter->szRouter = (char *)malloc(strlen(szRouter) + 1); if(pWCRouter->szRouter == NULL) { ProcessErrorCode("ROUT0005", szRouter, 'n', 'n'); return(FALSE); } strcpy(pWCRouter->szRouter, szRouter); pWCRouter->cDestination = WCRouter_ToScreen; //Screen output requires a temporary filename bResult = GenerateTempFileName(&pWCRouter->szRouterFileName); if(bResult == FALSE) { ProcessErrorCode("ROUT0004", szRouter, 'n', 'n'); return(FALSE); } pWCRouter->cRouterType = WCRouter_System; if((pWCRouter->fp = fopen(pWCRouter->szRouterFileName, "wb")) == NULL) { ProcessErrorCode("ROUT0005", szRouter, 'n', 'n'); return(FALSE); } return(TRUE); }
// FileCopy //------------------------------------------------------------------------------ void TestFileIO::FileCopy() const { // generate a process unique file path AStackString<> path; GenerateTempFileName( path ); // generate copy file name AStackString<> pathCopy( path ); pathCopy += ".copy"; // make sure nothing is left from previous runs FileIO::FileDelete( path.Get() ); FileIO::FileDelete( pathCopy.Get() ); TEST_ASSERT( FileIO::FileExists( path.Get() ) == false ); TEST_ASSERT( FileIO::FileExists( pathCopy.Get() ) == false ); // create it FileStream f; TEST_ASSERT( f.Open( path.Get(), FileStream::WRITE_ONLY ) == true ); f.Close(); // copy it TEST_ASSERT( FileIO::FileCopy( path.Get(), pathCopy.Get() ) ); TEST_ASSERT( FileIO::FileExists( pathCopy.Get() ) == true ); // copy without overwrite allowed should fail const bool allowOverwrite = false; TEST_ASSERT( FileIO::FileCopy( path.Get(), pathCopy.Get(), allowOverwrite ) == false ); // cleanup VERIFY( FileIO::FileDelete( path.Get() ) ); VERIFY( FileIO::FileDelete( pathCopy.Get() ) ); }
// ReadOnly //------------------------------------------------------------------------------ void TestFileIO::ReadOnly() const { // generate a process unique file path AStackString<> path; GenerateTempFileName( path ); // create it FileStream f; TEST_ASSERT( f.Open( path.Get(), FileStream::WRITE_ONLY ) == true ); f.Close(); // should not be read only TEST_ASSERT( FileIO::GetReadOnly( path ) == false ); // set readonly TEST_ASSERT( FileIO::SetReadOnly( path.Get(), true ) == true ); // should be read only TEST_ASSERT( FileIO::GetReadOnly( path ) == true ); // delete should fail TEST_ASSERT( FileIO::FileDelete( path.Get() ) == false ); // clean up TEST_ASSERT( FileIO::SetReadOnly( path.Get(), false ) == true ); TEST_ASSERT( FileIO::GetReadOnly( path ) == false ); TEST_ASSERT( FileIO::FileDelete( path.Get() ) == true ); }
// FileDelete //------------------------------------------------------------------------------ void TestFileIO::FileDelete() const { // generate a process unique file path AStackString<> path; GenerateTempFileName( path ); // create it FileStream f; TEST_ASSERT( f.Open( path.Get(), FileStream::WRITE_ONLY ) == true ); f.Close(); // ensure exists TEST_ASSERT( FileIO::FileExists( path.Get() ) == true ); // delete TEST_ASSERT( FileIO::FileDelete( path.Get() ) == true ); TEST_ASSERT( FileIO::FileExists( path.Get() ) == false ); }
// FileTime //------------------------------------------------------------------------------ void TestFileIO::FileTime() const { // generate a process unique file path AStackString<> path; GenerateTempFileName( path ); // create it FileStream f; TEST_ASSERT( f.Open( path.Get(), FileStream::WRITE_ONLY ) == true ); f.Close(); // get last write time const uint64_t oldTime = FileIO::GetFileLastWriteTime( path ); TEST_ASSERT( oldTime != 0 ); // wait for some time that is bigger than filesystem time granularity #if defined( __OSX__ ) // HFS+ has surprisingly poor time resolution (1 second) Thread::Sleep( 1100 ); #else Thread::Sleep( 500 ); #endif // modify file FileStream f2; TEST_ASSERT( f.Open( path.Get(), FileStream::WRITE_ONLY ) == true ); f.Write( (uint32_t)0 ); f.Close(); // get new last write time const uint64_t newTime = FileIO::GetFileLastWriteTime( path ); TEST_ASSERT( newTime > oldTime ); // manually set time back TEST_ASSERT( FileIO::SetFileLastWriteTime( path, oldTime ) == true ); uint64_t timeNow = FileIO::GetFileLastWriteTime( path ); TEST_ASSERT( timeNow == oldTime ); }
//Setup all routers. Parse through the WebCLIPS.INI and open // all required routers. int SetupRouters(void) { char szRouterList[MAX_SETTINGS_LINE_LEN], *p, szEchoEntry[MAX_ROUTER_NAME_LEN + 4 + 1]; //<router> "Echo" = 4 char szRouterFile[MAXNAMLEN + 1]; int iMaxPriority = 40; char szRouterTemp[MAX_ROUTER_NAME_LEN + 1], szRouterTemp2[MAX_ROUTER_NAME_LEN + 1]; struct WebCLIPSRouter *pWCRouter; int bSetRouterResult, bResult; //Get the list of routers GetWebCLIPSSettings( g_szScreenName, // section name "RouterList", // entry name "", // default value szRouterList, // return value sizeof(szRouterList), // max length g_szWebCLIPSINI ); //Set up the standard CLIPS routers : stdout, wdialog, wwarning, werror if(InitializeStdCLIPSRouters() != 0) return(-1); //If no additional routers requested then exit if(strlen(szRouterList) == 0) { return(0); } //Search for '|'. Set each router name in the string array p = strtok(szRouterList, "|"); g_pUDFWebCLIPSRouter = (struct WebCLIPSRouter *)calloc(1, sizeof(struct WebCLIPSRouter)); pWCRouter = g_pUDFWebCLIPSRouter; while(p) { strcpy(szRouterTemp2, p); Trim(szRouterTemp2, szRouterTemp); strcpy(szEchoEntry, szRouterTemp); strcat(szEchoEntry, "Echo"); //Get <router>Echo entry GetWebCLIPSSettings( g_szScreenName, // section name szEchoEntry, // entry name "", // default value szRouterFile, // return value sizeof(szRouterFile), // max length g_szWebCLIPSINI ); //If router is set to 'no' or is absent then get the next router. Also, if // there is a request for stdout, wdialog, wwarning, werror to be directed // to the screen, then ignore it. These will be handled by automatically. // // Also, ignore requests to 'dribble' to screen. The reason is that because // there are no </form> tags any facts that are asserted from a screen actually // get asserted TWICE..this is undesirable if(strcasecmp(szRouterFile, "no") == 0 || strlen(szRouterFile) == 0 || (strcasecmp(szRouterFile, "screen") == 0 && (strcasecmp(szRouterTemp, "stdout") == 0 || strcasecmp(szRouterTemp, "dribble") == 0 || strcasecmp(szRouterTemp, "wdialog") == 0 || strcasecmp(szRouterTemp, "wwarning") == 0|| strcasecmp(szRouterTemp, "werror") == 0))) { p = strtok(NULL, "|"); continue; } //Set the name of the router. pWCRouter->szRouter = (char *)malloc(strlen(szRouterTemp) + 1); if (!pWCRouter->szRouter) { ProcessErrorCode("ROUT0003", szRouterTemp, 'n', 'n'); return(-1); } strcpy(pWCRouter->szRouter, szRouterTemp); //If router is to be directed to the screen, then get a // temporary file name else use the entry in WebCLIPS.INI if(strcasecmp(szRouterFile, "screen") == 0) { pWCRouter->cDestination = WCRouter_ToScreen; //Generate temporary filename. bResult = GenerateTempFileName(&pWCRouter->szRouterFileName); if(bResult == FALSE) { ProcessErrorCode("ROUT0001", szRouterFile, 'n', 'n'); return(-1); } } else { pWCRouter->cDestination = WCRouter_ToFile; pWCRouter->szRouterFileName = (char *)malloc(strlen(szRouterFile) + 1); if (!pWCRouter->szRouterFileName) { ProcessErrorCode("ROUT0003", szRouterFile, 'n', 'n'); return(-1); } strcpy(pWCRouter->szRouterFileName, szRouterFile); } //Router request for dribble. if(strcasecmp(szRouterTemp, "dribble") == 0) { bSetRouterResult = DribbleOn(pWCRouter->szRouterFileName); if(bSetRouterResult == FALSE) //Error, process error and end program { ProcessErrorCode("DRBL0002", szRouterFile, 'n', 'n'); return(-1); } pWCRouter->cRouterType = WCRouter_Dribble; } else { if((pWCRouter->fp = fopen(pWCRouter->szRouterFileName, "wb")) == NULL) { ProcessErrorCode("ROUT0003", pWCRouter->szRouterFileName, 'n', 'n'); return(FALSE); } pWCRouter->cRouterType = WCRouter_Userdefined; } //System-defined router. Set priority appropriately if(strcasecmp(szRouterTemp, "wclips") == 0 || strcasecmp(szRouterTemp, "wtrace") == 0 || strcasecmp(szRouterTemp, "wagenda") == 0 || strcasecmp(szRouterTemp, "stdout") == 0 || strcasecmp(szRouterTemp, "wdialog") == 0 || strcasecmp(szRouterTemp, "wwarning") == 0 || strcasecmp(szRouterTemp, "werror") == 0 || strcasecmp(szRouterTemp, "wdisplay") == 0) iMaxPriority = 20; //Get next router p = &p[strlen(p) + 1]; p = strtok(p, "|"); //If there is another router to process, then allocate space for it if(p != NULL) { pWCRouter->pNextRouter = (struct WebCLIPSRouter *)calloc(1, sizeof(struct WebCLIPSRouter)); pWCRouter = pWCRouter->pNextRouter; } } // while(p) if(AddRouter(USERDEFINED_ROUTER, iMaxPriority, UDFWCQuery, UDFWCPrint, NULL, NULL, UDFWCExit) == 0) { ProcessErrorCode("ROUT0005", "User-Defined WebCLIPS Routers", 'n', 'n'); return(-1); } return(0); }
int ProcessCookies(void) { char szKeepFacts[7], *szFileName; char szTargetScreen[MAX_SCREEN_NAME_SIZE + 1], *p; long ltime, lDays; struct tm *GMTime; int iHours, iMins; int bResult; //19 = "Set-Cookie: " + " Facts=" char szCookieParm[MAXNAMLEN + MAX_SCREEN_NAME_SIZE + 19 + 1]; //7 = "Date | " char szCookieExpireParm[MAX_COOKIE_TIME_SIZE + 7 + 1]; char szCookieTime[MAX_COOKIE_TIME_SIZE + 1]; //Determine if the screen wants to preserve facts using the // 'cookie' option GetWebCLIPSSettings( g_szScreenName, // section name "PreserveFacts", // entry name "no", // default value szKeepFacts, // return value sizeof(szKeepFacts), // max length g_szWebCLIPSINI ); //No cookies requested, get out if(strcasecmp(szKeepFacts, "cookie") != 0) return(0); //Determine which screen the facts are being saved for GetWebCLIPSSettings( g_szScreenName, // section name "SaveFactsFor", // entry name "", // default value szTargetScreen, // return value sizeof(szTargetScreen), // max length g_szWebCLIPSINI ); if(strlen(szTargetScreen) == 0) { PrintMIMEHeader(); ProcessErrorCode("COOK0001", g_szScreenName, 'n', 'n'); return(-1); } //Append ' Facts'. This is where the target screen will look to // get the name of the file to load. strcpy(szCookieParm, "Set-Cookie: "); strcat(szCookieParm, szTargetScreen); strcat(szCookieParm, " Facts="); //Get a unique filename to store facts bResult = GenerateTempFileName(&szFileName); if(bResult == FALSE) { ProcessErrorCode("COOK0002", g_szScreenName, 'n', 'n'); return(-1); } else { //Copy the file for the PreserveFacts function call strcpy(g_szSaveCookiesFile, szFileName); //Add the filename to the CookieParm string strcat(szCookieParm, szFileName); strcat(szCookieParm, "; "); //Append a semicolon and a space free(szFileName); } //Get any screen-specific cookie expiration information GetWebCLIPSSettings( g_szScreenName, // section name "CookieExpiration", // entry name "", // default value szCookieExpireParm, // return value sizeof(szCookieExpireParm), // max length g_szWebCLIPSINI ); //If there is none then check to see if there is any // default expiration information for the cookie if(strlen(szCookieExpireParm) == 0) { GetWebCLIPSSettings( "System", // section name "CookieExpiration", // entry name "", // default value szCookieExpireParm, // return value sizeof(szCookieExpireParm), // max length g_szWebCLIPSINI ); } //If there is still no expiration info found then the cookie will // be written without an 'expires'. This will have the effect that // the cookie will be present until the end of the session. This // means the cookie will be present until IE or NN is stopped/closed // terminated ... whatever. if(strlen(szCookieExpireParm) == 0) { printf("%s\n", szCookieParm); return(0); } //We have an expiration parameter. Check to see if it is of the // type : Date | 'Standard Cookie expiration'. If we find a '|' // then we assume that anything to the right of it is a properly // formed 'Standard Cookie expiration' as given in the document : // http://home.netscape.com/newsref/std/cookie_spec.html. p = strtok(szCookieExpireParm, " \t"); if(strcasecmp(p, "date") == 0) { p = strchr(p, NULL); p++; //Get past NULL p = strchr(p, '|'); //Up to the pipe ( '|' ) p++; //Past the pipe while(!isalpha(*p)) //Past the white space p++; strcat(szCookieParm, "expires="); strcat(szCookieParm, p); strcat(szCookieParm, "; "); //append a semicolon and a space printf("%s\n", szCookieParm); return(0); } //If we are here then the expires parameter is of the // form Days, Hours, Mins. This will be used to modify the // current time (GMT) and format an expiration date. //Construct a CTimeSpan object given the entry in WebCLIPS.INI //Days parameter first p = strtok(szCookieExpireParm, ","); lDays = atol(p); //Hours next p = strtok(NULL, ","); iHours = atoi(p); //Minutes next p = strtok(NULL, ","); iMins = atoi(p); //Get the current time time(<ime); //Add Days, Hours, Mins in terms of seconds to ltime ltime += ToSeconds(lDays, iHours, iMins, 0); //Convert to GMT/UTC time GMTime = localtime(<ime); //Clear the buffer memset(szCookieTime, '\0', sizeof(szCookieTime) + 1); strftime(szCookieTime, MAX_COOKIE_TIME_SIZE, "expires=%A, %d-%b-%Y %H:%M:%S GMT; ", GMTime); //Append it to current cookie contents and print it. strcat(szCookieParm, szCookieTime); //append the GMT time printf("%s\n", szCookieParm); return(0); }
//Function to write all facts as hidden <INPUT> items, if desired. int PreserveFactState(char *szPrevScreen) { struct fact * factptr; char szCurrFact[FACT_BUFFER_SIZE + 1]; char *pStart; int bPreserveFactsUsingTags = FALSE, bPreserveFactsUsingDisk = FALSE; int bPreserveFactsUsingCookie = FALSE, bResult; char szKeepFacts[7], *szFileName; FILE *fp; //Determine if *previous* screen wants to preserve ALL facts for // further processing. GetWebCLIPSSettings( szPrevScreen, // section name "PreserveFacts", // entry name "no", // default value szKeepFacts, // return value sizeof(szKeepFacts), // max length g_szWebCLIPSINI ); //Set bPreserveFact flags appropriately if(strcasecmp(szKeepFacts, "yes") == 0) { bPreserveFactsUsingTags = TRUE; } if(strcasecmp(szKeepFacts, "disk") == 0 || strcasecmp(szKeepFacts, "cookie") == 0) { //We are preserving facts for just this session if(strcasecmp(szKeepFacts, "disk") == 0) { bPreserveFactsUsingDisk = TRUE; //Generate unique filename bResult = GenerateTempFileName(&szFileName); if(bResult == FALSE) { ProcessErrorCode("PSRV0001", NULL, 'y', 'n'); return(-1); } } //We are preserving facts across sessions if(strcasecmp(szKeepFacts, "cookie") == 0) { bPreserveFactsUsingCookie = TRUE; szFileName = g_szSaveCookiesFile; } //Open the file if((fp = fopen(szFileName, "wb")) == NULL) { free(szFileName); ProcessErrorCode("PSRV0002", szFileName, 'y', 'y'); return(-1); } } //For each fact preserve it in the appropriate manner factptr = GetNextFact(NULL); while(factptr) { memset(szCurrFact, '\0', FACT_BUFFER_SIZE + 1); GetFactPPForm(szCurrFact, FACT_BUFFER_SIZE, factptr); pStart = strchr(szCurrFact, '('); //Skip (initial-fact) if(strcmp(szCurrFact, "(initial-fact)") == 0) { factptr = GetNextFact(factptr); continue; } //Whether or not fact preservation is desired, the "ScreenName" // fact must be kept so WebCLIPS knows what to do next time. if(strstr(pStart, "(ScreenName ") != NULL) //ScreenName found! { printf("<INPUT TYPE=\"hidden\" NAME=\"fact\" VALUE=\"%s\">\n", pStart); factptr = GetNextFact(factptr); continue; } //If fact preservation is desired and the facts are to be // preserved using <hidden> HTML tags then, the keep the fact. if(bPreserveFactsUsingTags == TRUE) { printf("<INPUT TYPE=\"hidden\" NAME=\"fact\" VALUE=\""); PrintURLEncode(pStart); printf("\">\n"); factptr = GetNextFact(factptr); continue; } //If we want to save facts using disk then write the fact to // to the file. if(bPreserveFactsUsingDisk == TRUE || bPreserveFactsUsingCookie == TRUE) { fprintf(fp, "%s\n", pStart); factptr = GetNextFact(factptr); continue; } factptr = GetNextFact(factptr); } // while(factptr) //Close the file, format <hidden> tag with filename if(bPreserveFactsUsingDisk == TRUE) { //Write out a <hidden> tag containing the filename for the next // program printf("<INPUT type=\"hidden\" name=\"WC_SavedFacts\" value=\"%s\">", szFileName); free(szFileName); //Free up space from GenerateTempFileName() fclose(fp); } //Close the file if(bPreserveFactsUsingCookie == TRUE) { fclose(fp); } return(0); }