Beispiel #1
0
//---------------------------------------------------------------------------
///ReadDoubleFromFile reads the first line of a file
///and converts it to a double.
///From http://www.richelbilderbeek.nl/CppReadDoubleFromFile.htm
double Test::ReadDoubleFromFile(const std::string& fileName)
{
  const std::vector<std::string> v = FileToVector(fileName);
  assert(v.empty()==false);
  const double d = std::atof(v[0].c_str());
  return d;
}
void QtQmakeWatcherMainDialog::OnQmake()
{
  //Save text to file
  {
    const std::string s = ui->edit_pro->document()->toPlainText().toStdString();
    std::ofstream f("tmp.pro");
    f << s << '\n';
  }
  //Execute commands
  {
    const bool has_error = std::system("cp Makefile oldmake");
    assert(!has_error);
    if (has_error) throw std::runtime_error("'cp Makefile oldmake' failed");
  }
  {
    const bool has_error = std::system("qmake tmp.pro");
    assert(!has_error);
    if (has_error) throw std::runtime_error("'qmake tmp.pro' failed");
  }
  {
    const bool has_error = std::system("diff Makefile oldmake > tmp.txt");
    assert(!has_error);
    if (has_error) throw std::runtime_error("'diff Makefile oldmake > tmp.txt' failed");
  }
  //Display Makefile
  {
    ui->edit_makefile->clear();
    const std::vector<std::string> v(FileToVector("Makefile"));
    BOOST_FOREACH(const std::string& s, v)
    {
      ui->edit_makefile->appendPlainText(QString(s.c_str()));
    }
  }
Beispiel #3
0
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
                            long *Epoch) {
  long E = Epoch ? *Epoch : 0;
  for (auto &X : ListFilesInDir(Path, Epoch)) {
    auto FilePath = DirPlusFile(Path, X);
    if (Epoch && GetEpoch(FilePath) < E) continue;
    V->push_back(FileToVector(FilePath));
  }
}
//---------------------------------------------------------------------------
void __fastcall TFormCreateDoc::ButtonLoadClick(TObject *Sender)
{
    if (!OpenDialog1->Execute()) return;
    m_plaintext = FileToVector(OpenDialog1->FileName.c_str());
    VectorToStringList(m_plaintext,RichEdit1->Lines);
    EditNquestions->Text = m_plaintext.size() - 1;

    ButtonCreateDoc->Enabled = (m_plaintext.size() > 1);
    m_path = GetPath(OpenDialog1->FileName.c_str());

}
Beispiel #5
0
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
                            long *Epoch) {
  long E = Epoch ? *Epoch : 0;
  auto Files = ListFilesInDir(Path, Epoch);
  for (size_t i = 0; i < Files.size(); i++) {
    auto &X = Files[i];
    auto FilePath = DirPlusFile(Path, X);
    if (Epoch && GetEpoch(FilePath) < E) continue;
    if ((i % 1000) == 0 && i)
      Printf("Loaded %zd/%zd files from %s\n", i, Files.size(), Path);
    V->push_back(FileToVector(FilePath));
  }
}
Beispiel #6
0
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
                            long *Epoch, size_t MaxSize) {
  long E = Epoch ? *Epoch : 0;
  std::vector<std::string> Files;
  ListFilesInDirRecursive(Path, Epoch, &Files, /*TopDir*/true);
  size_t NumLoaded = 0;
  for (size_t i = 0; i < Files.size(); i++) {
    auto &X = Files[i];
    if (Epoch && GetEpoch(X) < E) continue;
    NumLoaded++;
    if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
      Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
    V->push_back(FileToVector(X, MaxSize));
  }
}
Beispiel #7
0
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
                            long *Epoch, size_t MaxSize) {
  long E = Epoch ? *Epoch : 0;
  auto Files = ListFilesInDir(Path, Epoch);
  size_t NumLoaded = 0;
  for (size_t i = 0; i < Files.size(); i++) {
    auto &X = Files[i];
    auto FilePath = DirPlusFile(Path, X);
    if (Epoch && GetEpoch(FilePath) < E) continue;
    NumLoaded++;
    if ((NumLoaded & (NumLoaded - 1)) == 0 && NumLoaded >= 1024)
      Printf("Loaded %zd/%zd files from %s\n", NumLoaded, Files.size(), Path);
    V->push_back(FileToVector(FilePath, MaxSize));
  }
}
const std::string QtDialog::GetQtCreatorVersion()
{
  //'2>' denotes -AFAIK- 'Write to file only, no screen output'
  std::system("qtcreator -version 2> tmp.txt");
  const std::vector<std::string> v = FileToVector("tmp.txt");
  const std::size_t sz = v.size();
  assert(sz > 1);
  for (std::size_t i=0; i!=sz; ++i)
  {
    const std::string& s = v[i];
    if (s.substr(0,11) == std::string("Qt Creator "))
    {
      return s.substr(11,5);
    }
  }
  return "";
}
Beispiel #9
0
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V) {
  for (auto &X : ListFilesInDir(Path))
    V->push_back(FileToVector(DirPlusFile(Path, X)));
}
int CollectDataFlow(const std::string &DFTBinary, const std::string &DirPath,
                    const Vector<SizedFile> &CorporaFiles) {
  Printf("INFO: collecting data flow: bin: %s dir: %s files: %zd\n",
         DFTBinary.c_str(), DirPath.c_str(), CorporaFiles.size());
  MkDir(DirPath);
  auto Temp = TempPath(".dft");
  for (auto &F : CorporaFiles) {
    // For every input F we need to collect the data flow and the coverage.
    // Data flow collection may fail if we request too many DFSan tags at once.
    // So, we start from requesting all tags in range [0,Size) and if that fails
    // we then request tags in [0,Size/2) and [Size/2, Size), and so on.
    // Function number => DFT.
    std::unordered_map<size_t, Vector<uint8_t>> DFTMap;
    std::unordered_set<std::string> Cov;
    std::queue<std::pair<size_t, size_t>> Q;
    Q.push({0, F.Size});
    while (!Q.empty()) {
      auto R = Q.front();
      Printf("\n\n\n********* Trying: [%zd, %zd)\n", R.first, R.second);
      Q.pop();
      Command Cmd;
      Cmd.addArgument(DFTBinary);
      Cmd.addArgument(std::to_string(R.first));
      Cmd.addArgument(std::to_string(R.second));
      Cmd.addArgument(F.File);
      Cmd.addArgument(Temp);
      Printf("CMD: %s\n", Cmd.toString().c_str());
      if (ExecuteCommand(Cmd)) {
        // DFSan has failed, collect tags for two subsets.
        if (R.second - R.first >= 2) {
          size_t Mid = (R.second + R.first) / 2;
          Q.push({R.first, Mid});
          Q.push({Mid, R.second});
        }
      } else {
        Printf("********* Success: [%zd, %zd)\n", R.first, R.second);
        std::ifstream IF(Temp);
        std::string L;
        while (std::getline(IF, L, '\n')) {
          // Data flow collection has succeeded.
          // Merge the results with the other runs.
          if (L.empty()) continue;
          if (L[0] == 'C') {
            // Take coverage lines as is, they will be the same in all attempts.
            Cov.insert(L);
          } else if (L[0] == 'F') {
            size_t FunctionNum = 0;
            std::string DFTString;
            if (ParseDFTLine(L, &FunctionNum, &DFTString)) {
              auto &DFT = DFTMap[FunctionNum];
              if (DFT.empty()) {
                // Haven't seen this function before, take DFT as is.
                DFT = DFTStringToVector(DFTString);
              } else if (DFT.size() == DFTString.size()) {
                // Have seen this function already, merge DFTs.
                DFTStringAppendToVector(&DFT, DFTString);
              }
            }
          }
        }
      }
    }
    auto OutPath = DirPlusFile(DirPath, Hash(FileToVector(F.File)));
    // Dump combined DFT to disk.
    Printf("Producing DFT for %s\n", OutPath.c_str());
    std::ofstream OF(OutPath);
    for (auto &DFT: DFTMap)
      OF << "F" << DFT.first << " " << DFT.second << std::endl;
    for (auto &C : Cov)
      OF << C << std::endl;
  }
  RemoveFile(Temp);
  // Write functions.txt.
  Command Cmd;
  Cmd.addArgument(DFTBinary);
  Cmd.setOutputFile(DirPlusFile(DirPath, "functions.txt"));
  ExecuteCommand(Cmd);
  return 0;
}
void ribi::gtst::Parameters::ReadFromFile(const std::string& filename)
{
  if (!boost::filesystem::exists(filename))
  {
    const std::string error
      = std::string("File ")
      + filename
      + std::string(" not found");
    throw std::runtime_error(error);
  }
  assert(boost::filesystem::exists(filename));

  const std::vector<std::string> v = FileToVector(filename);
  std::for_each(v.begin(),v.end(),
    [this](const std::string& s)
    {
      //Trim_copy s to remove Microsoft Windows line endings
      this->Parse(boost::algorithm::trim_copy(s));
    }
  );

  ///Check if at least initial group 1 and 2 are present
  std::map<int,int> tally;
  std::for_each(m_participants.begin(),m_participants.end(),
    [&tally](const boost::shared_ptr<Participant>& p)
  {
      const GroupAssigner * const a = p->GetGroupAssigner();
      assert(a);
      if (dynamic_cast<const GroupAssignerPredetermined*>(a))
      {
        ++tally[dynamic_cast<const GroupAssignerPredetermined*>(a)->GetGroup()];
      }
    }
  );
  if (tally[1]==0)
  {
    throw std::runtime_error("Group #1 must exist and contain either 3 or 5 participants");
  }
  if (tally[1]!=3 && tally[1]!=5)
  {
    throw std::runtime_error("Group #1 must contain either 3 or 5 participants");
  }
  if (tally[2]==0)
  {
    throw std::runtime_error("Group #2 must exist and contain either 3 or 5 participants");
  }
  if (tally[2]!=3 && tally[2]!=5)
  {
    throw std::runtime_error("Group #2 must contain either 3 or 5 participants");
  }
  std::vector<int> ids;
  std::transform(
    tally.begin(),tally.end(),std::back_inserter(ids),
    [](const std::pair<const int,int>& p) { return p.first; } );
    //boost::lambda::bind(&std::pair<const int,int>::first, boost::lambda::_1));
  const int n_ids = boost::numeric_cast<int>(ids.size());
  for (int i=0; i!=n_ids; ++i)
  {
    //At index 0 there must be ID 1
    if (ids[i] != i+1)
    {
      const std::string s
        = std::string("Group #")
        + std::to_string(i+1)
        + std::string(" is missing") ;
      throw std::runtime_error(s.c_str());
    }
  }

  if (m_choose_action->GetOptions().size() < 2)
  {
    throw std::runtime_error("Please specify at least two choose_action_options");
  }
  if (this->m_voting->GetOptions().size() < 2)
  {
    throw std::runtime_error("Please specify at least two voting_options");
  }
}
//---------------------------------------------------------------------------
void TFormScoreTests::ReadFile(const String& filename)
{
  const int row = StringGridStudents->RowCount - 1;
  const std::string file_stripped = RemoveExtension(RemovePath(filename.c_str()));
  ++StringGridStudents->RowCount;
  StringGridStudents->Cells[0][row] = row;
  StringGridStudents->Cells[1][row] = file_stripped.c_str();
  StringGridStudents->Cells[2][row] = "";
  StringGridStudents->Cells[3][row] = "";

  const int key = EditKey->Text.ToInt();
  const int penalty = EditPenalty->Text.ToInt();
  const Encranger e(key);


  //Load file
  std::vector<std::string> v = FileToVector(filename.c_str());

  const int sz = v.size();
  for (int i=0; i!=sz; ++i)
  {
    //Deencrypt file
    v[i] = e.Deencrypt(v[i]);
    //Remove asterisks
    v[i] = ReplaceAll(v[i],"*","");
  }

  const std::string username = FindUserName(v);

  if (username.empty())
  {
    StringGridStudents->Cells[3][row] = "Username not found";
    StringGridStudents->Cells[2][row] = "1.0 or 1.1";
    return;
  }

  if (username != file_stripped)
  {
    StringGridStudents->Cells[3][row] = "Filename differs from username";
    StringGridStudents->Cells[2][row] = "1.1";
    return;
  }


  int n_correct = 0;
  const int n_questions = CountQuestions(v);
  LabelNquestions->Caption = "#questions: " + IntToStr(n_questions);

  for (int i=0; i!=sz; ++i)
  {
    if (v[i].empty()) continue;

    const std::vector<std::string> line_markup = SeperateString(v[i],",");

    //Replace {comma} by a comma for each std::string in v
    const std::vector<std::string> line = ReplaceAll(line_markup,"{comma}",",");

    OutputDebugString(line.size() >= 1 ? line[0].c_str() : "line[0] = {}");
    OutputDebugString(line.size() >= 2 ? line[1].c_str() : "line[1] = {}");
    OutputDebugString(line.size() >= 3 ? line[2].c_str() : "line[2] = {}");
    OutputDebugString(line.size() >= 4 ? line[3].c_str() : "line[3] = {}");

    //assert(line.size() == 2); WORKAROUND



    if (line[0]=="NAME") continue;
    const bool is_correct = (line[line.size()-1]=="Y"); //WORKAROUND
    assert(line[line.size()-1]=="Y" || line[line.size()-1]=="N"); //WORKAROUND 
    if (is_correct) ++n_correct;
    ScoreQuestion(line[0],is_correct);
  }

  //Calculate student's score
  assert(n_questions - penalty > 0);
  const double fraction
      = static_cast<double>(n_correct - penalty)
      / static_cast<double>(n_questions - penalty);
  const double init_score = 1.0 + (9.0 * fraction);
  const double score_above_one = (init_score < 1.0 ? 1.0 : init_score);
  const double score_below_ten = (score_above_one > 10.0 ? 10.0 : score_above_one);
  const double score = score_below_ten;

  StringGridStudents->Cells[2][row] = score;
}