Code organizing

Programming, for all ages and all languages.
Post Reply
User avatar
d4n1l0d
Member
Member
Posts: 42
Joined: Sat Dec 02, 2006 4:12 pm

Code organizing

Post by d4n1l0d »

I'm starting a simple project using C language.
I have a lot of code files and their respective headers.
And I'm organizing the code this way in the project root folder: All the code files and the "makefile" are at root level, all header files are into a folder called "inc" and I have a special folder called "strings". In this special folder I have header files that contain all the strings used by the code files.
Example:
Code file: error.c

Code: Select all

#include <stdio.h>
#include "inc/error.h"
#include "strings/error_str.h"

void geterror( int errorid)
{
  printf(ERRORSTR1,errorid);
}
String Header file: error_str.h

Code: Select all

#ifndef _ERROR_STR_
#define _ERROR_STR_

#define ERRORSTR1 "Error number %d"

#endif
Is this a good way of organizing my code?

Edit: The stuff that I'm doing with the strings... I guess that "special" headers for strings are useless, but a friend told me it's better to do this way, the question is: Is this really necessary?
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Code organizing

Post by Troy Martin »

No. A thousand times no.

It's easier to not have the separate files and just use the strings literally in the function parameters or in #defines at the top of the .c file, if it's something that's going to be used over and over and over again.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
Steve the Pirate
Member
Member
Posts: 152
Joined: Fri Dec 15, 2006 7:01 am
Location: Brisbane, Australia
Contact:

Re: Code organizing

Post by Steve the Pirate »

With your includes, you might want to just pass gcc the '-I inc' option so you can just #include "somefile.h"
My Site | My Blog
Symmetry - My operating system.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Code organizing

Post by Firestryke31 »

I'm trying to get into the habit of putting all of my strings into an array to make localization easier. That way, it's as simple as

Code: Select all

const std::string english[] =
{
  "An English string!\n",
  "Another English string!\n",
};

enum LocalizationIndex
{
  str1,
  str2,
};

const std::string *local;

int main()
{
  // Choose language dynamically here!
  // for demo purposes just direct assign
  local = english;
  cout << local[str1] << local[str2];
  return 0;
}
Of course, instead of directly compiling all of the localizations in it would be better to dynamically load them from a file, but that works for simple projects. Also, I put the array into a separate C++ file.

Edit: the concept also should work for C, just replace std::string with char *, and cout with the equivalent printf()s.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Code organizing

Post by Firestryke31 »

Because the strings are all in one file instead of spread throughout the code, so if I use a string in more than one place and I need to update it (the program version number, for instance, or if I want to change all of the error dialog box titles from "Oops!" to "There was an error:") then I don't have to find every instance of it in every file. It also means I can send that one file off to be localized, instead of the entire program. If it's done right I can also load a localization from an external file.

From a quick Google it also looks to be GPLed, but I just spent about a minute looking around. I should look into it more before I continue arguing, and the above argument might be entirely pointless.

Edit: I found the actual site, and it's not as bad as I thought it was. It does, however, look like they make some assumptions about your environment, but once again, it was a quick glance. The above argument (other than changing strings in multiple places at once) seems to be more or less invalid based on what I saw, though.

I think for now I'll stick with my method and I don't feel the need to convince anyone to it, so use what you want.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Code organizing

Post by neon »

Actually, your method has been used without problems plenty of times in large software (from what I have seen). It is not just used for localization but also for the reasons posted by Firestryke31. I myself am doing it using string table resources (a little different, but works the same.)

However, that alone is not enough. You should have a way for routines to be able to handle both ASCII and Unicode variants. Thus you can change your strings to Unicode, and be able to build your project using the Unicode string functions, without much effort. Then again, as berkus kind of pointed out, there are other methods.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
d4n1l0d
Member
Member
Posts: 42
Joined: Sat Dec 02, 2006 4:12 pm

Re: Code organizing

Post by d4n1l0d »

thank you all :D
I'll use a string array =]
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Code organizing

Post by Firestryke31 »

In the only implementation I have so far I actually use std:wstring. I just didn't type it in the example above. From my work on iPod modding it actually uses something similar to the string table, but for images and other resources too.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
Post Reply