Facebook
From Gentle Shama, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 143
  1. /*
  2.         Copyright (c) 2020 Rafał Rajtar
  3.  
  4.         Permission is hereby granted, free of charge, to any person obtaining a copy
  5.         of this software and associated documentation files (the "Software"), to deal
  6.         in the Software without restriction, including without limitation the rights
  7.         to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8.         copies of the Software, and to permit persons to whom the Software is
  9.         furnished to do so, subject to the following conditions:
  10.  
  11.         The above copyright notice and this permission notice shall be included in all
  12.         copies or substantial portions of the Software.
  13.  
  14.         THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15.         IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16.         FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17.         AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18.         LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19.         OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20.         SOFTWARE.
  21. */
  22.  
  23. #include <iostream>
  24. #include <filesystem>
  25. #include <fstream>
  26. #include <istream>
  27. #include <string>
  28.  
  29. using namespace std;
  30. namespace fs = std::filesystem;
  31.  
  32. template<class... Args>
  33. void println(Args... args) {
  34.         (std::cout << ... << args) << "\n";
  35. }
  36.  
  37. int sum_of_lines = 0;
  38. fs::path root_path;
  39.  
  40. int printFileData(fs::path path) {
  41.         ifstream file;
  42.         file.open(path);
  43.  
  44.         int lines = 0;
  45.         string line;
  46.  
  47.         while (getline(file, line)) lines++;
  48.         auto fn = path.filename();
  49.         println(fn, std::string(30 - fn.string().length(), ' '), lines);
  50.         return lines;
  51. }
  52.  
  53. int printAllWithExtension(const char* ext) {
  54.         println("Listing Files With ", ext, " Extension");
  55.         int sum_of_lines = 0;
  56.         for (auto f : fs::recursive_directory_iterator(root_path)) {
  57.                 auto path = f.path();
  58.                 if (path.extension().string() == ext) {
  59.                         sum_of_lines += printFileData(path);
  60.                 }
  61.         }
  62.         println("Lines Of Code With This Extension: ", sum_of_lines, "\n");
  63.         return sum_of_lines;
  64. }
  65.  
  66. template<class T>
  67. void printAllWithExtensions(T ext) {
  68.         sum_of_lines += printAllWithExtension(ext);
  69.         println("Total Lines Of Code: ", sum_of_lines, "\n\n\n");
  70. }
  71.  
  72. template<class T, class... U>
  73. void printAllWithExtensions(T ext, U... exts) {
  74.         sum_of_lines += printAllWithExtension(ext);
  75.         printAllWithExtensions(exts...);
  76. }
  77.  
  78. int main() {
  79.         println("PROGRAM START");
  80.  
  81.         root_path = "C:\\Users\\haski\\source\\repos\\KBU\\KBU";
  82.  
  83.         printAllWithExtensions(".cpp", ".h", ".glsl");
  84.        
  85.         println("PROGRAM END");
  86.         system("pause");
  87. }