Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

First C++ Project (Ghoul Script Configuration File Parser)


deathtaker26
 Share

Recommended Posts

Okay, before we begin I want to say that I have been trying very hard at c++ lately. I have been doing research and tutorials non-stop. So here's the result of my work.

About Ghoul Script Configuration Files Project:

>! Ghoul Script is going to be the name of the sub-scripting language that will be added to the engine I'm working on. It's like a C++ sad script if we can think back to that. I needed some practice and a type of file that could be read fairly fast but style had a nice syntax like xml. It is still in the works but this the reader for the Ghoul Script Configuration File or .GSCF for short.

The Syntax looks much like a .ini except with end tags as such [channel][/channel]
The code currently only makes 1 instance of each node but I am making it make arrays of each node so you can have multiple of the same nodes on each channel.

Config.GSCF:
```
//Ghoul Scripting Configuration Files are case sensitive.
[Innitial Settings]
Game_Name:Soulz Studios
IP:127.0.0.1
Port:1667
Menu_Music:Menu.midi
[/Innitial Settings]

[Update Settings]
Version:1.0
Update_URL:ftp://ftp.soulzinc.com/soulzstudios/updates/
[/Update Settings]

[News]
Header:News
Body:This is the news, it's pretty bland and boring at the moment. You should fix that.
[/News]

```
You can make comments as well, the parser simply ignores the comments and continues to the next line.

This library is still in the works I currently have a few features to add such as writing a file. and replacing Values. but I'll release that once I've finished. In the mean time here is the GSCF Parser code:

Parser.cpp
```
//Ghoul Script Configuration File Parser
#include #include #include //including files go after including libraries!
#include "Parser.h"

using namespace std;
string ReadGSCF(string filename, string channel, string node){

string value = "undefined";
string Channel;
string EndChannel;

//Clarify Channel
Channel = "[" + channel + "]";
EndChannel = "[/" + channel + "]";

//Open File Stream
ifstream qFile(filename);
string qLine;
string GSCF;

//While not end of file.
int i = 0;
while(!qFile.eof()){
bool read = false;
start:
getline(qFile, qLine);

    //checks to see if it has reached the channel

while (qLine == Channel){
read = true;
qLine.clear();
goto start;
}
//checks for EndChannel
while (qLine == EndChannel){
read = false;
qLine.clear();
goto start;
}

if(read == true){
    size_t pos;
pos = qLine.find(":");
string n = qLine.substr (0, pos);
if(n == node){
value = qLine.substr(pos + 1);
return value;
}
goto start;
}
}

qFile.close();

return value;

}

```
Where it's at it's pretty much a .ini reader that has : instead of = and uses [/channel] tags, in the near future it will be a lot more though :D

Example of Parser in use:
```
#include
#include
#include

//include files go last!
#include "Parser.h"

int main(){

std::cout << "Loading Innitials..." << std::endl;
std::cout << "Game Name: " << ReadGSCF("config.gscf","Innitial Settings","Game_Name") << std::endl;
std::cout << "Game Version: " << ReadGSCF("config.gscf","Update Settings","Version") < Output from usage:
[![](http://www.freemmorpgmaker.com/files/imagehost/pics/65094ba3e516de277762e8cd4a4a2728.PNG)](http://www.freemmorpgmaker.com/files/imagehost/#65094ba3e516de277762e8cd4a4a2728.PNG)

```
Link to comment
Share on other sites

>scripting language.
It's not even close to Turing complete.

>code snippets.
Learn to indent and actually use newlines. Stop commenting the obvious, C/C++ programmers already know what the majority of your code is doing, especially if you are applying the concept of _literate programming_: you aren't (learn it). Don't stack so many lines of code into a single function, write multiple functions that have their names tell what they are doing.  Stick to one way of expressing your variable names (I am seeing full names, full names with prefixes, abbreviations).

@Niall:

> Who told you to use GoTo statements? T_T

@Captain:

> This code is pretty bad, bro.
> I'd suggest using For instead of Goto.

