Skip to content

Commit

Permalink
Fixed C# files not getting approved
Browse files Browse the repository at this point in the history
Problem was encoding: When you make a C# project, the .cs file is
encoded as "UTF-8 with BOM", meaning that there are three hidden symbols
in the beginning of the file.
  • Loading branch information
Duckapple committed Nov 14, 2019
1 parent c597e99 commit dd08c63
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions commands/submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from helpers.config import getConfig, getUrl
from commands.archive import archiveCommand
from helpers.sound import losesound, winsound
from helpers.fileutils import undoBOM


class Response(Enum):
Expand Down Expand Up @@ -122,6 +123,7 @@ def postSubmission(config, session, problemName, programFile):
data["mainclass"] = detectClassName(programFile)

sub_files = []
undoBOM(programFile["relativePath"])
with open(programFile["relativePath"]) as sub_file:
sub_files.append(
(
Expand Down
20 changes: 20 additions & 0 deletions helpers/fileutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,23 @@ def findProblemLocation(problemName):
if os.path.exists(folder + problemName):
return folder
return None


def undoBOM(path):
import sys, codecs
BUFSIZE = 4096
BOMLEN = len(codecs.BOM_UTF8)

with open(path, "r+b") as fp:
chunk = fp.read(BUFSIZE)
if chunk.startswith(codecs.BOM_UTF8):
i = 0
chunk = chunk[BOMLEN:]
while chunk:
fp.seek(i)
fp.write(chunk)
i += len(chunk)
fp.seek(BOMLEN, os.SEEK_CUR)
chunk = fp.read(BUFSIZE)
fp.seek(-BOMLEN, os.SEEK_CUR)
fp.truncate()

0 comments on commit dd08c63

Please sign in to comment.