load file content to buffer (synchronous version for small files or when preferred)
(fn string, b *Buffer)
| 322 | |
| 323 | // load file content to buffer (synchronous version for small files or when preferred) |
| 324 | func loadFileToBuffer(fn string, b *Buffer) error { |
| 325 | totalAddedLN := 0 //the number of lines has been added into buffer |
| 326 | |
| 327 | // Get file size for progress tracking |
| 328 | fileInfo, err := os.Stat(fn) |
| 329 | if err != nil { |
| 330 | return err |
| 331 | } |
| 332 | var fileSize int64 |
| 333 | if !fileInfo.IsDir() { |
| 334 | fileSize = fileInfo.Size() |
| 335 | } |
| 336 | |
| 337 | // Create progress tracker |
| 338 | progress := newProgressTracker(fileSize, true) |
| 339 | |
| 340 | scanner, err := getFileScanner(fn) |
| 341 | if err != nil { |
| 342 | return err |
| 343 | } |
| 344 | scanner.Split(bufio.ScanLines) |
| 345 | //set separator, if user does not provide it. |
| 346 | var detectLines []string //lines as detect separator data |
| 347 | if b.sep == 0 { |
| 348 | //read 10 lines to detect separator |
| 349 | lineNumber := 10 |
| 350 | for scanner.Scan() { |
| 351 | line := scanner.Text() |
| 352 | //skip empty line |
| 353 | if line == "\n" { |
| 354 | continue |
| 355 | } |
| 356 | //ignore first n lines |
| 357 | if args.SkipNum > 0 { |
| 358 | args.SkipNum-- |
| 359 | continue |
| 360 | } |
| 361 | //ignore line with specified prefix |
| 362 | if skipLine(line, args.SkipSymbol) { |
| 363 | continue |
| 364 | } |
| 365 | detectLines = append(detectLines, line) |
| 366 | if len(detectLines) >= lineNumber { |
| 367 | break |
| 368 | } |
| 369 | } |
| 370 | //if the suffix of file name is ".csv", set separator to ",". |
| 371 | //if the suffix of file name is "tsv", set separator to "\t". |
| 372 | if strings.HasSuffix(fn, ".csv") { |
| 373 | b.sep = ',' |
| 374 | } else if strings.HasSuffix(fn, ".tsv") { |
| 375 | b.sep = '\t' |
| 376 | } else { |
| 377 | sd := sepDetecor{} |
| 378 | b.sep = sd.sepDetect(detectLines) |
| 379 | } |
| 380 | |
| 381 | } |