1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/* Copyright (C) 2004 MySQL AB
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 */
/**
* @file myx_gc_utilites.cpp
* @brief Some common utility functions.
*
*/
#ifdef _WINDOWS
#include <windows.h>
#include <direct.h>
#else
#endif // ifdef _WINDOWS
#include "myx_gc_utilities.h"
#include "glib.h"
//----------------------------------------------------------------------------------------------------------------------
/**
* @brief Converts the given string, which is supposed to be an UTF-8 encoded text into an UTF-16 string.
* @param Source Contains the source string encoded in UTF-8.
* @return The converted string in UTF-16 encoding.
*/
wstring Utf8ToUtf16(const string& Source)
{
wstring Result;
#ifdef _WINDOWS
const char* Raw = Source.c_str();
int Size = MultiByteToWideChar(CP_UTF8, 0, Raw, -1, NULL, 0);
wchar_t* Buffer = new wchar_t[Size];
MultiByteToWideChar(CP_UTF8, 0, Raw, -1, Buffer, Size);
Result += Buffer;
delete[] Buffer;
#else
TODO: Conversion for non Windows platforms.
#endif
return Result;
}
//----------------------------------------------------------------------------------------------------------------------
/**
* @brief Converts the given string into an ANSI string.
* @param Source Contains the source string encoded in UTF-16.
* @return The converted string in ANSI encoding.
* @note The current user locale is used to convert the Unicode string to ANSI.
*/
string Utf16ToANSI(const wstring& Source)
{
string Result;
#ifdef _WINDOWS
const wchar_t* Raw = Source.c_str();
int Size = WideCharToMultiByte(CP_ACP, 0, Raw, -1, NULL, 0, NULL, NULL);
char* Buffer = new char[Size];
WideCharToMultiByte(CP_ACP, 0, Raw, -1, Buffer, Size, NULL, NULL);
Result += Buffer;
delete[] Buffer;
#else
TODO: Conversion for non Windows platforms.
#endif
return Result;
}
//----------------------------------------------------------------------------------------------------------------------
/**
* @brief Converts the given string, which is supposed to be an UTF-8 encoded text into an ANSI string.
* @param Source Contains the source string encoded in UTF-8.
* @return The converted string in ANSI encoding.
* @note The current user locale is used to convert the Unicode string to ANSI.
*/
string Utf8ToANSI(const string& Source)
{
return Utf16ToANSI(Utf8ToUtf16(Source));
}
//----------------------------------------------------------------------------------------------------------------------
/**
* @brief User defined file handling callback for handling file i/o in png lib.
* We have make it so because of the FILE allocation and png being in a DLL on Windows.
*/
void PNGRead(png_structp png_ptr, png_bytep data, png_size_t length)
{
fread(data, length, 1, (FILE*) png_ptr->io_ptr);
}
//----------------------------------------------------------------------------------------------------------------------
/**
* @brief Loads a png file.
* @param Filename An ANSI encoded file name (can contain path info) to the png file.
* @return Returns a pointer to a TImage structure containing the image data.
* @note The return memory must be freed using FreeImage().
*/
TImage* LoadPNG(const string& Filename)
{
TImage *Image = NULL;
FILE* File;
png_structp PNG;
png_infop Info;
png_uint_32 Width, Height;
int depth, junk, color_type;
int Offset;
File = fopen(Filename.c_str(), "rb");
try
{
if (File)
{
png_byte Magic[8];
if (fread(Magic, sizeof(Magic), 1, File) != 1)
{
fprintf(stderr, "can't read '%s': %s\n", Filename, strerror(errno));
fclose(File);
return NULL;
};
if (!png_check_sig(Magic, sizeof(Magic)))
{
fclose(File);
return NULL;
};
PNG = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!PNG)
{
fclose(File);
return NULL;
};
Info = png_create_info_struct(PNG);
if (!Info)
{
fclose(File);
png_destroy_read_struct(&PNG, NULL, NULL);
return NULL;
};
png_set_read_fn(PNG, File, &PNGRead);
png_init_io(PNG, File);
png_set_sig_bytes(PNG, sizeof(Magic));
png_read_info(PNG, Info);
png_get_IHDR(PNG, Info, &Width, &Height, &depth, &color_type,
&junk, &junk, &junk);
/* sanity check */
if (Width < 1 || Height < 1)
{
fclose(File);
png_destroy_read_struct(&PNG, &Info, NULL);
return NULL;
}
Image = (TImage*) malloc(sizeof(TImage));
if (!Image)
{
fclose(File);
png_destroy_read_struct(&PNG, &Info, NULL);
return NULL;
};
Image->Data = (unsigned char*) malloc(Width * Height * Info->channels);
if (!Image->Data)
{
fclose(File);
png_destroy_read_struct(&PNG, &Info, NULL);
return NULL;
};
Image->Width = Width;
Image->Height = Height;
Image->ColorType = (TColorType) Info->color_type;
Image->Channels = Info->channels;
// normalize to 8bpp with alpha channel
if (color_type == PNG_COLOR_TYPE_PALETTE && depth <= 8)
png_set_expand(PNG);
if (color_type == PNG_COLOR_TYPE_GRAY && depth <= 8)
png_set_expand(PNG);
if (png_get_valid(PNG, Info, PNG_INFO_tRNS))
png_set_expand(PNG);
if (depth == 16)
png_set_strip_16(PNG);
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(PNG);
/* do the transforms */
png_read_update_info(PNG, Info);
Offset= 0;
/* read Data */
for (unsigned int I = 0; I < Height; I++)
{
png_read_row(PNG, &Image->Data[Offset], NULL);
Offset += Info->channels * Width;
}
png_read_end(PNG, Info);
png_destroy_read_struct(&PNG, &Info, NULL);
fclose(File);
};
}
catch (...)
{
if (Image)
FreeImage(Image);
Image = NULL;
};
return Image;
}
//----------------------------------------------------------------------------------------------------------------------
void FreeImage(TImage* Image)
{
free(Image->Data);
free(Image);
}
//----------------------------------------------------------------------------------------------------------------------
/**
* @brief Returns the current working folder (ANSI encoded).
* @return The current working folder.
*/
string GetCurrentDir(void)
{
char Buffer[MAX_PATH];
getcwd(Buffer, sizeof(Buffer));
return string(Buffer);
}
//----------------------------------------------------------------------------------------------------------------------
/**
* @brief Sets the current working folder (folder name must be ANSI encoded).
* @param Folder The new folder to be set.
*/
void SetCurrentDir(const string& Folder)
{
chdir(Folder.c_str());
}
//----------------------------------------------------------------------------------------------------------------------
/**
* @brief ExtractFilePath extracts the drive and directory parts of the given filename. The resulting string is the
leftmost characters of FileName, up to and including the colon or backslash that separates the path information
from the name and extension. The resulting string is empty if FileName contains no drive and directory parts.
* @param Filename The file name (ANSI encoded) of which the path is to be extracted.
* @return The extracted path part (ANSI encoded).
*/
string ExtractFilePath(const string& Filename)
{
gchar* Raw = g_path_get_dirname(Filename.c_str());
string Result(Raw);
g_free(Raw);
return Result;
}
//----------------------------------------------------------------------------------------------------------------------
/**
* @brief Platform neutral file open routine.
* @param Filename The name of file encode in UTF-8.
* @param OpenMode The mode how to open the file (the same as used for fopen calls).
* @return A pointer to a FILE structure if the file could be opened, NULL otherwise.
*/
FILE* OpenFile(string Filename, const char* OpenMode)
{
#ifdef _WINDOWS
wstring Name = Utf8ToUtf16(Filename);
wstring Mode = Utf8ToUtf16(string(OpenMode));
return _wfopen(Name.c_str(), Mode.c_str());
#else
FILE *file;
char * local_filename;
if (!(local_filename= g_filename_from_utf8(filename,-1,NULL,NULL,NULL)))
return NULL;
file = fopen(local_filename, mode);
g_free(local_filename);
return file;
#endif // #ifdef _WINDOWS
}
//----------------------------------------------------------------------------------------------------------------------
|