github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/preprocessor/parse_include_list.go (about)

     1  package preprocessor
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // parseIncludeList - parse list of includes
     9  // Example :
    10  //
    11  //	exit.o: exit.c /usr/include/stdlib.h /usr/include/features.h \
    12  //	   /usr/include/stdc-predef.h /usr/include/x86_64-linux-gnu/sys/cdefs.h
    13  func parseIncludeList(line string) (lines []string, err error) {
    14  	line = strings.Replace(line, "\t", " ", -1)
    15  	line = strings.Replace(line, "\r", " ", -1) // Added for Mac endline symbol
    16  	line = strings.Replace(line, "\xFF", " ", -1)
    17  	line = strings.Replace(line, "\u0100", " ", -1)
    18  
    19  	sepLines := strings.Split(line, "\n")
    20  	var indexes []int
    21  	for i := range sepLines {
    22  		if !(strings.Index(sepLines[i], ":") < 0) {
    23  			indexes = append(indexes, i)
    24  		}
    25  	}
    26  	if len(indexes) > 1 {
    27  		for i := 0; i < len(indexes); i++ {
    28  			var partLines []string
    29  			var block string
    30  			if i+1 == len(indexes) {
    31  				block = strings.Join(sepLines[indexes[i]:], "\n")
    32  			} else {
    33  				block = strings.Join(sepLines[indexes[i]:indexes[i+1]], "\n")
    34  			}
    35  			partLines, err = parseIncludeList(block)
    36  			if err != nil {
    37  				return lines, fmt.Errorf("part of lines : %v. %v", i, err)
    38  			}
    39  			lines = append(lines, partLines...)
    40  		}
    41  		return
    42  	}
    43  
    44  	index := strings.Index(line, ":")
    45  	if index < 0 {
    46  		err = fmt.Errorf("cannot find `:` in line : %v", line)
    47  		return
    48  	}
    49  	line = line[index+1:]
    50  	parts := strings.Split(line, "\\\n")
    51  
    52  	for _, p := range parts {
    53  		p = strings.TrimSpace(p)
    54  		begin := 0
    55  		for i := 0; i <= len(p)-2; i++ {
    56  			if p[i] == '\\' && p[i+1] == ' ' {
    57  				i++
    58  				continue
    59  			}
    60  			if p[i] == ' ' {
    61  				lines = append(lines, p[begin:i])
    62  				begin = i + 1
    63  			}
    64  			if i == len(p)-2 {
    65  				lines = append(lines, p[begin:])
    66  			}
    67  		}
    68  	}
    69  again:
    70  	for i := range lines {
    71  		if lines[i] == "" {
    72  			lines = append(lines[:i], lines[i+1:]...)
    73  			goto again
    74  		}
    75  		lines[i] = strings.Replace(lines[i], "\\", "", -1)
    76  	}
    77  
    78  	return
    79  }