Annotation of OpenXM/src/hgm/mh/src/sfile.c, Revision 1.8
1.1 takayama 1: /*
1.8 ! takayama 2: $OpenXM: OpenXM/src/hgm/mh/src/sfile.c,v 1.7 2013/02/24 21:36:49 takayama Exp $
1.1 takayama 3: */
4: #include <stdio.h>
1.6 takayama 5: #include <stdlib.h>
6: #include <string.h>
1.1 takayama 7: #include "sfile.h"
8: #define SSIZE 5
1.8 ! takayama 9: int MH_DEBUG = 1;
1.3 takayama 10:
1.4 takayama 11: void *mh_malloc(int s) {
12: void *p;
1.8 ! takayama 13: static int total=0;
! 14: total += s;
! 15: if (MH_DEBUG) printf("mh_malloc total allocated memory: %f M\n",(float)total/(float)(1024*1024));
1.4 takayama 16: p = (void*)malloc(s);
17: if (p == NULL) {
18: fprintf(stderr,"No memory.\n"); mh_exit(-1);
19: }
20: return(p);
21: }
22: mh_free(void *p) {
1.8 ! takayama 23: return(0);
1.7 takayama 24: if (MH_DEBUG) printf("mh_free at %p\n",p);
1.8 ! takayama 25: free(p); /* free in mh_free */
1.4 takayama 26: return(0);
27: }
28:
29: mh_exit(int n) {
30: static int standalone=0;
1.5 takayama 31: if (n == MH_RESET_EXIT) { standalone=1; return(0);}
1.4 takayama 32: if (standalone) exit(n);
33: else {
34: fprintf(stderr,"Fatal error mh_exit(%d) in mh-w-n.\n",n);
35: return(n);
36: }
37: }
1.1 takayama 38:
39: struct SFILE *mh_fopen(char *name,char *mode,int byFile) {
40: struct SFILE *sfp;
1.3 takayama 41: sfp = (struct SFILE *)mh_malloc(sizeof(struct SFILE));
1.1 takayama 42: sfp->byFile=0; sfp->s=NULL; sfp->pt=0; sfp->len=0;sfp->limit=0; sfp->fp=NULL;
1.2 takayama 43: sfp->forRead=1; sfp->copied=0;
1.1 takayama 44:
45: if (byFile) {
46: sfp->byFile = 1;
1.5 takayama 47: if (strcmp(name,"stdout")==0) {
48: sfp->fp = stdout;
49: }else{
50: sfp->fp = fopen(name,mode);
51: }
1.1 takayama 52: if (sfp->fp == NULL) return(NULL);
53: else return(sfp);
54: }else if (strcmp(mode,"r")==0) {
55: sfp->byFile=0;
56: sfp->s=name;
57: sfp->pt=0;
58: sfp->len=strlen(name);
59: sfp->limit=sfp->len+1;
60: sfp->forRead=1;
61: return(sfp);
62: }else if (strcmp(mode,"w")==0) {
63: sfp->byFile=0;
1.3 takayama 64: sfp->s=(char *)mh_malloc(SSIZE);
65: sfp->s[0]=0;
1.1 takayama 66: sfp->pt=0;
67: sfp->len=0;
68: sfp->limit= SSIZE;
69: sfp->forRead=0;
70: return(sfp);
71: }else{
72: fprintf(stderr,"Error: unknown mode %s in the string mode.\n",mode);
73: return NULL;
74: }
75: }
76:
77: char *mh_fgets(char *str,int size,struct SFILE *sfp) {
78: int i,pt,len;
79: char *s;
80: if (sfp->byFile) return fgets(str,size,sfp->fp);
81: s = sfp->s; len = sfp->len; pt = sfp->pt;
82: if (s[pt] == 0) return NULL;
83: if (pt >= len) return NULL;
84: if (size != 0) str[0]=0;
85: for (i=0; i<size-1; i++) {
86: if (s[pt] != 0) {
87: str[i] = s[pt]; str[i+1] = 0;
88: pt++; sfp->pt = pt;
89: if (s[pt] == 0) return str;
90: if (pt >= len) return str;
91: if (str[i] == '\n') return str;
92: }
93: }
94: return str;
95: }
96: int mh_fputs(char *str,struct SFILE *sfp) {
97: int i,pt,len,limit;
98: int inputLen;
99: char *s;
100: if (sfp->byFile) return fputs(str,sfp->fp);
101: s = sfp->s; len = sfp->len; pt = sfp->pt; limit=sfp->limit;
102: inputLen=strlen(str);
103: if (inputLen+len+1 > limit) {
1.5 takayama 104: limit = (inputLen+len+1)*2;
1.1 takayama 105: s = (char *) mh_malloc(limit);
106: if (s == NULL) return(EOF);
107: strcpy(s,sfp->s);
1.8 ! takayama 108: mh_free(sfp->s);
1.1 takayama 109: }
110: strcpy(&(s[len]),str);
111: len += inputLen;
1.5 takayama 112: /* printf("mh_fputs(%d):[%s]\n",len,s); */
1.1 takayama 113: sfp->s=s; sfp->len=len; sfp->limit=limit;
114: return(0);
115: }
116:
117: /* Note: copy the resulting string sfp->s before mh_fclose */
118: int mh_fclose(struct SFILE *sfp) {
1.3 takayama 119: if (!sfp) return(-1);
1.1 takayama 120: if (sfp->byFile) return fclose(sfp->fp);
121: if (! (sfp->forRead)) {
1.2 takayama 122: if (!sfp->copied) fprintf(stderr,"Warning in mh_fclose. sfp->s has not been copied, but deallocated.\n");
1.8 ! takayama 123: if (sfp->s != NULL) { mh_free(sfp->s); sfp->s = NULL; }
1.1 takayama 124: }
1.8 ! takayama 125: mh_free(sfp);
1.1 takayama 126: }
127:
1.2 takayama 128: int mh_outstr(char *str,int size,struct SFILE *sfp) {
129: int i;
130: if (sfp->byFile) {
131: fprintf(stderr,"Error in mh_outstr. mh_outstr is called in the file i/o mode.\n");
132: return 0;
133: }
134: if (size) str[0] = 0;
135: for (i = 0; (i<size-1) && (i<sfp->len); i++) {
136: str[i] = (sfp->s)[i]; str[i+1] = 0;
137: }
138: sfp->copied=1;
139: return(i);
140: }
141:
142:
1.3 takayama 143: #ifdef TEST
1.1 takayama 144: /* for debugging */
145: dump(struct SFILE *sfp) {
146: printf("byFile=%d\n",sfp->byFile);
147: if (sfp->s) printf("sfp->s=%s\n",sfp->s);
148: printf("pt=%d\n",sfp->pt);
149: printf("len=%d\n",sfp->len);
150: printf("limit=%d\n",sfp->limit);
151: printf("fp=%p\n",sfp->fp);
152: }
153:
1.2 takayama 154: #define TESTSIZE 1024
1.1 takayama 155: main() {
156: struct SFILE *sfp;
1.2 takayama 157: char str[TESTSIZE];
1.1 takayama 158: int i;
159: /*
160: sfp = mh_fopen("hoge\nafo\nbho\ncat\ndot\ndolphin\n","r",0);
161: while (mh_fgets(str,1024,sfp)) {
162: printf(str);
163: }
164: mh_fclose(sfp);
165:
166: sfp = mh_fopen("sfile.h","r",1);
167: while (mh_fgets(str,1024,sfp)) {
168: printf(str);
169: }
170: mh_fclose(sfp);
171: */
172: sfp = mh_fopen("","w",0);
173: for (i=0; i<3; i++) {
174: mh_fputs("hoge\n",sfp);
175: mh_fputs("hage\n",sfp);
176: dump(sfp);
177: }
178: mh_fputs("end end\n",sfp);
179: printf("result=%s\n",sfp->s);
1.2 takayama 180: mh_outstr(str,TESTSIZE,sfp);
181: mh_fclose(sfp);
1.1 takayama 182: }
1.3 takayama 183: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>