Skip to content

Commit 8a03727

Browse files
committed
Change all bare std::ofstream/ifstream to OIIO::ofstream/ifstream (#1188)
Because they are UTF-8 safe and work properly on MinGW. Simplified a bit more as I went -- some reading idioms could be replaced by a single call to Filesystem::read_text_file, some close() calls were redundant because streams close upon destruction / scope exit, some open flags unnecessary because the default write mode already will truncate an existing file. Signed-off-by: Larry Gritz <[email protected]>
1 parent 79c7299 commit 8a03727

File tree

6 files changed

+16
-35
lines changed

6 files changed

+16
-35
lines changed

src/liboslcomp/oslcomp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ OSLCompilerImpl::preprocess_file (const std::string &filename,
159159
std::string &result)
160160
{
161161
// Read file contents into a string
162-
std::ifstream instream;
162+
OIIO::ifstream instream;
163163
OIIO::Filesystem::open(instream, filename);
164164
if (! instream.is_open()) {
165165
error (ustring(filename), 0, "Could not open \"%s\"\n", filename.c_str());
@@ -599,7 +599,7 @@ OSLCompilerImpl::compile (string_view filename,
599599
if (m_output_filename.size() == 0)
600600
m_output_filename = default_output_filename ();
601601

602-
std::ofstream oso_output;
602+
OIIO::ofstream oso_output;
603603
OIIO::Filesystem::open (oso_output, m_output_filename);
604604
if (! oso_output.good()) {
605605
error (ustring(), 0, "Could not open \"%s\"",

src/liboslexec/llvm_instance.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,8 +1224,9 @@ BackendLLVM::run ()
12241224
if (safegroup.size() > 235)
12251225
safegroup = Strutil::format ("TRUNC_%s_%d", safegroup.substr(safegroup.size()-235), group().id());
12261226
std::string name = Strutil::format ("%s.ll", safegroup);
1227-
std::ofstream out (name, std::ios_base::out | std::ios_base::trunc);
1228-
if (out.good()) {
1227+
OIIO::ofstream out;
1228+
OIIO::Filesystem::open(out, name);
1229+
if (out) {
12291230
out << ll.bitcode_string (ll.module());
12301231
} else {
12311232
shadingcontext()->error ("Could not write to '%s'", name);
@@ -1253,8 +1254,9 @@ BackendLLVM::run ()
12531254
if (safegroup.size() > 235)
12541255
safegroup = Strutil::format ("TRUNC_%s_%d", safegroup.substr(safegroup.size()-235), group().id());
12551256
std::string name = Strutil::format ("%s_opt.ll", safegroup);
1256-
std::ofstream out (name, std::ios_base::out | std::ios_base::trunc);
1257-
if (out.good()) {
1257+
OIIO::ofstream out;
1258+
OIIO::Filesystem::open(out, name);
1259+
if (out) {
12581260
out << ll.bitcode_string (ll.module());
12591261
} else {
12601262
shadingcontext()->error ("Could not write to '%s'", name);

src/liboslexec/shadingsys.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3108,7 +3108,7 @@ ShadingSystemImpl::archive_shadergroup (ShaderGroup *group, string_view filename
31083108

31093109
bool ok = true;
31103110
std::string groupfilename = tmpdir + "/shadergroup";
3111-
std::ofstream groupfile;
3111+
OIIO::ofstream groupfile;
31123112
OIIO::Filesystem::open(groupfile, groupfilename);
31133113
if (groupfile.good()) {
31143114
groupfile << group->serialize();

src/oslc/oslcmain.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,11 @@ main (int argc, const char *argv[])
183183
ok = compiler.compile_buffer (sourcecode, osobuffer, args, "",
184184
shader_path);
185185
if (ok) {
186-
std::ofstream file;
186+
OIIO::ofstream file;
187187
OIIO::Filesystem::open (file, compiler.output_filename());
188-
if (file.good()) {
188+
if (file)
189189
file << osobuffer;
190-
file.close ();
191-
}
190+
ok = file.good();
192191
}
193192
} else {
194193
// Ordinary compile from file

src/osltoy/osltoyapp.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -731,12 +731,10 @@ OSLToyMainWindow::action_save ()
731731
}
732732
std::string text = texteditor->text_string();
733733

734-
std::ofstream out (filename, std::ios_base::out | std::ios_base::trunc);
735-
if (out.good()) {
734+
OIIO::ofstream out;
735+
OIIO::Filesystem::open (out, filename);
736+
if (out)
736737
out << text;
737-
out.close ();
738-
}
739-
740738
if (out.fail()) {
741739
std::string msg = OIIO::Strutil::format ("Could not write %s", filename);
742740
QErrorMessage err (nullptr);

src/testshade/testshade.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,24 +141,6 @@ set_shadingsys_options ()
141141

142142

143143

144-
/// Read the entire contents of the named file and place it in str,
145-
/// returning true on success, false on failure.
146-
inline bool
147-
read_text_file (string_view filename, std::string &str)
148-
{
149-
std::ifstream in (filename.c_str(), std::ios::in | std::ios::binary);
150-
if (in) {
151-
std::ostringstream contents;
152-
contents << in.rdbuf();
153-
in.close ();
154-
str = contents.str();
155-
return true;
156-
}
157-
return false;
158-
}
159-
160-
161-
162144
static void
163145
compile_buffer (const std::string &sourcecode,
164146
const std::string &shadername)
@@ -190,7 +172,7 @@ shader_from_buffers (std::string shadername)
190172
if (! OIIO::Strutil::ends_with (oslfilename, ".osl"))
191173
oslfilename += ".osl";
192174
std::string sourcecode;
193-
if (! read_text_file (oslfilename, sourcecode)) {
175+
if (! OIIO::Filesystem::read_text_file (oslfilename, sourcecode)) {
194176
std::cerr << "Could not open \"" << oslfilename << "\"\n";
195177
exit (EXIT_FAILURE);
196178
}

0 commit comments

Comments
 (0)