📄 hshell.c
字号:
pos = ftell(src.f); rewind(src.f); for (line=1,col=0,i=0; i<=pos; i++){ c = fgetc(src.f); if (c == '\n'){ ++line; col = 0; } else ++col; } sprintf(s,"line %d/col %d/char %ld in %s", line, col, pos, src.name); } return s;}/* EXPORT->GetCh: get next character from given source */int GetCh(Source *src){ int c; if (!src->pbValid){ c = fgetc(src->f); ++src->chcount; } else{ c = src->putback; src->pbValid = FALSE; } return c;}/* EXPORT->UnGetCh: return given character to given source */void UnGetCh(int c, Source *src){ if (src->pbValid == TRUE) { ungetc(src->putback,src->f); src->chcount--; } src->putback = c; src->pbValid = TRUE;}/* EXPORT->SkipLine: skip to next line in source */Boolean SkipLine(Source *src){ int c; c = GetCh(src); while (c != EOF && c != '\n') c = GetCh(src); return(c!=EOF);}/* EXPORT->ReadLine: read to next newline in source */Boolean ReadLine(Source *src,char *s){ int c; c = GetCh(src); while (c != EOF && c != '\n') *s++=c,c=GetCh(src); *s=0; return(c!=EOF);}/* EXPORT->ReadUntilLine: read to next occurrence of string */void ReadUntilLine (Source *src, char *s){ char buf[20*MAXSTRLEN]; do { if (!ReadLine (src, buf)) HError (5013, "ReadUntilLine: reached EOF while scanning for '%s'", s); } while (strcmp (buf, s) != 0);}/* EXPORT->SkipComment: skip comment if any */void SkipComment(Source *src){ const char comch = '#'; int c; c = GetCh(src); while (c != EOF && (isspace(c) || c == comch)) { if (c == comch) while (c != EOF && c != '\n') c = GetCh(src); c = GetCh(src); } UnGetCh(c,src);}/* EXPORT->SkipWhiteSpace: skip white space if any */void SkipWhiteSpace(Source *src){ int c; c=GetCh(src); UnGetCh(c,src); if (!isspace(c)) return; /* Does not alter wasNewline! */ src->wasNewline=FALSE; do { c=GetCh(src); if (c=='\n') src->wasNewline=TRUE; } while(c != EOF && isspace(c)); if (c==EOF) src->wasNewline=TRUE; UnGetCh(c,src);}/* EXPORT->ParseString: get next string from src and store it in s */char *ParseString(char *src, char *s){ Boolean wasQuoted; int c,q; wasQuoted=FALSE; *s=0; q=0; while (isspace((int) *src)) src++; if (*src == DBL_QUOTE || *src == SING_QUOTE){ wasQuoted = TRUE; q = *src; src++; } else if (*src==0) return(NULL); while(!wasQuoted || *src!=0) { if (wasQuoted) { if (*src == q) return (src+1); } else { if (*src==0 || isspace((int) *src)) return (src); } if (*src==ESCAPE_CHAR) { src++; if (src[0]>='0' && src[1]>='0' && src[2]>='0' && src[0]<='7' && src[1]<='7' && src[2]<='7') { c = 64*(src[0] - '0') + 8*(src[1] - '0') + (src[2] - '0'); src+=2; } else c = *src; } else c = *src; *s++ = c; if (c==0) break; *s=0; src++; } return NULL;}/* EXPORT->ReadString: get next string from src and store it in s */Boolean ReadString(Source *src, char *s){ /* could be just: return ReadStringWithLen(src,s,MAXSTRLEN); but this is called often so do it like this.. */ int i,c,n,q; src->wasQuoted=FALSE; q=0; while (isspace(c=GetCh(src))); if (c == EOF) return FALSE; if (c == DBL_QUOTE || c == SING_QUOTE){ src->wasQuoted = TRUE; q = c; c = GetCh(src); } for (i=0; i<MAXSTRLEN; i++){ if (src->wasQuoted){ if (c == EOF) HError(5013,"ReadString: File end within quoted string"); if (c == q) { s[i] = '\0'; return TRUE; } } else { if (c == EOF || isspace(c)){ UnGetCh(c,src); s[i] = '\0'; return TRUE; } } if (c==ESCAPE_CHAR) { c = GetCh(src); if (c == EOF) return(FALSE); if (c>='0' && c<='7') { n = c - '0'; c = GetCh(src); if (c == EOF || c<'0' || c>'7') return(FALSE); n = n*8 + c - '0'; c = GetCh(src); if (c == EOF || c<'0' || c>'7') return(FALSE); c += n*8 - '0'; } } s[i] = c; c = GetCh(src); } HError(5013,"ReadString: String too long"); return FALSE;} /* EXPORT->ReadStringWithLen: get next string from src and store it in s */Boolean ReadStringWithLen(Source *src, char *s, int buflen){ int i,c,n,q; src->wasQuoted=FALSE; q=0; while (isspace(c=GetCh(src))); if (c == EOF) return FALSE; if (c == DBL_QUOTE || c == SING_QUOTE){ src->wasQuoted = TRUE; q = c; c = GetCh(src); } for (i=0; i<buflen ; i++){ if (src->wasQuoted){ if (c == EOF) HError(5013,"ReadString: File end within quoted string"); if (c == q) { s[i] = '\0'; return TRUE; } } else { if (c == EOF || isspace(c)){ UnGetCh(c,src); s[i] = '\0'; return TRUE; } } if (c==ESCAPE_CHAR) { c = GetCh(src); if (c == EOF) return(FALSE); if (c>='0' && c<='7') { n = c - '0'; c = GetCh(src); if (c == EOF || c<'0' || c>'7') return(FALSE); n = n*8 + c - '0'; c = GetCh(src); if (c == EOF || c<'0' || c>'7') return(FALSE); c += n*8 - '0'; } } s[i] = c; c = GetCh(src); } HError(5013,"ReadStringWithLen: String too long"); return FALSE;}/* EXPORT->ReadRawString: get next raw string (i.e. word) from src and store it in s *//* ReadRawString: ie ignore normal HTK escaping */Boolean ReadRawString(Source *src, char *s){ int i,c; while (isspace(c=GetCh(src))); if (c == EOF) return FALSE; for (i=0; i<MAXSTRLEN ; i++){ if (c == EOF || isspace(c)){ UnGetCh(c,src); s[i] = '\0'; return TRUE; } s[i] = c; c = GetCh(src); } HError (5013, "ReadRawString: String too long"); return FALSE;}/* EXPORT->WriteString: Write string s in readable format */void WriteString(FILE *f,char *s,char q){ Boolean noSing,noDbl; int n; unsigned char *p; if (s[0]!=SING_QUOTE) noSing=TRUE; else noSing=FALSE; if (s[0]!=DBL_QUOTE) noDbl=TRUE; else noDbl=FALSE; if (q!=ESCAPE_CHAR && q!=SING_QUOTE && q!=DBL_QUOTE) { q=0; if (!noDbl || !noSing) { if (noSing && !noDbl) q=SING_QUOTE; else q=DBL_QUOTE; } } if (q>0 && q!=ESCAPE_CHAR) fputc(q,f); for (p=(unsigned char*)s;*p;p++) { if (*p==ESCAPE_CHAR || *p==q || (q==ESCAPE_CHAR && p==(unsigned char*)s && (*p==SING_QUOTE || *p==DBL_QUOTE))) fputc(ESCAPE_CHAR,f),fputc(*p,f); else if (isprint(*p) || noNumEscapes) fputc(*p,f); else { n=*p; fputc(ESCAPE_CHAR,f); fputc(((n/64)%8)+'0',f);fputc(((n/8)%8)+'0',f);fputc((n%8)+'0',f); } } if (q>0 && q!=ESCAPE_CHAR) fputc(q,f);}/* EXPORT->ReWriteString: Convert string s to writeable format */char *ReWriteString(char *s,char *dst, char q){ static char stat[MAXSTRLEN*4]; Boolean noSing,noDbl; int n; unsigned char *p,*d; if (dst==NULL) d=(unsigned char*)(dst=stat); else d=(unsigned char*)dst; if (s[0]!=SING_QUOTE) noSing=TRUE; else noSing=FALSE; if (s[0]!=DBL_QUOTE) noDbl=TRUE; else noDbl=FALSE; if (q!=ESCAPE_CHAR && q!=SING_QUOTE && q!=DBL_QUOTE) { q=0; if (!noDbl || !noSing) { if (noSing && !noDbl) q=SING_QUOTE; else q=DBL_QUOTE; } } if (q>0 && q!=ESCAPE_CHAR) *d++=q; for (p=(unsigned char*)s;*p;p++) { if (*p==ESCAPE_CHAR || *p==q || (q==ESCAPE_CHAR && p==(unsigned char*)s && (*p==SING_QUOTE || *p==DBL_QUOTE))) *d++=ESCAPE_CHAR,*d++=*p; else if (isprint(*p) || noNumEscapes) *d++=*p; else { n=*p; *d++=ESCAPE_CHAR; *d++=((n/64)%8)+'0';*d++=((n/8)%8)+'0';*d++=(n%8)+'0'; } } if (q>0 && q!=ESCAPE_CHAR) *d++=q; *d=0; return(dst);}/* IsVAXOrder: returns true if machine has VAX ordered bytes */static Boolean IsVAXOrder(void){ short x, *px; unsigned char *pc; px = &x; pc = (unsigned char *) px; *pc = 1; *(pc+1) = 0; /* store bytes 1 0 */ return x==1; /* does it read back as 1? */}/* SwapInt32: swap byte order of int32 data value *p */void SwapInt32(int32 *p){ char temp,*q; q = (char*) p; temp = *q; *q = *(q+3); *(q+3) = temp; temp = *(q+1); *(q+1) = *(q+2); *(q+2) = temp;}/* SwapShort: swap byte order of short data value *p */void SwapShort(short *p){ char temp,*q; q = (char*) p; temp = *q; *q = *(q+1); *(q+1) = temp;}/* EXPORT->ReadShort: read n short's from src in ascii or binary */Boolean RawReadShort(Source *src, short *s, int n, Boolean bin, Boolean swap){ int j,k,count=0,x; short *p; if (bin){ if (fread(s,sizeof(short),n,src->f) != n) return FALSE; if (swap) for(p=s,j=0;j<n;p++,j++) SwapShort(p); /* Need to swap to machine order */ count = n*sizeof(short); } else { if (src->pbValid) { ungetc(src->putback, src->f); src->pbValid = FALSE; } for (j=1; j<=n; j++){ if (fscanf(src->f,"%d%n",&x,&k) != 1) return FALSE; *s++ = x; count += k; } } src->chcount += count; return TRUE;}/* EXPORT->ReadInt: read n ints from src in ascii or binary */Boolean RawReadInt(Source *src, int *i, int n, Boolean bin, Boolean swap){ int j,k,count=0; int *p; if (bin){ if (fread(i,sizeof(int),n,src->f) != n) return FALSE; if (swap) for(p=i,j=0;j<n;p++,j++) SwapInt32((int32*)p); /* Read in SUNSO unless natReadOrder=T */ count = n*sizeof(int); } else { if (src->pbValid) { ungetc(src->putback, src->f); src->pbValid = FALSE; } for (j=1; j<=n; j++){ if (fscanf(src->f,"%d%n",i,&k) != 1) return FALSE; i++; count += k; } } src->chcount += count; return TRUE;}/* EXPORT->ReadFloat: read n floats from src in ascii or binary */Boolean RawReadFloat(Source *src, float *x, int n, Boolean bin, Boolean swap){ int k,count=0,j; float *p; if (bin){ if (fread(x,sizeof(float),n,src->f) != n) return FALSE; if (swap) for(p=x,j=0;j<n;p++,j++) SwapInt32((int32*)p); /* Read in SUNSO unless natReadOrder=T */ count += n*sizeof(float); } else { if (src->pbValid) { ungetc(src->putback, src->f); src->pbValid = FALSE; } for (j=1; j<=n; j++){ if (fscanf(src->f,"%e%n",x,&k) != 1) return FALSE; x++; count += k; } } src->chcount += count; return TRUE;}/* EXPORT->ReadShort: read n short's from src in ascii or binary */Boolean ReadShort(Source *src, short *s, int n, Boolean binary){ return(RawReadShort(src,s,n,binary,(vaxOrder && !natReadOrder)));}/* EXPORT->ReadInt: read n ints from src in ascii or binary */Boolean ReadInt(Source *src, int *i, int n, Boolean binary){ return(RawReadInt(src,i,n,binary,(vaxOrder && !natReadOrder)));}/* EXPORT->ReadFloat: read n floats from src in ascii or binary */Boolean ReadFloat(Source *src, float *x, int n, Boolean binary){ return(RawReadFloat(src,x,n,binary,(vaxOrder && !natReadOrder)));}/* EXPORT->KeyPressed: returns TRUE if input is pending on stdin */Boolean KeyPressed(int tWait){ Boolean rtn=FALSE; char c[1]; #ifdef UNIX { long numchars=0; fflush(stdout); ioctl(0,FIONREAD,&numchars); if ( numchars > 0){ read(0, c, 1); rtn=TRUE; } }#endif#ifdef WIN32 INPUT_RECORD inRec; DWORD charToRead = 1; DWORD charsRead, charsPeeked; HANDLE stdinHdle; stdinHdle = GetStdHandle( STD_INPUT_HANDLE ); PeekConsoleInput( stdinHdle, &inRec, charToRead, &charsPeeked ); if( charsPeeked > 0 ){ ReadConsoleInput( stdinHdle, &inRec, charToRead, &charsRead ); FlushConsoleInputBuffer( stdinHdle ); if(inRec.Event.KeyEvent.bKeyDown && inRec.Event.KeyEvent.uChar.AsciiChar == '\r') { rtn = TRUE; } } #endif return rtn;} /* -------------------- Error Reporting -------------------- *//* If HAPI is controlling things want it to be able to control exit behaviour.*/void Exit(int exitcode){ if (exitcode==0 && showConfig) PrintConfig(); exit(exitcode);}/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -