diff trace.c @ 0:4be1ca60a49b default tip

first commit.
author tkaito
date Sat, 05 Feb 2011 02:13:58 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/trace.c	Sat Feb 05 02:13:58 2011 +0900
@@ -0,0 +1,158 @@
+#include <stdio.h>
+#include "trace.h"
+
+
+static FILE *fileptr;
+const char *filename;
+
+void
+pad_trace(unsigned int pad)
+{
+    fprintf(fileptr, "%d\n", pad);
+}
+
+
+int
+PadfileOpen(const char* name)
+{
+    filename = name;
+    fileptr = fopen(filename, "rb+");
+    
+    if (!fileptr) {
+      fileptr = fopen(filename, "wb+");
+      if (!fileptr) {
+	printf("can't open output file.\n");
+	return 0;
+      }
+    }
+    
+    return 1;
+}
+
+
+void
+PadfileRead(TraceBuffPtr buff)
+{
+    TraceBuffPtr tmp = buff;
+    fread(buff->cappad, sizeof(SGO_PAD), PAD_BUFSIZE, fileptr);
+    
+    while (!feof(fileptr)) {
+      TraceBuffPtr new_buff = (TraceBuffPtr)malloc(sizeof(TraceBuff));
+      tmp->next = new_buff;
+      new_buff->next = NULL;
+      tmp = new_buff;
+
+      fread(new_buff->cappad, sizeof(SGO_PAD), PAD_BUFSIZE, fileptr);
+    }
+}
+
+
+void
+PadfileWrite(TraceBuffPtr buff)
+{
+    TraceBuffPtr tmp = buff;
+    fwrite(buff->cappad, sizeof(SGO_PAD), PAD_BUFSIZE, fileptr);
+    
+    while (tmp->next != NULL) {
+      tmp = tmp->next;
+      fwrite(tmp->cappad, sizeof(SGO_PAD), PAD_BUFSIZE, fileptr);
+    }
+}
+
+
+TraceBuffPtr
+CapturePad(TraceBuffPtr buff, SGO_PAD *pad)
+{
+    static int count;
+
+    if (count !=PAD_BUFSIZE) {
+      buff->cappad[count] = pad[0];
+      ++count;
+      return buff;
+    } else {
+      TraceBuffPtr new_buff = (TraceBuffPtr)malloc(sizeof(TraceBuff));
+      new_buff->cappad[0] = pad[0];
+      buff->next = new_buff;
+      new_buff->next = NULL;
+
+      count = 1;
+      return new_buff;
+    }
+}
+
+
+TraceBuffPtr
+TracePad(TraceBuffPtr buff, SGO_PAD *pad)
+{
+    static int count;
+
+    if (count !=PAD_BUFSIZE) {
+      pad[0] = buff->cappad[count];
+      ++count;
+      return buff;
+    } else {
+      buff = buff->next;
+      pad[0] = buff->cappad[0];
+      
+      count = 1;
+      return buff;
+    }
+}
+
+
+void
+PadfileClose(TraceBuffPtr buff)
+{
+    fclose(fileptr);
+
+    TraceBuffPtr trace = buff;
+
+    while (trace->next != NULL) {
+      TraceBuffPtr tmp = trace;
+      trace = trace->next;
+      free(tmp);
+    }
+    free(trace);
+}
+
+#ifdef USE_MYRAND
+static FILE *myrand_fp;
+extern int runmode;
+
+int myrand_open_file(char *filename)
+{
+    myrand_fp = fopen(filename, "r+");
+    if (!myrand_fp) {
+	myrand_fp = fopen(filename, "w+");
+	if (myrand_fp) {
+	    return 0;
+	} else {
+	    return 1;
+	}
+    }
+    return 0;
+}
+
+static char buf[16];
+
+int myrand()
+{
+    int r = 0;
+    if (runmode) {
+	if (runmode == 1) {
+	    r = rand();
+	    fprintf(myrand_fp, "%d\n", r);
+	} else if (runmode == 2) {
+//            dbg_printf("check0\n");
+	    if (fgets(buf, 16, myrand_fp)) {
+		sscanf(buf, "%d", &r);
+	    } else {
+		fseek(myrand_fp, 0, SEEK_SET);
+	    }
+	}
+    } else {
+	r = rand();
+    }
+    return r;
+}
+#endif				// USE_MYRAND