View Single Post
Old 07-8-2016, 11:28 AM   #1432
MarioNintendo
Expect delays.
FFR Simfile AuthorFFR Music ProducerFFR Veteran
 
MarioNintendo's Avatar
 
Join Date: Mar 2008
Location: Montreal, QC
Age: 26
Posts: 3,836
Default Re: The VideoGame MegaPack 4 (SUBMISSIONS CLOSED)!

Here's the code, that could be useful in the future Let me know if it works for you guys (I count on you mina to tell me how to get better at writing code through criticism)
Code:
// sm_simfile_count.cpp, written by MarioNintendo July 8 2016

// This program is used to count how many simfiles a simfile artist made in one pack from the songlist.
// The simfile artist's name must be placed at the end of the songname between parantheses (e.g., Classical Insanity (Rebirth0))
// in the songlist to be used by this code

// If you have the pack but not its songlist, you can generate it yourself.
// Simply open a terminal, cd to the pack's directory, then do "ls > songlist.txt"

#include<iostream>
#include<fstream>  // for ifstream, read songlist
#include<string>
#include<map>
#include<algorithm>  // for count function
using namespace std;


int strpos(string, char, int);
template <typename A, typename B> multimap<B, A> flip_map(map<A,B> &); 

int main(int argc, char* argv[]) {
  if (argc == 1) {
    cout << "Usage:\n./a.out <songlist1>.txt <songlist2>.txt ... <songlistn>.txt\n";
  }
  if (argc > 1) {
    for (int i=1; i<argc; ++i) {
      ifstream myfile(argv[i]);
      if (myfile.is_open()) {   
        cout << "------------- " << argv[i] << " ----------\n";
        map<string,int> results;  
        string line;  
        int nb_files=0;  

        // In the songlist, we extract simartists from the name of the file (e.g., Classical Insanity (Rebirth0) wouls yield Rebirth0)
        while (getline(myfile, line)) {   
          int n_left_par = count(line.begin(), line.end(), '(');
          int n_right_par = count(line.begin(), line.end(), ')');  
          if (n_left_par == n_right_par && n_right_par > 0) {   
            int last_left = strpos(line, '(', n_left_par);  
            int last_right = strpos(line, ')', n_right_par);
            string sim_artist = line.substr(last_left, last_right-last_left - 1);  
            results[sim_artist]++;  
          }   
          ++nb_files;
        }   
        cout << "** TOTAL: " << nb_files << " files ** \n";

        // Reverse the map to enable ordering in ascending order
        multimap<int, string> reverse_results = flip_map(results);  
        for(multimap<int, string>::reverse_iterator iter = reverse_results.rbegin(); iter != reverse_results.rend(); ++iter ) {   
          cout << iter->first << ": " << iter->second << endl;
        }   
      }   
      else {   
        cout << "Error: Could not open " << argv[i] << ". Skipping that file.\n";
      }   
    }   
  }
  return 0;
}
/* strpos
 *
 * Will return position of n-th occurence of a char in a string.
 * (Inspired by http://stackoverflow.com/a/18972477/5565172 )
 *
 * Takes string, the char we're looking for, and the value of n as arguments
 */
int strpos(string haystack, char needle, int nth) {
  string read;  
  for (int i=1 ; i<nth+1 ; ++i)
  {
    std::size_t found = haystack.find(needle);
    read += haystack.substr(0,found+1); // the read part of the haystack is stocked in the read string
    haystack.erase(0, found+1);  // remove the read part of the haystack up to the i-th needle
    if (i == nth)
    {   
      return read.size();
    }   
  }
  return -1; 
}

/* flip_map
 *
 * This function is used to revert a map. 
 * (It has been taken from here: http://stackoverflow.com/a/23050953)
 *
 * Takes a map as an argument
 */
template <typename A, typename B> multimap<B, A> flip_map(map<A,B> & src) {
  multimap<B,A> dst;
  for(typename map<A, B>::const_iterator it = src.begin(); it != src.end(); ++it) {
    dst.insert(pair<B, A>(it -> second, it -> first));
  }
  return dst;
}
Here's the output for this pack (from the songlist in the OP):
Code:
** TOTAL: 153 files **
28: MarioNintendo
14: hi19hi19
10: cedo
9: Tsuka
8: bmah
6: Nullifidian
6: MacGravel
5: CyclopsDragon
4: wv
4: dub 
4: cerdo_claro
4: Gundam-Dude
4: Ferrari
3: porkypink
3: XelNya
3: Kommisar
3: Jombo
3: James May 
2: Yesssss
2: Scylaax
2: MooMoo_Cowfreak
2: MacGravel & Tsuka
2: Dj_Ossa
1: rCaliberGX
1: moches
1: kommisar & cedo
1: hi19hi19 & mina
1: ferrari
1: Tim 
1: Pazzaz
1: Nullidifian
1: Meno
1: MarioNintendo & MacGravel
1: Lofty
1: Kraezymann
1: JX
1: FoxX
1: Flossy
1: Detrimentalist
1: Choof
1: Charu & Gradiant
1: Cedolad & MarioNintendo
1: Antronach
1: ??? TheDudeWhoMadeTemperanceAndVengance ??? 
1: 3-T Scroat
__________________
--- Coming soon, after official FFR tournament! ---


Quote:
Originally Posted by macgravel View Post
Mario's files are top notch, so every time he submits a file I get excited in my pants.
Click the bricks



Last edited by MarioNintendo; 07-8-2016 at 01:07 PM..
MarioNintendo is offline   Reply With Quote