Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
Choose a Base Repository
cuberite/cuberite
36451/MCServer
4264/cuberite
Akheon23/cuberite
Alyxey/cuberite
Ba4life/cuberite
BrainShit/MCServer
C-Elegans/cuberite
ChriPiv/MCServer
Dam63/MCServer
DevToaster/MCServer
DjKiDD/MCServer
Erbelding/cuberite
ErrAza/MCServer
FX-Master/MCServer
Floppy012/MCServer
FullStackUnicorns/MCServer
GameClub2000/cuberite
HackerTon/MCServer
HaoTNN/cuberite
Haxi52/cuberite
HelenaKitty/EbooMC
Hillvith/MCServer
Hmaal/MCServer
Howaner/MCServer
Infinity-Codeur/MCServer
JABirchall/MCServer
JJOL/cuberite
JoeClacks/MCServer
JoseDiaz27/MCServer
Jothle12/MCServer
Kungfumoo/MCServer
Limb/MCServer
LinEvil/cuberite
MaxM357/MCServer
Motoburger1/MCServer
MuhammadWang/MCServer
Nerkyator/MCServer
Noraaron1/MCServer
Pheo/MCServer
PixeLInc/cuberite
PyroCybin/cuberite
Raekye/MCServer
RedEnraged96/MCServer-1
Redd-Dasan/cuberite
Renoh47/MCServer
Roro534/cuberite
SOSDAN/cuberite
SamMauldin/MCServer
Samvbnm/MCServer
Schwertspize/cuberite
Seadragon91/cuberite
Sentir101/MCServer
Solexid/MCServer
StefanNemeth/cuberite
Tank-D/MCServer
ThisIsAgent1k/MCServer
ThuGie/MCServer
TigerHix/MCServer
TonyMo/MCServer
Toyz/MCServer
Tri125/MCServer
UltraCoderRU/cuberite
WillEccles/cuberite
Xury/MCServer
Zeenjayli/MCServer
Zignixx/cuberite
acrade/cuberite
adesnos/cuberite
ameuret/cuberite
arekinath/MCServer
arnaudlahalle/cuberite
artemist/cuberite
axisd/MCServer
balika011/MCServer
bendl/cuberite
benhc123/MCServer
bibo38/Cuberite
birkett/cuberite
bowlofstew/MCServer
cacaxi/MCServer
carriercomm/cuberite
cedeel/MCServer
cheryl0047/MCServer
christopher-montagna/MCServer
codebucketdev/MCServer
coding12/cuberite
commshare/MCServer
crexalbo/MCServer
crosbymichael/MCServer
daemon777/MCServer
daniel0916/MCServer
darkzmii/cuberite
dave-tucker/cuberite
daveh86/MCServer
debug-stick/cuberal
deckerrj/cuberite
decterous/MCServer
default0/MCServer
dmonger/MCServer
dougvj/MCServer
electromatter/cuberite
ezhangle/cuberite
f0rb1d/MCServer
gdevillele/cuberite
geekpower14/MCServer
giriko/MCServer
greatman/MCServer
guijun/MCServer
gushromp/MCServer
hallucino/cuberite
hammermaps/MCServer
harryhare/MCServer
hide92795/cuberite
hmartinh/cuberite
ionux/MCServer
irrelevantdotcom/MCServer
jacky526/cuberite
jalons/MCServer
jamesliu96/MCServer
jamesmunns/MCServer
jamestait/cuberite
jamiepg1/MCServer
jammet/MCServer
jaumealoy/MCServer
jayr0flmc/cuberite
jimfinnis/MCServer
jimiy/cuberite
joeflezer/MCServer
john092/MCServer
josephcopenhaver/cuberite
joshi07/MCServer2
jpdamon/cuberite
kharashubham/cuberite
kjanku1/MCServer
lighth7015/cuberite
lima900/MCServer
linnemannr/MCServer
lkolbly/MCServer
lvshiling/MCServer
madtomic/MCServer
maki-chan/MCServer
maniak89/MCServer
marmot21/MCServer
martindev2/cuberite
marvinkopf/cuberite
meiavy/cuberite
mgueydan/MCServer
mhmd05/cuberite
mjhanninen/cuberite
mjssw/cuberite
mmdk95/cuberite
mrhabib/MCServer
narroo/MCServer
nathankot/MCServer
necrophcodr/cuberite
nesco/MCServer
nevercast/cuberite
nicodinh/cuberite
nounoursheureux/MCServer
p-mcgowan/MCServer
paizzj/cuberite
parlock/cuberite
patthekenny/MCServer
pillsbury7901/MCServer
planetx/MCServer
prabhjeet27/cuberite
psrivast7788/MCServer
ravenscroftj/MCServer
rcrimp/MCServer
rhamilton1415/cuberite
ricucremop/cuberite
ryansnewell/MCServer
scribblemaniac/cuberite
scriptsboy/cuberite
seijikun/MCServer
shaunstanislaus/MCServer
sircodes/MCServer
skarlitz/MCServer
soemthlng/cuberite
stpinker/MCServer
straemer/MCServer
thatchristoph/MCServer
then0rTh/cuberite
tomthoros/MCServer
tonetheman/MCServer
tt7truong/MCServer
ubiGG/cuberite
vyeluri5/MCServer
w00tc0d3/MCServer
waluoo/MCServer
wang108/MCServer
wnwkimo/cuberite
worktycho/MCServer
xanahopper/MCServer
yamamushi/MCServer
yinyunqiao/MCServer
yo4you/cuberite
zackp30/cuberite
zeliard/MCServer
Nothing to show
Choose a Head Repository
cuberite/cuberite
36451/MCServer
4264/cuberite
Akheon23/cuberite
Alyxey/cuberite
Ba4life/cuberite
BrainShit/MCServer
C-Elegans/cuberite
ChriPiv/MCServer
Dam63/MCServer
DevToaster/MCServer
DjKiDD/MCServer
Erbelding/cuberite
ErrAza/MCServer
FX-Master/MCServer
Floppy012/MCServer
FullStackUnicorns/MCServer
GameClub2000/cuberite
HackerTon/MCServer
HaoTNN/cuberite
Haxi52/cuberite
HelenaKitty/EbooMC
Hillvith/MCServer
Hmaal/MCServer
Howaner/MCServer
Infinity-Codeur/MCServer
JABirchall/MCServer
JJOL/cuberite
JoeClacks/MCServer
JoseDiaz27/MCServer
Jothle12/MCServer
Kungfumoo/MCServer
Limb/MCServer
LinEvil/cuberite
MaxM357/MCServer
Motoburger1/MCServer
MuhammadWang/MCServer
Nerkyator/MCServer
Noraaron1/MCServer
Pheo/MCServer
PixeLInc/cuberite
PyroCybin/cuberite
Raekye/MCServer
RedEnraged96/MCServer-1
Redd-Dasan/cuberite
Renoh47/MCServer
Roro534/cuberite
SOSDAN/cuberite
SamMauldin/MCServer
Samvbnm/MCServer
Schwertspize/cuberite
Seadragon91/cuberite
Sentir101/MCServer
Solexid/MCServer
StefanNemeth/cuberite
Tank-D/MCServer
ThisIsAgent1k/MCServer
ThuGie/MCServer
TigerHix/MCServer
TonyMo/MCServer
Toyz/MCServer
Tri125/MCServer
UltraCoderRU/cuberite
WillEccles/cuberite
Xury/MCServer
Zeenjayli/MCServer
Zignixx/cuberite
acrade/cuberite
adesnos/cuberite
ameuret/cuberite
arekinath/MCServer
arnaudlahalle/cuberite
artemist/cuberite
axisd/MCServer
balika011/MCServer
bendl/cuberite
benhc123/MCServer
bibo38/Cuberite
birkett/cuberite
bowlofstew/MCServer
cacaxi/MCServer
carriercomm/cuberite
cedeel/MCServer
cheryl0047/MCServer
christopher-montagna/MCServer
codebucketdev/MCServer
coding12/cuberite
commshare/MCServer
crexalbo/MCServer
crosbymichael/MCServer
daemon777/MCServer
daniel0916/MCServer
darkzmii/cuberite
dave-tucker/cuberite
daveh86/MCServer
debug-stick/cuberal
deckerrj/cuberite
decterous/MCServer
default0/MCServer
dmonger/MCServer
dougvj/MCServer
electromatter/cuberite
ezhangle/cuberite
f0rb1d/MCServer
gdevillele/cuberite
geekpower14/MCServer
giriko/MCServer
greatman/MCServer
guijun/MCServer
gushromp/MCServer
hallucino/cuberite
hammermaps/MCServer
harryhare/MCServer
hide92795/cuberite
hmartinh/cuberite
ionux/MCServer
irrelevantdotcom/MCServer
jacky526/cuberite
jalons/MCServer
jamesliu96/MCServer
jamesmunns/MCServer
jamestait/cuberite
jamiepg1/MCServer
jammet/MCServer
jaumealoy/MCServer
jayr0flmc/cuberite
jimfinnis/MCServer
jimiy/cuberite
joeflezer/MCServer
john092/MCServer
josephcopenhaver/cuberite
joshi07/MCServer2
jpdamon/cuberite
kharashubham/cuberite
kjanku1/MCServer
lighth7015/cuberite
lima900/MCServer
linnemannr/MCServer
lkolbly/MCServer
lvshiling/MCServer
madtomic/MCServer
maki-chan/MCServer
maniak89/MCServer
marmot21/MCServer
martindev2/cuberite
marvinkopf/cuberite
meiavy/cuberite
mgueydan/MCServer
mhmd05/cuberite
mjhanninen/cuberite
mjssw/cuberite
mmdk95/cuberite
mrhabib/MCServer
narroo/MCServer
nathankot/MCServer
necrophcodr/cuberite
nesco/MCServer
nevercast/cuberite
nicodinh/cuberite
nounoursheureux/MCServer
p-mcgowan/MCServer
paizzj/cuberite
parlock/cuberite
patthekenny/MCServer
pillsbury7901/MCServer
planetx/MCServer
prabhjeet27/cuberite
psrivast7788/MCServer
ravenscroftj/MCServer
rcrimp/MCServer
rhamilton1415/cuberite
ricucremop/cuberite
ryansnewell/MCServer
scribblemaniac/cuberite
scriptsboy/cuberite
seijikun/MCServer
shaunstanislaus/MCServer
sircodes/MCServer
skarlitz/MCServer
soemthlng/cuberite
stpinker/MCServer
straemer/MCServer
thatchristoph/MCServer
then0rTh/cuberite
tomthoros/MCServer
tonetheman/MCServer
tt7truong/MCServer
ubiGG/cuberite
vyeluri5/MCServer
w00tc0d3/MCServer
waluoo/MCServer
wang108/MCServer
wnwkimo/cuberite
worktycho/MCServer
xanahopper/MCServer
yamamushi/MCServer
yinyunqiao/MCServer
yo4you/cuberite
zackp30/cuberite
zeliard/MCServer
Nothing to show
  • 10 commits
  • 2 files changed
  • 1 commit comment
  • 3 contributors
