int GfCreateDir(char *path) { if (path == NULL) { return GF_DIR_CREATION_FAILED; } const int BUFSIZE = 1024; char buf[BUFSIZE]; strncpy(buf, path, BUFSIZE); path = buf; #ifdef WIN32 #define mkdir(x) _mkdir(x) // Translate path. const char DELIM = '\\'; int i; for (i = 0; i < BUFSIZE && buf[i] != '\0'; i++) { if (buf[i] == '/') { buf[i] = DELIM; } } #else // WIN32 #define mkdir(x) mkdir((x), S_IRWXU); const char DELIM = '/'; #endif // WIN32 int err = mkdir(buf); if (err == -1) { if (errno == ENOENT) { char *end = strrchr(buf, DELIM); *end = '\0'; GfCreateDir(buf); *end = DELIM; err = mkdir(buf); } } if (err == -1 && errno != EEXIST) { return GF_DIR_CREATION_FAILED; } else { return GF_DIR_CREATED; } }
void SegLearn::writeKarma() { // Build the directory name. char path[sizeof(filename)]; strncpy(path, filename, sizeof(path)); char* end = strrchr(path, '/'); if (end != NULL) { *end = '\0'; } // Create the directory and try to write data. if (GfCreateDir(path) == GF_DIR_CREATED) { // Try to write data. FILE *fd = fopen(filename, "wb"); if (fd != NULL) { // Create header: Magic Number, #segments, string, version. int magic = MAGIC1; int magic2 = MAGIC2; char string[] = STRINGID; // The magic numbers are used to catch 32/64 bit mismatches and little/big // endian mismatches. Here the patterns I expect at the beginning of the files. // I call 4 bytes a UNIT, MAGIC1 bytes A1, A2, A3, A4, MAGIC2 bytes B1, B2, B3, B4, // a zeroed byte 00): // 32bit big endian : UNIT1-A1A2A3A4; UNIT2-B1B2B3B4; ... // 32bit little endian: UNIT1-A4A3A2A1; UNIT2-B4B3B2B1; ... // 64bit big endian : UNIT1-00000000; UNIT2-A1A2A3A4; UNIT3-00000000; UNIT4-B1B2B3B4; ... // 64bit little endian: UNIT1-A4A3A2A1; UNIT2-00000000; UNIT3-B4B3B2B1; UNIT4-00000000: ... // // Like you can see there is created a unique pattern for each architecture in // UNIT1 and UNIT2. fwrite(&magic, sizeof(magic), 1, fd); // magic number. fwrite(&magic2, sizeof(magic2), 1, fd); // magic number 2. fwrite(&nseg, sizeof(nseg), 1, fd); // # segments. fwrite(string, sizeof(string), 1, fd); // string. for (int i = 0; i < nseg; i++) { fwrite(&updateid[i], sizeof(updateid[0]), 1, fd); fwrite(&radius[i], sizeof(radius[0]), 1, fd); } fclose(fd); } } }
/** Save a screen shot in png format. @ingroup screen */ void GfuiScreenShot(void * /* notused */) { unsigned char *img; const int BUFSIZE = 1024; char buf[BUFSIZE]; struct tm *stm; time_t t; int sw, sh, vw, vh; char path[BUFSIZE]; snprintf(path, BUFSIZE, "%sscreenshots", GetLocalDir()); // Ensure that screenshot directory exists. if (GfCreateDir(path) == GF_DIR_CREATED) { GfScrGetSize(&sw, &sh, &vw, &vh); img = (unsigned char*)malloc(vw * vh * 3); if (img == NULL) { return; } glPixelStorei(GL_PACK_ROW_LENGTH, 0); glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadBuffer(GL_FRONT); glReadPixels((sw-vw)/2, (sh-vh)/2, vw, vh, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)img); t = time(NULL); stm = localtime(&t); snprintf(buf, BUFSIZE, "%s/torcs-%4d%02d%02d%02d%02d%02d.png", path, stm->tm_year+1900, stm->tm_mon+1, stm->tm_mday, stm->tm_hour, stm->tm_min, stm->tm_sec); GfImgWritePng(img, buf, vw, vh); free(img); } }