version 1.15, 2013/03/08 04:54:01 |
version 1.21, 2016/02/13 02:19:00 |
|
|
/* |
/* |
$OpenXM: OpenXM/src/hgm/mh/src/sfile.c,v 1.14 2013/03/07 05:23:31 takayama Exp $ |
$OpenXM: OpenXM/src/hgm/mh/src/sfile.c,v 1.20 2015/04/02 05:45:41 takayama Exp $ |
*/ |
*/ |
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
#ifndef STANDALONE |
#include <ctype.h> |
#include <R.h> |
|
#include <R_ext/Utils.h> |
|
#endif |
|
#include "sfile.h" |
#include "sfile.h" |
#define SSIZE 5 |
#define SSIZE 5 |
int MH_DEBUG = 0; |
int MH_DEBUG = 0; |
|
|
|
static int isIntString(char s[]); |
|
|
void mh_check_intr(int interval) { |
void mh_check_intr(int interval) { |
static int intr=0; |
static int intr=0; |
if (intr >= interval) { |
if (intr >= interval) { |
Line 26 void *mh_malloc(int s) { |
|
Line 25 void *mh_malloc(int s) { |
|
void *p; |
void *p; |
static int total=0; |
static int total=0; |
total += s; |
total += s; |
if (MH_DEBUG) printf("mh_malloc total allocated memory: %f M\n",(float)total/(float)(1024*1024)); |
if (MH_DEBUG) oxprintf("mh_malloc total allocated memory: %f M\n",(float)total/(float)(1024*1024)); |
#ifdef STANDALONE |
#ifdef STANDALONE |
p = (void*)malloc(s); |
p = (void*)malloc(s); |
#else |
#else |
p = (void *)R_alloc(1,s); |
p = (void *)R_alloc(1,s); |
#endif |
#endif |
if (p == NULL) { |
if (p == NULL) { |
fprintf(stderr,"No memory.\n"); mh_exit(-1); |
oxprintfe("No memory.\n"); mh_exit(-1); |
} |
} |
return(p); |
return(p); |
} |
} |
int mh_free(void *p) { |
int mh_free(void *p) { |
if (MH_DEBUG) printf("mh_free at %p\n",p); |
if (MH_DEBUG) oxprintf("mh_free at %p\n",p); |
#ifdef STANDALONE |
#ifdef STANDALONE |
free(p); /* free in mh_free */ |
free(p); /* free in mh_free */ |
#endif |
#endif |
Line 51 int mh_exit(int n) { |
|
Line 50 int mh_exit(int n) { |
|
if (n == MH_RESET_EXIT) { standalone=1; return(0);} |
if (n == MH_RESET_EXIT) { standalone=1; return(0);} |
if (standalone) exit(n); |
if (standalone) exit(n); |
else { |
else { |
fprintf(stderr,"Fatal error mh_exit(%d) in mh-w-n.\n",n); |
oxprintfe("Fatal error mh_exit(%d) in mh-w-n.\n",n); |
return(n); |
return(n); |
} |
} |
#else |
#else |
Line 68 struct SFILE *mh_fopen(char *name,char *mode,int byFil |
|
Line 67 struct SFILE *mh_fopen(char *name,char *mode,int byFil |
|
if (byFile) { |
if (byFile) { |
sfp->byFile = 1; |
sfp->byFile = 1; |
if (strcmp(name,"stdout")==0) { |
if (strcmp(name,"stdout")==0) { |
sfp->fp = stdout; |
sfp->fp = oxstdout; |
|
}else if (strcmp(name,"stdin")==0) { |
|
sfp->fp = oxstdin; |
}else{ |
}else{ |
sfp->fp = fopen(name,mode); |
sfp->fp = fopen(name,mode); |
} |
} |
Line 92 struct SFILE *mh_fopen(char *name,char *mode,int byFil |
|
Line 93 struct SFILE *mh_fopen(char *name,char *mode,int byFil |
|
sfp->forRead=0; |
sfp->forRead=0; |
return(sfp); |
return(sfp); |
}else{ |
}else{ |
fprintf(stderr,"Error: unknown mode %s in the string mode.\n",mode); |
oxprintfe("Error: unknown mode %s in the string mode.\n",mode); |
return NULL; |
return NULL; |
} |
} |
} |
} |
Line 117 char *mh_fgets(char *str,int size,struct SFILE *sfp) { |
|
Line 118 char *mh_fgets(char *str,int size,struct SFILE *sfp) { |
|
return str; |
return str; |
} |
} |
int mh_fputs(char *str,struct SFILE *sfp) { |
int mh_fputs(char *str,struct SFILE *sfp) { |
int i,pt,len,limit; |
int len,limit; |
int inputLen; |
int inputLen; |
char *s; |
char *s; |
if (sfp->byFile) return fputs(str,sfp->fp); |
if (sfp->byFile) return fputs(str,sfp->fp); |
s = sfp->s; len = sfp->len; pt = sfp->pt; limit=sfp->limit; |
s = sfp->s; len = sfp->len; limit=sfp->limit; |
inputLen=strlen(str); |
inputLen=strlen(str); |
if (inputLen+len+1 > limit) { |
if (inputLen+len+1 > limit) { |
limit = (inputLen+len+1)*2; |
limit = (inputLen+len+1)*2; |
Line 132 int mh_fputs(char *str,struct SFILE *sfp) { |
|
Line 133 int mh_fputs(char *str,struct SFILE *sfp) { |
|
} |
} |
strcpy(&(s[len]),str); |
strcpy(&(s[len]),str); |
len += inputLen; |
len += inputLen; |
/* printf("mh_fputs(%d):[%s]\n",len,s); */ |
/* oxprintf("mh_fputs(%d):[%s]\n",len,s); */ |
sfp->s=s; sfp->len=len; sfp->limit=limit; |
sfp->s=s; sfp->len=len; sfp->limit=limit; |
return(0); |
return(0); |
} |
} |
Line 142 int mh_fclose(struct SFILE *sfp) { |
|
Line 143 int mh_fclose(struct SFILE *sfp) { |
|
if (!sfp) return(-1); |
if (!sfp) return(-1); |
if (sfp->byFile) return fclose(sfp->fp); |
if (sfp->byFile) return fclose(sfp->fp); |
if (! (sfp->forRead)) { |
if (! (sfp->forRead)) { |
if (!sfp->copied) fprintf(stderr,"Warning in mh_fclose. sfp->s has not been copied, but deallocated.\n"); |
if (!sfp->copied) oxprintfe("Warning in mh_fclose. sfp->s has not been copied, but deallocated.\n"); |
if (sfp->s != NULL) { mh_free(sfp->s); sfp->s = NULL; } |
if (sfp->s != NULL) { mh_free(sfp->s); sfp->s = NULL; } |
} |
} |
mh_free(sfp); |
mh_free(sfp); |
Line 152 int mh_fclose(struct SFILE *sfp) { |
|
Line 153 int mh_fclose(struct SFILE *sfp) { |
|
int mh_outstr(char *str,int size,struct SFILE *sfp) { |
int mh_outstr(char *str,int size,struct SFILE *sfp) { |
int i; |
int i; |
if (sfp->byFile) { |
if (sfp->byFile) { |
fprintf(stderr,"Error in mh_outstr. mh_outstr is called in the file i/o mode.\n"); |
oxprintfe("Error in mh_outstr. mh_outstr is called in the file i/o mode.\n"); |
return 0; |
return 0; |
} |
} |
if (size) str[0] = 0; |
if (size) str[0] = 0; |
Line 164 int mh_outstr(char *str,int size,struct SFILE *sfp) { |
|
Line 165 int mh_outstr(char *str,int size,struct SFILE *sfp) { |
|
} |
} |
|
|
void mh_error(char *s,int code) { |
void mh_error(char *s,int code) { |
fprintf(stderr,"Error: %s\n",s); |
oxprintfe("Error: %s\n",s); |
mh_exit(code); |
mh_exit(code); |
} |
} |
|
|
|
struct mh_token mh_getoken(char s[],int smax,struct SFILE *sfp) { |
|
static int w0=0; |
|
static int wn=0; |
|
static char work[MH_SSIZE]; |
|
char *r; |
|
int t,t2; |
|
struct mh_token token; |
|
token.type = MH_TOKEN_EOF; |
|
token.ival=0; token.dval=0.0; |
|
while (1) { |
|
if (w0 >= wn) { |
|
r = mh_fgets(work,MH_SSIZE-1,sfp); |
|
if (r == NULL) { |
|
token.type = MH_TOKEN_EOF; |
|
return token; |
|
} |
|
/* oxprintf("work=%s\n",work); */ |
|
w0 = 0; wn=strlen(work); |
|
} |
|
t=w0; |
|
while ((work[t] <= ' ') && (t < wn)) t++; |
|
if (t == wn) { |
|
w0=wn; |
|
continue; |
|
} |
|
if (work[t] == ',') {w0=t+1; continue; } |
|
t2 = t; |
|
while((work[t2] > ' ') && (work[t2] != '=') |
|
&& (work[t2] != ',')) t2++; |
|
/* work[t] ... work[t2-1] is a token */ |
|
/* %abc=123#comment is not allowed. but %abc=123,# or %abc=123 # is OK. */ |
|
if ((t == t2) && (work[t] == '=')) { |
|
token.type=MH_TOKEN_EQ; |
|
w0=t2+1; |
|
return token; |
|
} |
|
if ((work[t] == '%') && (work[t+1] == '%')) { |
|
/* comment */ |
|
w0 =wn; |
|
continue; |
|
} |
|
if (work[t] == '#') { |
|
/* comment */ |
|
w0=wn; |
|
continue; |
|
} |
|
if (work[t] == '%') { |
|
if (t2-t-2 >= smax) { |
|
oxprintfe("s is too small in mh_getoken.\n"); |
|
mh_exit(-1); |
|
} |
|
strncpy(s,&(work[t+1]),t2-(t+1)); s[t2-(t+1)] = 0; |
|
token.type=MH_TOKEN_ID; |
|
w0 = t2; |
|
return token; |
|
} |
|
/* The case of double or int */ |
|
strncpy(s,&(work[t]),t2-t); s[t2-t]=0; |
|
if (isIntString(s)) { |
|
token.type=MH_TOKEN_INT; |
|
sscanf(s,"%d",&(token.ival)); |
|
sscanf(s,"%lg",&(token.dval)); |
|
}else{ |
|
token.type=MH_TOKEN_DOUBLE; |
|
sscanf(s,"%lg",&(token.dval)); |
|
} |
|
w0 = t2; |
|
return token; |
|
} |
|
} |
|
|
|
static int isIntString(char s[]) { |
|
int i; |
|
for (i=0; i<strlen(s); i++) { |
|
if (isdigit(s[i]) || (s[i]=='-')) continue; |
|
else return(0); |
|
} |
|
return(1); |
|
} |
|
|
|
void mh_print_token(struct mh_token tk,char *s) { |
|
int type; |
|
type = tk.type; |
|
oxprintf("type=%d\n",type); |
|
switch(type) { |
|
case MH_TOKEN_ID: |
|
oxprintf("ID=%s\n",s); break; |
|
case MH_TOKEN_EQ: |
|
oxprintf("MH_TOKEN_EQ\n"); break; |
|
default: |
|
oxprintf("NUM=%s, ival=%d, dval=%lg\n",s,tk.ival,tk.dval); break; |
|
} |
|
} |
|
|
#ifdef TEST |
#ifdef TEST |
/* for debugging */ |
/* for debugging */ |
dump(struct SFILE *sfp) { |
dump(struct SFILE *sfp) { |
printf("byFile=%d\n",sfp->byFile); |
oxprintf("byFile=%d\n",sfp->byFile); |
if (sfp->s) printf("sfp->s=%s\n",sfp->s); |
if (sfp->s) oxprintf("sfp->s=%s\n",sfp->s); |
printf("pt=%d\n",sfp->pt); |
oxprintf("pt=%d\n",sfp->pt); |
printf("len=%d\n",sfp->len); |
oxprintf("len=%d\n",sfp->len); |
printf("limit=%d\n",sfp->limit); |
oxprintf("limit=%d\n",sfp->limit); |
printf("fp=%p\n",sfp->fp); |
oxprintf("fp=%p\n",sfp->fp); |
} |
} |
|
|
|
|
#define TESTSIZE 1024 |
#define TESTSIZE 1024 |
main() { |
main() { |
struct SFILE *sfp; |
struct SFILE *sfp; |
char str[TESTSIZE]; |
char str[TESTSIZE]; |
int i; |
int i; |
|
struct mh_token tk; |
|
sfp = mh_fopen("%%abc\n%abs=1.234\n %abs = \n 1.235\n%abs=\n1.236\n\n%abs=1.237\n%ival=-234,#comment\n","r",0); |
|
while ((tk=mh_getoken(str,TESTSIZE,sfp)).type != MH_TOKEN_EOF) { |
|
mh_print_token(tk,str); |
|
} |
|
return 0; |
/* |
/* |
sfp = mh_fopen("hoge\nafo\nbho\ncat\ndot\ndolphin\n","r",0); |
sfp = mh_fopen("hoge\nafo\nbho\ncat\ndot\ndolphin\n","r",0); |
while (mh_fgets(str,1024,sfp)) { |
while (mh_fgets(str,1024,sfp)) { |
printf(str); |
oxprintf(str); |
} |
} |
mh_fclose(sfp); |
mh_fclose(sfp); |
|
|
sfp = mh_fopen("sfile.h","r",1); |
sfp = mh_fopen("sfile.h","r",1); |
while (mh_fgets(str,1024,sfp)) { |
while (mh_fgets(str,1024,sfp)) { |
printf(str); |
oxprintf(str); |
} |
} |
mh_fclose(sfp); |
mh_fclose(sfp); |
*/ |
*/ |
|
|
dump(sfp); |
dump(sfp); |
} |
} |
mh_fputs("end end\n",sfp); |
mh_fputs("end end\n",sfp); |
printf("result=%s\n",sfp->s); |
oxprintf("result=%s\n",sfp->s); |
mh_outstr(str,TESTSIZE,sfp); |
mh_outstr(str,TESTSIZE,sfp); |
mh_fclose(sfp); |
mh_fclose(sfp); |
} |
} |