GDCM  2.2.0
ReadUTF8QtDir.cxx
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*
* GDCM API expect a const char * as input for SetFileName
* In order to use this API from Qt, here is a simple test that
* shows how to do it in a portable manner:
*
* http://doc.qt.nokia.com/latest/qdir.html#navigation-and-directory-operations
*/
#include "gdcmReader.h"
#include "gdcmDirectory.h"
#include <QDir>
#include <QString>
#include <QCoreApplication>
#include <string>
#include <fstream>
#include <stdio.h> // fopen
static int TestBothFuncs(const char *info , const char *ba_str)
{
int res = 0;
FILE *f = fopen( ba_str, "r" );
if( f )
{
std::cout << info << " fopen: " << ba_str << std::endl;
fclose(f);
++res;
}
gdcm::Reader reader;
std::ifstream is( ba_str );
if( is.is_open() )
{
std::cout << info << " is_open: " << ba_str << std::endl;
++res;
}
reader.SetStream( is );
if( reader.CanRead() == true )
{
std::cout << info << " SetStream/CanRead:" << ba_str << std::endl;
++res;
}
is.close();
reader.SetFileName( ba_str );
if( reader.CanRead() == true )
{
std::cout << info << " SetFileName/CanRead:" << ba_str << std::endl;
++res;
}
return 4 - res;
}
static int scanFolder(const char dirname[])
{
int res = 0;
unsigned int nfiles = dir.Load( dirname, true );
const gdcm::Directory::FilenamesType &filenames = dir.GetFilenames();
for( unsigned int i = 0; i < nfiles; ++i )
{
const char *ba_str = filenames[i].c_str();
res += TestBothFuncs("GDCM",ba_str);
}
return res;
}
static int scanFolderQt(QDir const &dir, QStringList& files)
{
int res = 0;
QFileInfoList children = dir.entryInfoList(QDir::AllEntries|QDir::NoDotAndDotDot);
for ( int i=0; i<children.count(); i++ ) {
QFileInfo file = children.at(i);
if ( file.isDir() == true ) {
res += scanFolderQt(QDir(file.absoluteFilePath()), files);
continue;
}
// Convert back from the internal representation to 8bits
// toLocal8Bit() returns by copy. Need to store explicitely the QByteArray
QByteArray str = file.absoluteFilePath().toLocal8Bit();
const char *ba_str1 = str.constData();
res += TestBothFuncs("QString", ba_str1);
}
return res;
}
int main(int argc, char *argv[])
{
// very important:
QCoreApplication qCoreApp( argc , argv );
if( argc < 2 )
{
std::cerr << argv[0] << " dir " << std::endl;
return 1;
}
int res = 0;
const char *dirname = argv[1];
res += scanFolder( dirname );
QDir dir( QString::fromLocal8Bit(dirname) );
QStringList files;
res += scanFolderQt( dir, files);
if( res )
std::cerr << "Problem with UTF-8" << std::endl;
else
std::cerr << "Success with UTF-8" << std::endl;
return res;
}

Generated on Wed Jun 13 2012 20:40:36 for GDCM by doxygen 1.8.1
SourceForge.net Logo