Exemple #1
0
void print_locals(Env *env, FILE *stream)
{
	const Binding *bnd = 0;
	static bool rec = false;

	assert(env != 0);
	assert(stream != 0);

	if (rec == true) {
		fputws(L"...", stream);
		return;
	}

	rec = true;

	for (bnd = env->bindings; bnd != UNBOUND; bnd = bnd->link) {
		fputws(bnd->name, stream);
		fputwc(L'=', stream);
		print_value(bnd->value, stream);
		if (bnd->link != 0) {
			fputwc(L',', stream);
		}
	}

	rec = false;
}
Exemple #2
0
LISPTR lisp_print(LISPTR x, FILE* out)
{
	if (consp(x)) {
		fputwc('(', out);
		while (true) {
			lisp_print(car(x), out);
			x = cdr(x);
			if (!consp(x)) {
				if (x != NIL) {
					fputws(L" . ", out);
					lisp_print(x, out);
				}
				break;
			}
			fputwc(' ', out);
		}
		fputwc(')', out);
	} else if (symbolp(x)) {
		fputws(string_text(symbol_name(x)), out);
	} else if (numberp(x)) {
		fwprintf(out, L"%g", number_value(x));
	} else if (stringp(x)) {
		fputwc('"', out);
		fputws(string_text(x), out);
		fputwc('"', out);
	} else {
		fputws(L"*UNKOBJ*", out);
	}
	return x;
}
Exemple #3
0
void print_env(Env *env, FILE *stream)
{
	const Binding *bnd = 0;
	static bool rec = 0;

	if (rec == 1) {
		fputws(L"...", stream);
		return;
	}

	rec = 1;

	fputws(L"#<Env", stream);
	for (; env != 0; env = env->link) {
		for (bnd = env->bindings; bnd != UNBOUND; bnd = bnd->link) {
			fputwc(L' ', stream);
			fputws(bnd->name, stream);
			fputwc(L'=', stream);
			print_value(bnd->value, stream);
			if (bnd->link != 0) {
				fputwc(L',', stream);
			}
		}
	}

	rec = 0;

	fputwc(L'>', stream);
}
Exemple #4
0
void show_string(Array a, FILE* out) {
	fputwc(L'"', out);
	int i;
	for(i = 0; i < a.len; i++)
		show_stringed_char(a.items[i].chara, out);
	fputwc(L'"', out);
}
// Exercise the interaction between fpos and seek.
TEST(stdio, fpos_t_and_seek) {
  ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
  uselocale(LC_GLOBAL_LOCALE);

  // In glibc-2.16 fseek doesn't work properly in wide mode
  // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
  // to close and re-open the file. We do it in order to make the test pass
  // with all glibcs.

  TemporaryFile tf;
  FILE* fp = fdopen(tf.fd, "w+");
  ASSERT_TRUE(fp != NULL);

  wchar_t mb_two_bytes = 0x00a2;
  wchar_t mb_three_bytes = 0x20ac;
  wchar_t mb_four_bytes = 0x24b62;

  // Write to file.
  ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
  ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
  ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));

  fflush(fp);
  fclose(fp);

  fp = fopen(tf.filename, "r");
  ASSERT_TRUE(fp != NULL);

  // Store a valid position.
  fpos_t mb_two_bytes_pos;
  ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));

  // Move inside mb_four_bytes with fseek.
  long offset_inside_mb = 6;
  ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));

  // Store the "inside multi byte" position.
  fpos_t pos_inside_mb;
  ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
#if defined(__BIONIC__)
  ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
#endif

  // Reading from within a byte should produce an error.
  ASSERT_EQ(WEOF, fgetwc(fp));
  ASSERT_EQ(EILSEQ, errno);

  // Reverting to a valid position should work.
  ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
  ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));

  // Moving withing a multi byte with fsetpos should work but reading should
  // produce an error.
  ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
  ASSERT_EQ(WEOF, fgetwc(fp));
  ASSERT_EQ(EILSEQ, errno);

  fclose(fp);
}
/*---------------------------------------------------------------------------
**        Name : WirteLine
**      Author : Barry Tang
**        Date : 2009/10/15
** Description : Write a line end into the file
**       Input : None
**      Output : None
**---------------------------------------------------------------------------*/
VOID CUnicodeFile::WriteLineEnd(VOID)
{
	//The file can not for writing
	if (m_fileOperateFlag == FOR_READ)
	{
		return;
	}

	fputwc(0x000d, m_pFile);
	fputwc(0x000a, m_pFile);
}
Exemple #7
0
/**
 * Funkcja wypisująca informacje o podpowiedziach dla danego słowa na wyjście
 * błędów: numer wiersza słowa, numer kolumny słowa, słowo i listę
 * podpowiedzi.
 * @param [in] pstate Stan parsera, z którego pochodzą dane.
 * @param [in] word_column_num Numer kolumny słowa do komunikatu.
 * @param [in] wlist word_list z podpowiedziami do komunikatu.
 * @param [out] out Strumień wyjściowy dla komunikatu.
 */
