static void create_directory(const path &p) { if(!CreateDirectory(p.directory_str().c_str(), NULL)) { TCHAR chBuf[2048]; swprintf_s(chBuf, _countof(chBuf), L"Can't create directory:[%s]!!", p.buf); TRACEERR("[path] [create_directory]", chBuf, GetLastError()); throw path_error("CreateDirectory"); } }
static void move(const path &from, const path &to, bool replace=false) { if(!MoveFileEx(from.buf, to.buf, replace?MOVEFILE_REPLACE_EXISTING:0)) { TCHAR chBuf[1024]; swprintf_s(chBuf, _countof(chBuf), L"Can't move file from:[%s] to:[%s]!!", from.buf, to.buf); TRACEERR("[path] [move]", chBuf, GetLastError()); throw path_error("MoveFileEx"); } }
static void remove(const path &p) { if(exists(p) && !DeleteFile(p.buf)) { TCHAR chBuf[1024]; swprintf_s(chBuf, _countof(chBuf), L"Can't delete file [%s]!!", p.buf); TRACEERR("[path] [remove]", chBuf, GetLastError()); throw path_error("DeleteFile"); } }
static void copy(const path &from, const path &to, bool replace=false) { if(replace && exists(to)) remove(to); if(!CopyFile(from.buf, to.buf, true)) { TCHAR chBuf[2048]; swprintf_s(chBuf, _countof(chBuf), L"Can't copy file from:[%s] to:[%s]!!", from.buf, to.buf); TRACEERR("[path] [copy]", chBuf, GetLastError()); throw path_error("CopyFile"); } }
static path relative_to(const path &from, const path &to, bool fromdir=true) { if(PathIsSameRoot(from.buf, to.buf)) { path p; if(!PathRelativePathTo(p.buf, from.buf, fromdir?FILE_ATTRIBUTE_DIRECTORY:0, to.buf, 0)) { TCHAR chBuf[1024]; swprintf_s(chBuf, _countof(chBuf), L"PathRelativePathTo() from:[%s] to:[%s] failed!!", from.buf, to.buf); TRACEERR("[path] [relative_to]", chBuf, GetLastError()); throw path_error("PathRelativePathTo"); } return p; } else if(to.has_root()) return to; else { TRACEE("[path] [relative_to] * ERROR: unable to create relative path: paths have different roots!!"); throw std::runtime_error("unable to create relative path: paths have different roots"); } }
pnm_img *pnm_read(const char file_name[]){ FILE* fp; int status; int p; char buffer[MAX_LINE_SIZE]; // open file fp = fopen(file_name,"r"); if(fp == NULL) { path_error(file_name); return NULL; } // get PPM type read_line(buffer,MAX_LINE_SIZE,fp); if(buffer[0] != 'P') { printf("Bad header format\n"); return NULL; } p = atoi(buffer+1); // read past any comments do { read_line(buffer,MAX_LINE_SIZE,fp); } while(buffer[0] == '#'); // get dimensions char* current_position; int width = strtol(buffer,¤t_position,10); if (*current_position == '\n'){ read_line(buffer,MAX_LINE_SIZE,fp); current_position = buffer; } int height = strtol(current_position,¤t_position,10); pnm_img* img = pnm_create(width, height, p); if (*current_position == '\n'){ read_line(buffer,MAX_LINE_SIZE,fp); current_position = buffer; } img->max_color = strtol(current_position,¤t_position,10); // extract pixel data if(p == 5 || p == 6){ fread(img->pixels, sizeof(char),height*width*num_channels(p),fp); } else{ int R, G, B; if (num_channels(p) == 3){ pnm_pixmap* pm = (pnm_pixmap*)img->pixels; for(int i = 0; i< height*width; i++){ status = fscanf(fp," %i %i %i", &R, &G, &B); if(status == EOF) { printf("Bad file format\n"); return NULL; } pm[i].R = (unsigned char)R; pm[i].G = (unsigned char)G; pm[i].B = (unsigned char)B; } } else{ int grey; for(int i = 0; i< height*width; i++){ status = fscanf(fp," %i", &grey); if(status == EOF) { printf("Bad file format\n"); return NULL; } *((unsigned char*)img->pixels+i) = (unsigned char)grey; } } } fclose(fp); return img; }