/* * The fp file pointer is open for read on a file which has contents * that are encoded in the user's locale charset. That multibyte stream * of characters is converted to wide characters and returned one at * a time. * * Not sure what to do if an uninterpretable character happens. Returning * the bad character now. */ UCS read_a_wide_char(FILE *fp, void *input_cs) /* input_cs ignored in Windows */ { #ifdef _WINDOWS _TINT val; val = _fgettc(fp); if(val == _TEOF) return(CCONV_EOF); return((UCS) val); #else /* UNIX */ unsigned long octets_so_far, remaining_octets; unsigned char *inputp; unsigned char inputbuf[20]; int c; UCS ucs; c = fgetc(fp); if(c == EOF) return(CCONV_EOF); /* * Read enough bytes to make up a character and convert it to UCS-4. */ memset(inputbuf, 0, sizeof(inputbuf)); inputbuf[0] = (unsigned char) c; octets_so_far = 1; for(;;){ remaining_octets = octets_so_far; inputp = inputbuf; ucs = mbtow(input_cs, &inputp, &remaining_octets); switch(ucs){ case CCONV_BADCHAR: return(bad_char); case CCONV_NEEDMORE: if(octets_so_far >= sizeof(inputbuf)) return(bad_char); c = fgetc(fp); if(c == EOF) return(CCONV_EOF); inputbuf[octets_so_far++] = (unsigned char) c; break; default: /* got a good UCS-4 character */ return(ucs); } } return(bad_char); #endif /* UNIX */ }
void GetStr(FILE *pf,LPTSTR pbuf) { while( !feof(pf) ) { *pbuf = _fgettc(pf); if(*pbuf == _T('\n')) { break; } pbuf++; } *pbuf = _T('\0'); }
/****************************************************************************** * Calls command for each line of a registry file. * Correctly processes comments (in # form), line continuation. * * Parameters: * in - input stream to read from * command - command to be called for each line */ void processRegLines(FILE *in, CommandAPI command) { LPTSTR line = NULL; /* line read from input stream */ ULONG lineSize = REG_VAL_BUF_SIZE; line = HeapAlloc(GetProcessHeap(), 0, lineSize * sizeof(TCHAR)); CHECK_ENOUGH_MEMORY(line); while (!feof(in)) { LPTSTR s; /* The pointer into line for where the current fgets should read */ s = line; for (;;) { size_t size_remaining; int size_to_get; TCHAR *s_eol; /* various local uses */ /* Do we need to expand the buffer ? */ assert (s >= line && s <= line + lineSize); size_remaining = lineSize - (s-line); if (size_remaining < 2) { /* room for 1 character and the \0 */ TCHAR *new_buffer; size_t new_size = lineSize + REG_VAL_BUF_SIZE; if (new_size > lineSize) /* no arithmetic overflow */ new_buffer = HeapReAlloc (GetProcessHeap(), 0, line, new_size * sizeof(TCHAR)); else new_buffer = NULL; CHECK_ENOUGH_MEMORY(new_buffer); line = new_buffer; s = line + lineSize - size_remaining; lineSize = new_size; size_remaining = lineSize - (s-line); } /* Get as much as possible into the buffer, terminated either by * eof, error, eol or getting the maximum amount. Abort on error. */ // // This line is surely foobar, don't want to read INT_MAX in buffer at s, it's never going to be that big... // size_to_get = (size_remaining > INT_MAX ? INT_MAX : size_remaining); // // Looks as if 'lineSize' contains the number of characters of buffer size // size_to_get = (size_remaining > lineSize ? lineSize : size_remaining); if (NULL == _fgetts(s, size_to_get, in)) { if (ferror(in)) { //_tperror(_T("While reading input")); perror ("While reading input"); //exit(IO_ERROR); return; } else { assert (feof(in)); *s = _T('\0'); /* It is not clear to me from the definition that the * contents of the buffer are well defined on detecting * an eof without managing to read anything. */ } } /* If we didn't read the eol nor the eof go around for the rest */ s_eol = _tcschr (s, _T('\n')); if (!feof (in) && !s_eol) { s = _tcschr (s, _T('\0')); /* It should be s + size_to_get - 1 but this is safer */ continue; } /* If it is a comment line then discard it and go around again */ if (line [0] == _T('#')) { s = line; continue; } /* Remove any line feed. Leave s_eol on the \0 */ if (s_eol) { *s_eol = _T('\0'); if (s_eol > line && *(s_eol-1) == _T('\r')) *--s_eol = _T('\0'); } else { s_eol = _tcschr (s, _T('\0')); } /* If there is a concatenating \\ then go around again */ if (s_eol > line && *(s_eol-1) == _T('\\')) { int c; s = s_eol-1; /* The following error protection could be made more self- * correcting but I thought it not worth trying. */ if ((c = _fgettc(in)) == _TEOF || c != _T(' ') || (c = _fgettc(in)) == _TEOF || c != _T(' ')) _tprintf(_T("ERROR - invalid continuation.\n")); continue; } break; /* That is the full virtual line */ } command(line); } command(NULL); HeapFree(GetProcessHeap(), 0, line); }
bool translate(_TCHAR**lpszInput,size_t ilen,int*marker,LPCTSTR lpszFormat,...){ FILE file; va_list args; // file._flag = _IOREAD|_IOSTRG|_IOMYBUF; file._ptr = file._base = (char*)(*lpszInput); file._cnt = (int)ilen * sizeof(**lpszInput); // *marker = -1; // va_start(args,lpszFormat); #if defined(_UNICODE) if( (_winput_s_l(&file,lpszFormat,NULL,args) >= 0) && (*marker >= 0) ){ #else if( (_input_s_l(&file,(const unsigned char*)lpszFormat,NULL,args) >= 0) && (*marker >= 0) ){ #endif *lpszInput = ( (_TCHAR*)file._ptr ) - 1; va_end(args); return true; } va_end(args); return false; } int _tmain(int argc, _TCHAR* argv[]){ _TCHAR szINI[MAX_PATH]; _TCHAR mode[64]; LARGE_INTEGER version; LPTSTR lpszInput,lpszOutput,lpszINI,lpszBuffer,lpCh,lpCCs; FILE*output,*input; size_t i,len,ilen,std_out,hidden; errno_t err; long bomlen; int line,marker; unsigned int x; _TCHAR ccs; // if( argc < 4 ){ return usage(); } // lpszINI = argv[argc - 3]; lpszInput = argv[argc - 2]; lpszOutput = argv[argc - 1]; // if( ::GetFileAttributes(lpszInput) == INVALID_FILE_ATTRIBUTES ){ return usage(); } // for(ccs = _T('a'),i = argc - 4;i--;){ lpCh = argv[i + 1]; if( !_tcschr(_T("-/"),*lpCh) )continue; for(len = 0; *lpCh ;++len,++lpCh); if( !len )continue; for(--lpCh; len-- && !_tcschr(_T("aUue"),*lpCh) ; --lpCh); if( !++len )continue; ccs = *lpCh; break; } switch( ccs ){ case _T('a'):{ lpCCs = _T(""); break; } case _T('U'):{ lpCCs = _T(", ccs=UNICODE"); break; } case _T('u'):{ lpCCs = _T(", ccs=UTF-8"); break; } case _T('e'):{ lpCCs = _T(", ccs=UTF-16LE"); break; } } _stprintf_s(mode,_T("rt%s"),lpCCs); if( err = _tfopen_s(&input,lpszInput,mode) ){ lc_print(_T(".866"),_T("\r\nошибка при открытии файла-шаблона\r\n")); return usage(); } _stprintf_s(mode,_T("wt%s"),lpCCs); if( err = _tfopen_s(&output,lpszOutput,mode) ){ lc_print(_T(".866"),_T("\r\nошибка при создании выходного файла\r\n")); fclose(input); return usage(); } // bomlen = ftell(input); for(len = 0; !feof(input) ;_fgettc(input),++len); lpszBuffer = (LPTSTR)malloc( (len + 1) * sizeof(_TCHAR) ); lpszBuffer[len] = _T('\0'); fseek(input,bomlen,SEEK_SET); for(lpCh = lpszBuffer; !feof(input) ;*(lpCh++) = _fgettc(input)); fclose(input); // ::GetFullPathName(lpszINI,sizeof(szINI) / sizeof(_TCHAR),szINI,NULL); version.LowPart = ::GetPrivateProfileInt(_T("general"),_T("low"),0x00000000,szINI); version.HighPart = ::GetPrivateProfileInt(_T("general"),_T("high"),0x00000000,szINI); // for(line = 1,hidden = 0,std_out = 0,lpCh = lpszBuffer; *lpCh ;++lpCh){ switch( *lpCh ){ case _T('\n'):{ ++line; break; } case _T('%'):{ if( lpCh[1] == _T('%') ){ ++lpCh; }else{ ilen = len - (lpCh - lpszBuffer); if( translate(&lpCh,ilen,&marker,_T("%%<*%n"),&marker) ){ ++hidden; continue; } if( translate(&lpCh,ilen,&marker,_T("%%*>%n"),&marker) ){ --hidden; continue; } if( translate(&lpCh,ilen,&marker,_T("%%<<%n"),&marker) ){ ++std_out; continue; } if( translate(&lpCh,ilen,&marker,_T("%%>>%n"),&marker) ){ --std_out; continue; } if( translate(&lpCh,ilen,&marker,_T("%%qw++%n"),&marker) ){ if( !hidden ){ _ftprintf(output,_T("%I64u"),version.QuadPart); } if( std_out ){ _ftprintf(stdout,_T("%I64u"),version.QuadPart); } ++version.QuadPart; continue; } if( translate(&lpCh,ilen,&marker,_T("%%++qw%n"),&marker) ){ ++version.QuadPart; if( !hidden ){ _ftprintf(output,_T("%I64u"),version.QuadPart); } if( std_out ){ _ftprintf(stdout,_T("%I64u"),version.QuadPart); } continue; } if( translate(&lpCh,ilen,&marker,_T("%%qw%n"),&marker) ){ if( !hidden ){ _ftprintf(output,_T("%I64u"),version.QuadPart); } if( std_out ){ _ftprintf(stdout,_T("%I64u"),version.QuadPart); } continue; } if( translate(&lpCh,ilen,&marker,_T("%%dw.%u++%n"),&x,&marker) ){ if(x > 1){ fclose(output); return out_of_range(lpszInput,line); } DWORD&value = ((LPDWORD)&version.LowPart)[x]; if( !hidden ){ _ftprintf(output,_T("%I32u"),value); } if( std_out ){ _ftprintf(stdout,_T("%I32u"),value); } ++value; continue; } if( translate(&lpCh,ilen,&marker,_T("%%++dw.%u%n"),&x,&marker) ){ if(x > 1){ fclose(output); return out_of_range(lpszInput,line); } DWORD&value = ((LPDWORD)&version.LowPart)[x]; ++value; if( !hidden ){ _ftprintf(output,_T("%I32u"),value); } if( std_out ){ _ftprintf(stdout,_T("%I32u"),value); } continue; } if( translate(&lpCh,ilen,&marker,_T("%%dw.%u%n"),&x,&marker) ){ if(x > 1){ fclose(output); return out_of_range(lpszInput,line); } DWORD&value = ((LPDWORD)&version.LowPart)[x]; if( !hidden ){ _ftprintf(output,_T("%I32u"),value); } if( std_out ){ _ftprintf(stdout,_T("%I32u"),value); } continue; } if( translate(&lpCh,ilen,&marker,_T("%%w.%u++%n"),&x,&marker) ){ if(x > 3){ fclose(output); return out_of_range(lpszInput,line); } WORD&value = ((LPWORD)&version.LowPart)[x]; if( !hidden ){ _ftprintf(output,_T("%hu"),value); } if( std_out ){ _ftprintf(stdout,_T("%hu"),value); } ++value; continue; } if( translate(&lpCh,ilen,&marker,_T("%%++w.%u%n"),&x,&marker) ){ if(x > 3){ fclose(output); return out_of_range(lpszInput,line); } WORD&value = ((LPWORD)&version.LowPart)[x]; ++value; if( !hidden ){ _ftprintf(output,_T("%hu"),value); } if( std_out ){ _ftprintf(stdout,_T("%hu"),value); } continue; } if( translate(&lpCh,ilen,&marker,_T("%%w.%u%n"),&x,&marker) ){ if(x > 3){ fclose(output); return out_of_range(lpszInput,line); } WORD&value = ((LPWORD)&version.LowPart)[x]; if( !hidden ){ _ftprintf(output,_T("%hu"),value); } if( std_out ){ _ftprintf(stdout,_T("%hu"),value); } continue; } if( translate(&lpCh,ilen,&marker,_T("%%b.%u++%n"),&x,&marker) ){ if(x > 7){ fclose(output); return out_of_range(lpszInput,line); } BYTE&value = ((LPBYTE)&version.LowPart)[x]; if( !hidden ){ _ftprintf(output,_T("%hu"),(WORD)value); } if( std_out ){ _ftprintf(stdout,_T("%hu"),(WORD)value); } ++value; continue; } if( translate(&lpCh,ilen,&marker,_T("%%++b.%u%n"),&x,&marker) ){ if(x > 7){ fclose(output); return out_of_range(lpszInput,line); } BYTE&value = ((LPBYTE)&version.LowPart)[x]; ++value; if( !hidden ){ _ftprintf(output,_T("%hu"),(WORD)value); } if( std_out ){ _ftprintf(stdout,_T("%hu"),(WORD)value); } continue; } if( translate(&lpCh,ilen,&marker,_T("%%b.%u%n"),&x,&marker) ){ if(x > 7){ fclose(output); return out_of_range(lpszInput,line); } BYTE&value = ((LPBYTE)&version.LowPart)[x]; if( !hidden ){ _ftprintf(output,_T("%hu"),(WORD)value); } if( std_out ){ _ftprintf(stdout,_T("%hu"),(WORD)value); } continue; } } } } if( !hidden ){ _fputtc(*lpCh,output); } if(std_out){ if( _tcschr(_T("Uue"),ccs) ){ lc_print(_T(".OCP"),_T("%c"),*lpCh); }else{ oem_print(_T("%c"),*lpCh); } } } free(lpszBuffer); fclose(output); // for(i = argc - 4;i--;){ lpCh = argv[i + 1]; if( _tcschr(_T("-/"),*lpCh) ){ if( _tcschr(lpCh + 1,_T('r')) ){ break; } } } if( !(i + 1) ){ _stprintf_s(mode,_T("%#.8x"),version.LowPart); ::WritePrivateProfileString(_T("general"),_T("low"),mode,szINI); _stprintf_s(mode,_T("%#.8x"),version.HighPart); ::WritePrivateProfileString(_T("general"),_T("high"),mode,szINI); } // return S_OK; }
_TINT (_RTLENTRY _EXPFUNC _gettc)( FILE *fp ) { return( _fgettc( fp ) ); }