| 1 | // Aseprite |
| 2 | // Copyright (C) 2022 Igara Studio S.A. |
| 3 | // Copyright (C) 2001-2017 David Capello |
| 4 | // |
| 5 | // This program is distributed under the terms of |
| 6 | // the End-User License Agreement for Aseprite. |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include "config.h" |
| 10 | #endif |
| 11 | |
| 12 | #include "app/file/split_filename.h" |
| 13 | #include "base/fs.h" |
| 14 | |
| 15 | #include <cstring> |
| 16 | |
| 17 | namespace app { |
| 18 | |
| 19 | // Splits a file-name like "my_ani0000.pcx" to "my_ani" and ".pcx", |
| 20 | // returning the number of the center; returns "-1" if the function |
| 21 | // can't split anything |
| 22 | int split_filename(const std::string& filename, |
| 23 | std::string& left, |
| 24 | std::string& right, |
| 25 | int& width) |
| 26 | { |
| 27 | left = base::get_file_title_with_path(filename); |
| 28 | right = base::get_file_extension(filename); |
| 29 | if (!right.empty()) |
| 30 | right.insert(right.begin(), '.'); |
| 31 | |
| 32 | // Remove all trailing numbers in the "left" side. |
| 33 | std::string result_str; |
| 34 | width = 0; |
| 35 | int num = -1; |
| 36 | int order = 1; |
| 37 | |
| 38 | auto it = left.rbegin(); |
| 39 | auto end = left.rend(); |
| 40 | |
| 41 | while (it != end) { |
| 42 | const int chr = *it; |
| 43 | if (chr >= '0' && chr <= '9') { |
| 44 | if (num < 0) |
| 45 | num = 0; |
| 46 | |
| 47 | num += order*(chr-'0'); |
| 48 | order *= 10; |
| 49 | ++width; |
| 50 | ++it; |
| 51 | } |
| 52 | else |
| 53 | break; |
| 54 | } |
| 55 | |
| 56 | if (width > 0) |
| 57 | left.erase(left.end()-width, left.end()); |
| 58 | |
| 59 | return num; |
| 60 | } |
| 61 | |
| 62 | } // namespace app |
| 63 | |