Closest you can get would be a robust TryParse
method.
But instead of re-inventing the wheel, try first a few libraries, they might do the job:
https://github.com/TinyCsvParser/TinyCsvParser
https://github.com/nreco/csv
Note that CSV parsing can be a difficult task even though it's a simple format.
Even if you can't use a library, there are plenty of ideas you can grab from them.
If I were to detect CSV content, I'd do the following:
- ensure that a line contains readable characters
- optionally detecting file enconding might help
- ensure that a line isn't incredibly long, else it's likely to be binary, see #1
- detect that first line has repeating separators
- try parse lines
More or less this:
- find 1st index of CR/LF or LF
- read up to that
- find separators in it
- try parse the rest of the file, check against column count
- if it fails then it's probably not CSV
It's pretty much all the heuristics you can try unless I'm mistaken.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…