#include "stdafx.h" #include "DirectoryBrowser.h" // ---------------------------------------------------------------------------------------- // FileBrowser // // Description: Simply returns file w/full path selected by user (false if no file is selected) // ---------------------------------------------------------------------------------------- bool SimpleFileBrowser( char *file_name_str ) { OPENFILENAME ofn; // common dialog box structure char dirFile[MAX_FILE_STR_LEN]; // buffer for file name and path char nameFile[MAX_FILE_STR_LEN]; // buffer just for file name and extension // Initialize OPENFILENAME ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); // length of structure, Confirmed ofn.hwndOwner = NULL; // no owner //hInstance not needed ofn.lpstrFilter = "All\0*.*\0BMP\0*.bmp\0RAW\0*.raw\0BIN\0*.bin\0RAW STACK\0*.stack.raw\0BIN STACK\0*.stack.bin\0CBIN STACK\0*.stack.cbin\0"; ofn.nFilterIndex = 1; // BMP Image as default file type ofn.lpstrFile = dirFile; // This is where the path and file name the user selects is stored ofn.lpstrFile[0] = '\0'; //'\0' so that GetOpenFileName does not use the contents of szFile to initialize itself ofn.nMaxFile = sizeof(dirFile); // max file name size ofn.lpstrFileTitle = nameFile; // where just the name and ext of selected file is stored ofn.nMaxFileTitle = sizeof(nameFile); ofn.lpstrInitialDir = NULL; ofn.lpstrTitle = "Select the input file:"; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; // Display the Open dialog box. if ( GetOpenFileName(&ofn)==TRUE ) { sprintf( file_name_str, "%s", ofn.lpstrFile ); return true; } else { return false; } } // ---------------------------------------------------------------------------------------- // FileBrowser // // Description: Obtains input file and parses file // ---------------------------------------------------------------------------------------- input_info* FileBrowser(void) { /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/commondialogboxlibrary/usingcommondialogboxes.asp http://msdn2.microsoft.com/en-us/library/ms646829.aspx */ input_info* info = new input_info; OPENFILENAME ofn; // common dialog box structure char dirFile[MAX_FILE_STR_LEN]; // buffer for file name and path char nameFile[MAX_FILE_STR_LEN]; // buffer just for file name and extension info->directory = (char*) malloc(sizeof(char)*MAX_FILE_STR_LEN); info->filename = (char*) malloc(sizeof(char)*MAX_FILE_STR_LEN); info->basename = (char*) malloc(sizeof(char)*MAX_FILE_STR_LEN); info->full_path_name = (char*) malloc(sizeof(char)*MAX_FILE_STR_LEN); // Initialize OPENFILENAME ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); // length of structure, Confirmed ofn.hwndOwner = NULL; // no owner //hInstance not needed ofn.lpstrFilter = "All\0*.*\0BMP\0*.bmp\0RAW\0*.raw\0BIN\0*.bin\0RAW STACK\0*.stack.raw\0BIN STACK\0*.stack.bin\0CBIN STACK\0*.stack.cbin\0"; ofn.nFilterIndex = 1; // BMP Image as default file type ofn.lpstrFile = dirFile; // This is where the path and file name the user selects is stored ofn.lpstrFile[0] = '\0'; //'\0' so that GetOpenFileName does not use the contents of szFile to initialize itself ofn.nMaxFile = sizeof(dirFile); // max file name size ofn.lpstrFileTitle = nameFile; // where just the name and ext of selected file is stored ofn.nMaxFileTitle = sizeof(nameFile); ofn.lpstrInitialDir = NULL; ofn.lpstrTitle = "Select input file (or first slice):"; /* */ ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; // Display the Open dialog box. if (GetOpenFileName(&ofn)==TRUE) { CString input_fname, base_fname; int left_bracket_indx, offset, num_cnt = 0; ofn.lpstrFile[ofn.nFileOffset] = '\0'; strcpy(info->directory, ofn.lpstrFile); strcpy(info->filename, ofn.lpstrFileTitle); strcpy(info->full_path_name, info->directory); strcat(info->full_path_name, info->filename); input_fname = info->filename; //determine type of input file if (input_fname.GetLength() > 4 ) { //.bmp file if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 4, ".bmp") == 0) { info->input_type = INPUT_IS_BMP; offset = input_fname.GetLength() - 5; } //.raw file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 4, ".raw") == 0) { //.1b.raw file if ( (input_fname.GetLength() > 7) && (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 7, ".1b.raw") == 0) ) { info->input_type = INPUT_IS_CBIN; offset = input_fname.GetLength() - 8; } else if( (input_fname.GetLength() > 7) && (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 7, ".8b.raw") == 0) ) { info->input_type = INPUT_IS_RAW; offset = input_fname.GetLength() - 8; } else { info->input_type = INPUT_IS_RAW; offset = input_fname.GetLength() - 5; } } //.bin file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 4, ".bin") == 0) { offset = input_fname.GetLength() - 5; } //.cbin file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 5, ".cbin") == 0) { info->input_type = INPUT_IS_CBIN; offset = input_fname.GetLength() - 6; } else //since .stack is attached to either .bin or .raw, error if no 3 char ext found { info->input_type = INPUT_IS_UNKNOWN; return info; } //now check for a possible .stack prefix if (input_fname.GetLength() > 10) { //.stack.raw file if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 10, ".stack.raw") == 0) { info->input_type = INPUT_IS_RAW_STACK; strcpy(info->basename, info->filename); info->basename[input_fname.GetLength() - 10] = '\0'; } //.stack.bin file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 10, ".stack.bin") == 0) { info->input_type = INPUT_IS_BIN_STACK; strcpy(info->basename, info->filename); info->basename[input_fname.GetLength() - 10] = '\0'; } //.stack.cbin file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 11, ".stack.cbin") == 0) { info->input_type = INPUT_IS_CBIN_STACK; strcpy(info->basename, info->filename); info->basename[input_fname.GetLength() - 11] = '\0'; } //.1b.raw stack file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 7, ".1b.raw") == 0) { info->input_type = INPUT_IS_CBIN_STACK; strcpy(info->basename, info->filename); info->basename[input_fname.GetLength() - 7] = '\0'; } //.8b.raw stack file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 7, ".8b.raw") == 0) { info->input_type = INPUT_IS_RAW_STACK; strcpy(info->basename, info->filename); info->basename[input_fname.GetLength() - 7] = '\0'; } } } else { info->input_type = INPUT_IS_UNKNOWN; return info; } // if the input is a stack, it's just one file, so don't do anything fancy, just return the file name and base name (no ext) if ( !((info->input_type == INPUT_IS_RAW_STACK) || (info->input_type == INPUT_IS_BIN_STACK) || (info->input_type == INPUT_IS_CBIN_STACK)) ) { //attach a ] at end input_fname.Insert(input_fname.GetLength(), ']'); //go backwards from end and find first not-number char 48 = '0', 57 = '9' while ( (offset >= 0) && (input_fname[offset] >= '0') && (input_fname[offset] <= '9') ) { num_cnt++; offset--; } if (offset < 0) return NULL; //default outname prefix will be the base name (anything _021... or 02... will be removed) for (int i = 0; i < offset; i++) { base_fname.Insert(i, input_fname[i]); } if (input_fname[offset] != '_') { base_fname.Insert(offset, input_fname[offset]); } //if the input slice has more than 3 digits at end if (num_cnt > 3) { //this will be the index where the '[' gets inserted for a 4 digit num left_bracket_indx = offset + num_cnt - 3; num_cnt = 4; } //if the input slice has less than 4 digits else { //this will be index of '[' left_bracket_indx = offset + 1; } //convert everything in front of '[' to an integer, to place in the 'start slice' box info->max_dig_cnt = num_cnt; info->attached_number = atoi(input_fname.GetBuffer()+left_bracket_indx); //deternine number of attached digits info->attached_number_dig_cnt = 1; while (info->attached_number >= pow(10.0f, info->attached_number_dig_cnt) ) { info->attached_number_dig_cnt++; } //if the resulting number has zeros in front, move the '[' index forwards to first real digit left_bracket_indx = left_bracket_indx + num_cnt - info->attached_number_dig_cnt; //insert the '[' input_fname.Insert(left_bracket_indx, '['); info->left_bracket_indx = left_bracket_indx; strcpy(info->basename, base_fname.GetBuffer()); base_fname.ReleaseBuffer(); strcpy(info->filename, input_fname.GetBuffer()); input_fname.ReleaseBuffer(); } return info; } else { return NULL; } } // ---------------------------------------------------------------------------------------- // FileBrowser // // Description: Obtains automatically (no user dialog) input file and parses file // ---------------------------------------------------------------------------------------- input_info* FileBrowser_auto( char *directory_name_str, char *file_name_str ) { //get all the parameter values from the dialog //RW_Dialog* dlg = (RW_Dialog*) dlg_share; /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/commondialogboxlibrary/usingcommondialogboxes.asp http://msdn2.microsoft.com/en-us/library/ms646829.aspx */ input_info* info = new input_info; OPENFILENAME ofn; // common dialog box structure char dirFile[MAX_FILE_STR_LEN]; // buffer for file name and path char nameFile[MAX_FILE_STR_LEN]; // buffer just for file name and extension info->directory = (char*) malloc(sizeof(char)*MAX_FILE_STR_LEN); info->filename = (char*) malloc(sizeof(char)*MAX_FILE_STR_LEN); info->basename = (char*) malloc(sizeof(char)*MAX_FILE_STR_LEN); info->full_path_name = (char*) malloc(sizeof(char)*MAX_FILE_STR_LEN); // Initialize OPENFILENAME ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); // length of structure, Confirmed ofn.hwndOwner = NULL; // no owner //hInstance not needed ofn.lpstrFilter = "All\0*.*\0BMP\0*.bmp\0RAW\0*.raw\0BIN\0*.bin\0RAW STACK\0*.stack.raw\0BIN STACK\0*.stack.bin\0CBIN STACK\0*.stack.cbin\0"; ofn.nFilterIndex = 1; // BMP Image as default file type ofn.lpstrFile = dirFile; // This is where the path and file name the user selects is stored ofn.lpstrFile[0] = '\0'; //'\0' so that GetOpenFileName does not use the contents of szFile to initialize itself ofn.nMaxFile = sizeof(dirFile); // max file name size ofn.lpstrFileTitle = nameFile; // where just the name and ext of selected file is stored ofn.nMaxFileTitle = sizeof(nameFile); ofn.lpstrInitialDir = NULL; ofn.lpstrTitle = "Select input file (or first slice):"; /* */ ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; // Display the Open dialog box. //if (GetOpenFileName(&ofn)==TRUE) { CString input_fname, base_fname; int left_bracket_indx, offset, num_cnt = 0; ofn.lpstrFile[ofn.nFileOffset] = '\0'; //strcpy(info->directory, ofn.lpstrFile); //strcpy(info->filename, ofn.lpstrFileTitle); //strcpy(info->full_path_name, info->directory); //strcat(info->full_path_name, info->filename); strcpy(info->directory, directory_name_str); strcpy(info->filename, file_name_str); strcpy(info->full_path_name, info->directory); strcat(info->full_path_name, info->filename); input_fname = info->filename; //determine type of input file if (input_fname.GetLength() > 4 ) { //.bmp file if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 4, ".bmp") == 0) { info->input_type = INPUT_IS_BMP; offset = input_fname.GetLength() - 5; } //.raw file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 4, ".raw") == 0) { //.1b.raw file if ( (input_fname.GetLength() > 7) && (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 7, ".1b.raw") == 0) ) { info->input_type = INPUT_IS_CBIN; offset = input_fname.GetLength() - 8; } else if( (input_fname.GetLength() > 7) && (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 7, ".8b.raw") == 0) ) { info->input_type = INPUT_IS_RAW; offset = input_fname.GetLength() - 8; } else { info->input_type = INPUT_IS_RAW; offset = input_fname.GetLength() - 5; } } //.bin file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 4, ".bin") == 0) { offset = input_fname.GetLength() - 5; } //.cbin file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 5, ".cbin") == 0) { info->input_type = INPUT_IS_CBIN; offset = input_fname.GetLength() - 6; } else //since .stack is attached to either .bin or .raw, error if no 3 char ext found { info->input_type = INPUT_IS_UNKNOWN; return info; } //now check for a possible .stack prefix if (input_fname.GetLength() > 10) { //.stack.raw file if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 10, ".stack.raw") == 0) { info->input_type = INPUT_IS_RAW_STACK; strcpy(info->basename, info->filename); info->basename[input_fname.GetLength() - 10] = '\0'; } //.stack.bin file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 10, ".stack.bin") == 0) { info->input_type = INPUT_IS_BIN_STACK; strcpy(info->basename, info->filename); info->basename[input_fname.GetLength() - 10] = '\0'; } //.stack.cbin file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 11, ".stack.cbin") == 0) { info->input_type = INPUT_IS_CBIN_STACK; strcpy(info->basename, info->filename); info->basename[input_fname.GetLength() - 11] = '\0'; } //.1b.raw stack file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 7, ".1b.raw") == 0) { info->input_type = INPUT_IS_CBIN_STACK; strcpy(info->basename, info->filename); info->basename[input_fname.GetLength() - 7] = '\0'; } //.8b.raw stack file else if (strcmp( input_fname.GetBuffer() + input_fname.GetLength() - 7, ".8b.raw") == 0) { info->input_type = INPUT_IS_RAW_STACK; strcpy(info->basename, info->filename); info->basename[input_fname.GetLength() - 7] = '\0'; } } } else { info->input_type = INPUT_IS_UNKNOWN; return info; } // if the input is a stack, it's just one file, so don't do anything fancy, just return the file name and base name (no ext) if ( !((info->input_type == INPUT_IS_RAW_STACK) || (info->input_type == INPUT_IS_BIN_STACK) || (info->input_type == INPUT_IS_CBIN_STACK)) ) { //attach a ] at end input_fname.Insert(input_fname.GetLength(), ']'); //go backwards from end and find first not-number char 48 = '0', 57 = '9' while ( (offset >= 0) && (input_fname[offset] >= '0') && (input_fname[offset] <= '9') ) { num_cnt++; offset--; } if (offset < 0) return NULL; //default outname prefix will be the base name (anything _021... or 02... will be removed) for (int i = 0; i < offset; i++) { base_fname.Insert(i, input_fname[i]); } if (input_fname[offset] != '_') { base_fname.Insert(offset, input_fname[offset]); } //if the input slice has more than 3 digits at end if (num_cnt > 3) { //this will be the index where the '[' gets inserted for a 4 digit num left_bracket_indx = offset + num_cnt - 3; num_cnt = 4; } //if the input slice has less than 4 digits else { //this will be index of '[' left_bracket_indx = offset + 1; } //convert everything in front of '[' to an integer, to place in the 'start slice' box info->max_dig_cnt = num_cnt; info->attached_number = atoi(input_fname.GetBuffer()+left_bracket_indx); //deternine number of attached digits info->attached_number_dig_cnt = 1; while (info->attached_number >= pow(10.0f, info->attached_number_dig_cnt) ) { info->attached_number_dig_cnt++; } //if the resulting number has zeros in front, move the '[' index forwards to first real digit left_bracket_indx = left_bracket_indx + num_cnt - info->attached_number_dig_cnt; //insert the '[' input_fname.Insert(left_bracket_indx, '['); info->left_bracket_indx = left_bracket_indx; strcpy(info->basename, base_fname.GetBuffer()); base_fname.ReleaseBuffer(); strcpy(info->filename, input_fname.GetBuffer()); input_fname.ReleaseBuffer(); } return info; } //else //{ // return NULL; //} } // ---------------------------------------------------------------------------------------- // DirectoryBrowser // // Description: Use for selecting a directory dialog // ---------------------------------------------------------------------------------------- input_info* DirectoryBrowser(void) { BROWSEINFO bi; input_info* info = new input_info; char dir_path[MAX_FILE_STR_LEN]; info->directory = (char*)malloc(sizeof(char) *MAX_FILE_STR_LEN); bi.hwndOwner = NULL; bi.pidlRoot = NULL; bi.pszDisplayName = dir_path; bi.lpszTitle = "Select Output Directory"; bi.ulFlags = BIF_NEWDIALOGSTYLE; bi.lpfn = NULL; SHGetPathFromIDList(SHBrowseForFolder(&bi), bi.pszDisplayName); strcpy(info->directory, bi.pszDisplayName); strcat(info->directory, "\\"); info->filename = NULL; return info; }