// read .bin-file into buffer unsigned char* HGPU_io_bin_file_read(int inf_index,size_t* binary_size){ unsigned char* result = NULL; FILE* file_to_read; char* buffer_bin = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); char* buffer_inf = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); if ((!buffer_bin) || (!buffer_inf)) HGPU_error(HGPU_ERROR_NO_MEMORY); sprintf_s(buffer_inf,HGPU_FILENAME_MAX,"program%u.bin",inf_index); HGPU_io_path_join_filename(&buffer_bin,HGPU_FILENAME_MAX,HGPU_io_path_inf,buffer_inf); if (HGPU_io_file_check_existence(buffer_bin)) { fopen_s(&file_to_read,buffer_bin,"rb"); fseek (file_to_read, 0, SEEK_END); *binary_size = ftell(file_to_read); rewind(file_to_read); result = (unsigned char*) calloc ((*binary_size) + 2,sizeof(unsigned char)); if (!result) HGPU_error(HGPU_ERROR_NO_MEMORY); size_t file_read_length = fread(result,1,(*binary_size),file_to_read); if (!file_read_length) free(result); if (fclose(file_to_read)) HGPU_error_note(HGPU_ERROR_FILE_NOT_CLOSED,"the file was not closed"); (*binary_size) = file_read_length; } free(buffer_bin); free(buffer_inf); return result; }
// file read and add to existing buffer void HGPU_io_file_read_add_with_path(char** source,const char* file_path,const char* file_name){ char* buffer = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); if (!buffer) HGPU_error(HGPU_ERROR_NO_MEMORY); HGPU_io_path_join_filename(&buffer,HGPU_FILENAME_MAX,file_path,file_name); HGPU_io_file_read_add(source,buffer); free(buffer); }
// read file into buffer char* HGPU_io_file_read_with_path(const char* file_path,const char* file_name){ char* buffer = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); if (!buffer) HGPU_error(HGPU_ERROR_NO_MEMORY); HGPU_io_path_join_filename(&buffer,HGPU_FILENAME_MAX,file_path,file_name); char* result = HGPU_io_file_read(buffer); free(buffer); return result; }
// get path void HGPU_io_path_get(char** path){ if (!path) HGPU_error(HGPU_ERROR_ARRAY_OUT_OF_BOUND); size_t path_length = HGPU_FILENAME_MAX; char* path_new = *path; if (!path_new) HGPU_string_resize(&path_new,path_length); if (!GetCurrentDir(path_new,(int) path_length)) HGPU_error_message(HGPU_ERROR_DEVICE_INITIALIZATION_FAILED,"could not obtain inf path"); HGPU_io_path_set_separator(&path_new,path_length); *path = path_new; }
// add buffer to array of buffers void HGPU_GPU_buffers_add(HGPU_GPU_buffer*** buffers,HGPU_GPU_buffer* buffer){ if ((!buffer) || (!buffer)) return; unsigned int number_of_buffers = HGPU_GPU_buffers_get_number(*buffers); HGPU_GPU_buffer** buffers_new = (HGPU_GPU_buffer**) realloc(*buffers,(number_of_buffers+2)*sizeof(HGPU_GPU_buffer*)); if (!buffers_new) HGPU_error(HGPU_ERROR_NO_MEMORY); buffers_new[number_of_buffers] = buffer; buffers_new[number_of_buffers+1] = NULL; (*buffers) = buffers_new; }
// write buffer into file with path void HGPU_io_file_write_with_path(const char* file_path,const char* file_name,const char* buffer){ char* filename_with_path = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); if (!filename_with_path) HGPU_error(HGPU_ERROR_NO_MEMORY); HGPU_io_path_join_filename(&filename_with_path,HGPU_FILENAME_MAX,file_path,file_name); FILE* file_for_write; fopen_s(&file_for_write,filename_with_path,"w+"); free(filename_with_path); if(file_for_write){ fprintf(file_for_write,buffer); if (fclose(file_for_write)) HGPU_error_note(HGPU_ERROR_FILE_NOT_CLOSED,"the file was not closed"); } else HGPU_error_message(HGPU_ERROR_FILE_NOT_FOUND,"error writting file"); }
// check file existance with path bool HGPU_io_file_check_existence_with_path(const char* file_path,const char* file_name){ FILE * is_file; bool flag = false; if (!file_name) return flag; char* buffer = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); if (!buffer) HGPU_error(HGPU_ERROR_NO_MEMORY); HGPU_io_path_join_filename(&buffer,HGPU_FILENAME_MAX,file_path,file_name); fopen_s(&is_file,buffer,"r"); if (is_file) { flag = true; fclose(is_file); } free(buffer); return flag; }
// delete .inf and .bin files int HGPU_io_inf_file_delete(int inf_index){ int err; char* buffer_path = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); char* buffer_inf = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); if ((!buffer_inf) || (!buffer_path)) HGPU_error(HGPU_ERROR_NO_MEMORY); sprintf_s(buffer_inf,HGPU_FILENAME_MAX,"program%u.inf",inf_index); HGPU_io_path_join_filename(&buffer_path,HGPU_FILENAME_MAX,HGPU_io_path_inf,buffer_inf); err = remove(buffer_path); // kill .inf-file if (!err){ sprintf_s(buffer_inf,HGPU_FILENAME_MAX,"program%u.bin",inf_index); HGPU_io_path_join_filename(&buffer_path,HGPU_FILENAME_MAX,HGPU_io_path_inf,buffer_inf); err = remove(buffer_path); // kill .bin-file } free(buffer_inf); free(buffer_path); return err; }
// combine path with filename void HGPU_io_path_join_filename(char** file_path_and_name,const size_t file_path_and_name_length,const char* file_path,const char* file_name){ if (!file_path_and_name) HGPU_error(HGPU_ERROR_ARRAY_OUT_OF_BOUND); size_t j = 0; char* file_new = *file_path_and_name; size_t f_length = strlen(file_name)+2; if (file_path) f_length += strlen(file_path); if (file_path_and_name_length<f_length) HGPU_string_resize(&file_new,f_length); if (file_path){ j = sprintf_s(file_new,f_length,"%s",file_path); HGPU_io_path_set_separator(&file_new,f_length); j = strlen(file_new); } j += sprintf_s(file_new+j,f_length-j,"%s",file_name); #ifdef _WIN32 HGPU_string_char_replace(file_new,92,47); // replace "\\" symbols with "/" in root cl path #endif (*file_path_and_name) = file_new; }
// read file into buffer char* HGPU_io_file_read(const char* file_name){ FILE* file_to_read; char buffer[HGPU_FILENAME_MAX]; sprintf_s(buffer,sizeof(buffer),"%s",file_name); fopen_s(&file_to_read,buffer,"r"); if(!file_to_read){ HGPU_error_message(HGPU_ERROR_FILE_NOT_FOUND,".cl-file not found"); return NULL; } fseek (file_to_read, 0, SEEK_END); unsigned int file_length = ftell(file_to_read); fseek (file_to_read, 0 , SEEK_SET); char* cl_kernels_source = (char*) calloc(file_length + 2, sizeof(char)); if (!cl_kernels_source) HGPU_error(HGPU_ERROR_NO_MEMORY); unsigned int file_read_length = (unsigned int) fread(cl_kernels_source, sizeof(char), file_length, file_to_read); if (!file_read_length) free(cl_kernels_source); if (fclose(file_to_read)) HGPU_error_note(HGPU_ERROR_FILE_NOT_CLOSED,"the file was not closed"); return cl_kernels_source; }
// rename .inf and .bin files int HGPU_io_inf_file_rename(int inf_index_old,int inf_index_new){ int err = 0; char* buffer_path_old = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); char* buffer_path_new = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); char* buffer_inf_old = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); char* buffer_inf_new = (char*) calloc(HGPU_FILENAME_MAX,sizeof(char)); if ((!buffer_path_old) || (!buffer_path_new) || (!buffer_inf_old) || (!buffer_inf_new)) HGPU_error(HGPU_ERROR_NO_MEMORY); sprintf_s(buffer_inf_old,HGPU_FILENAME_MAX,"program%u.inf",inf_index_old); HGPU_io_path_join_filename(&buffer_path_old,HGPU_FILENAME_MAX,HGPU_io_path_inf,buffer_inf_old); sprintf_s(buffer_inf_new,HGPU_FILENAME_MAX,"program%u.inf",inf_index_new); HGPU_io_path_join_filename(&buffer_path_new,HGPU_FILENAME_MAX,HGPU_io_path_inf,buffer_inf_new); err = rename(buffer_path_old,buffer_path_new); // rename .inf-file if (!err) { sprintf_s(buffer_inf_old,HGPU_FILENAME_MAX,"program%u.bin",inf_index_old); HGPU_io_path_join_filename(&buffer_path_old,HGPU_FILENAME_MAX,HGPU_io_path_inf,buffer_inf_old); sprintf_s(buffer_inf_new,HGPU_FILENAME_MAX,"program%u.bin",inf_index_new); HGPU_io_path_join_filename(&buffer_path_new,HGPU_FILENAME_MAX,HGPU_io_path_inf,buffer_inf_new); err = rename(buffer_path_old,buffer_path_new); // rename .inf-file } free(buffer_path_old); free(buffer_path_new); free(buffer_inf_old); free(buffer_inf_new); return err; }