void print_verbose_info(const struct parser_state * pstate,
        int word_column_num, const struct word_list * wlist, FILE * out)
{
    fwprintf(out, L"%d,%d %ls: ", pstate->line_num,
            word_column_num, word_buffer);

    const wchar_t * const * hints_array = word_list_get(wlist);
    for (size_t i = 0; i < word_list_size(wlist); i++)
    {
        if (i > 0)
            fputwc(L' ', out);
        fputws(hints_array[i], out);
    }
    fputwc(L'\n', out);
}
Exemple #8
0
int main()
{
    char file_in[ ]  = "local_in.txt",
         file_out[ ] = "local_out.txt";
    FILE *fp_in_wide, *fp_out_wide;
    wint_t wc;

    if ( setlocale( LC_CTYPE, "" ) == NULL)
       fwprintf( stderr,
                 L"Sorry, couldn't change to the system's native locale.\n"),
       exit(1);

    if (( fp_in_wide = fopen( file_in, "r" )) == NULL )
       fprintf( stderr, "Error opening the file %s\n", file_in), exit(2);

    if (( fp_out_wide = fopen( file_out, "w" )) == NULL )
       fprintf( stderr, "Error opening the file %s\n", file_out), exit(3);

    fwide( fp_in_wide, 1);            // Not strictly necessary, since first
    fwide( fp_out_wide, 1);           // file access also sets wide or byte mode.

    while (( wc = fgetwc( fp_in_wide )) != WEOF )
    {
       // ... process each wide character read ...

       if ( fputwc( (wchar_t)wc, fp_out_wide) == WEOF)
         break;
    }
    if ( ferror( fp_in_wide))
       fprintf( stderr, "Error reading the file %s\n", file_in);
    if ( ferror( fp_out_wide))
       fprintf( stderr, "Error writing to the file %s\n", file_out);

    return 0;
}
BOOL CCustomAutoComplete::SaveList(LPCTSTR pcFileName)
{
#ifdef _UNICODE
	FILE	*fp = _tfsopen(pcFileName, _T("wb"), _SH_DENYWR);
#else
	FILE	*fp = _tfsopen(pcFileName, _T("w"), _SH_DENYWR);
#endif

	if (fp == NULL)
		return FALSE;

#ifdef _UNICODE
//	Write Unicode byte-order mark 0xFEFF
	fputwc(0xFEFF, fp);

#endif
	for (int i = 0; i < m_asList.GetCount(); i++)
#ifdef _UNICODE
		_ftprintf(fp, _T("%s\r\n"), m_asList[i]);
#else
		_ftprintf(fp, _T("%s\n"), m_asList[i]);
#endif
	fclose(fp);

	return (ferror(fp) == 0);
}
static void bs1770gain_print_label(const char *label, int width, FILE *f)
{
#if defined (W_WIN32) // [
  wchar_t *wlabel;

  if (NULL==(wlabel=pbu_s2w(label))) {
    DMESSAGE("converting UTF-8 to UTF-16");
    goto wlabel;
  }
#endif // ]

  width+=6;

#if defined (W_WIN32) // [
  for (width-=wcslen(wlabel);0<width;--width)
    fputwc(L' ',f);
#else // ] [
  for (width-=strlen(label);0<width;--width)
    fputc(' ',f);
#endif // ]

#if defined (W_WIN32) // [
  fwprintf(f,L"%s:  ",wlabel);
#else // ] [
  fprintf(f,"%s:  ",label);
#endif // ]

#if defined (W_WIN32) // [
  free(wlabel);
wlabel:
  return;
#endif // ]
}
void CWE252_Unchecked_Return_Value__wchar_t_fputc_12_bad()
{
    if(globalReturnsTrueOrFalse())
    {
        /* FLAW: Do not check the return value */
        fputwc((wchar_t)L'A', stdout);
    }
    else
    {
        /* FIX: check the return value */
        if (fputwc((wchar_t)L'A', stdout) == WEOF)
        {
            printLine("fputwc failed!");
        }
    }
}
Exemple #12
0
int main()
{
    int c,i;
    wchar_t s[100];
    FILE *fil = fopen("hi.txt","w+");
    printf("%d\n",fwide(fil,0));
    fputwc(L'c',fil);
    fputws(L"hi dave\n",fil);
    fwprintf(fil,L"%ls\n",L"geez");
    printf("%d\n",fwide(fil,0));
    printf("%d\n",fputc('z',fil));
    printf("%d\n",fwide(fil,-1));
    printf("%d\n",fputc('y',fil));
    printf("%d\n",fputc('y',fil));
    rewind(fil);
    printf("%d\n",fwide(fil,1));
    printf("%04x\n",fgetwc(fil));
    printf("%04x\n",fgetwc(fil));
    printf(":%x: ",fgetws(s,100,fil));
    for (i=0; i <wcslen(s); i++)
        printf("%04x ",s[i]);
    printf("\n");
    fwscanf(fil,L"%ls ",s);
    for (i=0; i <wcslen(s); i++)
        printf("%04x ",s[i]);
    printf("\n");
    printf("%d\n",fwide(fil,-1));
    printf("%02x\n",fgetc(fil));
    fclose(fil);
}
Exemple #13
0
wint_t __cdecl putwc (
        wint_t ch,
        FILE *str
        )
{
        return fputwc(ch, str);
}
Exemple #14
0
DLL_EXPORTED
int
libintl_vfwprintf (FILE *stream, const wchar_t *format, va_list args)
{
  if (wcschr (format, '$') == NULL)
    return vfwprintf (stream, format, args);
  else
    {
      size_t length;
      wchar_t *result = libintl_vasnwprintf (NULL, &length, format, args);
      int retval = -1;
      if (result != NULL)
        {
          size_t i;
          for (i = 0; i < length; i++)
            if (fputwc (result[i], stream) == WEOF)
              break;
          free (result);
          if (i == length)
            {
              if (length > INT_MAX)
                errno = EOVERFLOW;
              else
                retval = length;
            }
        }
      return retval;
    }
}
bool CHTRichEditCtrl::SaveLog(LPCTSTR pszDefName)
{
	bool bResult = false;
	CFileDialog dlg(FALSE, _T("log"), pszDefName ? pszDefName : (LPCTSTR)m_strTitle, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Log Files (*.log)|*.log||"), this, 0);
	if (dlg.DoModal() == IDOK)
	{
		FILE* fp = _tfsopen(dlg.GetPathName(), _T("wb"), _SH_DENYWR);
		if (fp)
		{
			// write Unicode byte-order mark 0xFEFF
			fputwc(0xFEFF, fp);

			CString strText;
			GetWindowText(strText);
			fwrite(strText, sizeof(TCHAR), strText.GetLength(), fp);
			if (ferror(fp)){
				CString strError;
				strError.Format(_T("Failed to write log file \"%s\" - %s"), dlg.GetPathName(), _tcserror(errno));
				AfxMessageBox(strError, MB_ICONERROR);
			}
			else
				bResult = true;
			fclose(fp);
		}
		else{
			CString strError;
			strError.Format(_T("Failed to create log file \"%s\" - %s"), dlg.GetPathName(), _tcserror(errno));
			AfxMessageBox(strError, MB_ICONERROR);
		}
	}
	return bResult;
}
bool CServerList::SaveStaticServers()
{
	bool bResult = false;

	FILE* fpStaticServers = _tfsopen(thePrefs.GetMuleDirectory(EMULE_CONFIGDIR) + _T("staticservers.dat"), _T("wb"), _SH_DENYWR);
	if (fpStaticServers == NULL) {
		LogError(LOG_STATUSBAR, GetResString(IDS_ERROR_SSF));
		return bResult;
	}

	// write Unicode byte-order mark 0xFEFF
	if (fputwc(0xFEFF, fpStaticServers) != _TEOF)
	{
		bResult = true;
		POSITION pos = list.GetHeadPosition();
		while (pos)
		{
			const CServer* pServer = list.GetNext(pos);
			if (pServer->IsStaticMember())
			{
				if (_ftprintf(fpStaticServers, _T("%s:%i,%i,%s\r\n"), pServer->GetAddress(), pServer->GetPort(), pServer->GetPreference(), pServer->GetListName()) == EOF) {
					bResult = false;
					break;
				}
			}
		}
	}

	fclose(fpStaticServers);
	return bResult;
}
Exemple #17
0
/**
 * Funkcja, która finalizuje wczytywanie słowa i wypisuje o nim informacje.
 * Na standardowe wyjście słowo zostaje przepisane, lub przepisane poprzedzone
 * znakiem '#', jeśli go nie znaleziono w słowniku.
 * Jeśli w 'pstate' ustawiony jest tryb "verbose", wypisywane są również
 * dodatkowe informacje o podpowiedziach na wyjście błędów.
 * @param [in,out] pstate Stan parsera. Zostanie uaktualniona jedynie pozycja
 * w buforze.
 */
void process_word(struct parser_state * pstate)
{
    // finalizing buffer iterator
    *pstate->buffer_iterator = L'\0';
    pstate->buffer_iterator = word_buffer;

    // word for dictionary
    make_lower(word_buffer, word_buffer_lowered);

    // true position of the word
    int word_column_num = pstate->last_non_word_column_num + 1;

    // getting data from dictionary and printing it
    const bool is_in_dict = dictionary_find(pstate->dict, word_buffer_lowered);
    if (!is_in_dict && pstate->verbose)
    {
        {
            struct word_list wlist;
            dictionary_hints(pstate->dict, word_buffer_lowered, &wlist);
            print_verbose_info(pstate, word_column_num, &wlist, stderr);
        }
        fputwc(L'#', stdout);
    }
    fputws(word_buffer, stdout);
}
Exemple #18
0
void dyn_str_print_reverse(DynStr *dyn_str, FILE *fp) {
    wchar_t *current_ptr = dyn_str_ptr_to_last_char(dyn_str);

    while (current_ptr >= dyn_str->content) {
        fputwc(*current_ptr, fp);
        current_ptr--;
    }
}
void CWE252_Unchecked_Return_Value__wchar_t_fputc_11_bad()
{
    if(globalReturnsTrue())
    {
        /* FLAW: Do not check the return value */
        fputwc((wchar_t)L'A', stdout);
    }
}
Exemple #20
0
bool CLogFile::Open()
{
	if (m_fp != NULL)
		return true;

	m_fp = _tfsopen(m_strFilePath, _T("a+b"), _SH_DENYWR);
	if (m_fp != NULL)
	{
		m_tStarted = time(NULL);
		m_uBytesWritten = _filelength(_fileno(m_fp));
		if (m_uBytesWritten == 0)
		{
			if (m_eFileFormat == Unicode)
			{
				// write Unicode byte-order mark 0xFEFF
				fputwc(0xFEFF, m_fp);
			}
			else
			{
				ASSERT( m_eFileFormat == Utf8 );
				; // could write UTF-8 header..
			}
		}
		else if (m_uBytesWritten >= sizeof(WORD))
		{
			// check for Unicode byte-order mark 0xFEFF
			WORD wBOM;
			if (fread(&wBOM, sizeof(wBOM), 1, m_fp) == 1)
			{
				if (wBOM == 0xFEFF && m_eFileFormat == Unicode)
				{
					// log file already in Unicode format
					(void)fseek(m_fp, 0, SEEK_END); // actually not needed because file is opened in 'Append' mode..
				}
				else if (wBOM != 0xFEFF && m_eFileFormat != Unicode)
				{
					// log file already in UTF-8 format
					(void)fseek(m_fp, 0, SEEK_END); // actually not needed because file is opened in 'Append' mode..
				}
				else
				{
					// log file does not have the required format, create a new one (with the req. format)
					ASSERT( (m_eFileFormat==Unicode && wBOM!=0xFEFF) || (m_eFileFormat==Utf8 && wBOM==0xFEFF) );

					ASSERT( !m_bInOpenCall );
					if (!m_bInOpenCall) // just for safety
					{
						m_bInOpenCall = true;
						StartNewLogFile();
						m_bInOpenCall = false;
					}
				}
			}
		}
	}
	return m_fp != NULL;
}
Exemple #21
0
static wint_t
myfputwc(wchar_t wc, FILE *fp)
{
	if (wc & INVALID_CHAR) {
		wc &= ~INVALID_CHAR;
		return (fputc((int)wc, fp));
	}
	return (fputwc(wc, fp));
}
/*---------------------------------------------------------------------------
**        Name : SetUnicodeFlag
**      Author : Barry Tang
**        Date : 2009/10/15
** Description : Set the Unicode flag into the file
**       Input : None
**      Output : None
**---------------------------------------------------------------------------*/
VOID CUnicodeFile::SetUnicodeFlag(VOID)
{
	//The file can not for writing
	if (m_fileOperateFlag == FOR_READ)
	{
		return;
	}

	fputwc(0xfeff, m_pFile);
}
/*---------------------------------------------------------------------------
**        Name : WriteAWord
**      Author : Barry Tang
**        Date : 2009/12/11
** Description : Write a char into file
**       Input : ch, the char
**      Output :
**---------------------------------------------------------------------------*/
VOID CUnicodeFile::WriteAWord(TCHAR ch)
{
	//The file can not for writing
	if (m_fileOperateFlag == FOR_READ)
	{
		return;
	}

	fputwc(ch, m_pFile);
}
/* good1() reverses the blocks on the goto statement */
static void good1()
{
    goto sink;
sink:
    /* FIX: check for the correct return value */
    if (fputwc((wchar_t)L'A', stdout) == WEOF)
    {
        printLine("fputwc failed!");
    }
}
/* good1() uses the GoodSink on both sides of the "if" statement */
static void good1()
{
    if(globalReturnsTrueOrFalse())
    {
        /* FIX: check the return value */
        if (fputwc((wchar_t)L'A', stdout) == WEOF)
        {
            printLine("fputwc failed!");
        }
    }
    else
    {
        /* FIX: check the return value */
        if (fputwc((wchar_t)L'A', stdout) == WEOF)
        {
            printLine("fputwc failed!");
        }
    }
}
void CWE253_Incorrect_Check_of_Function_Return_Value__wchar_t_fputc_18_bad()
{
    goto sink;
sink:
    /* FLAW: fputwc() might fail, in which case the return value will be WEOF (-1), but
     * we are checking to see if the return value is 0 */
    if (fputwc((wchar_t)L'A', stdout) == 0)
    {
        printLine("fputwc failed!");
    }
}
Exemple #27
0
void WriteTweetsToFile(PPROGRAM_CONTEXT ProgramContext, PFILE_WRITER_CONTEXT FileWriterContext)
{
	for(PTWEET Tweet = ProgramContext->NodeContext.Data; Tweet != ProgramContext->NodeContext.Data + ProgramContext->NodeContext.ElementsPerNode; Tweet++)
	{		
#ifdef WRITE_DEBUG_INFO_TO_FILE
		PrintTweetDebugInfoToStream(FileWriterContext->OutputFile, ProgramContext, Tweet);
#endif	
		fputws(ProgramContext->TweetStrings + Tweet->TweetStringOffset, FileWriterContext->OutputFile);
		fputwc(L'\n', FileWriterContext->OutputFile);
	}	
}
extern "C" void FC_CSVRecordToFILEW(
    FILE*          pFile,
    const wchar_t* pBinStr, 
    int            iChars
)
{
    int     i;
    wchar_t cc;

    //first check if there are non print " or ; chars in there:
    if(FC_CSVNeedEscapeW(pBinStr, iChars))
    {
        fputwc('"', pFile);
        for(i=0; ;i++)
        {
            if(iChars>=0 && i>=iChars)
                break;

            cc = pBinStr[i];
            if(iChars<0 && cc==0)
                break;
        
            if(cc==_T('"'))
                fputwc('"', pFile);

            fputwc(cc, pFile);
        }
        fputwc('"', pFile);
    }
    else if(iChars<0)
    {
        fputws(pBinStr, pFile);
    }
    else
    {
        for(i=0; i<iChars; i++)
            fputwc(pBinStr[i], pFile);
    }
}
	BOOL CDuiAutoComplete::SaveList(LPCTSTR pszFileName)
	{
		FILE* fp = _tfsopen(pszFileName, _T("wb"), _SH_DENYWR);
		if (fp == NULL)
			return FALSE;

		// д Unicode ±àÂë×Ö½ÚÐò mark 0xFEFF
		fputwc(0xFEFF, fp);

		for (int i = 0; i < m_sStringArrayMap.GetSize(); i++)
			_ftprintf(fp, _T("%s\r\n"), m_sStringArrayMap[i]);
		fclose(fp);
		return !ferror(fp);
	}
BOOL CCustomAutoComplete::SaveList(LPCTSTR pszFileName)
{
	FILE* fp = _tfsopen(pszFileName, _T("wb"), _SH_DENYWR);
	if (fp == NULL)
		return FALSE;

	// write Unicode byte-order mark 0xFEFF
	fputwc(0xFEFF, fp);

	for (int i = 0; i < m_asList.GetCount(); i++)
		_ftprintf(fp, _T("%s\r\n"), m_asList[i]);
	fclose(fp);
	return !ferror(fp);
}