[BACK]Return to sfile.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / hgm / mh / src

Diff for /OpenXM/src/hgm/mh/src/sfile.c between version 1.9 and 1.14

version 1.9, 2013/02/25 12:12:52 version 1.14, 2013/03/07 05:23:31
Line 1 
Line 1 
 /*  /*
   $OpenXM: OpenXM/src/hgm/mh/src/sfile.c,v 1.8 2013/02/25 12:11:23 takayama Exp $    $OpenXM: OpenXM/src/hgm/mh/src/sfile.c,v 1.13 2013/03/07 03:00:43 takayama Exp $
  */  */
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
 #include <string.h>  #include <string.h>
   #ifndef STANDALONE
   #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;
   
   void mh_check_intr(int interval) {
     static int intr=0;
     if (intr >= interval) {
       intr = 0;
   #ifndef STANDALONE
       R_CheckUserInterrupt();
   #endif
     }else intr++;
   }
   
 void *mh_malloc(int s) {  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) printf("mh_malloc total allocated memory: %f M\n",(float)total/(float)(1024*1024));
   #ifdef STANDALONE
   p = (void*)malloc(s);    p = (void*)malloc(s);
   #else
     p = (void *)R_alloc(1,s);
   #endif
   if (p == NULL) {    if (p == NULL) {
         fprintf(stderr,"No memory.\n"); mh_exit(-1);      fprintf(stderr,"No memory.\n"); mh_exit(-1);
   }    }
   return(p);    return(p);
 }  }
 mh_free(void *p) {  int mh_free(void *p) {
   if (MH_DEBUG) printf("mh_free at %p\n",p);    if (MH_DEBUG) printf("mh_free at %p\n",p);
   #ifdef STANDALONE
   free(p); /* free in mh_free */    free(p); /* free in mh_free */
   #endif
   return(0);    return(0);
 }  }
   
 mh_exit(int n) {  int mh_exit(int n) {
   #ifdef STANDALONE
   static int standalone=0;    static int standalone=0;
   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);      fprintf(stderr,"Fatal error mh_exit(%d) in mh-w-n.\n",n);
         return(n);      return(n);
   }    }
   #else
     error("mh_exit(%d) is called.\n",n);
   #endif
 }  }
   
 struct SFILE *mh_fopen(char *name,char *mode,int byFile) {  struct SFILE *mh_fopen(char *name,char *mode,int byFile) {
Line 42  struct SFILE *mh_fopen(char *name,char *mode,int byFil
Line 66  struct SFILE *mh_fopen(char *name,char *mode,int byFil
   sfp->forRead=1; sfp->copied=0;    sfp->forRead=1; sfp->copied=0;
   
   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 = stdout;
         }else{      }else{
           sfp->fp = fopen(name,mode);        sfp->fp = fopen(name,mode);
         }      }
         if (sfp->fp == NULL) return(NULL);      if (sfp->fp == NULL) return(NULL);
         else return(sfp);      else return(sfp);
   }else if (strcmp(mode,"r")==0) {    }else if (strcmp(mode,"r")==0) {
         sfp->byFile=0;      sfp->byFile=0;
         sfp->s=name;      sfp->s=name;
         sfp->pt=0;      sfp->pt=0;
         sfp->len=strlen(name);      sfp->len=strlen(name);
         sfp->limit=sfp->len+1;      sfp->limit=sfp->len+1;
         sfp->forRead=1;      sfp->forRead=1;
         return(sfp);      return(sfp);
   }else if (strcmp(mode,"w")==0) {    }else if (strcmp(mode,"w")==0) {
         sfp->byFile=0;      sfp->byFile=0;
         sfp->s=(char *)mh_malloc(SSIZE);      sfp->s=(char *)mh_malloc(SSIZE);
         sfp->s[0]=0;      sfp->s[0]=0;
         sfp->pt=0;      sfp->pt=0;
         sfp->len=0;      sfp->len=0;
         sfp->limit= SSIZE;      sfp->limit= SSIZE;
         sfp->forRead=0;      sfp->forRead=0;
         return(sfp);      return(sfp);
   }else{    }else{
         fprintf(stderr,"Error: unknown mode %s in the string mode.\n",mode);      fprintf(stderr,"Error: unknown mode %s in the string mode.\n",mode);
         return NULL;      return NULL;
   }    }
 }  }
   
Line 82  char *mh_fgets(char *str,int size,struct SFILE *sfp) {
Line 106  char *mh_fgets(char *str,int size,struct SFILE *sfp) {
   if (pt >= len) return NULL;    if (pt >= len) return NULL;
   if (size != 0) str[0]=0;    if (size != 0) str[0]=0;
   for (i=0; i<size-1; i++) {    for (i=0; i<size-1; i++) {
         if (s[pt] != 0) {      if (s[pt] != 0) {
           str[i] = s[pt]; str[i+1] = 0;        str[i] = s[pt]; str[i+1] = 0;
           pt++; sfp->pt = pt;        pt++; sfp->pt = pt;
           if (s[pt] == 0) return str;        if (s[pt] == 0) return str;
           if (pt >= len) return str;        if (pt >= len) return str;
           if (str[i] == '\n') return str;        if (str[i] == '\n') return str;
         }      }
   }    }
   return str;    return str;
 }  }
Line 100  int mh_fputs(char *str,struct SFILE *sfp) {
Line 124  int mh_fputs(char *str,struct SFILE *sfp) {
   s = sfp->s; len = sfp->len; pt = sfp->pt; limit=sfp->limit;    s = sfp->s; len = sfp->len; pt = sfp->pt; 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;
         s = (char *) mh_malloc(limit);      s = (char *) mh_malloc(limit);
         if (s == NULL) return(EOF);      if (s == NULL) return(EOF);
         strcpy(s,sfp->s);      strcpy(s,sfp->s);
     mh_free(sfp->s);      mh_free(sfp->s);
   }    }
   strcpy(&(s[len]),str);    strcpy(&(s[len]),str);
Line 118  int mh_fclose(struct SFILE *sfp) {
Line 142  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) fprintf(stderr,"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);
     return(0);
 }  }
   
 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");      fprintf(stderr,"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;
   for (i = 0; (i<size-1) && (i<sfp->len); i++) {    for (i = 0; (i<size-1) && (i<sfp->len); i++) {
         str[i] = (sfp->s)[i]; str[i+1] = 0;      str[i] = (sfp->s)[i]; str[i+1] = 0;
   }    }
   sfp->copied=1;    sfp->copied=1;
   return(i);    return(i);
Line 155  main() {
Line 180  main() {
   struct SFILE *sfp;    struct SFILE *sfp;
   char str[TESTSIZE];    char str[TESTSIZE];
   int i;    int i;
 /*    /*
   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);      printf(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);      printf(str);
   }      }
   mh_fclose(sfp);      mh_fclose(sfp);
 */    */
   sfp = mh_fopen("","w",0);    sfp = mh_fopen("","w",0);
   for (i=0; i<3; i++) {    for (i=0; i<3; i++) {
         mh_fputs("hoge\n",sfp);      mh_fputs("hoge\n",sfp);
         mh_fputs("hage\n",sfp);      mh_fputs("hage\n",sfp);
         dump(sfp);      dump(sfp);
   }    }
   mh_fputs("end end\n",sfp);    mh_fputs("end end\n",sfp);
   printf("result=%s\n",sfp->s);    printf("result=%s\n",sfp->s);

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.14

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>