comparison liboctave/system/file-ops.cc @ 29654:d13d090cb03a stable

use std::size_t and std::ptrdiff_t in C++ code (bug #60471) Files affected: make_int.cc, file-editor-tab.cc, octave-qscintilla.cc, Cell.cc, Cell.h, call-stack.cc, call-stack.h, cellfun.cc, data.cc, debug.cc, dlmread.cc, error.cc, event-queue.h, fcn-info.cc, fcn-info.h, file-io.cc, ft-text-renderer.cc, gl2ps-print.cc, graphics.cc, graphics.in.h, help.cc, hex2num.cc, input.cc, latex-text-renderer.cc, load-path.cc, load-save.cc, load-save.h, ls-hdf5.cc, ls-mat-ascii.cc, ls-mat5.cc, ls-oct-text.cc, mex.cc, mexproto.h, mxarray.h, oct-map.cc, oct-stream.cc, oct-stream.h, pager.cc, pager.h, pr-output.cc, regexp.cc, settings.h, stack-frame.cc, stack-frame.h, strfns.cc, syminfo.cc, symrec.h, symscope.cc, symscope.h, symtab.cc, sysdep.cc, toplev.cc, utils.cc, utils.h, variables.cc, __fltk_uigetfile__.cc, __init_fltk__.cc, audioread.cc, gzip.cc, cdef-class.cc, cdef-manager.cc, cdef-method.cc, cdef-object.cc, cdef-object.h, ov-base-diag.cc, ov-base-diag.h, ov-base-mat.cc, ov-base-mat.h, ov-base-scalar.cc, ov-base-scalar.h, ov-base-sparse.h, ov-base.cc, ov-base.h, ov-cell.cc, ov-cell.h, ov-ch-mat.cc, ov-class.cc, ov-class.h, ov-classdef.cc, ov-fcn-handle.cc, ov-java.cc, ov-lazy-idx.h, ov-perm.cc, ov-perm.h, ov-range.h, ov-str-mat.cc, ov-struct.cc, ov-struct.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ov.h, ovl.cc, octave.cc, bp-table.cc, jit-ir.cc, jit-ir.h, jit-typeinfo.cc, jit-typeinfo.h, jit-util.h, lex.h, lex.ll, oct-lvalue.cc, oct-parse.yy, parse.h, profiler.h, pt-eval.cc, pt-eval.h, pt-jit.cc, pt-jit.h, pt-pr-code.cc, pt-tm-const.cc, pt-tm-const.h, Array.h, CMatrix.cc, DiagArray2.h, PermMatrix.h, Sparse.h, dMatrix.cc, fCMatrix.cc, fMatrix.cc, bsxfun-defs.cc, oct-fftw.cc, oct-fftw.h, randpoisson.cc, sparse-chol.cc, mx-inlines.cc, file-ops.cc, lo-sysdep.cc, oct-env.cc, oct-time.cc, action-container.cc, action-container.h, base-list.h, caseless-str.h, cmd-edit.cc, cmd-hist.cc, data-conv.cc, data-conv.h, f77-fcn.h, file-info.cc, file-info.h, kpse.cc, kpse.h, lo-cutils.h, lo-hash.h, lo-regexp.cc, oct-base64.cc, oct-base64.h, oct-binmap.h, oct-glob.cc, oct-shlib.cc, oct-shlib.h, oct-sort.cc, oct-sparse.h, oct-string.cc, quit.cc, unwind-prot.h, url-transfer.cc, main.in.cc, mkoctfile.in.cc, and shared-fcns.h. (grafted from aef11bb4e6d1f303ad9de5688fcb7244ef48867e)
author John W. Eaton <jwe@octave.org>
date Wed, 28 Apr 2021 22:57:42 -0400
parents 0a5b15007766
children 32f4357ac8d9 7f5bd197fea6
comparison
equal deleted inserted replaced
29652:5068a97f0f39 29654:d13d090cb03a
73 // The default value of tilde_additional_suffixes. This is set to 73 // The default value of tilde_additional_suffixes. This is set to
74 // whitespace or newline so that simple programs which do not perform 74 // whitespace or newline so that simple programs which do not perform
75 // any word separation get desired behavior. 75 // any word separation get desired behavior.
76 static const char *default_suffixes[] = { " ", "\n", ":", nullptr }; 76 static const char *default_suffixes[] = { " ", "\n", ":", nullptr };
77 77
78 static size_t 78 static std::size_t
79 tilde_find_prefix (const std::string& s, size_t& len) 79 tilde_find_prefix (const std::string& s, std::size_t& len)
80 { 80 {
81 len = 0; 81 len = 0;
82 82
83 size_t s_len = s.length (); 83 std::size_t s_len = s.length ();
84 84
85 if (s_len == 0 || s[0] == '~') 85 if (s_len == 0 || s[0] == '~')
86 return 0; 86 return 0;
87 87
88 string_vector prefixes = sys::file_ops::tilde_additional_prefixes; 88 string_vector prefixes = sys::file_ops::tilde_additional_prefixes;
89 89
90 if (! prefixes.empty ()) 90 if (! prefixes.empty ())
91 { 91 {
92 for (size_t i = 0; i < s_len; i++) 92 for (std::size_t i = 0; i < s_len; i++)
93 { 93 {
94 for (int j = 0; j < prefixes.numel (); j++) 94 for (int j = 0; j < prefixes.numel (); j++)
95 { 95 {
96 size_t pfx_len = prefixes[j].length (); 96 std::size_t pfx_len = prefixes[j].length ();
97 97
98 if (prefixes[j] == s.substr (i, pfx_len)) 98 if (prefixes[j] == s.substr (i, pfx_len))
99 { 99 {
100 len = pfx_len - 1; 100 len = pfx_len - 1;
101 return i + len; 101 return i + len;
108 } 108 }
109 109
110 // Find the end of a tilde expansion in S, and return the index 110 // Find the end of a tilde expansion in S, and return the index
111 // of the character which ends the tilde definition. 111 // of the character which ends the tilde definition.
112 112
113 static size_t 113 static std::size_t
114 tilde_find_suffix (const std::string& s) 114 tilde_find_suffix (const std::string& s)
115 { 115 {
116 size_t s_len = s.length (); 116 std::size_t s_len = s.length ();
117 117
118 string_vector suffixes = sys::file_ops::tilde_additional_suffixes; 118 string_vector suffixes = sys::file_ops::tilde_additional_suffixes;
119 119
120 size_t i = 0; 120 std::size_t i = 0;
121 121
122 for ( ; i < s_len; i++) 122 for ( ; i < s_len; i++)
123 { 123 {
124 if (sys::file_ops::is_dir_sep (s[i])) 124 if (sys::file_ops::is_dir_sep (s[i]))
125 break; 125 break;
126 126
127 if (! suffixes.empty ()) 127 if (! suffixes.empty ())
128 { 128 {
129 for (int j = 0; j < suffixes.numel (); j++) 129 for (int j = 0; j < suffixes.numel (); j++)
130 { 130 {
131 size_t sfx_len = suffixes[j].length (); 131 std::size_t sfx_len = suffixes[j].length ();
132 132
133 if (suffixes[j] == s.substr (i, sfx_len)) 133 if (suffixes[j] == s.substr (i, sfx_len))
134 return i; 134 return i;
135 } 135 }
136 } 136 }
142 // Take FNAME and return the tilde prefix we want expanded. 142 // Take FNAME and return the tilde prefix we want expanded.
143 143
144 static std::string 144 static std::string
145 isolate_tilde_prefix (const std::string& fname) 145 isolate_tilde_prefix (const std::string& fname)
146 { 146 {
147 size_t f_len = fname.length (); 147 std::size_t f_len = fname.length ();
148 148
149 size_t len = 1; 149 std::size_t len = 1;
150 150
151 while (len < f_len && ! sys::file_ops::is_dir_sep (fname[len])) 151 while (len < f_len && ! sys::file_ops::is_dir_sep (fname[len]))
152 len++; 152 len++;
153 153
154 return fname.substr (1, len); 154 return fname.substr (1, len);
158 // tilde. 158 // tilde.
159 159
160 static std::string 160 static std::string
161 tilde_expand_word (const std::string& filename) 161 tilde_expand_word (const std::string& filename)
162 { 162 {
163 size_t f_len = filename.length (); 163 std::size_t f_len = filename.length ();
164 164
165 if (f_len == 0 || filename[0] != '~') 165 if (f_len == 0 || filename[0] != '~')
166 return std::string (filename); 166 return std::string (filename);
167 167
168 // A leading '~/' or a bare '~' is *always* translated to the value 168 // A leading '~/' or a bare '~' is *always* translated to the value
172 if (f_len == 1 || sys::file_ops::is_dir_sep (filename[1])) 172 if (f_len == 1 || sys::file_ops::is_dir_sep (filename[1]))
173 return sys::env::get_home_directory () + filename.substr (1); 173 return sys::env::get_home_directory () + filename.substr (1);
174 174
175 std::string username = isolate_tilde_prefix (filename); 175 std::string username = isolate_tilde_prefix (filename);
176 176
177 size_t user_len = username.length (); 177 std::size_t user_len = username.length ();
178 178
179 std::string dirname; 179 std::string dirname;
180 180
181 if (sys::file_ops::tilde_expansion_preexpansion_hook) 181 if (sys::file_ops::tilde_expansion_preexpansion_hook)
182 { 182 {
289 return std::string (name); 289 return std::string (name);
290 else 290 else
291 { 291 {
292 std::string result; 292 std::string result;
293 293
294 size_t name_len = name.length (); 294 std::size_t name_len = name.length ();
295 295
296 // Scan through S expanding tildes as we come to them. 296 // Scan through S expanding tildes as we come to them.
297 297
298 size_t pos = 0; 298 std::size_t pos = 0;
299 299
300 while (1) 300 while (1)
301 { 301 {
302 if (pos > name_len) 302 if (pos > name_len)
303 break; 303 break;
304 304
305 size_t len; 305 std::size_t len;
306 306
307 // Make START point to the tilde which starts the expansion. 307 // Make START point to the tilde which starts the expansion.
308 308
309 size_t start = tilde_find_prefix (name.substr (pos), len); 309 std::size_t start = tilde_find_prefix (name.substr (pos), len);
310 310
311 result.append (name.substr (pos, start)); 311 result.append (name.substr (pos, start));
312 312
313 // Advance STRING to the starting tilde. 313 // Advance STRING to the starting tilde.
314 314
315 pos += start; 315 pos += start;
316 316
317 // Make FINI be the index of one after the last character of the 317 // Make FINI be the index of one after the last character of the
318 // username. 318 // username.
319 319
320 size_t fini = tilde_find_suffix (name.substr (pos)); 320 std::size_t fini = tilde_find_suffix (name.substr (pos));
321 321
322 // If both START and FINI are zero, we are all done. 322 // If both START and FINI are zero, we are all done.
323 323
324 if (! (start || fini)) 324 if (! (start || fini))
325 break; 325 break;
360 : dir + dir_sep_char () + file); 360 : dir + dir_sep_char () + file);
361 } 361 }
362 362
363 std::string dirname (const std::string& path) 363 std::string dirname (const std::string& path)
364 { 364 {
365 size_t ipos = path.find_last_of (dir_sep_chars ()); 365 std::size_t ipos = path.find_last_of (dir_sep_chars ());
366 366
367 return (ipos != std::string::npos) ? path.substr (0, ipos) : ""; 367 return (ipos != std::string::npos) ? path.substr (0, ipos) : "";
368 } 368 }
369 369
370 std::string tail (const std::string& path) 370 std::string tail (const std::string& path)
371 { 371 {
372 size_t ipos = path.find_last_of (dir_sep_chars ()); 372 std::size_t ipos = path.find_last_of (dir_sep_chars ());
373 373
374 if (ipos != std::string::npos) 374 if (ipos != std::string::npos)
375 ipos++; 375 ipos++;
376 else 376 else
377 ipos = 0; 377 ipos = 0;
385 385
386 if (dir_sep_char () == '/') 386 if (dir_sep_char () == '/')
387 retval = path; 387 retval = path;
388 else 388 else
389 { 389 {
390 size_t n = path.length (); 390 std::size_t n = path.length ();
391 for (size_t i = 0; i < n; i++) 391 for (std::size_t i = 0; i < n; i++)
392 { 392 {
393 if (path[i] == '/') 393 if (path[i] == '/')
394 retval += dir_sep_char(); 394 retval += dir_sep_char();
395 else 395 else
396 retval += path[i]; 396 retval += path[i];