Added the images for test.
[pom.git] / misc.cc
1
2 //////////////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify         //
4 // it under the terms of the version 3 of the GNU General Public License        //
5 // as published by the Free Software Foundation.                                //
6 //                                                                              //
7 // This program is distributed in the hope that it will be useful, but          //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of                   //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU             //
10 // General Public License for more details.                                     //
11 //                                                                              //
12 // You should have received a copy of the GNU General Public License            //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.         //
14 //                                                                              //
15 // Written by Francois Fleuret                                                  //
16 // (C) Ecole Polytechnique Federale de Lausanne                                 //
17 // Contact <pom@epfl.ch> for comments & bug reports                             //
18 //////////////////////////////////////////////////////////////////////////////////
19
20 #include <iostream>
21 #include <fstream>
22 #include <stdlib.h>
23
24 using namespace std;
25
26 #include "misc.h"
27
28 char *next_word(char *buffer, char *r, int buffer_size) {
29   char *s;
30   s = buffer;
31   if(r != 0) {
32     while((*r == ' ') || (*r == '\t') || (*r == ',')) r++;
33     if(*r == '"') {
34       r++;
35       while((*r != '"') && (*r != '\0') &&
36             (s<buffer+buffer_size-1))
37         *s++ = *r++;
38       if(*r == '"') r++;
39     } else {
40       while((*r != '\r') && (*r != '\n') && (*r != '\0') &&
41             (*r != '\t') && (*r != ' ') && (*r != ',')) {
42         if(s == buffer + buffer_size) {
43           cerr << "Buffer overflow in next_word." << endl;
44           exit(1);
45         }
46         *s++ = *r++;
47       }
48     }
49
50     while((*r == ' ') || (*r == '\t') || (*r == ',')) r++;
51     if((*r == '\0') || (*r=='\r') || (*r=='\n')) r = 0;
52   }
53   *s = '\0';
54
55   return r;
56 }
57
58 int pomsprintf(char *buffer, int buffer_length, char *format, int n_camera, int n_frame, int n_iteration) {
59   char *s = buffer, *t = format;
60
61   while(*t && s < buffer + buffer_length - 1) {
62     if(*t == '%') {
63       t++;
64       int v;
65       switch(*t) {
66       case 'c':
67         v = n_camera;
68         t++;
69         break;
70       case 'f':
71         v = n_frame;
72         t++;
73         break;
74       case 'i':
75         v = n_iteration;
76         t++;
77         break;
78       default:
79         cerr << "Unknown format type in " << format << "." << endl;
80         exit(1);
81       }
82
83       s += snprintf(s, buffer + buffer_length - s, "%d", v);
84     } else *(s++) = *(t++);
85   }
86
87   *(s++) = '\0';
88
89   return s - buffer;
90 }