File: test_stdio.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (142 lines) | stat: -rw-r--r-- 4,645 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Copyright (c) 2012 Artyom Beilis (Tonkikh)
// Copyright (c) 2019 Alexander Grund
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/nowide/cstdio.hpp>

#include <boost/nowide/convert.hpp>
#include "test.hpp"
#include <cstdlib>
#include <cstring>
#include <iostream>

bool file_exists(const std::string& filename)
{
#ifdef BOOST_WINDOWS
    FILE* f = boost::nowide::detail::wfopen(boost::nowide::widen(filename).c_str(), L"r");
#else
    FILE* f = std::fopen(filename.c_str(), "r");
#endif
    bool result = false;
    if(f)
    {
        std::fclose(f);
        result = true;
    }
    return result;
}

void create_test_file(const std::string& filename)
{
#ifdef BOOST_WINDOWS
    FILE* f = boost::nowide::detail::wfopen(boost::nowide::widen(filename).c_str(), L"w");
#else
    FILE* f = std::fopen(filename.c_str(), "w");
#endif
    TEST(f);
    TEST(std::fputs("test\n", f) >= 0);
    std::fclose(f);
}

#ifdef BOOST_MSVC
#include <crtdbg.h> // For _CrtSetReportMode
void noop_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned, uintptr_t)
{} // LCOV_EXCL_LINE
#endif

// coverity[root_function]
void test_main(int, char** argv, char**)
{
    const std::string prefix = argv[0];
    const std::string filename = prefix + "\xd7\xa9-\xd0\xbc-\xce\xbd.txt";
#ifdef BOOST_MSVC
    // Prevent abort on freopen(NULL, ...)
    _set_invalid_parameter_handler(noop_invalid_param_handler);
#endif

    std::cout << " -- fopen - existing file" << std::endl;
    {
        create_test_file(filename);
        FILE* f = boost::nowide::fopen(filename.c_str(), "r");
        TEST(f);
        char buf[16];
        TEST(std::fgets(buf, 16, f) != 0);
        TEST(strcmp(buf, "test\n") == 0);
        std::fclose(f);
    }
    std::cout << " -- remove" << std::endl;
    {
        create_test_file(filename);
        TEST(file_exists(filename));
        TEST(boost::nowide::remove(filename.c_str()) == 0);
        TEST(!file_exists(filename));
    }
    std::cout << " -- fopen non-existing file" << std::endl;
    {
        boost::nowide::remove(filename.c_str());
        TEST(!file_exists(filename));
        TEST(boost::nowide::fopen(filename.c_str(), "r") == nullptr);
        TEST(!file_exists(filename));
    }
    std::cout << " -- freopen" << std::endl;
    {
        create_test_file(filename);
        FILE* f = boost::nowide::fopen(filename.c_str(), "r+");
        TEST(f);
        std::cout << " -- Can read & write" << std::endl;
        {
            char buf[32];
            TEST(std::fgets(buf, 32, f) != 0);
            TEST(strcmp(buf, "test\n") == 0);
            TEST(std::fseek(f, 0, SEEK_END) == 0);
            TEST(std::fputs("foobar\n", f) >= 0);
        }
        // Reopen in read mode
        // Note that changing the mode is not possibly on all implementations
        // E.g. MSVC disallows NULL completely as the file parameter
        FILE* f2 = boost::nowide::freopen(nullptr, "r", f);
        if(!f2)
            f2 = boost::nowide::freopen(filename.c_str(), "r", f);
        std::cout << " -- no write possible" << std::endl;
        {
            TEST(f2 == f);
            TEST(std::fputs("not-written\n", f) < 0);
            TEST(std::fseek(f, 0, SEEK_SET) == 0);
            char buf[32];
            TEST(std::fgets(buf, 32, f) != 0);
            TEST(strcmp(buf, "test\n") == 0);
            TEST(std::fgets(buf, 32, f) != 0);
            TEST(strcmp(buf, "foobar\n") == 0);
        }
        std::cout << " -- Reopen different file" << std::endl;
        const std::string filename2 = filename + ".1.txt";
        TEST(boost::nowide::freopen(filename2.c_str(), "w", f) == f);
        {
            char buf[32];
            TEST(std::fputs("baz\n", f) >= 0);
            std::fclose(f);
            f = boost::nowide::fopen(filename2.c_str(), "r");
            TEST(f);
            TEST(std::fgets(buf, 32, f) != 0);
            TEST(strcmp(buf, "baz\n") == 0);
        }
        std::fclose(f);
        boost::nowide::remove(filename2.c_str());
    }
    std::cout << " -- rename" << std::endl;
    {
        create_test_file(filename);
        const std::string filename2 = filename + ".1.txt";
        boost::nowide::remove(filename2.c_str());
        TEST(file_exists(filename));
        TEST(!file_exists(filename2));
        TEST(boost::nowide::rename(filename.c_str(), filename2.c_str()) == 0);
        TEST(!file_exists(filename));
        TEST(file_exists(filename2));
        TEST(boost::nowide::remove(filename.c_str()) < 0);
        TEST(boost::nowide::remove(filename2.c_str()) == 0);
    }
}