home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1998 June
/
PCWorld_1998-06_cd.bin
/
software
/
sharware
/
grafika
/
EROICA32
/
_SETUP.3
/
Group2
/
UTFILE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1998-01-15
|
8KB
|
342 lines
/*-------------------------- Parallax Standard C_File ----------------------------
C_File: utfile.c
Purpose: This file contains the source code for the randomized
file selection routines.
--------------------------------------------------------------------------------
Copyright (c)1996 Parallax Software , All rights reserved.
------------------------------------------------------------------------------*/
#include "dde_test.h"
#include "utdde.h"
#include <dos.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static int tmpfilenum = 0;
static char tmpdir[50] = "";
static char base36[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* internal functions */
static void _InitTmpDir();
static int _CopyFile(char FAR* pFileNameTo, char FAR* pFileNameFrom);
static char FAR* _FileName(char FAR* filepath);
/* 970224 jk add redefinitions of far string functions */
#ifdef _WIN32
static char *_fstrcpy(char *string1, const char *string2)
{
return strcpy(string1, string2) ;
}
static char *_fstrcat(char *string1, const char *string2)
{
return strcat(string1, string2) ;
}
static size_t _fstrlen(const char *string)
{
return strlen(string);
}
static char *_fstrchr(const char *string, int c)
{
return strchr(string, c);
}
#endif
/* 970224 jk end */
/* ========================================= Parallax C Function ==================
@Name: _InitTmpDir
@Desc:
============================================================================== */
static void _InitTmpDir()
{
#if 0
int len;
strcpy(tmpdir, getenv("TMP"));
len = strlen(tmpdir);
if (tmpdir[len-1] != '\\')
{
tmpdir[len] = '\\';
tmpdir[len+1] = '\0';
} /* if (tmpdir[len-1] != '\\') */
#else
GetPrivateProfileString("System", "TmpDir", "", tmpdir, sizeof(tmpdir), ".\\dde_test.ini");
if ( *tmpdir == 0 ) {
#ifdef _WIN32
GetTempPath( sizeof(tmpdir), tmpdir );
#else
tmpdir[0] = (char)GetTempDrive(0);
lstrcpy( tmpdir+1, ":\\" );
#endif
}
if ( *tmpdir ) {
if ( tmpdir[lstrlen(tmpdir)-1] != '\\' ) {
lstrcat( tmpdir, "\\" );
}
}
/* strcpy(tmpdir, "C:\\TMP\\"); */
#endif
} /* _InitTmpDir() */
/* ========================================= Parallax C Function ==================
@Name: _FileName
@Desc:
============================================================================== */
static char FAR* _FileName(char FAR* filepath)
{
char FAR* pTmp0 = filepath;
char FAR* pTmp1;
/* get over drive letter */
pTmp1 = _fstrchr(pTmp0, ':');
if (pTmp1 != NULL) pTmp0 = pTmp1 + 1;
/* get over all subdirs */
pTmp1 = pTmp0;
do
{
pTmp0 = pTmp1 + 1;
pTmp1 = _fstrchr(pTmp0, '\\');
} while(pTmp1 != NULL);
return (pTmp0);
} /* _FileName() */
/* ========================================= Parallax C Function ==================
@Name: _CopyFile
@Desc:
============================================================================== */
static int _CopyFile(char FAR* pFileNameTo, char FAR* pFileNameFrom)
{
int ret=TRUE;
FILE* fhA = NULL;
FILE* fhB = NULL;
char fnameA[256];
char fnameB[256];
char buf[16000];
/* copy file names locally */
_fstrcpy(fnameA, pFileNameTo);
_fstrcpy(fnameB, pFileNameFrom);
/* open the files */
fhB = fopen(fnameB, "rb");
if (fhB == NULL)
{
ret = FALSE;
goto cleanup;
} /* if (fhB == NULL) */
fhA = fopen(fnameA, "wb");
if (fhA == NULL)
{
ret = FALSE;
goto cleanup;
} /* if (fhA == NULL) */
/* copy over */
while (!feof(fhB))
{
int n = fread(buf, 1, 16000, fhB);
fwrite(buf, 1, n, fhA);
} /* while (!feof(fhB)) */
cleanup:
if (fhA) fclose(fhA);
if (fhB) fclose(fhB);
return (ret);
} /* _CopyFile() */
/* ========================================= Parallax C Function ==================
@Name: UT_GetTmpFileName
@Desc:
============================================================================== */
/* 970224 jk add to cover the unsupported WIN31 function */
#ifdef _WIN32
static unsigned _dos_getfileattr(const char *path, unsigned *attrib)
{
DWORD temp = GetFileAttributes(path) ;
if (temp == 0xFFFFFFFF)
{
*attrib = 0 ;
return 1 ; /* error */
} else /* if (temp == 0xFFFFFFFF) */
{
*attrib = temp ;
return 0 ;
} /* if (temp == 0xFFFFFFFF) */
} /* _dos_getfileattr() */
#endif
/* 970224 jk end */
void UT_GetTmpFileName(void FAR* pTmpName)
{
int len,i,j;
unsigned attrib;
char fname[256];
if (tmpdir[0]==0) _InitTmpDir();
do
{
tmpfilenum++;
_fstrcpy(fname, tmpdir);
len = strlen(fname);
fname[len++] = 'U';
fname[len++] = 'T';
i = tmpfilenum;
for (j=5; j>=0; j--)
{
fname[len+j] = base36[ i%36 ];
i /= 36;
} /* for (j=5; j>=0; j--) */
len+=6;
fname[len++] = '.';
fname[len++] = 'T';
fname[len++] = 'M';
fname[len++] = 'P';
fname[len++] = '\0' ;
} while(_dos_getfileattr(fname, &attrib) == 0);
_fstrcpy(pTmpName, fname);
} /* GetTmpFileName() */
/* ========================================= Parallax C Function ==================
@Name: UT_GetTmpVersion
@Desc:
============================================================================== */
void UT_GetTmpVersion(void FAR* pFname, void FAR* pTmpName)
{
if (tmpdir[0]==0) _InitTmpDir();
_fstrcpy(pTmpName, tmpdir);
_fstrcat(pTmpName, _FileName(pFname));
_CopyFile(pTmpName, pFname);
} /* UT_GetTmpVersion() */
/* ========================================= Parallax C Function ==================
@Name: UT_DeleteFile
@Desc:
============================================================================== */
void UT_DeleteFile(void FAR* pName)
{
char fname[256];
_fstrcpy(fname, pName);
if ( *fname ) remove(fname);
} /* UT_DeleteFile() */
/* ========================================= Parallax C Function ==================
@Name: UT_ConstructUniqueLabel
@Desc:
============================================================================== */
void UT_ConstructUniqueLabel(int type, void FAR* pName)
{
char name[256];
int len,i,j;
tmpfilenum++;
switch (type)
{
case 0:
{
_fstrcpy(name, "[ Window ]");
break;
} /* case 0 */
case 1:
{
_fstrcpy(name, "( Document )");
break;
} /* case 1 */
default:
{
_fstrcpy(name, "< Layer >");
break;
} /* case default */
} /* swith (type) */
len = _fstrlen(name);
i = tmpfilenum;
for (j=5; j>=0; j--)
{
name[len+j] = base36[ i%36 ];
i /= 36;
} /* for (j=5; j>=0; j--) */
len+=6;
name[len++] = '\0';
_fstrcpy(pName, name);
} /* UT_ConstructUniqueLabel() */
/* ========================================= Parallax C Function ==================
@Name: UT_Sleep
@Desc:
============================================================================== */
void UT_Sleep(int sec)
{
time_t start;
time_t now;
_FMTMSG("Sleeping for %d seconds...", sec);
time(&start);
time(&now);
while(difftime(now, start) < sec)
{
Yield();
time(&now);
} /* while(difftime(now, start) < sec) */
} /* UT_Sleep() */
/* UTFILE.C */
/* end of file */