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
|
//////////////////////////////////////////////////////////////////////////
//
// pgScript - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////////////////
#include "pgAdmin3.h"
#include "pgscript/generators/pgsDictionaryGen.h"
#include <wx/txtstrm.h>
#include <wx/wfstream.h>
#include <wx/filename.h>
pgsDictionaryGen::pgsDictionaryGen(const wxString &file_path,
const bool &sequence, const long &seed, wxCSConv conv) :
pgsObjectGen(seed), m_file_path(file_path), m_conv(conv)
{
m_nb_lines = this->count_lines();
m_randomizer = pgsRandomizer(pnew pgsIntegerGen(1, m_nb_lines,
sequence, seed));
}
wxString pgsDictionaryGen::random()
{
return this->get_line(m_randomizer->random_long());
}
pgsDictionaryGen::~pgsDictionaryGen()
{
}
pgsDictionaryGen *pgsDictionaryGen::clone()
{
return pnew pgsDictionaryGen(*this);
}
const long &pgsDictionaryGen::nb_lines() const
{
return m_nb_lines;
}
long pgsDictionaryGen::count_lines()
{
long result = 0;
wxFileName file_path(m_file_path);
if (file_path.FileExists() && file_path.IsFileReadable())
{
wxFFileInputStream input(m_file_path);
if (input.IsOk())
{
#if wxUSE_UNICODE
wxTextInputStream text(input, wxT(" \t"), m_conv);
#else
wxTextInputStream text(input, wxT(" \t"));
#endif
wxString line;
while (!(line = text.ReadLine()).IsEmpty() && !input.Eof())
{
++result;
}
}
}
if (result < 0) result = 0;
wxASSERT(result >= 0);
return result;
}
wxString pgsDictionaryGen::get_line(long line_nb)
{
long current_line = 1;
if (m_nb_lines > 0)
{
wxFFileInputStream input(m_file_path);
if (input.IsOk())
{
#if wxUSE_UNICODE
wxTextInputStream text(input, wxT(" \t"), m_conv);
#else
wxTextInputStream text(input, wxT(" \t"));
#endif
while (current_line < line_nb && !input.Eof())
{
text.ReadLine();
++current_line;
}
return text.ReadLine();
}
}
return wxString();
}
|