<commit_before>// Copyright 2014 <NAME>.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package config

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"regexp"
	"strings"
)

// Config
type Config struct {
	Extensions         []string
	BinaryExtensions   []string
	ExcludeDirectories []string
	ExcludeFiles       []string
}

var configFilename = ""

// ------------------------------------------
// init
// ------------------------------------------
func init() {
	fullFilePath, _ := filepath.Abs(os.Args[0])

	// Set the config file name to [thisexecutablepath\thisexecutablefilename].config
	configFilename = fmt.Sprintf("%s\\%s", filepath.Dir(fullFilePath), strings.Replace(filepath.Base(fullFilePath), ".exe", ".config", 1))
}

// ------------------------------------------
// LoadConfig
// ------------------------------------------
func LoadConfig() Config {
	var c Config

	// Read whole the file
	var jsonstring, err = ioutil.ReadFile(configFilename)
	if err != nil {
		createConfig()

		// Read the file again, now it should exist
		jsonstring, err = ioutil.ReadFile(configFilename)
	}

	// Strip comments from config file
	var re = regexp.MustCompile("(?s)//.*?\n|/\\*.*?\\*/")
	var newJsonstring = re.ReplaceAll(jsonstring, nil)

	// Create config to be returned
	err = json.Unmarshal(newJsonstring, &c)

	if err != nil {
		fmt.Printf("Could not read the config, %v\n", err)
		c = Config{}
	}

	return c
}

// ------------------------------------------
// createConfig
// ------------------------------------------
func createConfig() {
	var sc Config

	// Extensions to count
	sc.Extensions = []string{
		".css",
		".go",
		".htm",
		".html",
		".js",
		".json",
		".less",
		".sass",
		".xml",
		".xsd",
	}

	// Binary extensions to count
	sc.BinaryExtensions = []string{
		".gif",
		".ico",
		".jpg",
		".png",
	}

	// Directories to exclude
	sc.ExcludeDirectories = []string{
		".svn",
		"bin",
		"obj",
		"Scripts\\jquery.ui",
		"_svn",
	}

	// Directories to exclude
	sc.ExcludeFiles = []string{
		configFilename,
		"jquery.min.js",
		"jquery.ui.js",
	}

	save(sc)
}

// ------------------------------------------
// save
// ------------------------------------------
func save(sc Config) {
	var jsonstring, err = json.MarshalIndent(&sc, "", "  ")
	if err != nil {
		fmt.Printf("json.Marshal(sc), %s %v\n", string(jsonstring), err)
	}

	err = ioutil.WriteFile(configFilename, jsonstring, 0666)
	if err != nil {
		fmt.Printf("ioutil.WriteFile, %v\n", err)
	}
}
<commit_msg>Strip config path when creating default config<commit_after>// Copyright 2014 <NAME>.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package config

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"regexp"
	"strings"
)

// Config
type Config struct {
	Extensions         []string
	BinaryExtensions   []string
	ExcludeDirectories []string
	ExcludeFiles       []string
}

var configFilename = ""

// ------------------------------------------
// init
// ------------------------------------------
func init() {
	fullFilePath, _ := filepath.Abs(os.Args[0])

	// Set the config file name to [thisexecutablepath\thisexecutablefilename].config
	configFilename = fmt.Sprintf("%s\\%s", filepath.Dir(fullFilePath), strings.Replace(filepath.Base(fullFilePath), ".exe", ".config", 1))
}

// ------------------------------------------
// LoadConfig
// ------------------------------------------
func LoadConfig() Config {
	var c Config

	// Read whole the file
	var jsonstring, err = ioutil.ReadFile(configFilename)
	if err != nil {
		createConfig()

		// Read the file again, now it should exist
		jsonstring, err = ioutil.ReadFile(configFilename)
	}

	// Strip comments from config file
	var re = regexp.MustCompile("(?s)//.*?\n|/\\*.*?\\*/")
	var newJsonstring = re.ReplaceAll(jsonstring, nil)

	// Create config to be returned
	err = json.Unmarshal(newJsonstring, &c)

	if err != nil {
		fmt.Printf("Could not read the config, %v\n", err)
		c = Config{}
	}

	return c
}

// ------------------------------------------
// createConfig
// ------------------------------------------
func createConfig() {
	var sc Config

	// Extensions to count
	sc.Extensions = []string{
		".css",
		".go",
		".htm",
		".html",
		".js",
		".json",
		".less",
		".sass",
		".xml",
		".xsd",
	}

	// Binary extensions to count
	sc.BinaryExtensions = []string{
		".gif",
		".ico",
		".jpg",
		".png",
	}

	// Directories to exclude
	sc.ExcludeDirectories = []string{
		".svn",
		"bin",
		"obj",
		"Scripts\\jquery.ui",
		"_svn",
	}

	// Directories to exclude
	sc.ExcludeFiles = []string{
		filepath.Base(configFilename),
		"jquery.min.js",
		"jquery.ui.js",
	}

	save(sc)
}

// ------------------------------------------
// save
// ------------------------------------------
func save(sc Config) {
	var jsonstring, err = json.MarshalIndent(&sc, "", "  ")
	if err != nil {
		fmt.Printf("json.Marshal(sc), %s %v\n", string(jsonstring), err)
	}

	err = ioutil.WriteFile(configFilename, jsonstring, 0666)
	if err != nil {
		fmt.Printf("ioutil.WriteFile, %v\n", err)
	}
}

{'fraction_non_alphanumeric': 0.2782491322792133, 'fraction_numerical': 0.0038565368299267257, 'mean_word_length': 1.9173228346456692, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1030588', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1648, 'n_words': 418}