Mercurial > vim
comparison src/misc2.c @ 19396:a961efb326e5 v8.2.0256
patch 8.2.0256: time and timer related code is spread out
Commit: https://github.com/vim/vim/commit/0a8fed6231c84e4e1b3a7dd6c0d95d3f98207fe0
Author: Bram Moolenaar <Bram@vim.org>
Date: Fri Feb 14 13:22:17 2020 +0100
patch 8.2.0256: time and timer related code is spread out
Problem: Time and timer related code is spread out.
Solution: Move time and timer related code to a new file. (Yegappan
Lakshmanan, closes #5604)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Fri, 14 Feb 2020 13:30:05 +0100 |
parents | 80b40bd5ec1a |
children | 607e5d7968b9 |
comparison
equal
deleted
inserted
replaced
19395:eb5ef6f5f58b | 19396:a961efb326e5 |
---|---|
4125 n = (n << 8) + (unsigned)c; | 4125 n = (n << 8) + (unsigned)c; |
4126 return (int)n; | 4126 return (int)n; |
4127 } | 4127 } |
4128 | 4128 |
4129 /* | 4129 /* |
4130 * Read 8 bytes from "fd" and turn them into a time_T, MSB first. | |
4131 * Returns -1 when encountering EOF. | |
4132 */ | |
4133 time_T | |
4134 get8ctime(FILE *fd) | |
4135 { | |
4136 int c; | |
4137 time_T n = 0; | |
4138 int i; | |
4139 | |
4140 for (i = 0; i < 8; ++i) | |
4141 { | |
4142 c = getc(fd); | |
4143 if (c == EOF) return -1; | |
4144 n = (n << 8) + c; | |
4145 } | |
4146 return n; | |
4147 } | |
4148 | |
4149 /* | |
4150 * Read a string of length "cnt" from "fd" into allocated memory. | 4130 * Read a string of length "cnt" from "fd" into allocated memory. |
4151 * Returns NULL when out of memory or unable to read that many bytes. | 4131 * Returns NULL when out of memory or unable to read that many bytes. |
4152 */ | 4132 */ |
4153 char_u * | 4133 char_u * |
4154 read_string(FILE *fd, int cnt) | 4134 read_string(FILE *fd, int cnt) |
4188 for (i = len - 1; i >= 0; --i) | 4168 for (i = len - 1; i >= 0; --i) |
4189 if (putc((int)(nr >> (i * 8)), fd) == EOF) | 4169 if (putc((int)(nr >> (i * 8)), fd) == EOF) |
4190 return FAIL; | 4170 return FAIL; |
4191 return OK; | 4171 return OK; |
4192 } | 4172 } |
4193 | |
4194 #ifdef _MSC_VER | |
4195 # if (_MSC_VER <= 1200) | |
4196 // This line is required for VC6 without the service pack. Also see the | |
4197 // matching #pragma below. | |
4198 # pragma optimize("", off) | |
4199 # endif | |
4200 #endif | |
4201 | |
4202 /* | |
4203 * Write time_T to file "fd" in 8 bytes. | |
4204 * Returns FAIL when the write failed. | |
4205 */ | |
4206 int | |
4207 put_time(FILE *fd, time_T the_time) | |
4208 { | |
4209 char_u buf[8]; | |
4210 | |
4211 time_to_bytes(the_time, buf); | |
4212 return fwrite(buf, (size_t)8, (size_t)1, fd) == 1 ? OK : FAIL; | |
4213 } | |
4214 | |
4215 /* | |
4216 * Write time_T to "buf[8]". | |
4217 */ | |
4218 void | |
4219 time_to_bytes(time_T the_time, char_u *buf) | |
4220 { | |
4221 int c; | |
4222 int i; | |
4223 int bi = 0; | |
4224 time_T wtime = the_time; | |
4225 | |
4226 // time_T can be up to 8 bytes in size, more than long_u, thus we | |
4227 // can't use put_bytes() here. | |
4228 // Another problem is that ">>" may do an arithmetic shift that keeps the | |
4229 // sign. This happens for large values of wtime. A cast to long_u may | |
4230 // truncate if time_T is 8 bytes. So only use a cast when it is 4 bytes, | |
4231 // it's safe to assume that long_u is 4 bytes or more and when using 8 | |
4232 // bytes the top bit won't be set. | |
4233 for (i = 7; i >= 0; --i) | |
4234 { | |
4235 if (i + 1 > (int)sizeof(time_T)) | |
4236 // ">>" doesn't work well when shifting more bits than avail | |
4237 buf[bi++] = 0; | |
4238 else | |
4239 { | |
4240 #if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4 | |
4241 c = (int)(wtime >> (i * 8)); | |
4242 #else | |
4243 c = (int)((long_u)wtime >> (i * 8)); | |
4244 #endif | |
4245 buf[bi++] = c; | |
4246 } | |
4247 } | |
4248 } | |
4249 | |
4250 #ifdef _MSC_VER | |
4251 # if (_MSC_VER <= 1200) | |
4252 # pragma optimize("", on) | |
4253 # endif | |
4254 #endif | |
4255 | 4173 |
4256 #endif | 4174 #endif |
4257 | 4175 |
4258 #if defined(FEAT_QUICKFIX) || defined(FEAT_SPELL) || defined(PROTO) | 4176 #if defined(FEAT_QUICKFIX) || defined(FEAT_SPELL) || defined(PROTO) |
4259 /* | 4177 /* |