Using the goto keyword is similar to writing a single branch instruction in Assembly. Why would you do something that the compiler already does for you: compile the for loop to _proper_ machine code?

@Niall:

> Also, your neglect for C functions makes me cry. Why u no use C-Standard library? :'[

He's using C++, so I can't really blame him.

Yours faithfully
  S.J.R. van Schaik.
Link to comment
Share on other sites

@S.J.R.:

> >scripting language.
> It's not even close to Turing complete.
>
> >code snippets.
> Learn to indent and actually use newlines. Stop commenting the obvious, C/C++ programmers already know what the majority of your code is doing, especially if you are applying the concept of _literate programming_: you aren't (learn it). Don't stack so many lines of code into a single function, write multiple functions that have their names tell what they are doing.  Stick to one way of expressing your variable names (I am seeing full names, full names with prefixes, abbreviations).
> Using the goto keyword is similar to writing a single branch instruction in Assembly. Why would you do something that the compiler already does for you: compile the for loop to _proper_ machine code?
> He's using C++, so I can't really blame him.
>
> Yours faithfully
>   S.J.R. van Schaik.

thank you for the tips, ill change my code around than. I used goto because everytime I would use a loop or an if statement than declare a variable the variable would not work outside of the loop or if statement unless I used a goto statement  to the beginning. What can I do to fix this?
Link to comment
Share on other sites

> I used goto because everytime I would use a loop or an if statement than declare a variable the variable would not work outside of the loop or if statement unless I used a goto statement  to the beginning. What can I do to fix this?

Why would you want to create global variables inside your script? Change global stuff with functions that accept parameters? I hope I understood your problem right…

-seal
Link to comment
Share on other sites

@Sealbreaker:

> Why would you want to create global variables inside your script? Change global stuff with functions that accept parameters? I hope I understood your problem right…
>
> -seal

I meant variables outside the if statement sorry, I got it now it was actually the comment at the begging of the file, I forgot to make the statement read false for these.

Here's the fixed code:
```
string ReadGSCF(string filename, string channel, string node){

string value = "undefined";
string Channel;
string EndChannel;

//Clarify Channel
Channel = "[" + channel + "]";
EndChannel = "[/" + channel + "]";

//Open File Stream
ifstream qFile(filename);
string qLine;
string GSCF;
bool read = false;
//While not end of file.
while(!qFile.eof()){

getline(qFile, qLine);

    //checks to see if it has reached the channel

if (qLine == Channel){
read = true;
}
//checks for EndChannel
if (qLine == EndChannel){
read = false;
}

if(read == true){
    size_t pos;
pos = qLine.find(":");
string n = qLine.substr (0, pos);
if(n == node){
value = qLine.substr(pos + 1);
return value;
}
}
}

qFile.close();

return value;

}

```
Link to comment
Share on other sites

@Captain:

> Have you ever thought of grouping variables? Instead of
> ```
> string Channel;
> string EndChannel;
>
> ```do
> ```
> string Channel, EndChannel;
>
> ```

actually I didn't know I could to this, thank you.

Also about the commenting I was told on many occasions I need to comment my work. It just shouldn't happen in C++?
Link to comment
Share on other sites

@Pete:

> Okay, don't do that.
> Start from the ground up.

You're no one to be telling someone else how they should or shouldn't do things.

If the system is a separate part that relies on no other aspect of the core application then it's fine to do this part first, and if not its fine to toy with it but this should be ultimately done after all the resources it relies on have been completed.

About the commenting, there are quite a few different styles for commenting, but namely you should only comment on things such as the header of a function by describing it's functionality and maybe at the top of a source file that explains what that file contains. Basically you should only comment things that are project specific and not universal knowledge for a programmer.

I would only suggest that mass commenting if you're learning, to remember things but that phase shouldn't even last long.

Also as it's been stated, you need to learn and possibly develop your own take of coding conventions, a constant way of coding things to keep persistent. Trust me it'll help all throughout your programming experience.
Link to comment
Share on other sites

