home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Language.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  4.7 KB  |  206 lines

  1. /* 
  2.  *  language.cpp 
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23. #include <windows.h>
  24. #include <winbase.h>
  25.  
  26. #define LANGUAGE_MODULE
  27. #include "Language.h"
  28. #undef  LANGUAGE_MODULE
  29.  
  30. //////////////////////////////////////////////////////////////////////
  31. // Construction/Destruction
  32. //////////////////////////////////////////////////////////////////////
  33. static char filenames[MAX_LANGUAGES][1024];
  34. static char * strings[MAX_LANGUAGES][MAX_STRINGS];
  35. static int    strings_alloc[MAX_LANGUAGES][MAX_STRINGS];
  36.  
  37.  
  38. CLanguage::CLanguage(char *program_directory)
  39. {
  40.     selected_language=0;
  41.     if(program_directory)
  42.         strcpy(this->program_directory, program_directory);
  43. }
  44.  
  45. CLanguage::~CLanguage()
  46. {
  47.     int i,j;
  48.     //Deallocate strings
  49.     for(i=0; i<MAX_LANGUAGES; i++)
  50.         for(j=0; j<MAX_STRINGS; j++){                  
  51.             if(strings_alloc[i][j] == 1)
  52.                 free(strings[i][j]);
  53.         }
  54.  
  55. }
  56.  
  57. int CLanguage::Start()
  58.  
  59. {
  60.     WIN32_FIND_DATA find_data;
  61.     char     directory[MAX_PATH];
  62.     HANDLE   search_handle;
  63.     int      no_lang_package_flag;
  64.     int i,j;
  65.  
  66.     sprintf(directory, "%s\\Lang\\*.lang.flask", program_directory);
  67.     i=0;
  68.     search_handle = FindFirstFile(directory, &find_data);
  69.     if(search_handle==INVALID_HANDLE_VALUE){
  70.         n_lang=0;
  71.     }
  72.     else{
  73.         strcpy( filenames[i++], find_data.cFileName );
  74.         while( FindNextFile(search_handle, &find_data ) ){
  75.             strcpy( filenames[i++], find_data.cFileName );
  76.         }
  77.         FindClose(search_handle);
  78.         n_lang = i;
  79.     }
  80.     no_lang_package_flag = !n_lang;
  81.  
  82.     if(n_lang==0)
  83.         n_lang=1;
  84.  
  85.     //initialize string pointers
  86.     for(i=0; i<MAX_LANGUAGES; i++)
  87.         for(j=0; j<MAX_STRINGS; j++){
  88.                   strings[i][j] = "";
  89.             strings_alloc[i][j] = 0;
  90.         }
  91.  
  92.  
  93.  
  94.     LoadDefaultStrings();
  95.     // load default strings in all language sets
  96.     for(i=0; i<n_lang; i++)
  97.         for(j=0; j<MAX_STRINGS; j++)
  98.             strings[i][j] = ds[j] ? ds[j]:NULL;
  99.  
  100.     //Load strings from files
  101.     if(!no_lang_package_flag){
  102.         for(i=0;i<n_lang;i++)
  103.             ReadLanguage(i);
  104.     }
  105.     selected_language=0;
  106.     return 0;
  107. }
  108.  
  109. int CLanguage::GetNumberLang()
  110. {
  111.     return n_lang;
  112. }
  113.  
  114. char * CLanguage::GetLanguageID()
  115. {
  116.     char *ID;
  117.     ID=strings[selected_language][0];
  118.     if(ID)
  119.         return ID;
  120.     else
  121.         return "unknown";
  122. }
  123. int CLanguage::GetLanguage()
  124. {
  125.     return selected_language;
  126. }
  127.  
  128. int CLanguage::SetLanguage(int n_lang)
  129. {
  130.     if(n_lang >= this->n_lang)
  131.         return 0;
  132.     selected_language = n_lang;
  133.     return 1;
  134. }
  135.  
  136. char * CLanguage::GetString(int str_id)
  137. {
  138.     if(str_id>MAX_STRINGS)
  139.         return "";
  140.  
  141.     return strings[selected_language][str_id]?strings[selected_language][str_id]:"";
  142. }
  143.  
  144. int CLanguage::GetStrings(FILE *file)
  145. {
  146.     return 0;
  147. }
  148.  
  149. int CLanguage::ReadLanguage(int language)
  150. {
  151.     FILE *f=NULL;
  152.     char  token=0;
  153.     int   str_num, string_chars;
  154.     char  full_name[MAX_PATH];
  155.  
  156.     sprintf(full_name, "%s\\Lang\\%s", program_directory, filenames[language]);
  157.     //open file
  158.     f=fopen(full_name, "rb");
  159.     if(!f)
  160.         return 0;
  161.  
  162.     while(!feof(f)){
  163.         //Search for token #
  164.         while(token!='#' && !feof(f))
  165.             fread(&token, 1, 1, f);
  166.         //get number after token
  167.         if(!feof(f)){
  168.             fscanf(f, "%d", &str_num);
  169.             if(str_num>MAX_STRINGS)
  170.                 continue;
  171.         }
  172.  
  173.         //Search for token $
  174.         while(token!='#' && !feof(f))
  175.             fread(&token, 1, 1, f);
  176.         while(token!='"' && !feof(f))
  177.             fread(&token, 1, 1, f);
  178.  
  179.         //Read every single byte of the string
  180.         if(!feof(f)){
  181.             fread(&token, 1, 1, f);
  182.             string_chars = 0;
  183.  
  184.             strings[language][str_num]=NULL;
  185.             while(token!='"' && !feof(f)){
  186.                 string_chars++;
  187.                 strings[language][str_num]=(char *)realloc(strings[language][str_num], string_chars);
  188.                 strings[language][str_num][string_chars - 1] = token;
  189.                 strings_alloc[language][str_num] = 1;
  190.                 fread(&token, 1, 1, f);
  191.             }
  192.             //NULL terminate the string
  193.             string_chars++;
  194.             strings[language][str_num]=(char *)realloc(strings[language][str_num], string_chars);
  195.             strings[language][str_num][string_chars - 1] = 0;
  196.  
  197.         }
  198.  
  199.  
  200.     }
  201.  
  202.     
  203.     fclose(f);
  204.     return 1;
  205. }
  206.