cachesim
A cache simulator
|
00001 /* -*- mode:c; coding: utf-8 -*- */ 00002 00003 #include "trace.h" 00004 00005 #include <stdlib.h> 00006 #include <string.h> 00007 #include <ctype.h> 00008 #include <limits.h> 00009 00010 enum 00011 { 00012 LINE_BUF_SIZE = 1024, 00013 MAX_LINE_LENGTH = 1000 00014 }; 00015 00020 struct Trace 00021 { 00022 FILE *f; 00023 FILE *log_f; 00024 char *path; 00025 int lineno; 00026 TraceStep step; 00027 }; 00028 00029 Trace * 00030 trace_open(const char *path, FILE *log_f) 00031 { 00032 Trace *t = (Trace*) calloc(1, sizeof(*t)); 00033 if (!path) { 00034 t->path = strdup("<stdin>"); 00035 t->f = stdin; 00036 } else { 00037 t->path = strdup(path); 00038 t->f = fopen(path, "r"); 00039 } 00040 t->log_f = log_f; 00041 if (!t->f) { 00042 t = trace_close(t); 00043 } 00044 return t; 00045 } 00046 00047 Trace * 00048 trace_close(Trace *t) 00049 { 00050 if (t) { 00051 if (t->f && t->f != stdin) { 00052 fclose(t->f); 00053 } 00054 free(t->path); 00055 } 00056 return NULL; 00057 } 00058 00059 static int 00060 trace_error(Trace *t, const char *text) 00061 { 00062 fprintf(t->log_f, "%s: %d: trace_next: %s\n", t->path, t->lineno, text); 00063 return -1; 00064 } 00065 00066 int 00067 trace_next(Trace *t) 00068 { 00069 char buf[LINE_BUF_SIZE], *p; 00070 int buflen, r; 00071 char modes[4]; 00072 int addr, size, n, n2; 00073 long long value; 00074 00075 while (fgets(buf, sizeof(buf), t->f)) { 00076 ++t->lineno; 00077 buflen = strlen(buf); 00078 if (buflen > MAX_LINE_LENGTH) { 00079 return trace_error(t, "line is too long"); 00080 } 00081 if ((p = strchr(buf, '#'))) { 00082 *p = 0; 00083 buflen = strlen(buf); 00084 } 00085 while (buflen > 0 && isspace(buf[buflen - 1])) { 00086 --buflen; 00087 } 00088 buf[buflen] = 0; 00089 if (!buflen) { 00090 continue; 00091 } 00092 // FIXME: реализовать чтение одного шага трассы 00093 return 1; 00094 } 00095 return 0; 00096 } 00097 00098 TraceStep * 00099 trace_get(Trace *t) 00100 { 00101 return &t->step; 00102 } 00103 00104 /* 00105 * Local variables: 00106 * c-basic-offset: 4 00107 * End: 00108 */