@[rose:

> link=topic=78903.msg846385#msg846385 date=1332446792]
> You're no one to be telling someone else how they should or shouldn't do things.
>
> If the system is a separate part that relies on no other aspect of the core application then it's fine to do this part first, and if not its fine to toy with it but this should be ultimately done after all the resources it relies on have been completed.
>
> About the commenting, there are quite a few different styles for commenting, but namely you should only comment on things such as the header of a function by describing it's functionality and maybe at the top of a source file that explains what that file contains. Basically you should only comment things that are project specific and not universal knowledge for a programmer.
>
> I would only suggest that mass commenting if you're learning, to remember things but that phase shouldn't even last long.
>
> Also as it's been stated, you need to learn and possibly develop your own take of coding conventions, a constant way of coding things to keep persistent. Trust me it'll help all throughout your programming experience.

Alright thanks rose, all the comments were only for me to remember variables as I was programming the parser. The parser didn't rely on anything else so it seemed pretty simple to start on it especially because EVERYTHING will rely on the parser.

The parser will be used to get anything from simple strings such as the game name all the way to important variables such as IP addresses, options to tell music to start or stop, etc.
Link to comment
Share on other sites

@Pete:

> Let me rephrase then:
>
> If I were you, I'd start from the ground up.
>
> Happy?

If the ground isn't starting from nothing that relies on no other library than i don't know what "the ground" is.

Also I just finished the write function:
```
void WriteGSCF(std::string filename, std::string channel, std::string node, std::string value){

ifstream qFile(filename);
string qLine;
string GSCF;
string file;

while(!qFile.eof()){
getline(qFile, qLine);

size_t pos;
pos = qLine.find(":");
string n = qLine.substr (0, pos);
if(n == node){
string cur_value = qLine.substr (pos + 1);
qLine = node + ":" + value ;
}

file = file + qLine + "\n";
}

ofstream qFileW(filename);
qFileW << file << endl;
qFileW.close();
}

```
However I must fix it to insert new nodes
Link to comment
Share on other sites

@Captain:

> It looks prettier :3

[Spaghetti code](http://en.wikipedia.org/wiki/Spaghetti_code).

@Captain:

> Have you ever thought of grouping variables? Instead of
> ```
> string Channel;
> string EndChannel;
>
> ```do
> ```
> string Channel, EndChannel;
>
> ```

That depends on the code convention you are applying. Mine doesn't allow it, never will allow it since it decreases readability.

@Crest:

> thank you for the tips, ill change my code around than. I used goto because everytime I would use a loop or an if statement than declare a variable the variable would not work outside of the loop or if statement unless I used a goto statement  to the beginning. What can I do to fix this?

Variables should always be declared at the start of a scope (a scope is defined by curly braces).

@Crest:

> Here's the fixed code:

The indentation and styling are still poor.

@Crest:

> Also about the commenting I was told on many occasions I need to comment my work. It just shouldn't happen in C++?

Comments are used:

* to tell that you still have to implement something.
* to tell that you still have to fix something.
* to elaborate code (e.g. resolving odd bugs, using uncommon formulae, etc.).

Don't use comments to state the obvious. Your code should in almost every case tell what it does. If it doesn't, you have programmed it poorly and should redo it or split it up and use function names that elaborate your code. (cfr. literate programming).

@Crest:

> I wasn't writing the scripting engine yet I was writing a file parser to read files that will be used later on in the engine.

Don't write a scripting engine, unless you actually know sufficient compiler theory.

Yours faithfully
  S.J.R. van Schaik.
Link to comment
Share on other sites

@Captain:

> What's wrong with using properly structured for statements?

I wasn't sure whether you were saying the goto-keyword was actually pretty or not. Apparently not, so you can safely ignore whatever I said.

@Crest:

> I have no idea what people are trying to help me with here anymore
> T_T

You've posted three snippets thus far, none really applying what I said about indentation and styling.

You still don't seem to comprehend neither literate programming neither the use of comments, even though I've described both.

Yours faithfully
  S.J.R. van Schaik.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...