Page 1 of 1

Code organizing

Posted: Sat Mar 14, 2009 9:09 pm
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?

Re: Code organizing

Posted: Sat Mar 14, 2009 9:42 pm
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.

Re: Code organizing

Posted: Sat Mar 14, 2009 11:51 pm
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"

Re: Code organizing

Posted: Sun Mar 15, 2009 12:00 am
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.

Re: Code organizing

Posted: Sun Mar 15, 2009 8:41 am
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.

Re: Code organizing

Posted: Sun Mar 15, 2009 9:59 am
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.

Re: Code organizing

Posted: Sun Mar 15, 2009 11:32 am
by d4n1l0d
thank you all :D
I'll use a string array =]

Re: Code organizing

Posted: Sun Mar 15, 2009 12:48 pm
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.