Permalink
Browse files

Added support for detecting text codec from environmental variables.

Variables LC_ALL, LC_CTYPE and LANG are checked if there is possible to
determine from them suitables text codec, if not
QTextCodec::codecForLocale() is used.
  • Loading branch information...
danfis committed Aug 19, 2008
1 parent a208675 commit f9da22589bea71cbe4b20802b9d035bace2aff74
Showing with 42 additions and 2 deletions.
  1. +42 −2 src/main.cpp
View
@@ -24,6 +24,7 @@
#include <QApplication>
#include <QTextCodec>
#include <unistd.h>
#include <stdlib.h>
#include "diff/diff.h"
#include "diff/text.h"
@@ -39,19 +40,45 @@ using std::string;
void usage(int argc, char *argv[]);
char *codecPart(char *str);
int main(int argc, char *argv[])
{
MILESTONE("");
MILESTONE("====== START QSHOWDIFF ======");
QTextCodec *codec = 0;
#ifdef DEFAULT_CODEC
/* Set up codec manualy */
QTextCodec *codec = QTextCodec::codecForName(DEFAULT_CODEC);
codec = QTextCodec::codecForName(DEFAULT_CODEC);
#else /* DEFAULT_CODEC */
char *var = NULL;
codec = 0;
var = getenv("LC_ALL");
if (var == NULL || strlen(var) <= 0)
var = getenv("LC_CTYPE");
if (var == NULL || strlen(var) <= 0)
var = getenv("LANG");
if (var != NULL){
var = codecPart(var);
codec = QTextCodec::codecForName(var);
DBG("Detected codec: '" << var << "'");
}
if (codec == 0){
DBG("Previously detected codec wasn't accepted. Falling to codecForLocale.");
codec = QTextCodec::codecForLocale();
}
#endif /* DEFAULT_CODEC */
DBG("Codec: " << (const char *)codec->name());
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForTr(codec);
#endif
string input_type;
@@ -110,3 +137,16 @@ void usage(int argc, char *argv[])
}
}
char *codecPart(char *str)
{
char *c;
c = str;
while (*c != 0 && *c != '.')
c++;
if (*c == '.' && *++c != 0){
return c;
}else{
return str;
}
}

0 comments on commit f9da225

Please sign in to comment.