Commits on Jun 21, 2014
Commits on Jun 22, 2014
Commits on Jun 23, 2014
Commits on Jun 26, 2014
Showing with 196 additions and 94 deletions.
  1. +174 −94 src/FurnaceRecipe.cpp
  2. +22 −0 src/FurnaceRecipe.h
View
@@ -5,7 +5,8 @@
#include "Item.h"
#include <fstream>
#include <sstream>
#define FURNACE_RECIPE_FILE "furnace.txt"
@@ -54,128 +55,207 @@ void cFurnaceRecipe::ReloadRecipes(void)
ClearRecipes();
LOGD("Loading furnace recipes...");
std::ifstream f;
char a_File[] = "furnace.txt";
f.open(a_File, std::ios::in);
std::ifstream f(FURNACE_RECIPE_FILE, std::ios::in);
if (!f.good())
{
f.close();
LOG("Could not open the furnace recipes file \"%s\"", a_File);
LOG("Could not open the furnace recipes file \"%s\"", FURNACE_RECIPE_FILE);
return;
}
unsigned int Line = 0;
AString ParsingLine;
// TODO: Replace this messy parse with a high-level-structured one (ReadLine / ProcessLine)
bool bSyntaxError = false;
while (f.good())
while (std::getline(f, ParsingLine))
{
char c;
Line++;
TrimString(ParsingLine);
if (ParsingLine.empty())
{
continue;
}
//////////////////////////////////////////////////////////////////////////
// comments
f >> c;
f.unget();
if( c == '#' )
switch (ParsingLine[0])
{
while( f.good() && c != '\n' )
case '#':
{
f.get( c );
// Comment
break;
}
case '!':
{
// Fuel
int IItemID = 0, IItemCount = 0, IItemHealth = 0, IBurnTime = 0;
AString::size_type BeginPos = 1; // Begin at one after exclamation mark (bang)
if (
!ReadMandatoryNumber(BeginPos, ":", ParsingLine, Line, IItemID) || // Read item ID
!ReadOptionalNumbers(BeginPos, ":", "=", ParsingLine, Line, IItemCount, IItemHealth) || // Read item count (and optionally health)
!ReadMandatoryNumber(BeginPos, "0123456789", ParsingLine, Line, IBurnTime, true) // Read item burn time - last value
)
{
break;
}
// Add to fuel list:
Fuel F;
F.In = new cItem((ENUM_ITEM_ID)IItemID, (char)IItemCount, (short)IItemHealth);
F.BurnTime = IBurnTime;
m_pState->Fuel.push_back(F);
break;
}
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
// Recipe
int IItemID = 0, IItemCount = 0, IItemHealth = 0, IBurnTime = 0;
int OItemID = 0, OItemCount = 0, OItemHealth = 0;
AString::size_type BeginPos = 0; // Begin at start of line
if (
!ReadMandatoryNumber(BeginPos, ":", ParsingLine, Line, IItemID) || // Read item ID
!ReadOptionalNumbers(BeginPos, ":", "@", ParsingLine, Line, IItemCount, IItemHealth) || // Read item count (and optionally health)
!ReadMandatoryNumber(BeginPos, "=", ParsingLine, Line, IBurnTime) || // Read item burn time
!ReadMandatoryNumber(BeginPos, ":", ParsingLine, Line, OItemID) || // Read result ID
!ReadOptionalNumbers(BeginPos, ":", "012456789", ParsingLine, Line, OItemCount, OItemHealth, true) // Read result count (and optionally health) - last value
)
{
break;
}
// Add to recipe list
Recipe R;
R.In = new cItem((ENUM_ITEM_ID)IItemID, (char)IItemCount, (short)IItemHealth);
R.Out = new cItem((ENUM_ITEM_ID)OItemID, (char)OItemCount, (short)OItemHealth);
R.CookTime = IBurnTime;
m_pState->Recipes.push_back(R);
break;
}
continue;
}
default:
{
LOGWARNING("Error in furnace recipes at line %d: Unexpected text: \"%s\". Ignoring line.",
Line, ParsingLine.c_str()
);
break;
}
} // switch (ParsingLine[0])
} // while (getline(ParsingLine))
//////////////////////////////////////////////////////////////////////////
// Line breaks
f.get( c );
while( f.good() && ( c == '\n' || c == '\r' ) ) { f.get( c ); }
if (f.eof())
LOG("Loaded " SIZE_T_FMT " furnace recipes and " SIZE_T_FMT " fuels", m_pState->Recipes.size(), m_pState->Fuel.size());
}
void cFurnaceRecipe::PrintParseError(unsigned int a_Line, size_t a_Position, const AString & a_CharactersMissing)
{
LOGWARN("Error parsing furnace recipes at line %i pos " SIZE_T_FMT ": missing '%s'", a_Line, a_Position, a_CharactersMissing.c_str());
}
bool cFurnaceRecipe::ReadMandatoryNumber(AString::size_type & a_Begin, const AString & a_Delimiter, const AString & a_Text, unsigned int a_Line, int & a_Value, bool a_IsLastValue)
{
// TODO: replace atoi with std::stoi
AString::size_type End;
if (a_IsLastValue)
{
End = a_Text.find_first_not_of(a_Delimiter, a_Begin);
}
else
{
End = a_Text.find_first_of(a_Delimiter, a_Begin);
if (End == AString::npos)
{
break;
PrintParseError(a_Line, a_Begin, a_Delimiter);
return false;
}
f.unget();
}
// stoi won't throw an exception if the string is alphanumeric, we should check for this
if (!DoesStringContainOnlyNumbers(a_Text.substr(a_Begin, End - a_Begin)))
{
PrintParseError(a_Line, a_Begin, "number");
return false;
}
a_Value = atoi(a_Text.substr(a_Begin, End - a_Begin).c_str());
a_Begin = End + 1; // Jump over delimiter
return true;
}
//////////////////////////////////////////////////////////////////////////
// Check for fuel
f >> c;
if( c == '!' ) // It's fuel :)
bool cFurnaceRecipe::ReadOptionalNumbers(AString::size_type & a_Begin, const AString & a_DelimiterOne, const AString & a_DelimiterTwo, const AString & a_Text, unsigned int a_Line, int & a_ValueOne, int & a_ValueTwo, bool a_IsLastValue)
{
// TODO: replace atoi with std::stoi
AString::size_type End, Begin = a_Begin;
End = a_Text.find_first_of(a_DelimiterOne, Begin);
if (End != AString::npos)
{
if (DoesStringContainOnlyNumbers(a_Text.substr(Begin, End - Begin)))
{
// Read item
int IItemID = 0, IItemCount = 0, IItemHealth = 0;
f >> IItemID;
f >> c; if( c != ':' ) { bSyntaxError = true; break; }
f >> IItemCount;
// Optional health
f >> c;
if( c != ':' )
f.unget();
a_ValueOne = std::atoi(a_Text.substr(Begin, End - Begin).c_str());
Begin = End + 1;
if (a_IsLastValue)
{
End = a_Text.find_first_not_of(a_DelimiterTwo, Begin);
}
else
{
f >> IItemHealth;
End = a_Text.find_first_of(a_DelimiterTwo, Begin);
if (End == AString::npos)
{
PrintParseError(a_Line, Begin, a_DelimiterTwo);
return false;
}
}
// Burn time
int BurnTime;
f >> c; if( c != '=' ) { bSyntaxError = true; break; }
f >> BurnTime;
// stoi won't throw an exception if the string is alphanumeric, we should check for this
if (!DoesStringContainOnlyNumbers(a_Text.substr(Begin, End - Begin)))
{
PrintParseError(a_Line, Begin, "number");
return false;
}
a_ValueTwo = atoi(a_Text.substr(Begin, End - Begin).c_str());
// Add to fuel list
Fuel F;
F.In = new cItem( (ENUM_ITEM_ID) IItemID, (char)IItemCount, (short)IItemHealth );
F.BurnTime = BurnTime;
m_pState->Fuel.push_back( F );
continue;
a_Begin = End + 1; // Jump over delimiter
return true;
}
f.unget();
//////////////////////////////////////////////////////////////////////////
// Read items
int IItemID = 0, IItemCount = 0, IItemHealth = 0;
f >> IItemID;
f >> c; if( c != ':' ) { bSyntaxError = true; break; }
f >> IItemCount;
// Optional health
f >> c;
if( c != ':' )
f.unget();
else
{
f >> IItemHealth;
return ReadMandatoryNumber(a_Begin, a_DelimiterTwo, a_Text, a_Line, a_ValueOne, a_IsLastValue);
}
}
return ReadMandatoryNumber(a_Begin, a_DelimiterTwo, a_Text, a_Line, a_ValueOne, a_IsLastValue);
}
int CookTime;
f >> c; if( c != '@' ) { bSyntaxError = true; break; }
f >> CookTime;
int OItemID = 0, OItemCount = 0, OItemHealth = 0;
f >> c; if( c != '=' ) { bSyntaxError = true; break; }
f >> OItemID;
f >> c; if( c != ':' ) { bSyntaxError = true; break; }
f >> OItemCount;
// Optional health
f >> c;
if( c != ':' )
f.unget();
else
{
f >> OItemHealth;
}
// Add to recipe list
Recipe R;
R.In = new cItem( (ENUM_ITEM_ID)IItemID, (char)IItemCount, (short)IItemHealth );
R.Out = new cItem( (ENUM_ITEM_ID)OItemID, (char)OItemCount, (short)OItemHealth );
R.CookTime = CookTime;
m_pState->Recipes.push_back( R );
}
if (bSyntaxError)
{
LOGERROR("ERROR: FurnaceRecipe, syntax error" );
}
LOG("Loaded " SIZE_T_FMT " furnace recipes and " SIZE_T_FMT " fuels", m_pState->Recipes.size(), m_pState->Fuel.size());
bool cFurnaceRecipe::DoesStringContainOnlyNumbers(const AString & a_String)
{
// TODO: replace this with std::all_of(a_String.begin(), a_String.end(), isdigit)
return (a_String.find_first_not_of("0123456789") == AString::npos);
}
View
@@ -41,6 +41,28 @@ class cFurnaceRecipe
private:
void ClearRecipes(void);
/** Calls LOGWARN with the line, position, and error */
static void PrintParseError(unsigned int a_Line, size_t a_Position, const AString & a_CharactersMissing);
/** Reads a number from a string given, starting at a given position and ending at a delimiter given
Updates beginning position to the delimiter found + 1, and updates the value to the one read
If it encounters a substring that is not fully numeric, it will call SetParseError() and return false; the caller should abort processing
Otherwise, the function will return true
*/
static bool ReadMandatoryNumber(AString::size_type & a_Begin, const AString & a_Delimiter, const AString & a_Text, unsigned int a_Line, int & a_Value, bool a_IsLastValue = false);
/** Reads two numbers from a string given, starting at a given position and ending at the first delimiter given, then again (with an updated position) until the second delimiter given
Updates beginning position to the second delimiter found + 1, and updates the values to the ones read
If it encounters a substring that is not fully numeric whilst reading the second value, it will call SetParseError() and return false; the caller should abort processing
If this happens whilst reading the first value, it will call ReadMandatoryNumber() with the appropriate position, as this may legitimately occur with the optional value and AString::find_first_of finding the incorrect delimiter. It will return the result of ReadMandatoryNumber()
True will be returned definitively for an optional value that is valid
*/
static bool ReadOptionalNumbers(AString::size_type & a_Begin, const AString & a_DelimiterOne, const AString & a_DelimiterTwo, const AString & a_Text, unsigned int a_Line, int & a_ValueOne, int & a_ValueTwo, bool a_IsLastValue = false);
/** Uses std::all_of to determine if a string contains only digits */
static bool DoesStringContainOnlyNumbers(const AString & a_String);
struct sFurnaceRecipeState;
sFurnaceRecipeState * m_pState;
};

Showing you all comments on commits in this comparison.

@madmaxoft

This comment has been minimized.

Show comment
Hide comment
@madmaxoft

madmaxoft Jun 22, 2014

Member

Parenthesis around the comparison

Member

madmaxoft commented on c476fc3 Jun 22, 2014

Parenthesis around the comparison