Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F118191
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
93 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/AndroidAppSettings.cfg b/AndroidAppSettings.cfg
new file mode 100644
index 0000000..804bb29
--- /dev/null
+++ b/AndroidAppSettings.cfg
@@ -0,0 +1,62 @@
+# The application settings for Android libSDL port
+AppSettingVersion=17
+LibSdlVersion=1.2
+AppName="Me and My Shadow"
+AppFullName=net.sourceforge.meandmyshadow
+ScreenOrientation=h
+InhibitSuspend=y
+AppDataDownloadUrl="Data|data.zip"
+VideoDepthBpp=32
+NeedDepthBuffer=n
+NeedStencilBuffer=n
+NeedGles2=n
+SwVideoMode=y
+SdlVideoResize=y
+SdlVideoResizeKeepAspect=n
+CompatibilityHacks=n
+CompatibilityHacksStaticInit=n
+CompatibilityHacksTextInputEmulatesHwKeyboard=n
+CompatibilityHacksPreventAudioChopping=n
+CompatibilityHacksAppIgnoresAudioBufferSize=n
+CompatibilityHacksAdditionalPreloadedSharedLibraries=""
+CompatibilityHacksSlowCompatibleEventQueue=n
+CompatibilityHacksTouchscreenKeyboardSaveRestoreOpenGLState=y
+AppUsesMouse=y
+AppNeedsTwoButtonMouse=y
+ShowMouseCursor=y
+ForceRelativeMouseMode=n
+AppNeedsArrowKeys=y
+AppNeedsTextInput=y
+AppUsesJoystick=n
+AppUsesAccelerometer=n
+AppUsesGyroscope=n
+AppUsesMultitouch=n
+AppRecordsAudio=n
+NonBlockingSwapBuffers=n
+RedefinedKeys="SPACE RETURN NO_REMAP NO_REMAP SPACE ESCAPE"
+AppTouchscreenKeyboardKeysAmount=2
+AppTouchscreenKeyboardKeysAmountAutoFire=0
+RedefinedKeysScreenKb="0 1 2 3 4 5 6 7 8 9"
+RedefinedKeysScreenKbNames="0 1 2 3 4 5 6 7 8 9"
+StartupMenuButtonTimeout=3000
+HiddenMenuOptions='OptionalDownloadConfig'
+FirstStartMenuOptions=''
+MultiABI=n
+AppMinimumRAM=64
+AppVersionCode=490
+AppVersionName="0.49"
+ResetSdlConfigForThisVersion=n
+DeleteFilesOnUpgrade="%"
+CompiledLibraries="sdl_image sdl_ttf sdl_mixer sdl_gfx curl archive lua_52"
+CustomBuildScript=n
+AppCflags='-O2 -finline-functions -frtti'
+AppLdflags=''
+AppOverlapsSystemHeaders=
+AppSubdirsBuild=''
+AppBuildExclude=''
+AppCmdline=''
+ReadmeText='^Readme text'
+MinimumScreenSize=s
+AdmobPublisherId=n
+AdmobTestDeviceId=
+AdmobBannerSize=
diff --git a/icon.png b/icon.png
new file mode 100644
index 0000000..ac58cf0
Binary files /dev/null and b/icon.png differ
diff --git a/src/Functions.cpp b/src/Functions.cpp
index 295938b..f4cf593 100644
--- a/src/Functions.cpp
+++ b/src/Functions.cpp
@@ -1,1774 +1,1773 @@
/*
* Copyright (C) 2011-2012 Me and My Shadow
*
* This file is part of Me and My Shadow.
*
* Me and My Shadow is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Me and My Shadow is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Me and My Shadow. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <SDL/SDL.h>
#ifdef __APPLE__
#include <SDL_mixer/SDL_mixer.h>
#include <SDL_gfx/SDL_gfxPrimitives.h>
#include <SDL_gfx/SDL_rotozoom.h>
#else
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_gfxPrimitives.h>
#include <SDL/SDL_rotozoom.h>
#endif
#include <SDL/SDL_syswm.h>
#include <string>
#include "Globals.h"
#include "Functions.h"
#include "FileManager.h"
#include "Objects.h"
#include "Player.h"
#include "GameObjects.h"
#include "LevelPack.h"
#include "TitleMenu.h"
#include "LevelEditSelect.h"
#include "LevelEditor.h"
#include "Game.h"
#include "LevelPlaySelect.h"
#include "Addons.h"
#include "InputManager.h"
#include "ImageManager.h"
#include "MusicManager.h"
#include "LevelPackManager.h"
#include "ThemeManager.h"
#include "GUIListBox.h"
#include "GUIOverlay.h"
#include "StatisticsManager.h"
#include "StatisticsScreen.h"
#include "Cursors.h"
#include "ScriptAPI.h"
#include "libs/tinyformat/tinyformat.h"
#include "libs/tinygettext/tinygettext.hpp"
#include "libs/tinygettext/log.hpp"
-extern "C" {
#include "libs/findlocale/findlocale.h"
-}
#ifdef HARDWARE_ACCELERATION
#include <GL/gl.h>
#include <GL/glu.h>
//fix some Windows header bug
#ifndef GL_BGR
#define GL_BGR GL_BGR_EXT
#endif
#ifndef GL_BGRA
#define GL_BGRA GL_BGRA_EXT
#endif
#endif
using namespace std;
#ifdef WIN32
#include <windows.h>
#include <shlobj.h>
#else
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#endif
//Workaround for the resizing below 800x600 for X systems.
#if defined(__linux__) && !defined(ANDROID)
#include<X11/Xlib.h>
#include<X11/Xutil.h>
#define __X11_INCLUDED__
#endif
//Initialise the imagemanager.
//The ImageManager is used to prevent loading images multiple times.
ImageManager imageManager;
//Initialise the musicManager.
//The MusicManager is used to prevent loading music files multiple times and for playing/fading music.
MusicManager musicManager;
//Initialise the levelPackManager.
//The LevelPackManager is used to prevent loading levelpacks multiple times and for the game to know which levelpacks there are.
LevelPackManager levelPackManager;
//The scriptExecutor used for executing scripts.
ScriptExecutor scriptExecutor;
//Map containing changed settings using command line arguments.
map<string,string> tmpSettings;
//Pointer to the settings object.
//It is used to load and save the settings file and change the settings.
Settings* settings=0;
#ifdef HARDWARE_ACCELERATION
GLuint screenTexture;
#endif
SDL_Surface* loadImage(string file){
//We use the imageManager to load the file.
return imageManager.loadImage(file);
}
void applySurface(int x,int y,SDL_Surface* source,SDL_Surface* dest,SDL_Rect* clip){
//The offset is needed to draw at the right location.
SDL_Rect offset;
offset.x=x;
offset.y=y;
//Let SDL do the drawing of the surface.
SDL_BlitSurface(source,clip,dest,&offset);
}
void drawRect(int x,int y,int w,int h,SDL_Surface* dest,Uint32 color){
//NOTE: We let SDL_gfx render it.
rectangleRGBA(dest,x,y,x+w,y+h,color >> 24,color >> 16,color >> 8,255);
}
//Draw a box with anti-aliased borders using SDL_gfx.
void drawGUIBox(int x,int y,int w,int h,SDL_Surface* dest,Uint32 color){
//Fill content's background color from function parameter
boxRGBA(dest,x+1,y+1,x+w-2,y+h-2,color >> 24,color >> 16,color >> 8,color >> 0);
//Draw first black borders around content and leave 1 pixel in every corner
lineRGBA(dest,x+1,y,x+w-2,y,0,0,0,255);
lineRGBA(dest,x+1,y+h-1,x+w-2,y+h-1,0,0,0,255);
lineRGBA(dest,x,y+1,x,y+h-2,0,0,0,255);
lineRGBA(dest,x+w-1,y+1,x+w-1,y+h-2,0,0,0,255);
//Fill the corners with transperent color to create anti-aliased borders
pixelRGBA(dest,x,y,0,0,0,160);
pixelRGBA(dest,x,y+h-1,0,0,0,160);
pixelRGBA(dest,x+w-1,y,0,0,0,160);
pixelRGBA(dest,x+w-1,y+h-1,0,0,0,160);
//Draw second lighter border around content
rectangleRGBA(dest,x+1,y+1,x+w-2,y+h-2,0,0,0,64);
//Create anti-aliasing in corners of second border
pixelRGBA(dest,x+1,y+1,0,0,0,50);
pixelRGBA(dest,x+1,y+h-2,0,0,0,50);
pixelRGBA(dest,x+w-2,y+1,0,0,0,50);
pixelRGBA(dest,x+w-2,y+h-2,0,0,0,50);
}
void drawLine(int x1,int y1,int x2,int y2,SDL_Surface* dest,Uint32 color){
//NOTE: We let SDL_gfx render it.
lineRGBA(dest,x1,y1,x2,y2,color >> 24,color >> 16,color >> 8,255);
}
void drawLineWithArrow(int x1,int y1,int x2,int y2,SDL_Surface* dest,Uint32 color,int spacing,int offset,int xsize,int ysize){
//Draw line first
drawLine(x1,y1,x2,y2,dest,color);
//calc delta and length
double dx=x2-x1;
double dy=y2-y1;
double length=sqrt(dx*dx+dy*dy);
if(length<0.001) return;
//calc the unit vector
dx/=length; dy/=length;
//Now draw arrows on it
for(double p=offset;p<length;p+=spacing){
drawLine(int(x1+p*dx+0.5),int(y1+p*dy+0.5),
int(x1+(p-xsize)*dx-ysize*dy+0.5),int(y1+(p-xsize)*dy+ysize*dx+0.5),dest,color);
drawLine(int(x1+p*dx+0.5),int(y1+p*dy+0.5),
int(x1+(p-xsize)*dx+ysize*dy+0.5),int(y1+(p-xsize)*dy-ysize*dx+0.5),dest,color);
}
}
bool createScreen(){
//Check if we are going fullscreen.
if(settings->getBoolValue("fullscreen"))
pickFullscreenResolution();
//Set the screen_width and height.
SCREEN_WIDTH=atoi(settings->getValue("width").c_str());
SCREEN_HEIGHT=atoi(settings->getValue("height").c_str());
//Update the camera.
camera.w=SCREEN_WIDTH;
camera.h=SCREEN_HEIGHT;
//Boolean if this is the first screen creation.
bool initial=true;
//Check if we should use gl or software rendering.
if(settings->getBoolValue("gl")){
#ifdef HARDWARE_ACCELERATION
SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,32);
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,8);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
//Set the video mode.
Uint32 flags=SDL_HWSURFACE | SDL_OPENGL;
if(settings->getBoolValue("fullscreen"))
flags|=SDL_FULLSCREEN;
else if(settings->getBoolValue("resizable"))
flags|=SDL_RESIZABLE;
if(SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,flags)==NULL){
fprintf(stderr,"FATAL ERROR: SDL_SetVideoMode failed\n");
return false;
}
//Delete the old screen.
//Warning: only if previous mode is OpenGL mode.
//NOTE: The previous mode can't switch during runtime.
if(screen){
SDL_FreeSurface(screen);
screen=NULL;
//There was a screen so this isn't the initial screen creation.
initial=false;
}
//Create a screen
screen=SDL_CreateRGBSurface(SDL_HWSURFACE,SCREEN_WIDTH,SCREEN_HEIGHT,32,0x00FF0000,0x0000FF00,0x000000FF,0);
//Create a texture.
glDeleteTextures(1,&screenTexture);
glGenTextures(1,&screenTexture);
//And set up gl correctly.
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glLoadIdentity();
#else
//NOTE: Hardware accelerated rendering requested but compiled without.
cerr<<"FATAL ERROR: Unable to use hardware acceleration (compiled without)."<<endl;
return false;
#endif
}else{
//Set the flags.
Uint32 flags=SCREEN_FLAGS;
#if !defined(ANDROID)
flags |= SDL_DOUBLEBUF;
#endif
if(settings->getBoolValue("fullscreen"))
flags|=SDL_FULLSCREEN;
else if(settings->getBoolValue("resizable"))
flags|=SDL_RESIZABLE;
//Check if there already was a screen.
if(screen)
initial=false;
//Create the screen and check if there weren't any errors.
screen=SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,flags);
if(screen==NULL){
fprintf(stderr,"FATAL ERROR: SDL_SetVideoMode failed\n");
return false;
}
}
//Now configure the newly created window (if windowed).
if(settings->getBoolValue("fullscreen")==false)
configureWindow(initial);
//Create the temp surface, just a replica of the screen surface, free the previous one if any.
if(tempSurface)
SDL_FreeSurface(tempSurface);
tempSurface=SDL_CreateRGBSurface(SCREEN_FLAGS|SDL_SRCALPHA,
screen->w,screen->h,screen->format->BitsPerPixel,
screen->format->Rmask,screen->format->Gmask,screen->format->Bmask,0);
//Set the the window caption.
SDL_WM_SetCaption(("Me and My Shadow "+version).c_str(),NULL);
SDL_EnableUNICODE(1);
//Nothing went wrong so return true.
return true;
}
+//Workaround for the resizing below 800x600 for Windows.
#ifdef WIN32
static WNDPROC m_OldWindowProc=NULL;
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
if(msg==WM_GETMINMAXINFO){
if(m_OldWindowProc){
CallWindowProc(m_OldWindowProc,hwnd,msg,wParam,lParam);
}else{
DefWindowProc(hwnd,msg,wParam,lParam);
}
RECT r={0,0,800,600};
AdjustWindowRect(&r,GetWindowLong(hwnd,GWL_STYLE),FALSE);
MINMAXINFO *info=(MINMAXINFO*)lParam;
info->ptMinTrackSize.x=r.right-r.left;
info->ptMinTrackSize.y=r.bottom-r.top;
return 0;
}else{
if(m_OldWindowProc){
return CallWindowProc(m_OldWindowProc,hwnd,msg,wParam,lParam);
}else{
return DefWindowProc(hwnd,msg,wParam,lParam);
}
}
}
#endif
void pickFullscreenResolution(){
//Vector that will hold the resolutions to choose from.
vector<_res> resolutionList;
//Enumerate available resolutions using SDL_ListModes()
//Note: we enumerate fullscreen resolutions because
// windowed resolutions always can be arbitrary
if(resolutionList.empty()){
SDL_Rect **modes=SDL_ListModes(NULL,SDL_FULLSCREEN|SCREEN_FLAGS|SDL_ANYFORMAT);
if(modes==NULL || ((intptr_t)modes) == -1){
cout<<"Error: Can't enumerate available screen resolutions."
" Use predefined screen resolutions list instead."<<endl;
static const _res predefinedResolutionList[] = {
{800,600},
{1024,600},
{1024,768},
{1152,864},
{1280,720},
{1280,768},
{1280,800},
{1280,960},
{1280,1024},
{1360,768},
{1366,768},
{1440,900},
{1600,900},
{1600,1200},
{1680,1080},
{1920,1080},
{1920,1200},
{2560,1440},
{3840,2160}
};
//Fill the resolutionList.
for(unsigned int i=0;i<sizeof(predefinedResolutionList)/sizeof(_res);i++){
resolutionList.push_back(predefinedResolutionList[i]);
}
}else{
//Fill the resolutionList.
for(unsigned int i=0;modes[i]!=NULL;i++){
//Check if the resolution is higher than the minimum (800x600).
if(modes[i]->w>=800 && modes[i]->h>=600){
_res res={modes[i]->w, modes[i]->h};
resolutionList.push_back(res);
}
}
//Reverse it so that we begin with the lowest resolution.
reverse(resolutionList.begin(),resolutionList.end());
}
}
//The resolution that will hold the final result, we start with the minimum (800x600).
_res closestMatch={800,600};
int width=atoi(getSettings()->getValue("width").c_str());
//int height=atoi(getSettings()->getValue("height").c_str());
//Now loop through the resolutionList.
for(int i=0;i<(int)resolutionList.size();i++){
//The delta between the closestMatch and the resolution from the list.
int dM=(closestMatch.w-resolutionList[i].w);
//The delta between the target width and the resolution from the list.
int dT=(width-resolutionList[i].w);
//Since the resolutions are getting higher the lower (more negative) the further away it is.
//That's why we check if the deltaMatch is lower than the the deltaTarget.
if((dM)<(dT)){
closestMatch.w=resolutionList[i].w;
closestMatch.h=resolutionList[i].h;
}
}
//Now set the resolution to the closest match.
char s[64];
sprintf(s,"%d",closestMatch.w);
getSettings()->setValue("width",s);
sprintf(s,"%d",closestMatch.h);
getSettings()->setValue("height",s);
}
#ifdef __X11_INCLUDED__
int handleXError(Display* disp,XErrorEvent* event){
//NOTE: This is UNTESTED code, there are still some things that should be tested/changed.
//NOTE: It checks against hardcoded opcodes, this should be based on included defines from the xf86vid headers instead.
//NOTE: This code assumes Xlib is in use, just like the resize restriction code for Linux.
//Print out the error message as normal.
char output[256];
XGetErrorText(disp,event->error_code,output,256);
cerr<<output<<endl;
//Check if the game is fullscreen.
if(getSettings()->getBoolValue("fullscreen")){
//Check for the exact error we want to handle differently.
if(event->error_code==BadValue && event->minor_code==10/*X_XF86VidModeSwitchToMode*/){
//The cause of this problem has likely something to do with fullscreen mode, so fallback to windowed.
cerr<<"ERROR: Xlib error code "<<event->error_code<<", request code "<<event->request_code<<"."<<endl;
cerr<<"ERROR: Falling back to windowed mode!"<<endl;
getSettings()->setValue("fullscreen","false");
createScreen();
return 0;
}
}
//Do the normal Xlib behaviour.
exit(1);
return 0;
}
#endif
void configureWindow(bool initial){
//We only need to configure the window if it's resizable.
if(!getSettings()->getBoolValue("resizable"))
return;
//Retrieve the WM info from SDL containing the window handle.
struct SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWMInfo(&wmInfo);
#ifdef __X11_INCLUDED__
//We assume that a linux system running meandmyshadow is also running an Xorg server.
if(wmInfo.subsystem==SDL_SYSWM_X11){
//Create the size hints to give to the window.
XSizeHints* sizeHints;
if(!(sizeHints=XAllocSizeHints())){
cerr<<"ERROR: Unable to allocate memory for XSizeHings."<<endl;
return;
}
//Configure the size hint.
sizeHints->flags=PMinSize;
sizeHints->min_width=800;
sizeHints->min_height=600;
//Set the normal hints of the window.
(void)wmInfo.info.x11.lock_func;
XSetNormalHints(wmInfo.info.x11.display,wmInfo.info.x11.wmwindow,sizeHints);
(void)wmInfo.info.x11.unlock_func;
//Free size hint structure
XFree(sizeHints);
}else{
//No X11 so an unsupported window manager.
cerr<<"WARNING: Unsupported window manager."<<endl;
}
#elif defined(WIN32)
//We overwrite the window proc of SDL
WNDPROC wndproc=(WNDPROC)GetWindowLong(wmInfo.window,GWL_WNDPROC);
if(wndproc!=NULL && wndproc!=(WNDPROC)WindowProc){
m_OldWindowProc=wndproc;
SetWindowLong(wmInfo.window,GWL_WNDPROC,(LONG)(WNDPROC)WindowProc);
}
#endif
}
void onVideoResize(){
//Check if the resize event isn't malformed.
if(event.resize.w<=0 || event.resize.h<=0)
return;
//Check the size limit.
if(event.resize.w<800)
event.resize.w=800;
if(event.resize.h<600)
event.resize.h=600;
//Check if it really resizes.
if(SCREEN_WIDTH==event.resize.w && SCREEN_HEIGHT==event.resize.h)
return;
char s[32];
//Set the new width and height.
sprintf(s,"%d",event.resize.w);
getSettings()->setValue("width",s);
sprintf(s,"%d",event.resize.h);
getSettings()->setValue("height",s);
//Do resizing.
if(!createScreen())
return;
//Tell the theme to resize.
if(!loadTheme(""))
return;
//The new resolution is valid.
//Now we can save the settings. (TODO: should we save?)
//saveSettings();
//And let the currentState update it's GUI to the new resolution.
currentState->resize();
}
bool init(){
//Initialze SDL.
if(SDL_Init(SDL_INIT_EVERYTHING)==-1) {
fprintf(stderr,"FATAL ERROR: SDL_Init failed\n");
return false;
}
//Initialze SDL_mixer (audio).
if(Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,512)==-1){
fprintf(stderr,"FATAL ERROR: Mix_OpenAudio failed\n");
return false;
}
//Set the volume.
Mix_Volume(-1,atoi(settings->getValue("sound").c_str()));
//Initialze SDL_ttf (fonts).
if(TTF_Init()==-1){
fprintf(stderr,"FATAL ERROR: TTF_Init failed\n");
return false;
}
#ifdef __X11_INCLUDED__
//Before creating the screen set the XErrorHandler in case of X11.
XSetErrorHandler(handleXError);
#endif
//Create the screen.
if(!createScreen())
return false;
//Load key config. Then initialize joystick support.
inputMgr.loadConfig();
inputMgr.openAllJoysitcks();
//Init tinygettext for translations for the right language
dictionaryManager = new tinygettext::DictionaryManager();
dictionaryManager->add_directory(getDataPath()+"locale");
dictionaryManager->set_charset("UTF-8");
//Check if user have defined own language. If not, find it out for the player using findlocale
string lang=getSettings()->getValue("lang");
if(lang.length()>0){
printf("Locale set by user to %s\n",lang.c_str());
language=lang;
}else{
FL_Locale *locale;
FL_FindLocale(&locale,FL_MESSAGES);
printf("Locale isn't set by user: %s\n",locale->lang);
language=locale->lang;
if(locale->country!=NULL){
language+=string("_")+string(locale->country);
}
if(locale->variant!=NULL){
language+=string("@")+string(locale->variant);
}
FL_FreeLocale(&locale);
}
//Now set the language in the dictionaryManager.
dictionaryManager->set_language(tinygettext::Language::from_name(language));
//Disable annoying 'Couldn't translate: blah blah blah'
tinygettext::Log::set_log_info_callback(NULL);
//Create the types of blocks.
for(int i=0;i<TYPE_MAX;i++){
Game::blockNameMap[Game::blockName[i]]=i;
}
//Structure that holds the event type/name pair.
struct EventTypeName{
int type;
const char* name;
};
//Create the types of game object event types.
{
const EventTypeName types[]={
{GameObjectEvent_PlayerWalkOn,"playerWalkOn"},
{GameObjectEvent_PlayerIsOn,"playerIsOn"},
{GameObjectEvent_PlayerLeave,"playerLeave"},
{GameObjectEvent_OnCreate,"onCreate"},
{GameObjectEvent_OnEnterFrame,"onEnterFrame"},
{GameObjectEvent_OnToggle,"onToggle"},
{GameObjectEvent_OnSwitchOn,"onSwitchOn"},
{GameObjectEvent_OnSwitchOff,"onSwitchOff"},
{0,NULL}
};
for(int i=0;types[i].name;i++){
Game::gameObjectEventNameMap[types[i].name]=types[i].type;
Game::gameObjectEventTypeMap[types[i].type]=types[i].name;
}
}
//Create the types of level event types.
{
const EventTypeName types[]={
{LevelEvent_OnCreate,"onCreate"},
{LevelEvent_OnSave,"onSave"},
{LevelEvent_OnLoad,"onLoad"},
{LevelEvent_OnReset,"onReset"},
{0,NULL}
};
for(int i=0;types[i].name;i++){
Game::levelEventNameMap[types[i].name]=types[i].type;
Game::levelEventTypeMap[types[i].type]=types[i].name;
}
}
//Register the ScriptAPI's functions in the scriptExecutor.
registerFunctions(getScriptExecutor());
//Nothing went wrong so we return true.
return true;
}
static TTF_Font* loadFont(const char* name,int size){
TTF_Font* tmpFont=TTF_OpenFont((getDataPath()+"font/"+name+".ttf").c_str(),size);
if(tmpFont){
return tmpFont;
}else{
#if defined(ANDROID)
//Android has built-in DroidSansFallback.ttf. (?)
return TTF_OpenFont("/system/fonts/DroidSansFallback.ttf",size);
#else
return TTF_OpenFont((getDataPath()+"font/DroidSansFallback.ttf").c_str(),size);
#endif
}
}
bool loadFonts(){
//Load the fonts.
//NOTE: This is a separate method because it will be called separately when re-initing in case of language change.
//First close the fonts if needed.
if(fontTitle)
TTF_CloseFont(fontTitle);
if(fontGUI)
TTF_CloseFont(fontGUI);
if(fontGUISmall)
TTF_CloseFont(fontGUISmall);
if(fontText)
TTF_CloseFont(fontText);
/// TRANSLATORS: Font used in GUI:
/// - Use "knewave" for languages using Latin and Latin-derived alphabets
/// - "DroidSansFallback" can be used for non-Latin writing systems
fontTitle=loadFont(_("knewave"),55);
fontGUI=loadFont(_("knewave"),32);
fontGUISmall=loadFont(_("knewave"),24);
/// TRANSLATORS: Font used for normal text:
/// - Use "Blokletters-Viltstift" for languages using Latin and Latin-derived alphabets
/// - "DroidSansFallback" can be used for non-Latin writing systems
fontText=loadFont(_("Blokletters-Viltstift"),16);
if(fontTitle==NULL || fontGUI==NULL || fontGUISmall==NULL || fontText==NULL){
printf("ERROR: Unable to load fonts! \n");
return false;
}
//Nothing went wrong so return true.
return true;
}
//Generate small arrows used for some GUI widgets.
static void generateArrows(){
TTF_Font* fontArrow=loadFont(_("knewave"),18);
if(arrowLeft1){
SDL_FreeSurface(arrowLeft1);
SDL_FreeSurface(arrowRight1);
SDL_FreeSurface(arrowLeft2);
SDL_FreeSurface(arrowRight2);
}
arrowLeft1=TTF_RenderUTF8_Blended(fontArrow,"<",themeTextColor);
arrowRight1=TTF_RenderUTF8_Blended(fontArrow,">",themeTextColor);
arrowLeft2=TTF_RenderUTF8_Blended(fontArrow,"<",themeTextColorDialog);
arrowRight2=TTF_RenderUTF8_Blended(fontArrow,">",themeTextColorDialog);
TTF_CloseFont(fontArrow);
}
bool loadTheme(string name){
//Load default fallback theme if it isn't loaded yet
if(objThemes.themeCount()==0){
if(objThemes.appendThemeFromFile(getDataPath()+"themes/Cloudscape/theme.mnmstheme")==NULL){
printf("ERROR: Can't load default theme file\n");
return false;
}
}
//Resize background or load specific theme
bool success=true;
if(name==""||name.empty()){
objThemes.scaleToScreen();
}else{
string theme=processFileName(name);
if(objThemes.appendThemeFromFile(theme+"/theme.mnmstheme")==NULL){
printf("ERROR: Can't load theme %s\n",theme.c_str());
success=false;
}
}
generateArrows();
//Everything went fine so return true.
return success;
}
static Mix_Chunk* loadWAV(const char* s){
Mix_Chunk* c=Mix_LoadWAV(s);
if(c!=NULL) return c;
printf("ERROR: Can't load sound file %s: %s\n",s,SDL_GetError());
return NULL;
}
static SDL_Cursor* loadCursor(const char* image[]){
int i,row,col;
//The array that holds the data (0=white 1=black)
Uint8 data[4*32];
//The array that holds the alpha mask (0=transparent 1=visible)
Uint8 mask[4*32];
//The coordinates of the hotspot of the cursor.
int hotspotX, hotspotY;
i=-1;
//Loop through the rows and columns.
//NOTE: We assume a cursor size of 32x32.
for(row=0;row<32;++row){
for(col=0; col<32;++col){
if(col % 8) {
data[i]<<=1;
mask[i]<<=1;
}else{
++i;
data[i]=mask[i]=0;
}
switch(image[4+row][col]){
case '+':
data[i] |= 0x01;
mask[i] |= 0x01;
break;
case '.':
mask[i] |= 0x01;
break;
default:
break;
}
}
}
//Get the hotspot x and y locations from the last line of the cursor.
sscanf(image[4+row],"%d,%d",&hotspotX,&hotspotY);
return SDL_CreateCursor(data,mask,32,32,hotspotX,hotspotY);
}
bool loadFiles(){
//Load the fonts.
if(!loadFonts())
return false;
fontMono=loadFont("VeraMono",12);
//Show a loading screen
{
SDL_Rect r={0,0,screen->w,screen->h};
SDL_FillRect(screen,&r,0);
SDL_Color fg={255,255,255};
SDL_Surface *surface=TTF_RenderUTF8_Blended(fontTitle,_("Loading..."),fg);
if(surface!=NULL){
r.x=(screen->w-surface->w)/2;
r.y=(screen->h-surface->h)/2;
SDL_BlitSurface(surface,NULL,screen,&r);
SDL_FreeSurface(surface);
}
SDL_Flip(screen);
}
musicManager.destroy();
//Load the music and play it.
if(musicManager.loadMusic((getDataPath()+"music/menu.music")).empty()){
printf("WARNING: Unable to load background music! \n");
}
musicManager.playMusic("menu",false);
//Always load the default music list for fallback.
musicManager.loadMusicList((getDataPath()+"music/default.list"));
//Load the configured music list.
getMusicManager()->loadMusicList((getDataPath()+"music/"+getSettings()->getValue("musiclist")+".list"));
getMusicManager()->setMusicList(getSettings()->getValue("musiclist"));
//Check if music is enabled.
if(getSettings()->getBoolValue("music"))
getMusicManager()->setEnabled();
//Load the sound effects
jumpSound=loadWAV((getDataPath()+"sfx/jump.wav").c_str());
hitSound=loadWAV((getDataPath()+"sfx/hit.wav").c_str());
saveSound=loadWAV((getDataPath()+"sfx/checkpoint.wav").c_str());
swapSound=loadWAV((getDataPath()+"sfx/swap.wav").c_str());
toggleSound=loadWAV((getDataPath()+"sfx/toggle.wav").c_str());
errorSound=loadWAV((getDataPath()+"sfx/error.wav").c_str());
collectSound=loadWAV((getDataPath()+"sfx/collect.wav").c_str());
achievementSound=loadWAV((getDataPath()+"sfx/achievement.ogg").c_str());
//Load the cursor images from the Cursor.h file.
cursors[CURSOR_POINTER]=loadCursor(pointer);
cursors[CURSOR_CARROT]=loadCursor(ibeam);
cursors[CURSOR_DRAG]=loadCursor(closedhand);
cursors[CURSOR_SIZE_HOR]=loadCursor(size_hor);
cursors[CURSOR_SIZE_VER]=loadCursor(size_ver);
cursors[CURSOR_SIZE_FDIAG]=loadCursor(size_fdiag);
cursors[CURSOR_SIZE_BDIAG]=loadCursor(size_bdiag);
cursors[CURSOR_REMOVE]=loadCursor(remove_cursor);
//Set the default cursor right now.
SDL_SetCursor(cursors[CURSOR_POINTER]);
levelPackManager.destroy();
//Now sum up all the levelpacks.
vector<string> v=enumAllDirs(getDataPath()+"levelpacks/");
for(vector<string>::iterator i=v.begin(); i!=v.end(); ++i){
levelPackManager.loadLevelPack(getDataPath()+"levelpacks/"+*i);
}
v=enumAllDirs(getUserPath(USER_DATA)+"levelpacks/");
for(vector<string>::iterator i=v.begin(); i!=v.end(); ++i){
levelPackManager.loadLevelPack(getUserPath(USER_DATA)+"levelpacks/"+*i);
}
v=enumAllDirs(getUserPath(USER_DATA)+"custom/levelpacks/");
for(vector<string>::iterator i=v.begin(); i!=v.end(); ++i){
levelPackManager.loadLevelPack(getUserPath(USER_DATA)+"custom/levelpacks/"+*i);
}
//Now we add a special levelpack that will contain the levels not in a levelpack.
LevelPack* levelsPack=new LevelPack;
levelsPack->levelpackName="Levels";
LevelPack* customLevelsPack=new LevelPack;
customLevelsPack->levelpackName="Custom Levels";
//List the addon levels and add them one for one.
v=enumAllFiles(getUserPath(USER_DATA)+"levels/");
for(vector<string>::iterator i=v.begin(); i!=v.end(); ++i){
levelsPack->addLevel(getUserPath(USER_DATA)+"levels/"+*i);
levelsPack->setLocked(levelsPack->getLevelCount()-1);
}
//List the custom levels and add them one for one.
v=enumAllFiles(getUserPath(USER_DATA)+"custom/levels/");
for(vector<string>::iterator i=v.begin(); i!=v.end(); ++i){
levelsPack->addLevel(getUserPath(USER_DATA)+"custom/levels/"+*i);
levelsPack->setLocked(levelsPack->getLevelCount()-1);
customLevelsPack->addLevel(getUserPath(USER_DATA)+"custom/levels/"+*i);
customLevelsPack->setLocked(customLevelsPack->getLevelCount()-1);
}
//Add them to the manager.
levelPackManager.addLevelPack(levelsPack);
levelPackManager.addLevelPack(customLevelsPack);
//Load statistics
statsMgr.loadPicture();
statsMgr.registerAchievements();
statsMgr.loadFile(getUserPath(USER_CONFIG)+"statistics");
//Do something ugly and slow
statsMgr.reloadCompletedLevelsAndAchievements();
statsMgr.reloadOtherAchievements();
//Load the theme, both menu and default.
//NOTE: Loading theme may fail and returning false would stop everything, default theme will be used instead.
if (!loadTheme(getSettings()->getValue("theme"))){
getSettings()->setValue("theme","%DATA%/themes/Cloudscape");
saveSettings();
}
//Nothing failed so return true.
return true;
}
bool loadSettings(){
settings=new Settings(getUserPath(USER_CONFIG)+"meandmyshadow.cfg");
settings->parseFile();
//Now apply settings changed through command line arguments, if any.
map<string,string>::iterator it;
for(it=tmpSettings.begin();it!=tmpSettings.end();++it){
settings->setValue(it->first,it->second);
}
tmpSettings.clear();
//Always return true?
return true;
}
bool saveSettings(){
settings->save();
//Always return true?
return true;
}
Settings* getSettings(){
return settings;
}
MusicManager* getMusicManager(){
return &musicManager;
}
LevelPackManager* getLevelPackManager(){
return &levelPackManager;
}
ScriptExecutor* getScriptExecutor(){
return &scriptExecutor;
}
void flipScreen(){
if(settings->getBoolValue("gl")){
#ifdef HARDWARE_ACCELERATION
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Create a texture from the screen surface.
glBindTexture(GL_TEXTURE_2D,screenTexture);
//Set the texture's stretching properties
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,screen->format->BytesPerPixel,screen->w,screen->h,0,GL_BGRA,GL_UNSIGNED_BYTE,screen->pixels);
glBegin(GL_QUADS);
glTexCoord2i(0,0); glVertex3f(0,0,0);
glTexCoord2i(1,0); glVertex3f(SCREEN_WIDTH,0,0);
glTexCoord2i(1,1); glVertex3f(SCREEN_WIDTH,SCREEN_HEIGHT,0);
glTexCoord2i(0,1); glVertex3f(0,SCREEN_HEIGHT,0);
glEnd();
SDL_GL_SwapBuffers();
#else
//NOTE: Trying to flip the screen using gl while compiled without.
cerr<<"FATAL ERROR: Unable to draw to screen using OpenGL (compiled without)."<<endl;
#endif
}else{
SDL_Flip(screen);
}
}
void clean(){
//Save statistics
statsMgr.saveFile(getUserPath(USER_CONFIG)+"statistics");
//We delete the settings.
if(settings){
delete settings;
settings=NULL;
}
//Get rid of the currentstate.
//NOTE: The state is probably already deleted by the changeState function.
if(currentState)
delete currentState;
//Destroy the GUI if present.
if(GUIObjectRoot){
delete GUIObjectRoot;
GUIObjectRoot=NULL;
}
//Destroy the imageManager.
imageManager.destroy();
//Destroy the musicManager.
musicManager.destroy();
//Destroy all sounds
Mix_FreeChunk(jumpSound);
Mix_FreeChunk(hitSound);
Mix_FreeChunk(saveSound);
Mix_FreeChunk(swapSound);
Mix_FreeChunk(toggleSound);
Mix_FreeChunk(errorSound);
Mix_FreeChunk(collectSound);
Mix_FreeChunk(achievementSound);
//Destroy the cursors.
for(int i=0;i<CURSOR_MAX;i++){
SDL_FreeCursor(cursors[i]);
cursors[i]=NULL;
}
//Destroy the levelPackManager.
levelPackManager.destroy();
levels=NULL;
//Close all joysticks.
inputMgr.closeAllJoysticks();
//Close the fonts and quit SDL_ttf.
TTF_CloseFont(fontTitle);
TTF_CloseFont(fontGUI);
TTF_CloseFont(fontGUISmall);
TTF_CloseFont(fontText);
TTF_CloseFont(fontMono);
TTF_Quit();
//Remove the temp surface.
SDL_FreeSurface(tempSurface);
//Stop audio.and quit
Mix_CloseAudio();
#ifndef __APPLE__
Mix_Quit();
#endif
//And finally quit SDL.
SDL_Quit();
}
void setNextState(int newstate){
//Only change the state when we aren't already exiting.
if(nextState!=STATE_EXIT){
nextState=newstate;
}
}
void changeState(){
//Check if there's a nextState.
if(nextState!=STATE_NULL){
//Delete the currentState.
delete currentState;
currentState=NULL;
//Set the currentState to the nextState.
stateID=nextState;
nextState=STATE_NULL;
//Init the state.
switch(stateID){
case STATE_GAME:
{
currentState=NULL;
Game* game=new Game();
currentState=game;
//Check if we should load record file or a level.
if(!Game::recordFile.empty()){
game->loadRecord(Game::recordFile.c_str());
Game::recordFile.clear();
}else{
game->loadLevel(levels->getLevelpackPath()+levels->getLevelFile());
levels->saveLevelProgress();
}
}
break;
case STATE_MENU:
currentState=new Menu();
break;
case STATE_LEVEL_SELECT:
currentState=new LevelPlaySelect();
break;
case STATE_LEVEL_EDIT_SELECT:
currentState=new LevelEditSelect();
break;
case STATE_LEVEL_EDITOR:
{
currentState=NULL;
LevelEditor* levelEditor=new LevelEditor();
currentState=levelEditor;
//Load the selected level.
levelEditor->loadLevel(levels->getLevelpackPath()+levels->getLevelFile());
}
break;
case STATE_OPTIONS:
currentState=new Options();
break;
case STATE_ADDONS:
currentState=new Addons();
break;
case STATE_CREDITS:
currentState=new Credits();
break;
case STATE_STATISTICS:
currentState=new StatisticsScreen();
break;
}
//NOTE: STATE_EXIT isn't mentioned, meaning that currentState is null.
//This way the game loop will break and the program will exit.
//Fade out, if fading is enabled.
int fade=0;
if(settings->getBoolValue("fading"))
fade=255;
SDL_BlitSurface(screen,NULL,tempSurface,NULL);
while(fade>0){
fade-=17;
if(fade<0)
fade=0;
SDL_FillRect(screen,NULL,0);
SDL_SetAlpha(tempSurface, SDL_SRCALPHA, fade);
SDL_BlitSurface(tempSurface,NULL,screen,NULL);
flipScreen();
SDL_Delay(25);
}
}
}
void musicStoppedHook(){
//We just call the musicStopped method of the MusicManager.
musicManager.musicStopped();
}
bool checkCollision(const SDL_Rect& a,const SDL_Rect& b){
//Check if the left side of box a isn't past the right side of b.
if(a.x>=b.x+b.w){
return false;
}
//Check if the right side of box a isn't left of the left side of b.
if(a.x+a.w<=b.x){
return false;
}
//Check if the top side of box a isn't under the bottom side of b.
if(a.y>=b.y+b.h){
return false;
}
//Check if the bottom side of box a isn't above the top side of b.
if(a.y+a.h<=b.y){
return false;
}
//We have collision.
return true;
}
void setCamera(const SDL_Rect* r,int count){
//SetCamera only works in the Level editor and when mouse is inside window.
if(stateID==STATE_LEVEL_EDITOR&&(SDL_GetAppState()&SDL_APPMOUSEFOCUS)){
//Get the mouse coordinates.
int x,y;
SDL_GetMouseState(&x,&y);
SDL_Rect mouse={x,y,0,0};
//Don't continue here if mouse is inside one of the boxes given as parameter.
for(int i=0;i<count;i++){
if(checkCollision(mouse,r[i]))
return;
}
//Check if the mouse is near the left edge of the screen.
//Else check if the mouse is near the right edge.
if(x<50){
//We're near the left edge so move the camera.
camera.x-=5;
}else if(x>SCREEN_WIDTH-50){
//We're near the right edge so move the camera.
camera.x+=5;
}
//Check if the mouse is near the top edge of the screen.
//Else check if the mouse is near the bottom edge.
if(y<50){
//We're near the top edge so move the camera.
camera.y-=5;
}else if(y>SCREEN_HEIGHT-50){
//We're near the bottom edge so move the camera.
camera.y+=5;
}
}
}
int parseArguments(int argc, char** argv){
//Loop through all arguments.
//We start at one since 0 is the command itself.
for(int i=1;i<argc;i++){
string argument=argv[i];
//Check if the argument is the data-dir.
if(argument=="--data-dir"){
//We need a second argument so we increase i.
i++;
if(i>=argc){
printf("ERROR: Missing argument for command '%s'\n\n",argument.c_str());
return -1;
}
//Configure the dataPath with the given path.
dataPath=argv[i];
if(!getDataPath().empty()){
char c=dataPath[dataPath.size()-1];
if(c!='/'&&c!='\\') dataPath+="/";
}
}else if(argument=="--user-dir"){
//We need a second argument so we increase i.
i++;
if(i>=argc){
printf("ERROR: Missing argument for command '%s'\n\n",argument.c_str());
return -1;
}
//Configure the userPath with the given path.
userPath=argv[i];
if(!userPath.empty()){
char c=userPath[userPath.size()-1];
if(c!='/'&&c!='\\') userPath+="/";
}
}else if(argument=="-f" || argument=="-fullscreen" || argument=="--fullscreen"){
tmpSettings["fullscreen"]="1";
}else if(argument=="-w" || argument=="-windowed" || argument=="--windowed"){
tmpSettings["fullscreen"]="0";
}else if(argument=="-mv" || argument=="-music" || argument=="--music"){
//We need a second argument so we increase i.
i++;
if(i>=argc){
printf("ERROR: Missing argument for command '%s'\n\n",argument.c_str());
return -1;
}
//Now set the music volume.
tmpSettings["music"]=argv[i];
}else if(argument=="-sv" || argument=="-sound" || argument=="--sound"){
//We need a second argument so we increase i.
i++;
if(i>=argc){
printf("ERROR: Missing argument for command '%s'\n\n",argument.c_str());
return -1;
}
//Now set sound volume.
tmpSettings["sound"]=argv[i];
}else if(argument=="-set" || argument=="--set"){
//We need a second and a third argument so we increase i.
i+=2;
if(i>=argc){
printf("ERROR: Missing argument for command '%s'\n\n",argument.c_str());
return -1;
}
//And set the setting.
tmpSettings[argv[i-1]]=argv[i];
}else if(argument=="-v" || argument=="-version" || argument=="--version"){
//Print the version.
printf("%s\n",version.c_str());
return 0;
}else if(argument=="-h" || argument=="-help" || argument=="--help"){
//If the help is requested we'll return false without printing an error.
//This way the usage/help text will be printed.
return -1;
}else{
//Any other argument is unknow so we return false.
printf("ERROR: Unknown argument %s\n\n",argument.c_str());
return -1;
}
}
//If everything went well we can return true.
return 1;
}
//Special structure that will recieve the GUIEventCallbacks of the messagebox.
struct msgBoxHandler:public GUIEventCallback{
public:
//Integer containing the ret(urn) value of the messageBox.
int ret;
public:
//Constructor.
msgBoxHandler():ret(0){}
void GUIEventCallback_OnEvent(std::string name,GUIObject* obj,int eventType){
//Make sure it's a click event.
if(eventType==GUIEventClick){
//Set the return value.
ret=obj->value;
//After a click event we can delete the GUI.
if(GUIObjectRoot){
delete GUIObjectRoot;
GUIObjectRoot=NULL;
}
}
}
};
msgBoxResult msgBox(string prompt,msgBoxButtons buttons,const string& title){
//Create the event handler.
msgBoxHandler objHandler;
//The GUI objects.
GUIObject* obj;
//Create the GUIObjectRoot, the height and y location is temp.
//It depends on the content what it will be.
GUIObject* root=new GUIObject((SCREEN_WIDTH-600)/2,200,600,200,GUIObjectFrame,title.c_str());
//Integer containing the current y location used to grow dynamic depending on the content.
int y=50;
//Now process the prompt.
{
//Pointer to the string.
char* lps=(char*)prompt.c_str();
//Pointer to a character.
char* lp=NULL;
//We keep looping forever.
//The only way out is with the break statement.
for(;;){
//As long as it's still the same sentence we continue.
//It will stop when there's a newline or end of line.
for(lp=lps;*lp!='\n'&&*lp!='\r'&&*lp!=0;lp++);
//Store the character we stopped on. (End or newline)
char c=*lp;
//Set the character in the string to 0, making lps a string containing one sentence.
*lp=0;
//Add a GUIObjectLabel with the sentence.
root->addChild(new GUIObject(0,y,root->width,25,GUIObjectLabel,lps,0,true,true,GUIGravityCenter));
//Increase y with 25, about the height of the text.
y+=25;
//Check the stored character if it was a stop.
if(c==0){
//It was so break out of the for loop.
lps=lp;
break;
}
//It wasn't meaning more will follow.
//We set lps to point after the "newline" forming a new string.
lps=lp+1;
}
}
//Add 70 to y to leave some space between the content and the buttons.
y+=70;
//Recalc the size of the message box.
root->top=(SCREEN_HEIGHT-y)/2;
root->height=y;
//Now we need to add the buttons.
//Integer containing the number of buttons to add.
int count=0;
//Array with the return codes for the buttons.
int value[3]={0};
//Array containing the captation for the buttons.
string button[3]={"","",""};
switch(buttons){
case MsgBoxOKCancel:
count=2;
button[0]=_("OK");value[0]=MsgBoxOK;
button[1]=_("Cancel");value[1]=MsgBoxCancel;
break;
case MsgBoxAbortRetryIgnore:
count=3;
button[0]=_("Abort");value[0]=MsgBoxAbort;
button[1]=_("Retry");value[1]=MsgBoxRetry;
button[2]=_("Ignore");value[2]=MsgBoxIgnore;
break;
case MsgBoxYesNoCancel:
count=3;
button[0]=_("Yes");value[0]=MsgBoxYes;
button[1]=_("No");value[1]=MsgBoxNo;
button[2]=_("Cancel");value[2]=MsgBoxCancel;
break;
case MsgBoxYesNo:
count=2;
button[0]=_("Yes");value[0]=MsgBoxYes;
button[1]=_("No");value[1]=MsgBoxNo;
break;
case MsgBoxRetryCancel:
count=2;
button[0]=_("Retry");value[0]=MsgBoxRetry;
button[1]=_("Cancel");value[1]=MsgBoxCancel;
break;
default:
count=1;
button[0]=_("OK");value[0]=MsgBoxOK;
break;
}
//Now we start making the buttons.
{
//Reduce y so that the buttons fit inside the frame.
y-=40;
double places[3]={0.0};
if(count==1){
places[0]=0.5;
}else if(count==2){
places[0]=0.4;
places[1]=0.6;
}else if(count==3){
places[0]=0.3;
places[1]=0.5;
places[2]=0.7;
}
//Loop to add the buttons.
for(int i=0;i<count;i++){
obj=new GUIObject(root->width*places[i],y,-1,36,GUIObjectButton,button[i].c_str(),value[i],true,true,GUIGravityCenter);
obj->eventCallback=&objHandler;
root->addChild(obj);
}
}
//Now we dim the screen and keep the GUI rendering/updating.
GUIOverlay* overlay=new GUIOverlay(root);
overlay->enterLoop(true);
//And return the result.
return (msgBoxResult)objHandler.ret;
}
struct fileDialogHandler:public GUIEventCallback{
public:
//The ret(urn) value, true=ok and false=cancel
bool ret;
//Boolean if it's a save dialog.
bool isSave;
//Boolean if the file should be verified.
bool verifyFile;
//Boolean if files should be listed instead of directories.
bool files;
//Pointer to the textfield containing the filename.
GUIObject* txtName;
//Pointer to the listbox containing the different files.
GUIListBox* lstFile;
//The extension the files listed should have.
const char* extension;
//The current filename.
string fileName;
//The current search path.
string path;
//Vector containing the search paths.
vector<string> searchPath;
public:
//Constructor.
fileDialogHandler(bool isSave=false,bool verifyFile=false, bool files=true):ret(false),
isSave(isSave),verifyFile(verifyFile),
files(files),txtName(NULL),lstFile(NULL),extension(NULL){}
void GUIEventCallback_OnEvent(std::string name,GUIObject* obj,int eventType){
//Check for the ok event.
if(name=="cmdOK"){
//Get the entered fileName from the text field.
std::string s=txtName->caption;
//If it doesn't contain a slash we need to add the path to the fileName.
if(s.find_first_of("/")==string::npos)
s=path+s;
//If the string empty we return.
if(s.empty() || s.find_first_of("*?")!=string::npos)
return;
//We only need to check for extensions if it isn't a folder dialog.
if(files){
//If there isn't right extension add it.
size_t found=s.find_first_of(".");
if(found!=string::npos)
s.replace(s.begin()+found+1,s.end(),extension);
else if (s.substr(found+1)!=extension)
s.append(string(".")+extension);
}
//Check if we should save or load the file.
//
if(isSave){
//Open the file with read permission to check if it already exists.
FILE* f;
f=fopen(processFileName(s).c_str(),"rb");
//Check if it exists.
if(f){
//Close the file.
fclose(f);
//Let the currentState render once to prevent multiple GUI overlapping and prevent the screen from going black.
currentState->render();
//Prompt the user with a Yes or No question.
/// TRANSLATORS: Filename is coming before this text
if(msgBox(tfm::format(_("%s already exists.\nDo you want to overwrite it?"),s),MsgBoxYesNo,_("Overwrite Prompt"))!=MsgBoxYes){
//He answered no, so we return.
return;
}
}
//Check if we should verify the file.
//Verifying only applies to files not to directories.
if(verifyFile && files){
//Open the file with write permission.
f=fopen(processFileName(s).c_str(),"wb");
//Check if their aren't problems.
if(f){
//Close the file.
fclose(f);
}else{
//Let the currentState render once to prevent multiple GUI overlapping and prevent the screen from going black.
currentState->render();
//The file can't be opened so tell the user.
msgBox(tfm::format(_("Can't open file %s."),s),MsgBoxOKOnly,_("Error"));
return;
}
}
}else if(verifyFile && files){
//We need to verify a file for opening.
FILE *f;
f=fopen(processFileName(s).c_str(),"rb");
//Check if it didn't fail.
if(f){
//Succes, so close the file.
fclose(f);
}else{
//Let the currentState render once to prevent multiple GUI overlapping and prevent the screen from going black.
currentState->render();
//Unable to open file so tell the user.
msgBox(tfm::format(_("Can't open file %s."),s),MsgBoxOKOnly,_("Error"));
return;
}
}
//If we haven't returned then it's fine.
//Set the fileName to the chosen file.
fileName=s;
//Delete the GUI.
if(GUIObjectRoot){
delete GUIObjectRoot;
GUIObjectRoot=NULL;
}
//Set return to true.
ret=true;
}else if(name=="cmdCancel"){
//Cancel means we can kill the gui.
if(GUIObjectRoot){
delete GUIObjectRoot;
GUIObjectRoot=NULL;
}
}else if(name=="lstFile"){
//Get a pointer to the listbox.
GUIListBox* obj1=lstFile;
//Make sure the option exist and change textfield to it.
if(obj1!=NULL && txtName!=NULL && obj1->value>=0 && obj1->value<(int)obj1->item.size()){
txtName->caption=obj1->item[obj1->value];
}
}else if(name=="lstSearchIn"){
//Get the searchpath listbox.
GUISingleLineListBox *obj1=dynamic_cast<GUISingleLineListBox*>(obj);
//Check if the entry exists.
if(obj1!=NULL && lstFile!=NULL && obj1->value>=0 && obj1->value<(int)searchPath.size()){
//Temp string.
string s;
//Get the new search path.
path=searchPath[obj1->value];
//Make sure it isn't empty.
if(!path.empty()){
//Process the filename.
s=processFileName(path);
}else{
//It's empty so we give the userpath.
s=getUserPath();
}
//Fill the list with files or directories.
if(files) {
lstFile->item=enumAllFiles(s,extension);
}else
lstFile->item=enumAllDirs(s);
//Remove any selection from the list.
lstFile->value=-1;
}
}
}
};
bool fileDialog(string& fileName,const char* title,const char* extension,const char* path,bool isSave,bool verifyFile,bool files){
//Pointer to GUIObject to make the GUI with.
GUIObject* obj;
//Create the fileDialogHandler, used for event handling.
fileDialogHandler objHandler(isSave,verifyFile,files);
//Vector containing the pathNames.
vector<string> pathNames;
//Set the extension of the objHandler.
objHandler.extension=extension;
//We now need to splits the given path into multiple path names.
if(path && path[0]){
//The string isn't empty.
//Pointer to the paths string.
char* lp=(char*)path;
//Pointer to the first newline.
char* lps=strchr(lp,'\n');
//Pointer used for checking if their's another newline.
//It will indicate if it's the last set or not.
char* lpe;
//Check for a newline.
if(lps){
//We have newline(s) so loop forever.
//We can only break out of the loop when the string ends.
for(;;){
//Add the first searchpath.
//This is the beginning of the string (lp) to the first newline. (lps)
objHandler.searchPath.push_back(string(lp,lps-lp));
//We should have another newline so search for it.
lpe=strchr(lps+1,'\n');
if(lpe){
//We found it so we add that to the pathname.
pathNames.push_back(string(lps+1,lpe-lps-1));
//And start over again by setting lp to the start of a new set of searchPath/pathName.
lp=lpe+1;
}else{
//There is no newline anymore, meaning the last entry, the rest of the string must be the pathName.
pathNames.push_back(string(lps+1));
//And break out of the loop.
break;
}
//We haven't broken out so search for a newline.
lps=strchr(lp,'\n');
//If there isn't a newline break.
if(!lps)
break;
}
}else{
//There is no newline thus the whole string is the searchPath.
objHandler.searchPath.push_back(path);
}
}else{
//Empty so put an empty string as searchPath.
objHandler.searchPath.push_back(string());
}
//It's time to create the GUI.
//If there are more than one pathNames we need to add a GUISingleLineListBox.
int base_y=pathNames.empty()?20:60;
//Create the frame.
GUIObject* root=new GUIObject(100,100-base_y/2,600,400+base_y,GUIObjectFrame,title?title:(isSave?_("Save File"):_("Load File")));
//Create the search path list box if needed.
if(!pathNames.empty()){
root->addChild(new GUIObject(8,40,184,36,GUIObjectLabel,_("Search In")));
GUISingleLineListBox* obj1=new GUISingleLineListBox(160,40,432,36);
obj1->item=pathNames;
obj1->value=0;
obj1->name="lstSearchIn";
obj1->eventCallback=&objHandler;
root->addChild(obj1);
}
//Add the FileName label and textfield.
root->addChild(new GUIObject(8,20+base_y,184,36,GUIObjectLabel,_("File Name")));
{
//Fill the textbox with the given fileName.
string s=fileName;
if(!isSave){
//But only if it isn't empty.
if(s.empty() && extension && extension[0])
s=string("*.")+string(extension);
}
//Create the textbox and add it to the GUI.
objHandler.txtName=new GUIObject(160,20+base_y,432,36,GUIObjectTextBox,s.c_str());
root->addChild(objHandler.txtName);
}
//Now we add the ListBox containing the files or directories.
{
GUIListBox* obj1=new GUIListBox(8,60+base_y,584,292);
//Get the searchPath.
string s=objHandler.searchPath[0];
//Make sure it isn't empty.
if(!s.empty()){
objHandler.path=s;
s=processFileName(s);
}else{
s=getUserPath();
}
//Check if we should list files or directories.
if(files){
//Fill the list with files.
obj1->item=enumAllFiles(s,extension);
}else{
//Fill the list with directories.
obj1->item=enumAllDirs(s);
}
obj1->name="lstFile";
obj1->eventCallback=&objHandler;
root->addChild(obj1);
objHandler.lstFile=obj1;
}
//Now create the OK and Cancel buttons.
obj=new GUIObject(200,360+base_y,192,36,GUIObjectButton,_("OK"));
obj->name="cmdOK";
obj->eventCallback=&objHandler;
root->addChild(obj);
obj=new GUIObject(400,360+base_y,192,36,GUIObjectButton,_("Cancel"));
obj->name="cmdCancel";
obj->eventCallback=&objHandler;
root->addChild(obj);
//Create the gui overlay.
GUIOverlay* overlay=new GUIOverlay(root);
overlay->enterLoop();
//Now determine what the return value is (and if there is one).
if(objHandler.ret)
fileName=objHandler.fileName;
return objHandler.ret;
}
diff --git a/src/libs/findlocale/findlocale.c b/src/libs/findlocale/findlocale.cpp
similarity index 98%
rename from src/libs/findlocale/findlocale.c
rename to src/libs/findlocale/findlocale.cpp
index 4be15e3..d4225dc 100644
--- a/src/libs/findlocale/findlocale.c
+++ b/src/libs/findlocale/findlocale.cpp
@@ -1,516 +1,516 @@
/*
Copyright (C) 2004 Adam D. Moss (the "Author"). All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is fur-
nished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the Author of the
Software shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written authorization
from the Author.
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifdef WIN32
#include <windows.h>
#include <winnt.h>
#endif
#include "findlocale.h"
static int
is_lcchar(const int c) {
return isalnum(c);
}
static void
lang_country_variant_from_envstring(const char *str,
char **lang,
char **country,
char **variant) {
int end = 0;
int start;
/* get lang, if any */
start = end;
while (is_lcchar(str[end])) {
++end;
}
if (start != end) {
int i;
int len = end - start;
- char *s = malloc(len + 1);
+ char *s = (char*)malloc(len + 1);
for (i=0; i<len; ++i) {
s[i] = tolower(str[start + i]);
}
s[i] = '\0';
*lang = s;
} else {
*lang = NULL;
}
if (str[end] && str[end]!=':') { /* not at end of str */
++end;
}
/* get country, if any */
start = end;
while (is_lcchar(str[end])) {
++end;
}
if (start != end) {
int i;
int len = end - start;
- char *s = malloc(len + 1);
+ char *s = (char*)malloc(len + 1);
for (i=0; i<len; ++i) {
s[i] = toupper(str[start + i]);
}
s[i] = '\0';
*country = s;
} else {
*country = NULL;
}
if (str[end] && str[end]!=':') { /* not at end of str */
++end;
}
/* get variant, if any */
start = end;
while (str[end] && str[end]!=':') {
++end;
}
if (start != end) {
int i;
int len = end - start;
- char *s = malloc(len + 1);
+ char *s = (char*)malloc(len + 1);
for (i=0; i<len; ++i) {
s[i] = str[start + i];
}
s[i] = '\0';
*variant = s;
} else {
*variant = NULL;
}
}
static int
accumulate_locstring(const char *str, FL_Locale *l) {
char *lang = NULL;
char *country = NULL;
char *variant = NULL;
if (str) {
lang_country_variant_from_envstring(str, &lang, &country, &variant);
if (lang) {
l->lang = lang;
l->country = country;
l->variant = variant;
return 1;
}
}
free(lang); free(country); free(variant);
return 0;
}
static int
accumulate_env(const char *name, FL_Locale *l) {
char *env;
char *lang = NULL;
char *country = NULL;
char *variant = NULL;
env = getenv(name);
if (env) {
return accumulate_locstring(env, l);
}
free(lang); free(country); free(variant);
return 0;
}
static void
canonise_fl(FL_Locale *l) {
/* this function fixes some common locale-specifying mistakes */
/* en_UK -> en_GB */
if (l->lang && 0 == strcmp(l->lang, "en")) {
if (l->country && 0 == strcmp(l->country, "UK")) {
free((void*)l->country);
l->country = strdup("GB");
}
}
/* ja_JA -> ja_JP */
if (l->lang && 0 == strcmp(l->lang, "ja")) {
if (l->country && 0 == strcmp(l->country, "JA")) {
free((void*)l->country);
l->country = strdup("JP");
}
}
}
#ifdef WIN32
#include <stdio.h>
#define ML(pn,sn) MAKELANGID(LANG_##pn, SUBLANG_##pn##_##sn)
#define MLN(pn) MAKELANGID(LANG_##pn, SUBLANG_DEFAULT)
#define RML(pn,sn) MAKELANGID(LANG_##pn, SUBLANG_##sn)
typedef struct {
LANGID id;
char* code;
} IDToCode;
static const IDToCode both_to_code[] = {
{ML(ENGLISH,US), "en_US.ISO_8859-1"},
{ML(ENGLISH,CAN), "en_CA"}, /* english / canadian */
{ML(ENGLISH,UK), "en_GB"},
{ML(ENGLISH,EIRE), "en_IE"},
{ML(ENGLISH,AUS), "en_AU"},
{MLN(GERMAN), "de_DE"},
{MLN(SPANISH), "es_ES"},
{ML(SPANISH,MEXICAN), "es_MX"},
{MLN(FRENCH), "fr_FR"},
{ML(FRENCH,CANADIAN), "fr_CA"},
{ML(FRENCH,BELGIAN), "fr_BE"}, /* ? */
{ML(DUTCH,BELGIAN), "nl_BE"}, /* ? */
{ML(PORTUGUESE,BRAZILIAN), "pt_BR"},
{MLN(PORTUGUESE), "pt_PT"},
{MLN(SWEDISH), "sv_SE"},
{ML(CHINESE,HONGKONG), "zh_HK"},
/* these are machine-generated and not yet verified */
{RML(AFRIKAANS,DEFAULT), "af_ZA"},
{RML(ALBANIAN,DEFAULT), "sq_AL"},
{RML(ARABIC,ARABIC_ALGERIA), "ar_DZ"},
{RML(ARABIC,ARABIC_BAHRAIN), "ar_BH"},
{RML(ARABIC,ARABIC_EGYPT), "ar_EG"},
{RML(ARABIC,ARABIC_IRAQ), "ar_IQ"},
{RML(ARABIC,ARABIC_JORDAN), "ar_JO"},
{RML(ARABIC,ARABIC_KUWAIT), "ar_KW"},
{RML(ARABIC,ARABIC_LEBANON), "ar_LB"},
{RML(ARABIC,ARABIC_LIBYA), "ar_LY"},
{RML(ARABIC,ARABIC_MOROCCO), "ar_MA"},
{RML(ARABIC,ARABIC_OMAN), "ar_OM"},
{RML(ARABIC,ARABIC_QATAR), "ar_QA"},
{RML(ARABIC,ARABIC_SAUDI_ARABIA), "ar_SA"},
{RML(ARABIC,ARABIC_SYRIA), "ar_SY"},
{RML(ARABIC,ARABIC_TUNISIA), "ar_TN"},
{RML(ARABIC,ARABIC_UAE), "ar_AE"},
{RML(ARABIC,ARABIC_YEMEN), "ar_YE"},
{RML(ARMENIAN,DEFAULT), "hy_AM"},
{RML(AZERI,AZERI_CYRILLIC), "az_AZ"},
{RML(AZERI,AZERI_LATIN), "az_AZ"},
{RML(BASQUE,DEFAULT), "eu_ES"},
{RML(BELARUSIAN,DEFAULT), "be_BY"},
/*{RML(BRETON,DEFAULT), "br_FR"},*/
{RML(BULGARIAN,DEFAULT), "bg_BG"},
{RML(CATALAN,DEFAULT), "ca_ES"},
{RML(CHINESE,CHINESE_HONGKONG), "zh_HK"},
{RML(CHINESE,CHINESE_MACAU), "zh_MO"},
{RML(CHINESE,CHINESE_SIMPLIFIED), "zh_CN"},
{RML(CHINESE,CHINESE_SINGAPORE), "zh_SG"},
{RML(CHINESE,CHINESE_TRADITIONAL), "zh_TW"},
/*{RML(CORNISH,DEFAULT), "kw_GB"},*/
{RML(CZECH,DEFAULT), "cs_CZ"},
{RML(DANISH,DEFAULT), "da_DK"},
{RML(DUTCH,DUTCH), "nl_NL"},
{RML(DUTCH,DUTCH_BELGIAN), "nl_BE"},
/*{RML(DUTCH,DUTCH_SURINAM), "nl_SR"},*/
{RML(ENGLISH,ENGLISH_AUS), "en_AU"},
{RML(ENGLISH,ENGLISH_BELIZE), "en_BZ"},
{RML(ENGLISH,ENGLISH_CAN), "en_CA"},
{RML(ENGLISH,ENGLISH_CARIBBEAN), "en_CB"},
{RML(ENGLISH,ENGLISH_EIRE), "en_IE"},
{RML(ENGLISH,ENGLISH_JAMAICA), "en_JM"},
{RML(ENGLISH,ENGLISH_NZ), "en_NZ"},
{RML(ENGLISH,ENGLISH_PHILIPPINES), "en_PH"},
{RML(ENGLISH,ENGLISH_SOUTH_AFRICA), "en_ZA"},
{RML(ENGLISH,ENGLISH_TRINIDAD), "en_TT"},
{RML(ENGLISH,ENGLISH_UK), "en_GB"},
{RML(ENGLISH,ENGLISH_US), "en_US"},
{RML(ENGLISH,ENGLISH_ZIMBABWE), "en_ZW"},
/*{RML(ESPERANTO,DEFAULT), "eo_"},*/
{RML(ESTONIAN,DEFAULT), "et_EE"},
{RML(FAEROESE,DEFAULT), "fo_FO"},
{RML(FARSI,DEFAULT), "fa_IR"},
{RML(FINNISH,DEFAULT), "fi_FI"},
{RML(FRENCH,FRENCH), "fr_FR"},
{RML(FRENCH,FRENCH_BELGIAN), "fr_BE"},
{RML(FRENCH,FRENCH_CANADIAN), "fr_CA"},
{RML(FRENCH,FRENCH_LUXEMBOURG), "fr_LU"},
{RML(FRENCH,FRENCH_MONACO), "fr_MC"},
{RML(FRENCH,FRENCH_SWISS), "fr_CH"},
/*{RML(GAELIC,GAELIC), "ga_IE"},*/
/*{RML(GAELIC,GAELIC_MANX), "gv_GB"},*/
/*{RML(GAELIC,GAELIC_SCOTTISH), "gd_GB"},*/
/*{RML(GALICIAN,DEFAULT), "gl_ES"},*/
{RML(GEORGIAN,DEFAULT), "ka_GE"},
{RML(GERMAN,GERMAN), "de_DE"},
{RML(GERMAN,GERMAN_AUSTRIAN), "de_AT"},
{RML(GERMAN,GERMAN_LIECHTENSTEIN), "de_LI"},
{RML(GERMAN,GERMAN_LUXEMBOURG), "de_LU"},
{RML(GERMAN,GERMAN_SWISS), "de_CH"},
{RML(GREEK,DEFAULT), "el_GR"},
{RML(GUJARATI,DEFAULT), "gu_IN"},
{RML(HEBREW,DEFAULT), "he_IL"},
{RML(HINDI,DEFAULT), "hi_IN"},
{RML(HUNGARIAN,DEFAULT), "hu_HU"},
{RML(ICELANDIC,DEFAULT), "is_IS"},
{RML(INDONESIAN,DEFAULT), "id_ID"},
{RML(ITALIAN,ITALIAN), "it_IT"},
{RML(ITALIAN,ITALIAN_SWISS), "it_CH"},
{RML(JAPANESE,DEFAULT), "ja_JP"},
{RML(KANNADA,DEFAULT), "kn_IN"},
{RML(KAZAK,DEFAULT), "kk_KZ"},
{RML(KONKANI,DEFAULT), "kok_IN"},
{RML(KOREAN,KOREAN), "ko_KR"},
/*{RML(KYRGYZ,DEFAULT), "ky_KG"},*/
{RML(LATVIAN,DEFAULT), "lv_LV"},
{RML(LITHUANIAN,LITHUANIAN), "lt_LT"},
{RML(MACEDONIAN,DEFAULT), "mk_MK"},
{RML(MALAY,MALAY_BRUNEI_DARUSSALAM), "ms_BN"},
{RML(MALAY,MALAY_MALAYSIA), "ms_MY"},
{RML(MARATHI,DEFAULT), "mr_IN"},
/*{RML(MONGOLIAN,DEFAULT), "mn_MN"},*/
{RML(NORWEGIAN,NORWEGIAN_BOKMAL), "nb_NO"},
{RML(NORWEGIAN,NORWEGIAN_NYNORSK), "nn_NO"},
{RML(POLISH,DEFAULT), "pl_PL"},
{RML(PORTUGUESE,PORTUGUESE), "pt_PT"},
{RML(PORTUGUESE,PORTUGUESE_BRAZILIAN), "pt_BR"},
{RML(PUNJABI,DEFAULT), "pa_IN"},
{RML(ROMANIAN,DEFAULT), "ro_RO"},
{RML(RUSSIAN,DEFAULT), "ru_RU"},
{RML(SANSKRIT,DEFAULT), "sa_IN"},
{RML(SERBIAN,DEFAULT), "hr_HR"},
{RML(SERBIAN,SERBIAN_CYRILLIC), "sr_SP"},
{RML(SERBIAN,SERBIAN_LATIN), "sr_SP"},
{RML(SLOVAK,DEFAULT), "sk_SK"},
{RML(SLOVENIAN,DEFAULT), "sl_SI"},
{RML(SPANISH,SPANISH), "es_ES"},
{RML(SPANISH,SPANISH_ARGENTINA), "es_AR"},
{RML(SPANISH,SPANISH_BOLIVIA), "es_BO"},
{RML(SPANISH,SPANISH_CHILE), "es_CL"},
{RML(SPANISH,SPANISH_COLOMBIA), "es_CO"},
{RML(SPANISH,SPANISH_COSTA_RICA), "es_CR"},
{RML(SPANISH,SPANISH_DOMINICAN_REPUBLIC), "es_DO"},
{RML(SPANISH,SPANISH_ECUADOR), "es_EC"},
{RML(SPANISH,SPANISH_EL_SALVADOR), "es_SV"},
{RML(SPANISH,SPANISH_GUATEMALA), "es_GT"},
{RML(SPANISH,SPANISH_HONDURAS), "es_HN"},
{RML(SPANISH,SPANISH_MEXICAN), "es_MX"},
{RML(SPANISH,SPANISH_MODERN), "es_ES"},
{RML(SPANISH,SPANISH_NICARAGUA), "es_NI"},
{RML(SPANISH,SPANISH_PANAMA), "es_PA"},
{RML(SPANISH,SPANISH_PARAGUAY), "es_PY"},
{RML(SPANISH,SPANISH_PERU), "es_PE"},
{RML(SPANISH,SPANISH_PUERTO_RICO), "es_PR"},
{RML(SPANISH,SPANISH_URUGUAY), "es_UY"},
{RML(SPANISH,SPANISH_VENEZUELA), "es_VE"},
{RML(SWAHILI,DEFAULT), "sw_KE"},
{RML(SWEDISH,SWEDISH), "sv_SE"},
{RML(SWEDISH,SWEDISH_FINLAND), "sv_FI"},
/*{RML(SYRIAC,DEFAULT), "syr_SY"},*/
{RML(TAMIL,DEFAULT), "ta_IN"},
{RML(TATAR,DEFAULT), "tt_TA"},
{RML(TELUGU,DEFAULT), "te_IN"},
{RML(THAI,DEFAULT), "th_TH"},
{RML(TURKISH,DEFAULT), "tr_TR"},
{RML(UKRAINIAN,DEFAULT), "uk_UA"},
{RML(URDU,URDU_PAKISTAN), "ur_PK"},
{RML(UZBEK,UZBEK_CYRILLIC), "uz_UZ"},
{RML(UZBEK,UZBEK_LATIN), "uz_UZ"},
{RML(VIETNAMESE,DEFAULT), "vi_VN"},
/*{RML(WALON,DEFAULT), "wa_BE"},*/
/*{RML(WELSH,DEFAULT), "cy_GB"},*/
};
static const IDToCode primary_to_code[] = {
{LANG_AFRIKAANS, "af"},
{LANG_ARABIC, "ar"},
{LANG_AZERI, "az"},
{LANG_BULGARIAN, "bg"},
/*{LANG_BRETON, "br"},*/
{LANG_BELARUSIAN, "by"},
{LANG_CATALAN, "ca"},
{LANG_CZECH, "cs"},
/*{LANG_WELSH, "cy"},*/
{LANG_DANISH, "da"},
{LANG_GERMAN, "de"},
{LANG_GREEK, "el"},
{LANG_ENGLISH, "en"},
/*{LANG_ESPERANTO, "eo"},*/
{LANG_SPANISH, "es"},
{LANG_ESTONIAN, "et"},
{LANG_BASQUE, "eu"},
{LANG_FARSI, "fa"},
{LANG_FINNISH, "fi"},
{LANG_FAEROESE, "fo"},
{LANG_FRENCH, "fr"},
/*{LANG_GAELIC, "ga"},*/
/*{LANG_GALICIAN, "gl"},*/
{LANG_GUJARATI, "gu"},
{LANG_HEBREW, "he"},
{LANG_HINDI, "hi"},
{LANG_SERBIAN, "hr"},
{LANG_HUNGARIAN, "hu"},
{LANG_ARMENIAN, "hy"},
{LANG_INDONESIAN, "id"},
{LANG_ITALIAN, "it"},
{LANG_JAPANESE, "ja"},
{LANG_GEORGIAN, "ka"},
{LANG_KAZAK, "kk"},
{LANG_KANNADA, "kn"},
{LANG_KOREAN, "ko"},
/*{LANG_KYRGYZ, "ky"},*/
{LANG_LITHUANIAN, "lt"},
{LANG_LATVIAN, "lv"},
{LANG_MACEDONIAN, "mk"},
/*{LANG_MONGOLIAN, "mn"},*/
{LANG_MARATHI, "mr"},
{LANG_MALAY, "ms"},
{LANG_NORWEGIAN, "nb"},
{LANG_DUTCH, "nl"},
{LANG_NORWEGIAN, "nn"},
{LANG_NORWEGIAN, "no"},/* unofficial? */
{LANG_PUNJABI, "pa"},
{LANG_POLISH, "pl"},
{LANG_PORTUGUESE, "pt"},
{LANG_ROMANIAN, "ro"},
{LANG_RUSSIAN, "ru"},
{LANG_SLOVAK, "sk"},
{LANG_SLOVENIAN, "sl"},
{LANG_ALBANIAN, "sq"},
{LANG_SERBIAN, "sr"},
{LANG_SWEDISH, "sv"},
{LANG_SWAHILI, "sw"},
{LANG_TAMIL, "ta"},
{LANG_THAI, "th"},
{LANG_TURKISH, "tr"},
{LANG_TATAR, "tt"},
{LANG_UKRAINIAN, "uk"},
{LANG_URDU, "ur"},
{LANG_UZBEK, "uz"},
{LANG_VIETNAMESE, "vi"},
/*{LANG_WALON, "wa"},*/
{LANG_CHINESE, "zh"},
};
static int num_primary_to_code =
sizeof(primary_to_code) / sizeof(*primary_to_code);
static int num_both_to_code =
sizeof(both_to_code) / sizeof(*both_to_code);
static const int
lcid_to_fl(LCID lcid,
FL_Locale *rtn) {
LANGID langid = LANGIDFROMLCID(lcid);
LANGID primary_lang = PRIMARYLANGID(langid);
LANGID sub_lang = SUBLANGID(langid);
int i;
/* try to find an exact primary/sublanguage combo that we know about */
for (i=0; i<num_both_to_code; ++i) {
if (both_to_code[i].id == langid) {
accumulate_locstring(both_to_code[i].code, rtn);
return 1;
}
}
/* fallback to just checking the primary language id */
for (i=0; i<num_primary_to_code; ++i) {
if (primary_to_code[i].id == primary_lang) {
accumulate_locstring(primary_to_code[i].code, rtn);
return 1;
}
}
return 0;
}
#endif
FL_Success
FL_FindLocale(FL_Locale **locale, FL_Domain domain) {
FL_Success success = FL_FAILED;
- FL_Locale *rtn = malloc(sizeof(FL_Locale));
+ FL_Locale *rtn = (FL_Locale*)malloc(sizeof(FL_Locale));
rtn->lang = NULL;
rtn->country = NULL;
rtn->variant = NULL;
#ifdef WIN32
/* win32 >= mswindows95 */
{
LCID lcid = GetThreadLocale();
if (lcid_to_fl(lcid, rtn)) {
success = FL_CONFIDENT;
}
if (success == FL_FAILED) {
/* assume US English on mswindows systems unless we know otherwise */
if (accumulate_locstring("en_US.ISO_8859-1", rtn)) {
success = FL_DEFAULT_GUESS;
}
}
}
#else
/* assume unixoid */
{
/* examples: */
/* sv_SE.ISO_8859-1 */
/* fr_FR.ISO8859-1 */
/* no_NO_NB */
/* no_NO_NY */
/* no_NO */
/* de_DE */
/* try the various vars in decreasing order of authority */
if (accumulate_env("LC_ALL", rtn) ||
accumulate_env("LC_MESSAGES", rtn) ||
accumulate_env("LANG", rtn) ||
accumulate_env("LANGUAGE", rtn)) {
success = FL_CONFIDENT;
}
if (success == FL_FAILED) {
/* assume US English on unixoid systems unless we know otherwise */
if (accumulate_locstring("en_US.ISO_8859-1", rtn)) {
success = FL_DEFAULT_GUESS;
}
}
}
#endif
if (success != FL_FAILED) {
canonise_fl(rtn);
}
*locale = rtn;
return success;
}
void
FL_FreeLocale(FL_Locale **locale) {
if (locale) {
FL_Locale *l = *locale;
if (l) {
if (l->lang) {
free((void*)l->lang);
}
if (l->country) {
free((void*)l->country);
}
if (l->variant) {
free((void*)l->variant);
}
free(l);
*locale = NULL;
}
}
}
diff --git a/src/libs/tinygettext/dictionary_manager.cpp b/src/libs/tinygettext/dictionary_manager.cpp
index 9e3fb5d..8742f3f 100644
--- a/src/libs/tinygettext/dictionary_manager.cpp
+++ b/src/libs/tinygettext/dictionary_manager.cpp
@@ -1,250 +1,251 @@
// tinygettext - A gettext replacement that works directly on .po files
// Copyright (C) 2006 Ingo Ruhnke <grumbel@gmx.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "dictionary_manager.hpp"
#include <memory>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <algorithm>
#include "log_stream.hpp"
#include "po_parser.hpp"
#include "unix_file_system.hpp"
+//FIXME: Because I'm too aggressive, I disabled C++ exceptions when compiling for Android :|
#if defined(ANDROID)
#define NO_EXCEPTIONS
#endif
namespace tinygettext {
static bool has_suffix(const std::string& lhs, const std::string rhs)
{
if (lhs.length() < rhs.length())
return false;
else
return lhs.compare(lhs.length() - rhs.length(), rhs.length(), rhs) == 0;
}
DictionaryManager::DictionaryManager(const std::string& charset_) :
dictionaries(),
search_path(),
charset(charset_),
use_fuzzy(true),
current_language(),
current_dict(0),
empty_dict(),
filesystem(new UnixFileSystem)
{
}
DictionaryManager::~DictionaryManager()
{
for(Dictionaries::iterator i = dictionaries.begin(); i != dictionaries.end(); ++i)
{
delete i->second;
}
}
void
DictionaryManager::clear_cache()
{
for(Dictionaries::iterator i = dictionaries.begin(); i != dictionaries.end(); ++i)
{
delete i->second;
}
dictionaries.clear();
current_dict = 0;
}
Dictionary&
DictionaryManager::get_dictionary()
{
if (current_dict)
{
return *current_dict;
}
else
{
if (current_language)
{
current_dict = &get_dictionary(current_language);
return *current_dict;
}
else
{
return empty_dict;
}
}
}
Dictionary&
DictionaryManager::get_dictionary(const Language& language)
{
//log_debug << "Dictionary for language \"" << spec << "\" requested" << std::endl;
//log_debug << "...normalized as \"" << lang << "\"" << std::endl;
assert(language);
Dictionaries::iterator i = dictionaries.find(language);
if (i != dictionaries.end())
{
return *i->second;
}
else // Dictionary for languages lang isn't loaded, so we load it
{
//log_debug << "get_dictionary: " << lang << std::endl;
Dictionary* dict = new Dictionary(charset);
dictionaries[language] = dict;
for (SearchPath::reverse_iterator p = search_path.rbegin(); p != search_path.rend(); ++p)
{
std::vector<std::string> files = filesystem->open_directory(*p);
std::string best_filename;
int best_score = 0;
for(std::vector<std::string>::iterator filename = files.begin(); filename != files.end(); filename++)
{
// check if filename matches requested language
if (has_suffix(*filename, ".po"))
{ // ignore anything that isn't a .po file
Language po_language = Language::from_env(filename->substr(0, filename->size()-3));
if (!po_language)
{
log_warning << *filename << ": warning: ignoring, unknown language" << std::endl;
}
else
{
int score = Language::match(language, po_language);
if (score > best_score)
{
best_score = score;
best_filename = *filename;
}
}
}
}
if (!best_filename.empty())
{
std::string pofile = *p + "/" + best_filename;
#ifndef NO_EXCEPTIONS
try
{
#endif
std::auto_ptr<std::istream> in = filesystem->open_file(pofile);
if (!in.get())
{
log_error << "error: failure opening: " << pofile << std::endl;
}
else
{
POParser::parse(pofile, *in, *dict);
}
#ifndef NO_EXCEPTIONS
}
catch(std::exception& e)
{
log_error << "error: failure parsing: " << pofile << std::endl;
log_error << e.what() << "" << std::endl;
}
#endif
}
}
return *dict;
}
}
std::set<Language>
DictionaryManager::get_languages()
{
std::set<Language> languages;
for (SearchPath::iterator p = search_path.begin(); p != search_path.end(); ++p)
{
std::vector<std::string> files = filesystem->open_directory(*p);
for(std::vector<std::string>::iterator file = files.begin(); file != files.end(); ++file)
{
if (has_suffix(*file, ".po"))
{
languages.insert(Language::from_env(file->substr(0, file->size()-3)));
}
}
}
return languages;
}
void
DictionaryManager::set_language(const Language& language)
{
if (current_language != language)
{
current_language = language;
current_dict = 0;
}
}
Language
DictionaryManager::get_language() const
{
return current_language;
}
void
DictionaryManager::set_charset(const std::string& charset_)
{
clear_cache(); // changing charset invalidates cache
charset = charset_;
}
void
DictionaryManager::set_use_fuzzy(bool t)
{
clear_cache();
use_fuzzy = t;
}
bool
DictionaryManager::get_use_fuzzy() const
{
return use_fuzzy;
}
void
DictionaryManager::add_directory(const std::string& pathname)
{
clear_cache(); // adding directories invalidates cache
search_path.push_back(pathname);
}
void
DictionaryManager::set_filesystem(std::auto_ptr<FileSystem> filesystem_)
{
filesystem = filesystem_;
}
} // namespace tinygettext
/* EOF */
diff --git a/src/libs/tinygettext/po_parser.cpp b/src/libs/tinygettext/po_parser.cpp
index 1a4dc0a..f489891 100644
--- a/src/libs/tinygettext/po_parser.cpp
+++ b/src/libs/tinygettext/po_parser.cpp
@@ -1,505 +1,506 @@
// tinygettext - A gettext replacement that works directly on .po files
// Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "po_parser.hpp"
#include <iostream>
#include <ctype.h>
#include <string>
#include <istream>
#include <string.h>
#include <map>
#include <stdlib.h>
#include "language.hpp"
#include "log_stream.hpp"
#include "dictionary.hpp"
#include "plural_forms.hpp"
+//FIXME: Because I'm too aggressive, I disabled C++ exceptions when compiling for Android :|
#if defined(ANDROID)
#define NO_EXCEPTIONS
#endif
namespace tinygettext {
bool POParser::pedantic = true;
void
POParser::parse(const std::string& filename, std::istream& in, Dictionary& dict)
{
POParser parser(filename, in, dict);
parser.parse();
}
class POParserError {};
POParser::POParser(const std::string& filename_, std::istream& in_, Dictionary& dict_, bool use_fuzzy_) :
filename(filename_),
in(in_),
dict(dict_),
use_fuzzy(use_fuzzy_),
running(false),
eof(false),
big5(false),
line_number(0),
current_line()//,
//conv()
{
}
POParser::~POParser()
{
}
void
POParser::warning(const std::string& msg)
{
log_warning << filename << ":" << line_number << ": warning: " << msg << ": " << current_line << std::endl;
//log_warning << "Line: " << current_line << std::endl;
}
void
POParser::error(const std::string& msg)
{
log_error << filename << ":" << line_number << ": error: " << msg << ": " << current_line << std::endl;
// Try to recover from an error by searching for start of another entry
do
next_line();
while(!eof && !is_empty_line());
#ifndef NO_EXCEPTIONS
throw POParserError();
#endif
}
void
POParser::next_line()
{
line_number += 1;
if (!std::getline(in, current_line))
eof = true;
}
void
POParser::get_string_line(std::ostringstream& out,unsigned int skip)
{
if (skip+1 >= static_cast<unsigned int>(current_line.size()))
error("unexpected end of line");
if (current_line[skip] != '"')
error("expected start of string '\"'");
std::string::size_type i;
for(i = skip+1; current_line[i] != '\"'; ++i)
{
if (big5 && static_cast<unsigned char>(current_line[i]) >= 0x81 && static_cast<unsigned char>(current_line[i]) <= 0xfe)
{
out << current_line[i];
i += 1;
if (i >= current_line.size())
error("invalid big5 encoding");
out << current_line[i];
}
else if (i >= current_line.size())
{
error("unexpected end of string");
}
else if (current_line[i] == '\\')
{
i += 1;
if (i >= current_line.size())
error("unexpected end of string in handling '\\'");
switch (current_line[i])
{
case 'a': out << '\a'; break;
case 'b': out << '\b'; break;
case 'v': out << '\v'; break;
case 'n': out << '\n'; break;
case 't': out << '\t'; break;
case 'r': out << '\r'; break;
case '"': out << '"'; break;
case '\\': out << '\\'; break;
default:
std::ostringstream err;
err << "unhandled escape '\\" << current_line[i] << "'";
warning(err.str());
out << current_line[i-1] << current_line[i];
break;
}
}
else
{
out << current_line[i];
}
}
// process trailing garbage in line and warn if there is any
for(i = i+1; i < current_line.size(); ++i)
if (!isspace(current_line[i]))
{
warning("unexpected garbage after string ignoren");
break;
}
}
std::string
POParser::get_string(unsigned int skip)
{
std::ostringstream out;
if (skip+1 >= static_cast<unsigned int>(current_line.size()))
error("unexpected end of line");
if (current_line[skip] == ' ' && current_line[skip+1] == '"')
{
get_string_line(out, skip+1);
}
else
{
if (pedantic)
warning("keyword and string must be separated by a single space");
for(;;)
{
if (skip >= static_cast<unsigned int>(current_line.size()))
error("unexpected end of line");
else if (current_line[skip] == '\"')
{
get_string_line(out, skip);
break;
}
else if (!isspace(current_line[skip]))
{
error("string must start with '\"'");
}
else
{
// skip space
}
skip += 1;
}
}
next:
next_line();
for(std::string::size_type i = 0; i < current_line.size(); ++i)
{
if (current_line[i] == '"')
{
if (i == 1)
if (pedantic)
warning("leading whitespace before string");
get_string_line(out, i);
goto next;
}
else if (isspace(current_line[i]))
{
// skip
}
else
{
break;
}
}
return out.str();
}
static bool has_prefix(const std::string& lhs, const std::string rhs)
{
if (lhs.length() < rhs.length())
return false;
else
return lhs.compare(0, rhs.length(), rhs) == 0;
}
void
POParser::parse_header(const std::string& header)
{
std::string from_charset;
std::string::size_type start = 0;
for(std::string::size_type i = 0; i < header.length(); ++i)
{
if (header[i] == '\n')
{
std::string line = header.substr(start, i - start);
if (has_prefix(line, "Content-Type:"))
{
// from_charset = line.substr(len);
unsigned int len = strlen("Content-Type: text/plain; charset=");
if (line.compare(0, len, "Content-Type: text/plain; charset=") == 0)
{
from_charset = line.substr(len);
for(std::string::iterator ch = from_charset.begin(); ch != from_charset.end(); ++ch)
*ch = static_cast<char>(toupper(*ch));
}
else
{
warning("malformed Content-Type header");
}
}
else if (has_prefix(line, "Plural-Forms:"))
{
PluralForms plural_forms = PluralForms::from_string(line);
if (!plural_forms)
{
warning("unknown Plural-Forms given");
}
else
{
if (!dict.get_plural_forms())
{
dict.set_plural_forms(plural_forms);
}
else
{
if (dict.get_plural_forms() != plural_forms)
{
warning("Plural-Forms missmatch between .po file and dictionary");
}
}
}
}
start = i+1;
}
}
if (from_charset.empty() || from_charset == "CHARSET")
{
warning("charset not specified for .po, fallback to utf-8");
from_charset = "UTF-8";
}
else if (from_charset == "BIG5")
{
big5 = true;
}
//conv.set_charsets(from_charset, dict.get_charset());
}
bool
POParser::is_empty_line()
{
if (current_line.empty())
{
return true;
}
else if (current_line[0] == '#')
{ // handle comments as empty lines
if (current_line.size() == 1 || (current_line.size() >= 2 && isspace(current_line[1])))
return true;
else
return false;
}
else
{
for(std::string::iterator i = current_line.begin(); i != current_line.end(); ++i)
{
if (!isspace(*i))
return false;
}
}
return true;
}
bool
POParser::prefix(const char* prefix_str)
{
return current_line.compare(0, strlen(prefix_str), prefix_str) == 0;
}
void
POParser::parse()
{
next_line();
// skip UTF-8 intro that some text editors produce
// see http://en.wikipedia.org/wiki/Byte-order_mark
if (current_line.size() >= 3 &&
current_line[0] == static_cast<unsigned char>(0xef) &&
current_line[1] == static_cast<unsigned char>(0xbb) &&
current_line[2] == static_cast<unsigned char>(0xbf))
{
current_line = current_line.substr(3);
}
// Parser structure
while(!eof)
{
#ifndef NO_EXCEPTIONS
try
{
#endif
bool fuzzy = false;
bool has_msgctxt = false;
std::string msgctxt;
std::string msgid;
while(prefix("#"))
{
if (current_line.size() >= 2 && current_line[1] == ',')
{
// FIXME: Rather simplistic hunt for fuzzy flag
if (current_line.find("fuzzy", 2) != std::string::npos)
fuzzy = true;
}
next_line();
}
if (!is_empty_line())
{
if (prefix("msgctxt"))
{
has_msgctxt = true;
msgctxt = get_string(7);
}
if (prefix("msgid"))
msgid = get_string(5);
else
error("expected 'msgid'");
if (prefix("msgid_plural"))
{
std::string msgid_plural = get_string(12);
std::vector<std::string> msgstr_num;
bool saw_nonempty_msgstr = false;
next:
if (is_empty_line())
{
if (msgstr_num.empty())
error("expected 'msgstr[N] (0 <= N <= 9)'");
}
else if (prefix("msgstr[") &&
current_line.size() > 8 &&
isdigit(current_line[7]) && current_line[8] == ']')
{
unsigned int number = static_cast<unsigned int>(current_line[7] - '0');
std::string msgstr = get_string(9);
if(!msgstr.empty())
saw_nonempty_msgstr = true;
if (number >= msgstr_num.size())
msgstr_num.resize(number+1);
msgstr_num[number] = msgstr; //conv.convert(msgstr);
goto next;
}
else
{
error("expected 'msgstr[N]'");
}
if (!is_empty_line())
error("expected 'msgstr[N]' or empty line");
if (saw_nonempty_msgstr)
{
if (use_fuzzy || !fuzzy)
{
if (!dict.get_plural_forms())
{
warning("msgstr[N] seen, but no Plural-Forms given");
}
else
{
if (msgstr_num.size() != dict.get_plural_forms().get_nplural())
{
warning("msgstr[N] count doesn't match Plural-Forms.nplural");
}
}
if (has_msgctxt)
dict.add_translation(msgctxt, msgid, msgid_plural, msgstr_num);
else
dict.add_translation(msgid, msgid_plural, msgstr_num);
}
if (0)
{
std::cout << (fuzzy?"fuzzy":"not-fuzzy") << std::endl;
std::cout << "msgid \"" << msgid << "\"" << std::endl;
std::cout << "msgid_plural \"" << msgid_plural << "\"" << std::endl;
for(std::vector<std::string>::size_type i = 0; i < msgstr_num.size(); ++i)
std::cout << "msgstr[" << i << "] \"" << msgstr_num[i] /*conv.convert(msgstr_num[i])*/ << "\"" << std::endl;
std::cout << std::endl;
}
}
}
else if (prefix("msgstr"))
{
std::string msgstr = get_string(6);
if (msgid.empty())
{
parse_header(msgstr);
}
else if(!msgstr.empty())
{
if (use_fuzzy || !fuzzy)
{
if (has_msgctxt)
dict.add_translation(msgctxt, msgid, msgstr /*conv.convert(msgstr)*/);
else
dict.add_translation(msgid, msgstr /*conv.convert(msgstr)*/);
}
if (0)
{
std::cout << (fuzzy?"fuzzy":"not-fuzzy") << std::endl;
std::cout << "msgid \"" << msgid << "\"" << std::endl;
std::cout << "msgstr \"" << msgstr /*conv.convert(msgstr)*/ << "\"" << std::endl;
std::cout << std::endl;
}
}
}
else
{
error("expected 'msgstr' or 'msgid_plural'");
}
}
if (!is_empty_line())
error("expected empty line");
next_line();
#ifndef NO_EXCEPTIONS
}
catch(POParserError&)
{
}
#endif
}
}
} // namespace tinygettext
/* EOF */
diff --git a/src/libs/tinygettext/po_parser.hpp b/src/libs/tinygettext/po_parser.hpp
index c56e435..6fa7c7c 100644
--- a/src/libs/tinygettext/po_parser.hpp
+++ b/src/libs/tinygettext/po_parser.hpp
@@ -1,80 +1,81 @@
// tinygettext - A gettext replacement that works directly on .po files
// Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef HEADER_TINYGETTEXT_PO_PARSER_HPP
#define HEADER_TINYGETTEXT_PO_PARSER_HPP
#include <iosfwd>
#include <string>
//#include "iconv.hpp"
namespace tinygettext {
class Dictionary;
class POParser
{
private:
std::string filename;
std::istream& in;
Dictionary& dict;
bool use_fuzzy;
bool running;
bool eof;
bool big5;
int line_number;
std::string current_line;
//IConv conv;
POParser(const std::string& filename, std::istream& in_, Dictionary& dict_, bool use_fuzzy = true);
~POParser();
void parse_header(const std::string& header);
void parse();
void next_line();
std::string get_string(unsigned int skip);
void get_string_line(std::ostringstream& str,unsigned int skip);
bool is_empty_line();
bool prefix(const char* );
+ //FIXME: Because I'm too aggressive, I disabled C++ exceptions when compiling for Android :|
#if defined(WIN32) || defined(ANDROID)
void error(const std::string& msg);
#else
void error(const std::string& msg) __attribute__((__noreturn__));
#endif
void warning(const std::string& msg);
public:
/** @param filename name of the istream, only used in error messages
@param in stream from which the PO file is read.
@param dict dictionary to which the strings are written */
static void parse(const std::string& filename, std::istream& in, Dictionary& dict);
static bool pedantic;
private:
POParser (const POParser&);
POParser& operator= (const POParser&);
};
} // namespace tinygettext
#endif
/* EOF */
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, May 15, 11:01 PM (3 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
63985
Default Alt Text
(93 KB)
Attached To
Mode
R79 meandmyshadow
Attached
Detach File
Event Timeline