formating.go 2.24 KB
Newer Older
Etienne Renault's avatar
Etienne Renault committed
1 2 3 4 5
// Copyright (C) 2020 Laboratoire de Recherche et Developpement
// de l'EPITA (LRDE).
//
// This file is part of Go2Pins, a tool for Golang model-checking
//
Etienne Renault's avatar
Etienne Renault committed
6
// Go2Pins is free software; you can redistribute it and/or modify it
Etienne Renault's avatar
Etienne Renault committed
7 8 9 10
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
Etienne Renault's avatar
Etienne Renault committed
11
// Go2Pins is distributed in the hope that it will be useful, but WITHOUT
Etienne Renault's avatar
Etienne Renault committed
12 13 14 15 16 17 18
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

Hugo Moreau's avatar
Hugo Moreau committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
package tools

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

func FormatArray(src, pattern string) {
	input, err := ioutil.ReadFile(src)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	output := ""
	scanner := bufio.NewScanner(strings.NewReader(string(input)))
	for scanner.Scan() {
		if strings.Contains(scanner.Text(), pattern) {
Hugo Moreau's avatar
Hugo Moreau committed
40
			array, counter := []string{""}, 0
Hugo Moreau's avatar
Hugo Moreau committed
41
			for i := 0; i < len(scanner.Text()); i++ {
Hugo Moreau's avatar
Hugo Moreau committed
42 43 44
				if string(scanner.Text()[i]) != "}" {
					array[counter] += string(scanner.Text()[i])
				}
Hugo Moreau's avatar
Hugo Moreau committed
45
				if string(scanner.Text()[i]) == "{" {
Hugo Moreau's avatar
Hugo Moreau committed
46 47
					counter += 1
					array = append(array, "")
Hugo Moreau's avatar
Hugo Moreau committed
48
				} else if string(scanner.Text()[i]) == "," {
Hugo Moreau's avatar
Hugo Moreau committed
49 50
					counter += 1
					array = append(array, "")
Hugo Moreau's avatar
Hugo Moreau committed
51
				} else if string(scanner.Text()[i]) == "}" {
Hugo Moreau's avatar
Hugo Moreau committed
52 53 54 55 56 57 58 59 60 61
					array[counter] += ","
					counter += 1
					array = append(array, "")
					array[counter] += string(scanner.Text()[i])
				}
			}
			max, counter := 0, 0
			for _, val := range array {
				if len(val) > max {
					max = len(val)
Hugo Moreau's avatar
Hugo Moreau committed
62 63
				}
			}
Hugo Moreau's avatar
Hugo Moreau committed
64 65 66 67 68 69 70 71 72
			max += 1
			output += fmt.Sprintf("%s\n", array[0])
			output += fmt.Sprintf("\t%*s// %d\n", -max, array[1], counter)
			counter += 1
			for i := 2; i < len(array)-1; i++ {
				output += fmt.Sprintf("\t%*s// %d\n", -max, array[i][1:], counter)
				counter += 1
			}
			output += fmt.Sprintf("%s\n", array[len(array)-1])
Hugo Moreau's avatar
Hugo Moreau committed
73 74 75 76 77 78 79 80 81 82
		} else {
			output += scanner.Text() + "\n"
		}
	}
	err = ioutil.WriteFile(src, []byte(output), 0644)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}