This repository has been archived by the owner on Feb 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TVPaint_EXPORT.grg
1216 lines (1054 loc) · 34.3 KB
/
TVPaint_EXPORT.grg
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
PARAM none
//////////////////////////////////////////////////////////////////////////////////////////
// TVPaint Export v1.1 //
// Author: Philip Børgesen (https://github.com/PhilipBorgesen) //
// //
// This script has been designed to work with TVPaint 11 for which it has been tested. //
// Correct behavior with any other version has not been tested and cannot be assumed. //
// //
// If you have questions or problems with the script, you can try to contact me through //
// the GitHub repository created for the script: //
// //
// Repository: https://github.com/PhilipBorgesen/tvpExport //
// Issues: https://github.com/PhilipBorgesen/tvpExport/issues //
// //
// Before contacting me, please read the documentation written as comments throughout //
// the script as they document how to use and tailor it. //
// For information on how to use the script from TVPaint, please visit: //
// //
// - https://www.tvpaint.com/doc/tvp11/index.php?id=lesson-advanced-functions-george- //
// editing-executing //
// - http://wiki.tvpaint.com/index.php?title=Running_George_Scripts //
// //
//////////////////////////////////////////////////////////////////////////////////////////
// //
// MIT License //
// //
// Copyright (c) 2017 Philip Jon Børgesen //
// //
// Permission is hereby granted, free of charge, to any person obtaining a copy //
// of this software and associated documentation files (the "Software"), to deal //
// in the Software without restriction, including without limitation the rights //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell //
// copies of the Software, and to permit persons to whom the Software is //
// furnished to do so, subject to the following conditions: //
// //
// The above copyright notice and this permission notice shall be included in all //
// copies or substantial portions of the Software. //
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE //
// SOFTWARE. //
// //
//////////////////////////////////////////////////////////////////////////////////////////
//
// Exports all non-hidden layers of all non-hidden TVPaint scenes as sequences of PNG
// images. The absolute file path for each exported frame is defined by a template with
// placeholders substituted by actual values.
//
// The template is declared by the George variable 'fileNameTemplate', which should
// be updated to match the file path scheme of the hosting operating system and the
// actual naming conventions / file structure of the project.
// For more information on file path schemes, see Wikipedia:
//
// https://en.wikipedia.org/wiki/Path_(computing)#Representations_of_paths_by_operating_system_and_shell
//
// (Linux and macOS are both Unix-like systems)
//
// When saving a frame, the following placeholder values in the template will be
// considered for replacement:
// <sceneName> : The name of the TVPaint scene that the frame belongs to.
// <layerName> : The name of the TVPaint layer that the frame belongs to.
// <lfsq####> : The number of the frame on the scene timeline, starting with 0001.
// <sq####> : ??? ID of the TVPaint file. Parsed from the project file name.
// <sh####> : Shot ID of the TVPaint file. Parsed from the project file name.
// <v###> : Version number of the TVPaint file. Parsed from the project file name.
// <creator> : Initials of the creator of the TVPaint file. Parsed from the project file name.
//
// The script expects the TVPaint project file name to follow the pattern:
// COM_<sq####>_<sh####>_ani_<v###>_<creator>.tvpp
// E.g.:
// COM_sq0111_sh0111_ani_v001_pz.tvpp
//
// If this naming convention doesn't match the actual naming convention used by the
// project, the George function 'parseFilename' should be updated accordingly.
fileNameTemplate = "P:/production/output/tvPaintTEST/shots/<sq####>/<sh####>/2DElements/<sceneName>/<v###>/<layerName>/COM_<sq####>_<sh####>_2D_<sceneName>_<v###>_<layerName>_<lfsq####>.png"
// Set to 1 to never again be prompted whether you want to overwrite a file.
// Obviously something you should be careful with!
overwriteDontWarn = 0
// Change these to use other placeholder values for the substituted variables.
// The number of #'s in a placeholder define the minimum number of characters the
// substituted variabel should use. If the substituted variabel uses fewer characters
// the value will be prefixed with zeroes to satisfy the intended size.
sceneNamePlaceholder = "<sceneName>"
layerNamePlaceholder = "<layerName>"
lfsqPlaceholder = "<lfsq####>"
sqPlaceholder = "<sq####>"
shPlaceholder = "<sh####>"
vPlaceholder = "<v###>"
creatorPlaceholder = "<creator>"
// Don't touch!
main()
//
// Given the basename of a TVPaint file, such as "COM_sq0111_sh0111_ani_v001_pz.tvpp",
// this function extracts the values to use for <sq####>, <sh####>, <v###> and <creator>.
// The extracted values should be returned as a string with each <placeholder>/value pair
// separated by '/', e.g. "<sq####> sq0111/<sh####> sh0111/<v###> v001/<creator> lz".
// By adding or removing <placeholder>/value pairs you can customize which placeholders
// to substitute based on values from the filename. Placeholders MUST NOT contain spaces.
// If the returned value is an empty string, the remainder of the script will think the
// function has failed and the script will abort.
//
FUNCTION parseFilename(filename) // (string) -> "<sq####> A/<sh####> B/<v###> C/<creator> D"
LOCAL num sq sh v creator
num = NewParse(filename, "_")
IF num != 6
num = num-1
tvWarn("ERROR: Filename doesn't match expected pattern (found " num " underscores, expected 6)")
RETURN ""
END
IF CMP("COM", var(1)) == 0
tvWarn("ERROR: Filename doesn't start with 'COM_'")
RETURN ""
END
IF CMP("ani", var(4)) == 0
tvWarn("ERROR: Filename doesn't contain '_ani_'")
RETURN ""
END
sq = var(2)
sh = var(3)
v = var(5)
num = NewParse(var(6), ".")
creator = var(1)
RETURN sqPlaceholder" "sq"/"shPlaceholder" "sh"/"vPlaceholder" "v"/"creatorPlaceholder" "creator
END
///////////////////////////////////
// Actual script -- don't touch! //
///////////////////////////////////
FUNCTION main() // () ->
LOCAL template projectID
template = getBaseTemplate()
IF CMP(template, "") == 1
RETURN ""
END
projectID = tvProjectCurrentID()
exportProject(template, projectID)
END
FUNCTION getBaseTemplate() // () -> string
LOCAL baseName res num template i placeholder placeholderValue
baseName = getFileBaseName()
IF LEN(baseName) == 0
RETURN ""
END
res = parseFilename(baseName)
IF LEN(res) == 0
RETURN ""
END
num = NewParse(res, "/")
template = fileNameTemplate
FOR i = 1 TO num
PARSE var(i) placeholder placeholderValue
template = substitute(template, placeholder, placeholderValue)
END
return template
END
FUNCTION getFileBaseName() // () -> string
LOCAL filePath lastSep res
filePath = tvProjectName(tvProjectCurrentID())
filePath = Replace(filePath, "\", "/") // In case of Windows
IF CMP(filePath, "/") == 1
tvWarn("Couldn't read naming convention based values from file name. Has the project been saved?")
RETURN ""
END
lastSep = LastPos(filePath, "/")
res = CUT(filePath, lastSep+1, LEN(filePath))
RETURN res
END
FUNCTION substitute(template, placeholder, value) // (string, string, string) -> string
LOCAL padding minWidth diff debug
padding = "</>0000000000000000000000000000000000000000000000000000000000000000000000"
minWidth = calculcateMinWidth(placeholder)
diff = minWidth-LEN(value)-1
IF diff > 0
padding = LeftString(padding, diff+3)
ELSE
padding = ""
END
value = padding""value
template = Replace(template, placeholder, value)
template = Replace(template, "</>", "")
RETURN template
END
FUNCTION calculcateMinWidth(placeholder) // (string) -> integer
LOCAL temp res
temp = Replace(placeholder, "#", "")
res = LEN(placeholder) - LEN(temp)
RETURN res
END
FUNCTION exportProject(template, projectID) // (string, ID) ->
LOCAL orginalProject originalConfig loop pos clipID
orginalProject = tvProjectCurrentID()
tvProjectSelect(projectID)
originalConfig = prepareExportConfiguration()
loop = 1
pos = 0
WHILE loop
clipID = tvClipEnumId(-1, pos)
IF CMP(clipID, "NONE") == 0
IF shouldExportClip(clipID) == 1
exportClip(template, clipID)
END
pos = pos+1
ELSE
loop = 0
END
END
resetProjectConfiguration(originalConfig)
tvProjectSelect(orginalProject)
END
// Setup export settings common for all layers
FUNCTION prepareExportConfiguration() // () -> "origSM|origASM|origClip|origBG|origLayer|origImage"
LOCAL origASM origLayer
origSM = tvSaveMode("PNG")
origASM = tvAlphaSaveMode("NoPreMultiply")
origClip = tvClipCurrentId()
origBG = tvBackground("")
origLayer = tvLayerCurrentID()
origImage = tvLayerGetImage()
tvBackground("NONE")
RETURN origSM"|"origASM"|"origClip"|"origBG"|"origLayer"|"origImage
END
// Reset export settings set by 'prepareExportConfiguration'
FUNCTION resetProjectConfiguration(originalConfig) // ("origASM|origClip|origLayer|origImage|origBG") ->
NewParse(originalConfig, "|")
tvSaveMode(var(1))
tvAlphaSaveMode(var(2))
tvClipSelect(var(3))
tvBackground(var(4))
tvLayerSet(var(5))
tvLayerImage(var(6))
END
FUNCTION shouldExportClip(clipID) // (ID) -> boolean
LOCAL shouldExport
shouldExport = (tvClipHidden(clipID) == 0)
RETURN shouldExport
END
FUNCTION exportClip(template, clipID) // (string, ID) ->
LOCAL sceneName
sceneName = tvClipName(clipID)
template = substitute(template, sceneNamePlaceholder, sceneName)
tvClipSelect(clipID)
doExportClip(template)
END
FUNCTION doExportClip(template) // (string) ->
LOCAL loop pos layerID frameCount
frameCount = tvLastImage() + 1
loop = 1
pos = 0
WHILE loop
layerID = tvLayerGetID(pos)
IF CMP(layerID, "NONE") == 0
IF shouldExportLayer(layerID) == 1
exportLayer(template, layerID, frameCount)
END
pos = pos+1
ELSE
loop = 0
END
END
END
// Whether the layer with ID 'layerID' should be exported
FUNCTION shouldExportLayer(layerID) // (ID) -> boolean
LOCAL display opacity isVisible
num = NewParse(tvLayerInfo(layerID), "|")
display = var(1)
opacity = var(3)
isVisible = (CMP(display, "ON") == 1) && (opacity > 0)
RETURN isVisible
END
FUNCTION exportLayer(template, layerID, frameCount) // (string, ID, integer) ->
LOCAL layerName originalLayer
NewParse(tvLayerInfo(layerID), "|")
layerName = var(4)
template = substitute(template, layerNamePlaceholder, layerName)
tvLayerSet(layerID)
doExportLayer(template, frameCount)
END
FUNCTION doExportLayer(template, frameCount) // (string, integer) ->
LOCAL i
FOR i = 1 TO frameCount
exportFrame(template, i-1)
END
END
FUNCTION exportFrame(template, frameIdx) // (string, integer) ->
template = substitute(template, lfsqPlaceholder, frameIdx+1)
tvInfoBar("Exporting: "template)
tvLayerImage(frameIdx)
saveFrame(template)
END
FUNCTION saveFrame(file) // (string) ->
LOCAL dir
dir = getFileDir(file)
makeDir(dir)
IF fileExists(file)
IF shouldOverwrite(file) != 1
RETURN ""
END
END
tvSaveImage(file)
END
FUNCTION makeDir(dir) // (string) -> string
LOCAL res
res = tvWriteTextFile("MkDir", dir)
IF CMP(res, "Exists") != 1
makeDir(getFileDir(dir))
res = tvWriteTextFile("MkDir", dir)
END
RETURN res
END
FUNCTION fileExists(file) // (string) -> boolean
LOCAL res
res = tvWriteTextFile("Exists", file)
res = CMP(res, "") == 0
RETURN res
END
FUNCTION getFileDir(file) // (string) -> string
LOCAL dir lastSep
dir = Replace(file, "\", "/")
lastSep = LastPos(dir, "/")
dir = CUT(file, 1, lastSep-1)
RETURN dir
END
// Whether the file at the specified location should be overwritten with the frame
// currently being exported.
FUNCTION shouldOverwrite(file) // (string) -> boolean
LOCAL options info overwrite skip overwriteAll res action
IF CMP(overwriteDontWarn, "1") == 1
RETURN 1
END
file = Replace(file, "/", "\")
info = "'"file"' already exists. What to do?"
overwrite = "Overwrite file"
skip = "Skip frame"
overwriteAll = "Overwrite it and don't warn me again"
options = info"|"overwrite"|"skip"|"overwriteAll
action = "-1"
WHILE action == "-1"
res = tvListRequest(options)
PARSE res action res
IF CMP(action, "-1") == 1
tvWarn("Sorry, but you need to make a choice. Don't be shy!")
ELSE
IF CMP(action, "0") == 1
tvWarn("Silly user, you can't select the question! Please try again - and good luck :)")
action = "-1"
END
END
END
IF CMP(action, "1") == 1
RETURN 1
END
IF CMP(action, "2") == 1
RETURN 0
END
IF CMP(action, "3") == 1
overwriteDontWarn = 1
RETURN 1
END
END
///////////////////////////////////////
// TVPaint command wrapper functions //
///////////////////////////////////////
FUNCTION tvListRequest(options) // (string) -> string
tv_ListRequest options
RETURN result
END
FUNCTION tvBackground(args) // (string) -> string
tv_Background args
RETURN result
END
FUNCTION tvWriteTextFile(mode, file) // (string, string) -> string
tv_WriteTextFile mode '"'file'"'
RETURN result
END
FUNCTION tvRequest(msg) // (string) -> boolean
tv_Request msg"|Yes|No"
RETURN result
END
FUNCTION tvSaveImage(file) // (string) ->
tv_SaveImage file
END
FUNCTION tvLayerGetImage() // () -> integer
tv_LayerGetImage
RETURN result
END
FUNCTION tvLayerImage(num) // (integer) ->
tv_LayerImage num
END
FUNCTION tvClipCurrentId() // () -> ID
tv_ClipCurrentId
RETURN result
END
FUNCTION tvClipSelect(clipID) // (ID) ->
tv_ClipSelect clipID
END
FUNCTION tvClipName(clipID) // (ID) -> string
tv_ClipName clipID
RETURN result
END
FUNCTION tvClipHidden(clipID) // (ID) -> string
tv_ClipHidden clipID
RETURN result
END
FUNCTION tvClipEnumId(parent, pos) // (???, integer) -> ID
tv_ClipEnumId parent pos
RETURN result
END
FUNCTION tvWarn(msg) // (string) ->
tv_Warn msg
END
FUNCTION tvProjectCurrentID() // () -> ID
tv_ProjectCurrentID
RETURN result
END
FUNCTION tvLayerInfo(id) // (integer) -> "display|pos|opacity|name|type|start|end"
LOCAL display pos opacity name type start end temp
tv_LayerInfo id
PARSE result display pos opacity name type start end temp
RETURN display"|"pos"|"opacity"|"name"|"type"|"start"|"end
END
FUNCTION tvSaveMode(mode) // (string) -> string
tv_SaveMode mode
RETURN result
END
FUNCTION tvAlphaSaveMode(mode) // (string) -> string
tv_AlphaSaveMode mode
RETURN result
END
FUNCTION tvLayerSet(id) // (ID) ->
tv_layerSet id
END
FUNCTION tvLayerGetID(id) // (ID) -> string
tv_LayerGetID id
RETURN result
END
FUNCTION tvProjectSelect(projectID) // (ID) ->
tv_ProjectSelect projectID
END
FUNCTION tvLayerCurrentID() // () -> ID
tv_LayerCurrentID
RETURN result
END
FUNCTION tvProjectName(projectID) // (ID) -> string
LOCAL projectName temp
tv_ProjectInfo projectID
PARSE result projectName temp
RETURN projectName
END
FUNCTION tvLastImage() // () -> integer
tv_LastImage
RETURN result
END
FUNCTION tvInfobar(msg) // (string) ->
tv_infobar msg
END
////////////////////////////////
// Embedded TVPaint functions //
////////////////////////////////
// The following scripts have been embedded as a transportable work-around
// for George's one-include-per-file limit:
//
// http://wiki.tvpaint.com/index.php?title=George_Procedures_:_Basic
// http://wiki.tvpaint.com/index.php?title=George_Procedures_:_Advanced
//
// =========================================================================
// THESE ARE NOT COVERED BY THE MIT LICENSE. I CLAIM NO COPYRIGHT OVER THEM.
// =========================================================================
//{{{** BASIC FUNCTIONS
//{{{ Find
//------------------------------------------
//
// Find
//
// Function: find a character into a string
// from a specified start position
//
// Call: Find(string,car,start)
//
// Arguments:
// string = characters string
// car = character to look for
// start = start position in string
//
// Return: position of character
// 0 if character does not exist
// into string
// -1 if invalid position
// (negative, null or higher
// than string length)
//
//------------------------------------------
FUNCTION Find(string,car,start)
LOCAL i size
i = start
size = LEN(string)
IF ((start <= 0) || (start > size) || (CMP(car,"") == 1))
RETURN -1
END
DO
IF (CMP(CHAR(string,i),car) == 1)
RETURN i
END
UNTIL ((i=i+1) > size)
RETURN 0
END
//}}}
//{{{ FirstPos(string,car)
//------------------------------------------
//
// FirstPos
//
// Function: find first occurence of a
// character into a string
//
// Call: FirstPos(string,car)
//
// Arguments:
// string = characters string
// car = character to look for
//
// Return: position of character
// or 0 id character does not
// exist into string.
//
//------------------------------------------
FUNCTION FirstPos(string,car)
RETURN Find(string,car,1)
END
//}}}
//{{{ LastPos(string,car)
//------------------------------------------
//
// LastPos
//
// Function: find last occurence of a
// character into a string
//
// Call: LastPos(string,car)
//
// Arguments:
// string = characters string
// car = character to look for
//
// Return: position of character
// or 0 if character does not
// exist into string.
//
//------------------------------------------
FUNCTION LastPos(string,car)
LOCAL loop myChar
pos = LEN(string)
loop = 1
WHILE loop
myChar = CHAR(string,pos)
IF CMP(myChar,car) == 1
loop =0
ELSE
pos = pos-1
END
IF pos<1
loop=0
END
END
RETURN pos
END
//}}}
//{{{ FindString(string,search,start)
//------------------------------------------
//
// FindString
//
// Function: find a substring into a string
// from a specified start position
//
// Call: FindString(string,search,start)
//
// Arguments:
// string = characters string
// search = substring to look for
// start = start position in string
//
// Return: start position of substring
// 0 if substring does not exist
// into string
// -1 if invalid start position
// (negative, null or higher
// than string length) or if
// substring is empty
//
//------------------------------------------
FUNCTION FindString(string,search,start)
LOCAL found j pos lastpos lstr lsrch
found = 0
lastpos = start
lstr = LEN(string)
lsrch = LEN(search)
IF (CMP(search,"") == 1)
RETURN -1
END
WHILE ((pos = Find(string,CHAR(search,1),lastpos)) > 0)
j = 1
found = 1
IF (j == lsrch)
RETURN pos
END
WHILE ((j < lsrch) && (found == 1))
IF ((pos+j) > lstr)
RETURN 0
ELSE
IF (CMP(CHAR(search,j+1),CHAR(string,pos+j)) == 0)
lastpos = pos+j
found = 0
END
END
j = j+1
END
IF (found == 1)
RETURN pos
END
END
RETURN pos
END
//}}}
//{{{ LeftString(string,number)
//------------------------------------------
//
// LeftString
//
// Function: extract a substring beginning
// at the start of a string
//
// Call: LeftString(string,number)
//
// Arguments:
// string = characters string
// number = number of character to
// extract from string
//
// Return: result substring
// or 0 if number is negative
// or null.
//
//------------------------------------------
FUNCTION LeftString(string,number)
LOCAL size
size = LEN(string)
IF (number > 0)
IF (number > size)
number = size
END
RETURN CUT(string,1,number)
END
RETURN 0
END
//}}}
//{{{ RightString(string,number)
//------------------------------------------
//
// RightString
//
// Function: extract a substring ending
// at the end of a string
//
// Call: RightString(string,number)
//
// Arguments:
// string = characters string
// number = number of character to
// extract from string
//
// Return: result substring
// or 0 if number is negative
// or null.
//
//------------------------------------------
FUNCTION RightString(string,number)
LOCAL size
size = LEN(string)
IF (number > 0)
IF (number > size)
number = size
END
RETURN CUT(string,size-number+1,size)
END
RETURN 0
END
//}}}
//{{{ MidString(string,first,size)
//------------------------------------------
//
// MidString
//
// Function: extract a substring from a
// string
//
// Call: MidString(string,first,size)
//
// Arguments:
// string = characters string
// first = start position in string
// cha?ne
// size = number of characters
//
// Return: result substring
// or 0 if number or first are
// negatives or null.
//
//------------------------------------------
FUNCTION MidString(string,first,size)
LOCAL ln
ln = LEN(string)
IF ((first > 0) && (size > 0))
IF ((first+size-1) > ln)
size = ln-first+1
END
RETURN CUT(string,first,first+size-1)
END
RETURN 0
END
//}}}
//{{{ InsertAtPos(string,insert,pos)
//------------------------------------------
//
// InsertAtPos
//
// Function: insert a string in another
// string at a specified position
//
// Call: InsertAtPos(string,insert,pos)
//
// Arguments:
// string = characters string
// insert = string to insert
// pos = start position for insertion
//
// Return: result string after insertion
// or 0 if start position is
// invalid (negative, null or
// higher than string length)
//
//------------------------------------------
FUNCTION InsertAtPos(string,insert,pos)
LOCAL workstr str1 size
size = LEN(string)
IF ((pos < 0) || (pos > size))
RETURN 0
END
IF (pos == 0)
RETURN insert""string
END
IF (pos == size)
RETURN string""insert
END
workstr = CUT(string,1,pos)
IF (pos < size)
str1 = CUT(string,pos+1,size)
RETURN workstr""insert""str1
ELSE
RETURN workstr""insert
END
END
//}}}
//}}}**
//{{{** ADVANCED STRING FUNCTIONS
//{{{ InsertAfter(string,insert,search)
//------------------------------------------
//
// InsertAfter
//
// Function: insert a string after each
// occurence of a character or
// a set of characters into
// another string
//
// Call: InsertAfter(string,insert,search)
//
// Arguments:
// string = characters string
// insert = string to insert
// search = character or substring
// to look for
//
// Return: result string after insertion
//
//------------------------------------------
FUNCTION InsertAfter(string,insert,search)
LOCAL pos workstr size
pos = 1
workstr = string
size = LEN(search)
WHILE ((pos = FindString(workstr,search,pos)) > 0)
workstr = InsertStringAtPos(workstr,insert,pos+size-1)
pos = pos+size+LEN(insert)
END
RETURN workstr
END
//}}}
//{{{ InsertBefore
//------------------------------------------
//
// InsertBefore
//
// Function: insert a string before each
// occurence of a character or
// a set of characters into
// another string
//
// Call: InsertBefore(string,insert,search)
//
// Arguments:
// string = characters string
// insert = string to insert
// search = character or substring
// to look for
//
// Return: result string after insertion
//
//------------------------------------------
FUNCTION InsertBefore(string,insert,search)
LOCAL pos workstr
pos = 1
workstr = string
WHILE ((pos = FindString(workstr,search,pos)) > 0)
workstr = InsertStringAtPos(workstr,insert,pos-1)
pos = pos+LEN(search)+LEN(insert)
END
RETURN workstr
END
//}}}
//{{{ Delete
//------------------------------------------
//
// Delete
//
// Function: delete all occurences of a
// substring into a string
//
// Call: Delete(string,search)
//
// Arguments:
// string = characters string
// search = substring to delete
//
// Return: result string after deletion
//
//------------------------------------------
FUNCTION Delete(string,search)
LOCAL workstr str1 str2 pos size
workstr = string