Skip to content

Commit

Permalink
Re #8635. Moved LoadAscii::isAscii into FileDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfpeterson committed Dec 18, 2013
1 parent 1700786 commit 2e0d923
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
Expand Up @@ -58,8 +58,6 @@ namespace Mantid
/// Returns a confidence value that this algorithm can load a file
virtual int confidence(Kernel::FileDescriptor & descriptor) const;

static bool isAscii(FILE *file);

protected:
/// Process the header information within the file.
virtual void processHeader(std::ifstream & file) const;
Expand Down
26 changes: 0 additions & 26 deletions Code/Mantid/Framework/DataHandling/src/LoadAscii.cpp
Expand Up @@ -81,32 +81,6 @@ namespace Mantid
return confidence;
}

/**
* Check if a file is a text file
* @param file :: The file pointer
* @returns true if the file an ascii text file, false otherwise
*/
bool LoadAscii::isAscii(FILE *file)
{
char data[256];
char *pend = &data[fread(data, 1, sizeof(data), file)];
fseek(file,0,SEEK_SET);
/*
* Call it a binary file if we find a non-ascii character in the
* first 256 bytes of the file.
*/
for( char *p = data; p < pend; ++p )
{
unsigned long ch = (unsigned long)*p;
if( !(ch <= 0x7F) )
{
return false;
}

}
return true;
}

//--------------------------------------------------------------------------
// Protected methods
//--------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/DataHandling/src/LoadRawHelper.cpp
Expand Up @@ -455,7 +455,7 @@ namespace Mantid
*/
bool LoadRawHelper::isAscii(FILE* file) const
{
return LoadAscii::isAscii(file);
return Kernel::FileDescriptor::isAscii(file);
}


Expand Down
Expand Up @@ -45,6 +45,8 @@ namespace Mantid
static bool isAscii(const std::string & filename, const size_t nbytes=256);
/// Returns true if the stream is considered ascii
static bool isAscii(std::istream & data, const size_t nbytes=256);
/// Returns true if the file is considered ascii
static bool isAscii(FILE* file, const size_t nbytes=256);

public:
/// Constructor accepting a filename
Expand Down
26 changes: 26 additions & 0 deletions Code/Mantid/Framework/Kernel/src/FileDescriptor.cpp
Expand Up @@ -69,6 +69,32 @@ namespace Mantid
return result;
}

/**
* Check if a file is a text file
* @param file :: The file pointer
* @returns true if the file an ascii text file, false otherwise
*/
bool FileDescriptor::isAscii(FILE* file, const size_t nbytes)
{
// read the data and reset the seek index back to the beginning
char data[nbytes];
char *pend = &data[fread(data, 1, sizeof(data), file)];
fseek(file,0,SEEK_SET);

// Call it a binary file if we find a non-ascii character in the
// first nbytes bytes of the file.
for( char *p = data; p < pend; ++p )
{
unsigned long ch = (unsigned long)*p;
if( !(ch <= 0x7F) )
{
return false;
}

}
return true;
}

//----------------------------------------------------------------------------------------------
// Public methods
//----------------------------------------------------------------------------------------------
Expand Down
22 changes: 22 additions & 0 deletions Code/Mantid/Framework/Kernel/test/FileDescriptorTest.h
Expand Up @@ -2,6 +2,7 @@
#define MANTID_KERNEL_FILEDESCRIPTORTEST_H_

#include <cxxtest/TestSuite.h>
#include <cstdio>

#include "MantidKernel/FileDescriptor.h"
#include "MantidKernel/ConfigService.h"
Expand Down Expand Up @@ -80,6 +81,27 @@ class FileDescriptorTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(1, is.tellg());
}

void test_isAscii_Returns_True_For_Stream_Pointing_At_Ascii()
{
FILE* handle = fopen(m_testAsciiPath.c_str(), "r");
if (handle)
{
TS_ASSERT(!FileDescriptor::isAscii(handle));
TS_ASSERT_EQUALS(0, ftell(handle));
fclose(handle);
}
}

void test_isAscii_Returns_False_For_Stream_Pointing_At_Ascii()
{
FILE* handle = fopen(m_testNonNexusPath.c_str(), "r");
if (handle)
{
TS_ASSERT(FileDescriptor::isAscii(handle));
TS_ASSERT_EQUALS(0, ftell(handle));
fclose(handle);
}
}

void test_Constructor_With_Existing_File_Initializes_Description_Fields()
{
Expand Down

0 comments on commit 2e0d923

Please sign in to comment.