summaryrefslogtreecommitdiffstats
path: root/src/misc/util/utilFile.c
diff options
context:
space:
mode:
authorAlan Mishchenko <alanmi@berkeley.edu>2010-11-01 01:35:04 -0700
committerAlan Mishchenko <alanmi@berkeley.edu>2010-11-01 01:35:04 -0700
commit6130e39b18b5f53902e4eab14f6d5cdde5219563 (patch)
tree0db0628479a1b750e9af1f66cb8379ebd0913d31 /src/misc/util/utilFile.c
parentf0e77f6797c0504b0da25a56152b707d3357f386 (diff)
downloadabc-6130e39b18b5f53902e4eab14f6d5cdde5219563.tar.gz
abc-6130e39b18b5f53902e4eab14f6d5cdde5219563.tar.bz2
abc-6130e39b18b5f53902e4eab14f6d5cdde5219563.zip
initial commit of public abc
Diffstat (limited to 'src/misc/util/utilFile.c')
-rw-r--r--src/misc/util/utilFile.c170
1 files changed, 170 insertions, 0 deletions
diff --git a/src/misc/util/utilFile.c b/src/misc/util/utilFile.c
new file mode 100644
index 00000000..abdcd635
--- /dev/null
+++ b/src/misc/util/utilFile.c
@@ -0,0 +1,170 @@
+/**CFile****************************************************************
+
+ FileName [utilFile.c]
+
+ SystemName [ABC: Logic synthesis and verification system.]
+
+ PackageName [Temporary file utilities.]
+
+ Synopsis [Temporary file utilities.]
+
+ Author [Niklas Een]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - September 29, 2010.]
+
+ Revision [$Id: utilFile.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+
+#include "abc_global.h"
+
+ABC_NAMESPACE_IMPL_START
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+#if defined(_MSC_VER)
+
+#include <Windows.h>
+#include <process.h>
+#include <io.h>
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static ABC_UINT64_T realTimeAbs() // -- absolute time in nano-seconds
+{
+ LARGE_INTEGER f, t;
+ double realTime_freq;
+ int ok;
+
+ ok = QueryPerformanceFrequency(&f); assert(ok);
+ realTime_freq = 1.0 / (__int64)(((ABC_UINT64_T)f.LowPart) | ((ABC_UINT64_T)f.HighPart << 32));
+
+ ok = QueryPerformanceCounter(&t); assert(ok);
+ return (ABC_UINT64_T)(__int64)(((__int64)(((ABC_UINT64_T)t.LowPart | ((ABC_UINT64_T)t.HighPart << 32))) * realTime_freq * 1000000000));
+}
+
+#endif
+
+
+
+// Opens a temporary file with given prefix and returns file descriptor (-1 on failure)
+// and a string containing the name of the file (to be freed by caller).
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int tmpFile(const char* prefix, const char* suffix, char** out_name)
+{
+#if !defined(_MSC_VER)
+ int fd;
+
+ *out_name = (char*)malloc(strlen(prefix) + strlen(suffix) + 7);
+ assert(*out_name != NULL);
+ sprintf(*out_name, "%sXXXXXX", prefix);
+ fd = mkstemp(*out_name);
+ if (fd == -1){
+ free(*out_name);
+ *out_name = NULL;
+ }else{
+ // Kludge:
+ close(fd);
+ unlink(*out_name);
+ strcat(*out_name, suffix);
+ fd = open(fd);
+ if (fd == -1){
+ free(*out_name);
+ *out_name = NULL;
+ }
+ }
+ return fd;
+
+#else
+ int i, fd;
+ *out_name = (char*)malloc(strlen(prefix) + strlen(suffix) + 27);
+ for (i = 0; i < 10; i++){
+ sprintf(*out_name, "%s%I64X%d%s", prefix, realTimeAbs(), _getpid(), suffix);
+ fd = _open(*out_name, O_CREAT | O_EXCL | O_BINARY | O_RDWR, _S_IREAD | _S_IWRITE);
+ if (fd == -1){
+ free(*out_name);
+ *out_name = NULL;
+ }
+ return fd;
+ }
+ assert(0); // -- could not open temporary file
+ return 0;
+#endif
+}
+
+
+//mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
+
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+/*
+int main(int argc, char** argv)
+{
+ char* tmp_filename;
+ int fd = tmpFile("__abctmp_", &tmp_filename);
+ FILE* out = fdopen(fd, "wb");
+
+ fprintf(out, "This file contains important information.\n");
+ fclose(out);
+
+ printf("Created: %s\n", tmp_filename);
+ free(tmp_filename);
+
+ return 0;
+}
+*/
+
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
+ABC_NAMESPACE_IMPL_END
+