<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>al_save_config_file_f() returns true when it shouldn&#39;t</title>
		<link>http://www.allegro.cc/forums/view/615452</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 18 Jun 2015 10:28:39 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have the following code to save out config files in my engine:</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span>next_buf_size <span class="k3">=</span> <span class="n">4096</span><span class="k2">;</span>
<span class="number">  2</span><span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span>is_aok<span class="k2">)</span> <span class="k2">{</span>
<span class="number">  3</span>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_350.html" target="_blank">free</a><span class="k2">(</span>buffer<span class="k2">)</span><span class="k2">;</span> buffer <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_551.html" target="_blank">malloc</a><span class="k2">(</span>next_buf_size<span class="k2">)</span><span class="k2">;</span>
<span class="number">  4</span>  memfile <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_open_memfile"><span class="a">al_open_memfile</span></a><span class="k2">(</span>buffer, next_buf_size, <span class="s">"wb"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  5</span>  next_buf_size <span class="k3">*</span><span class="k3">=</span> <span class="n">2</span><span class="k2">;</span>
<span class="number">  6</span>  is_aok <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_save_config_file_f"><span class="a">al_save_config_file_f</span></a><span class="k2">(</span>memfile, file-&gt;conf<span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>  file_size <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_ftell"><span class="a">al_ftell</span></a><span class="k2">(</span>memfile<span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span>  <a href="http://www.allegro.cc/manual/al_fclose"><span class="a">al_fclose</span></a><span class="k2">(</span>memfile<span class="k2">)</span><span class="k2">;</span>
<span class="number">  9</span><span class="k2">}</span>
<span class="number"> 10</span><span class="k1">if</span> <span class="k2">(</span><span class="k3">!</span>sfs_fspew<span class="k2">(</span>g_fs, file-&gt;path, <span class="s">"save"</span>, buffer, file_size<span class="k2">)</span><span class="k2">)</span>
<span class="number"> 11</span>  <span class="k1">goto</span> on_error<span class="k2">;</span>
<span class="number"> 12</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_350.html" target="_blank">free</a><span class="k2">(</span>buffer<span class="k2">)</span><span class="k2">;</span>
</div></div><p>

This is needed because games run in my engine may have their assets either on disk or compressed into a wad-like package and my sfs_*() routines abstract that so that everything works in both cases.  So I figured, okay, I could use a memfile to stage everything first, then call sfs_fspew() to write it out to whereever.  Except for one small problem: al_save_config_file_f() returns true even if the buffer is too small to hold the entire file (try setting next_buf_size to 1 before the loop, it still &quot;succeeds&quot;), so my code is broken.</p><p>Any way I could get around this, or if it could perhaps be fixed in Allegro?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Pascoe)</author>
		<pubDate>Tue, 16 Jun 2015 10:24:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Depending on how this error manifests itself, this can go both ways. Allegro doesn&#39;t check the return value of the file writing functions (shame on it) but it does check <span class="source-code"><a href="http://www.allegro.cc/manual/al_ferror"><span class="a">al_ferror</span></a></span> <s>which you could set</s> (EDIT: nvm, you&#39;re using the memfile addon). I&#39;d say Allegro&#39;s at the fault here, however.</p><p>EDIT: Another thing you might be able to do in a pinch is call <span class="source-code"><a href="http://www.allegro.cc/manual/al_feof"><span class="a">al_feof</span></a></span> on the file. It&#39;ll return true if an attempt was made to write past its size.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Tue, 16 Jun 2015 11:18:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the tip about <span class="source-code"><a href="http://www.allegro.cc/manual/al_feof"><span class="a">al_feof</span></a></span>, I didn&#39;t think to do that.  I generally don&#39;t use feof myself because fread/fwrite returning a size less than what was requested generally means the same thing.  But for this case it&#39;s a useful workaround.</p><p>Regardless, <span class="source-code"><a href="http://www.allegro.cc/manual/al_save_config_file_f"><span class="a">al_save_config_file_f</span></a></span> should really be returning false in this situation. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Pascoe)</author>
		<pubDate>Tue, 16 Jun 2015 11:40:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, it&#39;s a good idea. So why not contribute a patch? <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Tue, 16 Jun 2015 20:56:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I would, but I&#39;m not sure where to submit it to.  The GitHub repo is considered a mirror, so I don&#39;t know if it would be acceptable to send a pull request there.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Pascoe)</author>
		<pubDate>Wed, 17 Jun 2015 00:26:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think its ok to do a github pull request, but you can just send a patch to the Development forum here, or the AD mailing list.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Wed, 17 Jun 2015 00:31:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I even prefer a pull request, as it&#39;ll run the TravisCI/AppVeyor checks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Wed, 17 Jun 2015 07:13:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ll post the patch here because Git wants to commit all the CMake cruft and I don&#39;t feel like dealing with it at the moment. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>See attached.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Pascoe)</author>
		<pubDate>Wed, 17 Jun 2015 21:22:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks, applied. It took me awhile to do so, as that file was encoded in UTF-16 <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Thu, 18 Jun 2015 09:41:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Huh, wonder how that happened.  All I did was:</p><pre>git diff src/config.c &gt; config.patch</pre><p>

Although that was done from PowerShell (the default shell for GitHub for Windows), maybe PS outputs UTF-16 when redirecting output?  Pretty dumb of it if that&#39;s the case.</p><p>Sorry about that.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bruce Pascoe)</author>
		<pubDate>Thu, 18 Jun 2015 10:28:39 +0000</pubDate>
	</item>
</rss>
