summaryrefslogtreecommitdiffstats
path: root/source/core/slang-io.cpp
blob: da36edbbf4087be55a90c96d29a489216bdd9af2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "slang-io.h"
#include "exception.h"
#ifndef __STDC__
#define __STDC__ 1
#endif
#include <sys/stat.h>
#ifdef _WIN32
#include <direct.h>
#endif
namespace CoreLib
{
	namespace IO
	{
		using namespace CoreLib::Basic;

		CommandLineWriter * currentCommandWriter = nullptr;

		void SetCommandLineWriter(CommandLineWriter * writer)
		{
			currentCommandWriter = writer;
		}

		bool File::Exists(const String & fileName)
		{
#ifdef _WIN32
			struct _stat32 statVar;
			return ::_wstat32(((String)fileName).ToWString(), &statVar) != -1;
#else
			struct stat statVar;
			return ::stat(fileName.Buffer(), &statVar) == 0;
#endif
		}

		String Path::TruncateExt(const String & path)
		{
			int dotPos = path.LastIndexOf('.');
			if (dotPos != -1)
				return path.SubString(0, dotPos);
			else
				return path;
		}
		String Path::ReplaceExt(const String & path, const char * newExt)
		{
			StringBuilder sb(path.Length()+10);
			int dotPos = path.LastIndexOf('.');
			if (dotPos == -1)
				dotPos = path.Length();
			sb.Append(path.Buffer(), dotPos);
			sb.Append('.');
			sb.Append(newExt);
			return sb.ProduceString();
		}
		String Path::GetFileName(const String & path)
		{
			int pos = path.LastIndexOf('/');
			pos = Math::Max(path.LastIndexOf('\\'), pos) + 1;
			return path.SubString(pos, path.Length()-pos);
		}
		String Path::GetFileNameWithoutEXT(const String & path)
		{
			int pos = path.LastIndexOf('/');
			pos = Math::Max(path.LastIndexOf('\\'), pos) + 1;
			int dotPos = path.LastIndexOf('.');
			if (dotPos <= pos)
				dotPos = path.Length();
			return path.SubString(pos, dotPos - pos);
		}
		String Path::GetFileExt(const String & path)
		{
			int dotPos = path.LastIndexOf('.');
			if (dotPos != -1)
				return path.SubString(dotPos+1, path.Length()-dotPos-1);
			else
				return "";
		}
		String Path::GetDirectoryName(const String & path)
		{
			int pos = path.LastIndexOf('/');
			pos = Math::Max(path.LastIndexOf('\\'), pos);
			if (pos != -1)
				return path.SubString(0, pos);
			else
				return "";
		}
		String Path::Combine(const String & path1, const String & path2)
		{
			if (path1.Length() == 0) return path2;
			StringBuilder sb(path1.Length()+path2.Length()+2);
			sb.Append(path1);
			if (!path1.EndsWith('\\') && !path1.EndsWith('/'))
				sb.Append(PathDelimiter);
			sb.Append(path2);
			return sb.ProduceString();
		}
		String Path::Combine(const String & path1, const String & path2, const String & path3)
		{
			StringBuilder sb(path1.Length()+path2.Length()+path3.Length()+3);
			sb.Append(path1);
			if (!path1.EndsWith('\\') && !path1.EndsWith('/'))
				sb.Append(PathDelimiter);
			sb.Append(path2);
			if (!path2.EndsWith('\\') && !path2.EndsWith('/'))
				sb.Append(PathDelimiter);
			sb.Append(path3);
			return sb.ProduceString();
		}

		bool Path::CreateDir(const String & path)
		{
#if defined(_WIN32)
			return _wmkdir(path.ToWString()) == 0;
#else 
			return mkdir(path.Buffer(), 0777) == 0;
#endif
		}

		CoreLib::Basic::String File::ReadAllText(const CoreLib::Basic::String & fileName)
		{
			StreamReader reader(new FileStream(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite));
			return reader.ReadToEnd();
		}

		CoreLib::Basic::List<unsigned char> File::ReadAllBytes(const CoreLib::Basic::String & fileName)
		{
			RefPtr<FileStream> fs = new FileStream(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite);
			List<unsigned char> buffer;
			while (!fs->IsEnd())
			{
				unsigned char ch;
				int read = (int)fs->Read(&ch, 1);
				if (read)
					buffer.Add(ch);
				else
					break;
			}
			return _Move(buffer);
		}

		void File::WriteAllText(const CoreLib::Basic::String & fileName, const CoreLib::Basic::String & text)
		{
			StreamWriter writer(new FileStream(fileName, FileMode::Create));
			writer.Write(text);
		}
	}
}