/*====================================================================*\ FILE : UtilFiles.c PURPOSE : Provide some file utilities HISTORY.. DATE BUILD AUTHOR MODIFICATIONS 06-Mar-96 G-03-05 dmp $$1 Created 21-Mar-96 G-03-07 dmp $$2 include "UtilMessage.h" 22-Apr-96 G-03-11 mgs $$3 Changed header scheme 24-Sep-96 H-01-10 Xuekai $$4 Remove "protk.h" 30-Jun-97 H-03-15 Pavel $$5 #include "UtilString.h 06-Oct-97 H-03-25 Pavel $$6 more types support 13-Oct-97 H-03-26 Pavel $$7 Added ProUtilGetMdlTypeStr 22-Jan-98 H-03-37 aab $$8 Replaced PRO_TK_BAD_INPUTS by PRO_TYPE_INVALID 30-Oct-98 I-01-25 Agsh $$9 Added ProUtilFileOpen 01-Jun-99 I-03-12 mka $$10 More includes \*====================================================================*/ /*--------------------------------------------------------------------*\ Pro/TOOLKIT includes \*--------------------------------------------------------------------*/ #include #include #include #include /*--------------------------------------------------------------------*\ Application includes \*--------------------------------------------------------------------*/ #include "TestError.h" #include "UtilString.h" #include "UtilMessage.h" /*--------------------------------------------------------------------*\ Application global/external data \*--------------------------------------------------------------------*/ static struct str_to_type { char *str; ProType type; } str_type_table[] = { {"sec", PRO_2DSECTION}, {"prt", PRO_PART}, {"asm", PRO_ASSEMBLY}, {"drw", PRO_DRAWING}, {"mfg", PRO_MFG}, {"lay", PRO_LAYOUT}, {"frm", PRO_DWGFORM}, {"rep", PRO_REPORT}, {"mrk", PRO_MARKUP}, {"dgm", PRO_DIAGRAM}, {"???", PRO_TYPE_INVALID} }; /*====================================================================*\ Function : ProUtilGetProType Purpose : looks up the type for a specified string \*====================================================================*/ ProType ProUtilGetProType( char *type_str ) { int table_size; int i; table_size = sizeof(str_type_table)/sizeof(struct str_to_type); for( i = 0; i < table_size - 1; i++ ) { if( !ProUtilStrcmp(type_str, str_type_table[i].str) ) return( str_type_table[i].type ); } return( str_type_table[table_size-1].type ); } /*====================================================================*\ Function : ProUtilGetMdlTypeStr Purpose : looks up the string for a specified type \*====================================================================*/ char *ProUtilGetMdlTypeStr( ProMdlType mdltype ) { int table_size; int i; table_size = sizeof(str_type_table)/sizeof(struct str_to_type); for( i = 0; i < table_size - 1; i++ ) { if( str_type_table[i].type == (ProType)mdltype ) return( str_type_table[i].str ); } return( str_type_table[table_size-1].str ); } char *ProUtilModelnameGet(ProMdl *model, char *name, char *type); /*====================================================================*\ FUNCTION : UserGenFilename PURPOSE : Generate a name for an output file INPUT : p_obj - the model filext - file extension permission - file permission used to open the file OUTPUT : filename - file name used to open the file \*====================================================================*/ FILE *ProUtilGenFilePtr(ProMdl p_obj, char *filext, char *filename, char *permission) { char model_name[PRO_NAME_SIZE]; char type[PRO_EXTENSION_SIZE]; char *error; FILE *fp; char str[PRO_LINE_SIZE]; error = ProUtilModelnameGet(&p_obj, model_name, type); if( error == NULL ) return( NULL ); else strcpy(filename, model_name); strcat(filename,filext); if( (fp = fopen(filename, permission)) == NULL ) { sprintf(str, "Could not open the file '%s'", filename); ProUtilMsgPrint("gen", "USER %0s", str); } return(fp); } /*====================================================================*\ Function : ProUtilConfirmNameType Purpose : Confirm a valid name and type \*====================================================================*/ int ProUtilConfirmNameType(char *input_name, char *name, ProType *type) { char *dot_occur, /* occurrence of a dot in input_name */ *slash_occur, /* occurence of a slash */ type_str[PRO_EXTENSION_SIZE]; int name_length, /* length of a part name */ type_length; /* lenght of a part type */ /*** if there is a full path name, extract the filename first ***/ if((slash_occur = strrchr(input_name, '/')) != NULL) strcpy(input_name, &slash_occur[1]); /*** get the position of the dot in a file name ***/ if((dot_occur = strchr(input_name, '.')) != NULL) { /*** extract name ***/ if(( name_length = strlen(input_name) - strlen(dot_occur))>0) { strncpy(name,input_name,name_length); name[name_length] = '\0'; } else return( PRO_TK_BAD_INPUTS ); /*** extract type ***/ if ((type_length = strlen( &dot_occur[1] ))==3) { strncpy(type_str, &dot_occur[1], type_length); type_str[3]='\0'; /* get the mode */ return( (*type = ProUtilGetProType(type_str)) ); } else return( PRO_TK_BAD_INPUTS ); } else return( PRO_TK_BAD_INPUTS ); } /*============================================================================*\ Function: ProUtilFileOpen Purpose: Select file from disk \*============================================================================*/ ProError ProUtilFileOpen( char *extension, char *file_name) { ProLine w_ext; ProPath *w_path_arr, w_def_path, w_sel_path; ProName *w_path_lab_arr; ProError err; ProStringToWstring(w_def_path, "."); ProStringToWstring(w_ext, extension); /* No default dirs */ err = ProArrayAlloc(0, sizeof(ProPath), 1, (ProArray*)&w_path_arr); err = ProArrayAlloc(0, sizeof(ProPath), 1, (ProArray*)&w_path_lab_arr); /* Open file */ err = ProFileOpen(NULL, w_ext, w_path_arr, w_path_lab_arr, w_def_path, NULL, w_sel_path); TEST_CALL_REPORT("ProFileOpen()", "ProUtilFileOpen()", err, err != PRO_TK_NO_ERROR); ProArrayFree((ProArray*)&w_path_arr); ProArrayFree((ProArray*)&w_path_lab_arr); ProWstringToString(file_name, w_sel_path); return(err); }