/*
filename:  list_sorter.cpp

- This program opens a textfile pathname passed in as the first command-line
argument; otherwise, it presumes a textfile named 'the_list' located within the
same dir as the executable.
- Upon success, it leaves a sorted textfile pathname passed in as the second
command-line argument; otherwise, it leaves a 'sorted_list' textfile in the same
dir as the executable.

Minimal build & run:

  g++ list_sorter.cpp -std=c++14 -o list_sorter && ./list_sorter

*/

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <exception>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using std::cerr;
using std::equal;
using std::getline;
using std::ifstream;
using std::invalid_argument;
using std::ofstream;
using std::puts;
using std::remove;
using std::sort;
using std::stable_sort;
using std::string;
using std::to_string;
using std::unique;
using std::vector;

/** ensure length of string @c str is <= @c sz count of characters
 * @param str
 * @param sz the size limit
 * @return true if within size limit; throws @c invalid_argument otherwise
 */
bool chk_str_len(std::string const& str, std::size_t const sz)
{
  if (str.length() > sz)
    // NOTE: could use std::format here; if ever moving to a higher C++ std
    throw invalid_argument{str.substr(0, sz) + "...\n  length is beyond " +
                           to_string(sz) + "char limit"};

  return true;
}

/** ensure string @c str 's chars are within valid ASCII character range values
 *  [32 to 127]
 * @param str
 * @return true if all chars within range; throws @c invalid_argument otherwise
 */
bool chk_ascii_rng(std::string const& str)
{
  for (auto const ch : str) {
    auto const val = static_cast<int>(ch);
    if (val < 32 || val > 127)
      // NOTE: could use std::format here; if ever moving to a higher C++ std
      throw invalid_argument{str + " ;\n  ' " + string{ch} + " ' : (" +
                             to_string(val) +
                             ") char outside of valid ASCII range"};
  }

  return true;
}

/** perform case-insensitive comparison on two strings
 * @param str1
 * @param str2
 * @return true if strings are otherwise identical, apart from casing
 */
bool lower_compare(std::string const& str1, std::string const& str2)
{
  return equal(
      str1.cbegin(), str1.cend(), str2.cbegin(), str2.cend(),
      [](auto const a, auto const b) { return tolower(a) == tolower(b); });
}

int main(int argc, char* argv[])
{
  puts("reading the list...");
  ifstream ifs;
  if (argc > 1)
    ifs.open(argv[1]);
  else
    ifs.open("the_list");

  vector<string> list;
  for (string line; getline(ifs, line);)
    list.push_back(line);
  ifs.close();

  puts("cleaning the list...");
  for (auto& line : list) {
    static vector<char> const rm_chars{'>', '<', '>'};
    for (auto const ch : rm_chars) {
      line.erase(remove(line.begin(), line.end(), ch), line.cend());
    }

    try {
      chk_str_len(line, 120);
      chk_ascii_rng(line);
    } catch (invalid_argument const& e) {
      cerr << e.what() << "\n  ...list entry ignored\n";
      continue;
    }
  }

  puts("sorting the list by alpha...");
  sort(list.begin(), list.end());

  puts("sorting the list by size...");
  stable_sort(list.begin(), list.end(),
              [](auto const& a, auto const& b) { return a.size() < b.size(); });

  puts("removing list duplicates...");
  list.erase(unique(list.begin(), list.end(), lower_compare), list.cend());

  puts("titlecasing the list...");
  for (auto& line : list) {              // For each line in the list,
    line[0] = toupper(line[0]);          //  transform first char to upper case.
    for (auto& ch : line) {              // For each char in the line,
      if (*(&ch - sizeof(char)) == ' ')  //  if previous char was a space,
        ch = toupper(ch);                //  transform curr char to upper case.
    }
  }

  puts("writing sorted list...");
  ofstream ofs;
  if (argc > 2)
    ofs.open(argv[2]);
  else
    ofs.open("sorted_list");

  for (auto const& line : list)
    ofs << '>' << line << '\n';
  ofs << "\n>No Further Description Of The Attackers Has Been Published.\n";
  ofs.close();
}

// version 1.4
// written July 2025 by Anon

THE LIST:
https://rentry.co/anything_but_niggers

Edit

Pub: 04 Jul 2025 00:05 UTC

Edit: 29 Jul 2025 15:55 UTC

Views: 172