File: FileUtils.cpp

package info (click to toggle)
dasher 5.0.0~beta~repack2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 70,872 kB
  • sloc: xml: 181,314; cpp: 70,860; java: 8,020; python: 3,579; makefile: 939; sh: 324; ansic: 223; perl: 71
file content (55 lines) | stat: -rw-r--r-- 1,524 bytes parent folder | download | duplicates (5)
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
#include <string>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "../Common/Globber.h"
#include "../DasherCore/AbstractXMLParser.h"
#include "../DasherCore/DasherInterfaceBase.h"
#include "FileUtils.h"

int FileUtils::GetFileSize(const std::string &strFileName) {
  struct stat sStatInfo;

  if(!stat(strFileName.c_str(), &sStatInfo))
    return sStatInfo.st_size;
  else
    return 0;
}

void FileUtils::ScanFiles(AbstractParser *parser, const std::string &strPattern) {
  //System files.
  // PROGDATA is provided by the makefile
  string path(PROGDATA "/");
  path += strPattern;

  std::string user_data_dir= getenv("HOME");
  user_data_dir += "/.dasher/";

  //User files.
  mkdir(user_data_dir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  string user_path = user_data_dir;
  user_path += strPattern;

  const char *user[2], *sys[2];
  user[0] = user_path.c_str();
  sys[0] = path.c_str();
  user[1] = sys[1] = nullptr; //terminators

  globScan(parser, user, sys);
}

bool FileUtils::WriteUserDataFile(const std::string &filename, const std::string &strNewText, bool append) {
  if (strNewText.length() == 0)
    return true;
  std::string strFilename = getenv("HOME");
  strFilename += "/.dasher/";
  strFilename += filename;
  FILE* f = fopen(strFilename.c_str(), append ? "a+" : "w+");
  if (f == nullptr)
    return false;

  int written = fwrite(strNewText.c_str(), 1, strNewText.length(), f);
  fclose(f);
  return written == strNewText.length();
}