MCPcopy Index your code
hub / github.com/codechenx/FastTableViewer / loadFileToBuffer

Function loadFileToBuffer

io.go:324–440  ·  view source on GitHub ↗

load file content to buffer (synchronous version for small files or when preferred)

(fn string, b *Buffer)

Source from the content-addressed store, hash-verified

322
323// load file content to buffer (synchronous version for small files or when preferred)
324func 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 }

Calls 10

sepDetectMethod · 0.95
newProgressTrackerFunction · 0.85
getFileScannerFunction · 0.85
skipLineFunction · 0.85
fatalErrorFunction · 0.85
addDRToBufferFunction · 0.85
finishMethod · 0.80
incrementMethod · 0.80
detectAllColumnTypesMethod · 0.80
enableStringInterningMethod · 0.80