-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs50.h
108 lines (97 loc) · 4.15 KB
/
cs50.h
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
/**
* CS50 Library for C
* https://github.com/cs50/libcs50
*
* Based on Eric Roberts' genlib.c and simpio.c.
*
* Copyright (c) 2019
* All rights reserved
*
* BSD 3-Clause License
* http://www.opensource.org/licenses/BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of CS50 nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _CS50_H
#define _CS50_H
#include <float.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
/**
* Our own type for bytes.
*/
typedef unsigned char byte;
/**
* Our own type for strings.
*/
typedef char *string;
/**
* Prompts user for a line of text from standard input and returns the
* equivalent char; if text is not a single char, user is prompted
* to retry. If line can't be read, returns CHAR_MAX.
*/
char get_char(const string format, ...) __attribute__((format(printf, 1, 2)));
/**
* Prompts user for a line of text from standard input and returns the
* equivalent double as precisely as possible; if text does not represent
* a double or if value would cause underflow or overflow, user is
* prompted to retry. If line can't be read, returns DBL_MAX.
*/
double get_double(const string format, ...) __attribute__((format(printf, 1, 2)));
/**
* Prompts user for a line of text from standard input and returns the
* equivalent float as precisely as possible; if text does not represent
* a float or if value would cause underflow or overflow, user is prompted
* to retry. If line can't be read, returns FLT_MAX.
*/
float get_float(const string format, ...) __attribute__((format(printf, 1, 2)));
/**
* Prompts user for a line of text from standard input and returns the
* equivalent int; if text does not represent an int in [-2^31, 2^31 - 1)
* or would cause underflow or overflow, user is prompted to retry. If line
* can't be read, returns INT_MAX.
*/
int get_int(const string format, ...) __attribute__((format(printf, 1, 2)));
/**
* Prompts user for a line of text from standard input and returns the
* equivalent long; if text does not represent a long in
* [-2^63, 2^63 - 1) or would cause underflow or overflow, user is
* prompted to retry. If line can't be read, returns LONG_MAX.
*/
long get_long(const string format, ...) __attribute__((format(printf, 1, 2)));
/**
* Prompts user for a line of text from standard input and returns
* it as a string (char *), sans trailing line ending. Supports
* CR (\r), LF (\n), and CRLF (\r\n) as line endings. If user
* inputs only a line ending, returns "", not NULL. Returns NULL
* upon error or no input whatsoever (i.e., just EOF). Stores string
* on heap, but library's destructor frees memory on program's exit.
*/
string get_string(va_list *args, const string format, ...) __attribute__((format(printf, 2, 3)));
#define get_string(...) get_string(NULL, __VA_ARGS__)
#endif