Skip to content

Commit a7d8597

Browse files
committed
Initial working commit
0 parents  commit a7d8597

File tree

8 files changed

+815
-0
lines changed

8 files changed

+815
-0
lines changed

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
api.sh
2+
*.cpp
3+
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Dependency directories (remove the comment below to include it)
18+
# vendor/
19+
20+
cses-cli
21+
pkg/
22+
release/
23+
src/

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
bin=cses-cli
2+
.PHONY: clean
3+
all: clean $(bin)
4+
5+
cses-cli:
6+
echo ${CURDIR}
7+
GOPATH=${CURDIR} CGO_ENABLED=0 go build -o $(bin)
8+
9+
clean:
10+
rm -fv $(bin)

helper.go

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package main
2+
3+
import(
4+
"net/http"
5+
"os"
6+
"bytes"
7+
"bufio"
8+
"mime/multipart"
9+
"path/filepath"
10+
"io"
11+
"io/ioutil"
12+
"strings"
13+
"os/exec"
14+
"runtime"
15+
16+
)
17+
18+
var unicodeMap = map[string]string{
19+
"\\le": "\u2264",
20+
"\\dots": "...",
21+
"\\cdots": "\u22EF",
22+
"\\rightarrow": "\u21D2",
23+
"\\times": "\u00D7",
24+
"\\alpha": "\u03B1",
25+
"\\beta": "\u03B2",
26+
"\\gamma": "\u03B3",
27+
"\\delta": "\u03B4",
28+
"\\epsilon": "\u03F5",
29+
"\\zeta": "\u03B6",
30+
"\\eta": "\u03B7",
31+
"\\theta": "\u03B8",
32+
"\\iota": "\u03B9",
33+
"\\kappa": "\u03BA",
34+
"\\lambda": "\u03BB",
35+
// parallel: '2225',
36+
// mid: '2223',
37+
// dashv: '22A3',
38+
// vdash: '22A2',
39+
"\\leq": "\u2264",
40+
// geq: '2265',
41+
"\\ge": "\u2265",
42+
"\\lt": "\u003C",
43+
"\\gt": "\u003E",
44+
// succ: '227B',
45+
// prec: '227A',
46+
// approx: '2248',
47+
// succeq: '2AB0',
48+
// preceq: '2AAF',
49+
"\\supset": "\u2283",
50+
"\\subset": "\u2282",
51+
// supseteq: '2287',
52+
// subseteq: '2286',
53+
"\\uparrow": "\u2191",
54+
// '\\downarrow': '2193',
55+
// '\\updownarrow': '2195',
56+
// '\\Uparrow': '21D1',
57+
// '\\Downarrow': '21D3',
58+
// '\\Updownarrow': '21D5',
59+
"\\backslash": "\\",
60+
// '\\rangle': '27E9',
61+
// '\\langle': '27E8',
62+
"\\rbrace": "}",
63+
"\\lbrace": "{",
64+
"\\}": "}",
65+
"\\{": "{",
66+
"\\[": "[",
67+
"\\]": "]",
68+
"\\rceil": "\u2309",
69+
"\\lceil": "\u2308",
70+
"\\rfloor": "\u230B",
71+
"\\lfloor": "\u230A",
72+
"\\lbrack": "[",
73+
"\\rbrack": "]",
74+
}
75+
76+
func check(e error) {
77+
if e != nil {
78+
panic(e)
79+
}
80+
}
81+
82+
func UserHomeDir() string {
83+
if runtime.GOOS == "windows" {
84+
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
85+
if home == "" {
86+
home = os.Getenv("USERPROFILE")
87+
}
88+
return home
89+
}
90+
return os.Getenv("HOME")
91+
}
92+
93+
func cacheSet(filename string, data string, root string) {
94+
f, err := os.Create(filepath.Join(root, filename))
95+
check(err)
96+
97+
w := bufio.NewWriter(f)
98+
w.WriteString(data)
99+
100+
w.Flush()
101+
defer f.Close()
102+
}
103+
104+
func cacheGet(filename string, root string) ([]byte, bool) {
105+
path := filepath.Join(root, filename)
106+
107+
_, err := os.Stat(path)
108+
if os.IsNotExist(err) {
109+
return nil, false
110+
}
111+
content, err := ioutil.ReadFile(path)
112+
check(err)
113+
return content, true
114+
}
115+
116+
func getTaskFromCache(task string, root string) string {
117+
path := filepath.Join(root, task + ".task.html")
118+
119+
output, err := exec.Command("bash", "-c", "lynx -dump "+path).Output()
120+
check(err)
121+
data := string(output)
122+
123+
for k, v := range unicodeMap {
124+
data = strings.Replace(data, k, v, -1)
125+
}
126+
127+
return data
128+
}
129+
130+
func writeCodeFile(filename string, text string, template string) bool {
131+
if _, err := os.Stat(filename); err == nil {
132+
return true
133+
}
134+
135+
f, err := os.Create(filename)
136+
if err != nil {
137+
return false
138+
}
139+
_, err = f.WriteString("/*\n" + text + "*/\n" + template)
140+
if err != nil {
141+
f.Close()
142+
return false
143+
}
144+
145+
err = f.Close()
146+
if err != nil {
147+
return false
148+
}
149+
return true
150+
}
151+
152+
func newfileUploadRequest(uri string, params map[string]string, paramName, path string, cookie string) (*http.Request, error) {
153+
file, err := os.Open(path)
154+
if err != nil {
155+
return nil, err
156+
}
157+
defer file.Close()
158+
159+
body := &bytes.Buffer{}
160+
writer := multipart.NewWriter(body)
161+
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
162+
if err != nil {
163+
return nil, err
164+
}
165+
_, err = io.Copy(part, file)
166+
167+
for key, val := range params {
168+
_ = writer.WriteField(key, val)
169+
}
170+
err = writer.Close()
171+
if err != nil {
172+
return nil, err
173+
}
174+
175+
return newfileUploadRequestPost(uri, body, cookie, writer.FormDataContentType())
176+
}
177+
178+
func fileMeta(filename string) (string, string, string, bool) {
179+
parts := strings.Split(filepath.Base(filename), ".")
180+
lang := parts[2]
181+
option := ""
182+
if lang == "cpp" {
183+
lang = "C++"
184+
option = "C++17"
185+
}
186+
return parts[0], lang, option, true
187+
}

0 commit comments

Comments
 (0)