📄 vm_file_win.c
字号:
/*
//
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright(c) 2003-2007 Intel Corporation. All Rights Reserved.
//
*/
/*
* VM 64-bits buffered file operations library
* Windows implementation
*/
/* codecws compilation fence */
#if !defined(LINUX32) && !defined(OSX)
# include "ippdefs.h"
# include "vm_strings.h"
# include "vm_file.h"
#define DELETE_VM_FILE(A) \
if (A != NULL) { \
if (A[0].tbuf != NULL) \
free((void *)A[0].tbuf); \
free((void *)A); \
A = NULL; \
}
#ifdef THREAD_SAFETY
# define LOCK vm_mutex_lock(fd[0].fd)
# define UNLOCK vm_mutex_unlock(fd[0].fd)
#else
# define LOCK
# define UNLOCK
#endif
#define VM_MAX_TEMP_LINE 8192
static vm_char temporary_path[MAX_PATH];
static BOOL tmpup = FALSE;
/*
* low level temporary file support data
* structures and functions
*/
typedef struct {
void *next;
void *prev;
vm_file *handle;
vm_char *path;
} vm_temp_file_entry;
static vm_temp_file_entry *temp_list_head;
static void tempfile_add_entry(vm_temp_file_entry *r) {
if (temp_list_head != NULL) {
r[0].next = temp_list_head;
temp_list_head[0].prev = r;
temp_list_head = r;
} else
temp_list_head = r;
}
static void tempfile_remove_entry(vm_temp_file_entry *r) {
vm_temp_file_entry *p, *q;
BOOL sts;
sts = TRUE;
if ((p = temp_list_head) != NULL) {
while( sts ) {
if(p == r) {
if (p[0].prev == NULL) {
q = (vm_temp_file_entry *)p[0].next;
if (q != NULL)
q[0].prev = NULL;
temp_list_head = q;
} else {
q = (vm_temp_file_entry *)p[0].prev;
q[0].next = p[0].next;
q = (vm_temp_file_entry *)p[0].next;
q[0].prev = p[0].prev;
if (q[0].prev == NULL)
temp_list_head = q;
}
free(p[0].path);
free(p);
p = NULL;
} else
p = (vm_temp_file_entry *)p[0].next;
sts = (p != NULL) ;
}
}
}
static void tempfile_delete_by_handle(vm_file *fd) {
vm_temp_file_entry *p;
if ((p = temp_list_head) != NULL) {
while(p != NULL)
if (p[0].handle == fd) {
vm_file_remove(p[0].path);
tempfile_remove_entry(p);
return;
} else
p = (vm_temp_file_entry *)p[0].next;
}
}
static void tempfile_delete_all(void) {
vm_temp_file_entry *p, *q;
if ((p = temp_list_head) != NULL)
while ( p != NULL) {
q = p;
p = (vm_temp_file_entry *)p[0].next;
vm_file_close(q[0].handle); /* close and delete if temporary file is being closed */
}
}
void vm_file_remove_all_temporary( void ) {
tempfile_delete_all();
}
static vm_file *tempfile_create_file(vm_char *fname) {
Ipp32u nlen;
vm_temp_file_entry *t;
vm_file *rtv;
rtv = NULL;
nlen = vm_string_strlen(fname)+1;
t = malloc(sizeof(vm_temp_file_entry));
if (t != NULL) {
t[0].handle = NULL;
t[0].next = t[0].prev = NULL;
t[0].path = malloc(nlen*sizeof(vm_char));
if(t[0].path != NULL) {
vm_string_strcpy(t[0].path, fname);
if((t[0].handle = vm_file_fopen(t[0].path, _T("w+"))) != NULL) {
rtv = t[0].handle;
t[0].handle[0].ftemp = VM_TEMPORARY_PREFIX;
tempfile_add_entry(t);
}
}
}
return rtv;
}
static void tempfile_check(void) {
if (!tmpup) {
#ifndef _WIN32_WCE
if (GetTempPath(MAX_PATH, temporary_path) == 0 )
vm_string_strcpy(temporary_path, _T("C:\\"));
#else
vm_string_strcpy(temporary_path, _T("\\Windows"));
#endif
atexit(tempfile_delete_all);
}
tmpup = TRUE;
}
vm_file *vm_file_tmpfile(void) {
vm_file *rtv;
vm_char *nbf;
tempfile_check();
rtv = NULL;
nbf = (vm_char *)malloc(MAX_PATH*sizeof(vm_char));
if (nbf != NULL) {
if(vm_file_tmpnam(nbf) != NULL)
rtv = tempfile_create_file(nbf);
free(nbf);
}
return rtv;
}
vm_char *vm_file_tmpnam(vm_char *RESULT) {
vm_char *rtv;
tempfile_check();
rtv = NULL;
if (RESULT == NULL)
RESULT = (vm_char *)malloc(MAX_PATH*sizeof(vm_char));
if (RESULT != NULL)
if (GetTempFileName(temporary_path, _T("vmt"), 0, RESULT) != 0) {
rtv = RESULT;
DeleteFile(rtv); /* we need only name, not a file */
}
return rtv;
}
vm_char *vm_file_tmpnam_r(vm_char *RESULT) {
tempfile_check();
if (RESULT != NULL)
vm_file_tmpnam(RESULT);
return RESULT;
}
vm_char *vm_file_tempnam(vm_char *DIR, vm_char *PREFIX) {
vm_char *nbf;
tempfile_check();
nbf = (vm_char *)malloc(MAX_PATH*sizeof(vm_char));
if ((DIR == NULL) || (PREFIX == NULL))
vm_file_tmpnam(nbf);
else
if (GetTempFileName(DIR, PREFIX, 0, nbf) != 0) {
DeleteFile(nbf); /* we need only name, not a file */
} else {
free(nbf);
nbf = NULL;
}
return nbf;
}
/*
* file access functions
*/
vm_file *vm_file_fopen(const vm_char *fname, const vm_char *mode) {
Ipp32u i;
DWORD mds[4];
vm_char general ;
vm_char islog ;
vm_file *rtv;
general = islog = 0;
rtv = (vm_file *)malloc(sizeof(vm_file));
if (rtv != NULL) {
rtv[0].fd = NULL;
rtv[0].fsize = 0;
rtv[0].fattributes = 0;
/*#ifdef _WIN32_WCE*/
rtv[0].ftemp = 0;
/*#endif*/
vm_file_getinfo(fname, &rtv[0].fsize, &rtv[0].fattributes);
memset((void *)&rtv[0].lck, 0, sizeof(vm_mutex));
vm_mutex_init(&rtv[0].lck);
rtv[0].tbuf = (vm_char *)malloc(VM_MAX_TEMP_LINE*sizeof(vm_char));
if ((rtv[0].tbuf != NULL) && vm_mutex_is_valid(&rtv[0].lck)) {
/* prepare dwDesiredAccess */
mds[0] = 0;
for(i = 0; mode[i] != 0; ++i)
switch (mode[i]) {
case 'w' : mds[0] |= GENERIC_WRITE;
mds[2] = CREATE_ALWAYS;
mds[1] = 0;
general = 'w';
break;
case 'r' : mds[0] |= GENERIC_READ;
mds[2] = OPEN_EXISTING;
mds[1] = FILE_SHARE_READ;
general = 'r';
break;
case 'a' : mds[0] |= (GENERIC_WRITE | GENERIC_READ);
mds[2] = OPEN_ALWAYS;
mds[1] = 0;
general = 'a';
break;
case '+' : mds[0] |= (GENERIC_WRITE | GENERIC_READ);
mds[1] = 0;
break;
case 'l' : islog = 1; /* non-buffered file - for log files */
}
mds[3] = FILE_ATTRIBUTE_NORMAL | (islog == 0) ? 0 : FILE_FLAG_NO_BUFFERING;
rtv[0].fd = CreateFile((LPCTSTR)fname, mds[0], mds[1], NULL, mds[2], mds[3], NULL);
if (rtv[0].fd == INVALID_HANDLE_VALUE) {
DELETE_VM_FILE(rtv);
}
else {
/* check file open mode and move file pointer to the end of file
* if need it */
if (general == 'a')
vm_file_fseek(rtv, 0, VM_FILE_SEEK_END);
}
}
}
return rtv; // handle created - have to be destroyed by close function
}
Ipp64u vm_file_fseek(vm_file *fd, Ipp64s position, VM_FILE_SEEK_MODE mode){
Ipp64u rtv = 1;
DWORD posmd = 0;
union {
Ipp32s lpt[2];
Ipp64s hpt;
} pwt;
pwt.hpt = position;
switch (mode) {
case VM_FILE_SEEK_END : posmd = FILE_END;
break;
case VM_FILE_SEEK_SET : posmd = FILE_BEGIN;
break;
case VM_FILE_SEEK_CUR : posmd = FILE_CURRENT;
break;
}
if ((fd != NULL) && (fd[0].fd != INVALID_HANDLE_VALUE)) {
LOCK;
pwt.lpt[0] = SetFilePointer(fd[0].fd, pwt.lpt[0], (LONG *)&pwt.lpt[1], posmd);
if ((pwt.lpt[0] != -1) || (GetLastError() == NO_ERROR))
rtv = 0;
UNLOCK;
}
return rtv;
};
Ipp64u vm_file_ftell(vm_file *fd) {
Ipp64u rtv = 0;
union {
Ipp32s lpt[2];
Ipp64s hpt;
} pwt;
pwt.hpt = 0; // query current file position
if ((fd != NULL) && (fd[0].fd != INVALID_HANDLE_VALUE)) {
LOCK;
pwt.lpt[0] = SetFilePointer(fd[0].fd, pwt.lpt[0], (LONG *)&pwt.lpt[1], FILE_CURRENT);
if ((pwt.lpt[0] != -1) || (GetLastError() == NO_ERROR))
rtv = (Ipp64u)pwt.hpt;
UNLOCK;
}
return rtv;
};
Ipp32s vm_file_remove( vm_char *path) {
return DeleteFile(path);
}
Ipp32s vm_file_fclose(vm_file *fd){
Ipp32s rtv = 0;
if ((fd != NULL) && (fd[0].fd != INVALID_HANDLE_VALUE)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -